line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
1
|
|
|
|
|
|
|
package Apache2::Controller::SQL::MySQL; |
2
|
|
|
|
|
|
|
|
3
|
|
|
|
|
|
|
=head1 NAME |
4
|
|
|
|
|
|
|
|
5
|
|
|
|
|
|
|
Apache2::Controller::SQL::MySQL - useful database methods for MySQL |
6
|
|
|
|
|
|
|
|
7
|
|
|
|
|
|
|
=head1 VERSION |
8
|
|
|
|
|
|
|
|
9
|
|
|
|
|
|
|
Version 1.001.001 |
10
|
|
|
|
|
|
|
|
11
|
|
|
|
|
|
|
=cut |
12
|
|
|
|
|
|
|
|
13
|
1
|
|
|
1
|
|
1903
|
use version; |
|
1
|
|
|
|
|
2
|
|
|
1
|
|
|
|
|
7
|
|
14
|
|
|
|
|
|
|
our $VERSION = version->new('1.001.001'); |
15
|
|
|
|
|
|
|
|
16
|
|
|
|
|
|
|
=head1 SYNOPSIS |
17
|
|
|
|
|
|
|
|
18
|
|
|
|
|
|
|
package UFP::SFC::Controller::Tools; |
19
|
|
|
|
|
|
|
use base qw( |
20
|
|
|
|
|
|
|
Apache2::Controller |
21
|
|
|
|
|
|
|
Apache2::Controller::SQL::MySQL |
22
|
|
|
|
|
|
|
); |
23
|
|
|
|
|
|
|
# ... |
24
|
|
|
|
|
|
|
|
25
|
|
|
|
|
|
|
=head1 DESCRIPTION |
26
|
|
|
|
|
|
|
|
27
|
|
|
|
|
|
|
Provides some useful methods for interacting with a MySQL database. |
28
|
|
|
|
|
|
|
|
29
|
|
|
|
|
|
|
This isn't really standard or a necessary part of A2C, I just find it handy. |
30
|
|
|
|
|
|
|
|
31
|
|
|
|
|
|
|
=head1 DEPRECATED |
32
|
|
|
|
|
|
|
|
33
|
|
|
|
|
|
|
Don't depend on this. I intend to remove it in a future |
34
|
|
|
|
|
|
|
release because it is not relevant. |
35
|
|
|
|
|
|
|
|
36
|
|
|
|
|
|
|
=head1 METHODS |
37
|
|
|
|
|
|
|
|
38
|
|
|
|
|
|
|
=head2 insert_hash |
39
|
|
|
|
|
|
|
|
40
|
|
|
|
|
|
|
insert_hash( \%hashref ) |
41
|
|
|
|
|
|
|
|
42
|
|
|
|
|
|
|
Insert data into the database. |
43
|
|
|
|
|
|
|
|
44
|
|
|
|
|
|
|
# http://sfc.ufp/tools/register_crew/enterprise?captain=kirk&sci=spock&med=mccoy |
45
|
|
|
|
|
|
|
sub register_crew { |
46
|
|
|
|
|
|
|
my ($self, $ship) = @_; |
47
|
|
|
|
|
|
|
my $crew = $self->param(); |
48
|
|
|
|
|
|
|
$self->insert_hash({ |
49
|
|
|
|
|
|
|
table => "crew_$ship", |
50
|
|
|
|
|
|
|
data => $crew, |
51
|
|
|
|
|
|
|
}); |
52
|
|
|
|
|
|
|
$self->print("Warp factor 5, engage.\n"); |
53
|
|
|
|
|
|
|
return Apache2::Const::HTTP_OK; |
54
|
|
|
|
|
|
|
} |
55
|
|
|
|
|
|
|
|
56
|
|
|
|
|
|
|
Requires a database handle be assigned to C<< $self->{dbh} >>. |
57
|
|
|
|
|
|
|
See L. |
58
|
|
|
|
|
|
|
|
59
|
|
|
|
|
|
|
Hashref argument supports these fields: |
60
|
|
|
|
|
|
|
|
61
|
|
|
|
|
|
|
=over 4 |
62
|
|
|
|
|
|
|
|
63
|
|
|
|
|
|
|
=item table |
64
|
|
|
|
|
|
|
|
65
|
|
|
|
|
|
|
The SQL table to insert into. |
66
|
|
|
|
|
|
|
|
67
|
|
|
|
|
|
|
=item data |
68
|
|
|
|
|
|
|
|
69
|
|
|
|
|
|
|
The hash ref of field data to insert. |
70
|
|
|
|
|
|
|
|
71
|
|
|
|
|
|
|
=item on_dup_sql |
72
|
|
|
|
|
|
|
|
73
|
|
|
|
|
|
|
Optional string of SQL for after 'ON DUPLICATE KEY UPDATE'. |
74
|
|
|
|
|
|
|
Format it yourself. |
75
|
|
|
|
|
|
|
|
76
|
|
|
|
|
|
|
=item on_dup_bind |
77
|
|
|
|
|
|
|
|
78
|
|
|
|
|
|
|
Array ref of bind values for extra C> characters in C. |
79
|
|
|
|
|
|
|
|
80
|
|
|
|
|
|
|
=back |
81
|
|
|
|
|
|
|
|
82
|
|
|
|
|
|
|
=cut |
83
|
|
|
|
|
|
|
|
84
|
1
|
|
|
1
|
|
101
|
use strict; |
|
1
|
|
|
|
|
2
|
|
|
1
|
|
|
|
|
39
|
|
85
|
1
|
|
|
1
|
|
5
|
use warnings FATAL => 'all'; |
|
1
|
|
|
|
|
2
|
|
|
1
|
|
|
|
|
50
|
|
86
|
1
|
|
|
1
|
|
5
|
use English '-no_match_vars'; |
|
1
|
|
|
|
|
3
|
|
|
1
|
|
|
|
|
6
|
|
87
|
1
|
|
|
1
|
|
537
|
use Apache2::Controller::X; |
|
1
|
|
|
|
|
3
|
|
|
1
|
|
|
|
|
367
|
|
88
|
|
|
|
|
|
|
|
89
|
|
|
|
|
|
|
sub insert_hash { |
90
|
0
|
|
|
0
|
1
|
|
my ($self, $p) = @_; |
91
|
|
|
|
|
|
|
|
92
|
0
|
|
|
|
|
|
my ($table, $data, $on_dup_sql, $on_dup_bind) = @{$p}{qw( |
|
0
|
|
|
|
|
|
|
93
|
|
|
|
|
|
|
table data on_dup_sql on_dup_bind |
94
|
|
|
|
|
|
|
)}; |
95
|
|
|
|
|
|
|
|
96
|
0
|
|
|
|
|
|
my @bind = values %{$data}; |
|
0
|
|
|
|
|
|
|
97
|
|
|
|
|
|
|
|
98
|
0
|
0
|
|
|
|
|
my $sql |
99
|
|
|
|
|
|
|
= "INSERT INTO $table SET\n" |
100
|
0
|
|
|
|
|
|
. join(",\n", map {" $_ = ".(ref $_ ? $_ : '?')} keys %{$data}); |
|
0
|
|
|
|
|
|
|
101
|
|
|
|
|
|
|
|
102
|
0
|
0
|
|
|
|
|
if ($on_dup_sql) { |
103
|
0
|
|
|
|
|
|
$sql .= "\nON DUPLICATE KEY UPDATE\n$on_dup_sql\n"; |
104
|
0
|
0
|
|
|
|
|
push @bind, @{$on_dup_bind} if $on_dup_bind; |
|
0
|
|
|
|
|
|
|
105
|
|
|
|
|
|
|
} |
106
|
|
|
|
|
|
|
|
107
|
0
|
|
|
|
|
|
my $dbh = $self->{dbh}; |
108
|
0
|
|
|
|
|
|
my $id; |
109
|
0
|
|
|
|
|
|
eval { |
110
|
0
|
|
|
|
|
|
DEBUG("preparing handle for sql:\n$sql\n---\n"); |
111
|
0
|
|
|
|
|
|
my $sth = $dbh->prepare_cached($sql); |
112
|
0
|
|
|
|
|
|
$sth->execute(@bind); |
113
|
0
|
|
|
|
|
|
($id) = $dbh->selectrow_array(q{ SELECT LAST_INSERT_ID() }); |
114
|
|
|
|
|
|
|
}; |
115
|
0
|
0
|
|
|
|
|
if ($EVAL_ERROR) { |
116
|
0
|
|
|
|
|
|
a2cx message => "database error: $EVAL_ERROR", |
117
|
|
|
|
|
|
|
dump => { sql => $sql, bind => \@bind, }; |
118
|
|
|
|
|
|
|
} |
119
|
0
|
|
|
|
|
|
return $id; |
120
|
|
|
|
|
|
|
} |
121
|
|
|
|
|
|
|
|
122
|
|
|
|
|
|
|
=head1 SEE ALSO |
123
|
|
|
|
|
|
|
|
124
|
|
|
|
|
|
|
L |
125
|
|
|
|
|
|
|
|
126
|
|
|
|
|
|
|
L |
127
|
|
|
|
|
|
|
|
128
|
|
|
|
|
|
|
=head1 AUTHOR |
129
|
|
|
|
|
|
|
|
130
|
|
|
|
|
|
|
Mark Hedges, C |
131
|
|
|
|
|
|
|
|
132
|
|
|
|
|
|
|
=head1 COPYRIGHT AND LICENSE |
133
|
|
|
|
|
|
|
|
134
|
|
|
|
|
|
|
Copyright 2008-2010 Mark Hedges. CPAN: markle |
135
|
|
|
|
|
|
|
|
136
|
|
|
|
|
|
|
This library is free software; you can redistribute it and/or modify |
137
|
|
|
|
|
|
|
it under the same terms as Perl itself. |
138
|
|
|
|
|
|
|
|
139
|
|
|
|
|
|
|
This software is provided as-is, with no warranty |
140
|
|
|
|
|
|
|
and no guarantee of fitness |
141
|
|
|
|
|
|
|
for any particular purpose. |
142
|
|
|
|
|
|
|
|
143
|
|
|
|
|
|
|
=cut |
144
|
|
|
|
|
|
|
|
145
|
|
|
|
|
|
|
1; |
146
|
|
|
|
|
|
|
|