line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
1
|
|
|
|
|
|
|
package SQL::Smart; |
2
|
|
|
|
|
|
|
|
3
|
1
|
|
|
1
|
|
20386
|
use 5.006; |
|
1
|
|
|
|
|
5
|
|
|
1
|
|
|
|
|
53
|
|
4
|
|
|
|
|
|
|
#use strict; |
5
|
1
|
|
|
1
|
|
6
|
use warnings FATAL => 'all'; |
|
1
|
|
|
|
|
1
|
|
|
1
|
|
|
|
|
34
|
|
6
|
1
|
|
|
1
|
|
2186
|
use DBI; |
|
1
|
|
|
|
|
17670
|
|
|
1
|
|
|
|
|
553
|
|
7
|
|
|
|
|
|
|
require Exporter; |
8
|
|
|
|
|
|
|
@ISA = qw(Exporter); |
9
|
|
|
|
|
|
|
@EXPORT = qw / for_dbh sql /; |
10
|
|
|
|
|
|
|
our $dbh_cache; |
11
|
|
|
|
|
|
|
|
12
|
|
|
|
|
|
|
=head1 NAME |
13
|
|
|
|
|
|
|
|
14
|
|
|
|
|
|
|
SQL::Smart - A new smart way to do SQL query |
15
|
|
|
|
|
|
|
|
16
|
|
|
|
|
|
|
=head1 VERSION |
17
|
|
|
|
|
|
|
|
18
|
|
|
|
|
|
|
Version 0.01 |
19
|
|
|
|
|
|
|
|
20
|
|
|
|
|
|
|
=cut |
21
|
|
|
|
|
|
|
|
22
|
|
|
|
|
|
|
our $VERSION = '0.06'; |
23
|
|
|
|
|
|
|
|
24
|
|
|
|
|
|
|
|
25
|
|
|
|
|
|
|
=head1 SYNOPSIS |
26
|
|
|
|
|
|
|
|
27
|
|
|
|
|
|
|
use SQL::Smart; |
28
|
|
|
|
|
|
|
use DBI; |
29
|
|
|
|
|
|
|
|
30
|
|
|
|
|
|
|
for_dbh(DBI->connect('DBI:mysql:dbname;host=hostname', 'username', 'password')); |
31
|
|
|
|
|
|
|
|
32
|
|
|
|
|
|
|
$name = sql('select name from users where id=?', $id); # $name = 'Tom' |
33
|
|
|
|
|
|
|
@names = sql('select name from users'); # @names = ('Tom', 'Jerry'...) |
34
|
|
|
|
|
|
|
$rh_user = sql('select * from users where id=?', $id); # $rh_user = {id=>1, name=>'Tom'} |
35
|
|
|
|
|
|
|
@users = sql('select * from users'); # @users = ({id=>1, name=>'Tom'}, {id=>2, name=>'Jerry'}...) |
36
|
|
|
|
|
|
|
$last_insert_id = sql('insert into users (name) values (?)', 'Mary'); |
37
|
|
|
|
|
|
|
sql('update users set name=? where id=?', $id); |
38
|
|
|
|
|
|
|
sql('delete from users where id=?', $id); |
39
|
|
|
|
|
|
|
|
40
|
|
|
|
|
|
|
=head1 EXPORT |
41
|
|
|
|
|
|
|
|
42
|
|
|
|
|
|
|
A list of functions that can be exported. You can delete this section |
43
|
|
|
|
|
|
|
if you don't export anything, such as for a purely object-oriented module. |
44
|
|
|
|
|
|
|
|
45
|
|
|
|
|
|
|
=head1 SUBROUTINES/METHODS |
46
|
|
|
|
|
|
|
|
47
|
|
|
|
|
|
|
=head2 for_dbh |
48
|
|
|
|
|
|
|
|
49
|
|
|
|
|
|
|
=cut |
50
|
|
|
|
|
|
|
|
51
|
|
|
|
|
|
|
sub get_dbh{ |
52
|
0
|
0
|
|
0
|
0
|
|
unless($dbh_cache){ |
53
|
0
|
|
|
|
|
|
die "Need to call for_dbh first!"; |
54
|
|
|
|
|
|
|
} |
55
|
0
|
|
|
|
|
|
return $dbh_cache; |
56
|
|
|
|
|
|
|
} |
57
|
|
|
|
|
|
|
|
58
|
|
|
|
|
|
|
sub for_dbh { |
59
|
0
|
|
|
0
|
1
|
|
$dbh_cache = shift; |
60
|
|
|
|
|
|
|
} |
61
|
|
|
|
|
|
|
|
62
|
|
|
|
|
|
|
=head2 sql |
63
|
|
|
|
|
|
|
|
64
|
|
|
|
|
|
|
=cut |
65
|
|
|
|
|
|
|
|
66
|
|
|
|
|
|
|
sub flatten_result{ |
67
|
0
|
|
|
0
|
0
|
|
my @input = @_; |
68
|
0
|
|
|
|
|
|
my $has_more_than_one_key = (scalar(keys %{$input[0]}) > 1); |
|
0
|
|
|
|
|
|
|
69
|
0
|
0
|
|
|
|
|
my @output = map { |
70
|
0
|
|
|
|
|
|
$has_more_than_one_key ? $_ : ((values %$_)[0]) |
71
|
|
|
|
|
|
|
} @input; |
72
|
0
|
0
|
|
|
|
|
if(wantarray){ |
73
|
0
|
|
|
|
|
|
return @output; |
74
|
|
|
|
|
|
|
}else{ |
75
|
0
|
|
|
|
|
|
return $output[0]; |
76
|
|
|
|
|
|
|
} |
77
|
|
|
|
|
|
|
} |
78
|
|
|
|
|
|
|
|
79
|
|
|
|
|
|
|
sub sql{ |
80
|
0
|
|
|
0
|
1
|
|
my $sql_statement = shift; |
81
|
0
|
|
|
|
|
|
my @binds = @_; |
82
|
0
|
|
|
|
|
|
my $dbh = get_dbh(); |
83
|
0
|
0
|
|
|
|
|
unless($sql_statement =~ /^\s*(?:select|describe)/i){ |
84
|
0
|
|
|
|
|
|
my $sth = $dbh->prepare($sql_statement); |
85
|
0
|
|
|
|
|
|
$sth->execute(@binds); |
86
|
|
|
|
|
|
|
#$dbh->commit(); |
87
|
0
|
0
|
|
|
|
|
if($sql_statement =~ /^\s*insert\b/i){ |
88
|
0
|
|
|
|
|
|
return $dbh->last_insert_id(undef, undef, undef, undef); |
89
|
|
|
|
|
|
|
} |
90
|
|
|
|
|
|
|
}else{ |
91
|
0
|
|
|
|
|
|
my $sth = $dbh->prepare($sql_statement); |
92
|
0
|
|
|
|
|
|
$sth->execute(@binds); |
93
|
0
|
|
|
|
|
|
my $ra_result = $sth->fetchall_arrayref({}); |
94
|
0
|
0
|
|
|
|
|
if(wantarray){ |
95
|
0
|
|
|
|
|
|
return flatten_result(@$ra_result); |
96
|
|
|
|
|
|
|
}else{ |
97
|
0
|
0
|
|
|
|
|
return undef if scalar(@$ra_result) == 0; |
98
|
0
|
|
|
|
|
|
my $one_result = flatten_result($ra_result->[0]); |
99
|
0
|
|
|
|
|
|
return $one_result; |
100
|
|
|
|
|
|
|
} |
101
|
|
|
|
|
|
|
} |
102
|
|
|
|
|
|
|
} |
103
|
|
|
|
|
|
|
|
104
|
|
|
|
|
|
|
=head1 AUTHOR |
105
|
|
|
|
|
|
|
|
106
|
|
|
|
|
|
|
Priezt, C<< >> |
107
|
|
|
|
|
|
|
|
108
|
|
|
|
|
|
|
=head1 BUGS |
109
|
|
|
|
|
|
|
|
110
|
|
|
|
|
|
|
Please report any bugs or feature requests to C, or through |
111
|
|
|
|
|
|
|
the web interface at L. I will be notified, and then you'll |
112
|
|
|
|
|
|
|
automatically be notified of progress on your bug as I make changes. |
113
|
|
|
|
|
|
|
|
114
|
|
|
|
|
|
|
|
115
|
|
|
|
|
|
|
|
116
|
|
|
|
|
|
|
|
117
|
|
|
|
|
|
|
=head1 SUPPORT |
118
|
|
|
|
|
|
|
|
119
|
|
|
|
|
|
|
You can find documentation for this module with the perldoc command. |
120
|
|
|
|
|
|
|
|
121
|
|
|
|
|
|
|
perldoc SQL::Smart |
122
|
|
|
|
|
|
|
|
123
|
|
|
|
|
|
|
|
124
|
|
|
|
|
|
|
You can also look for information at: |
125
|
|
|
|
|
|
|
|
126
|
|
|
|
|
|
|
=over 4 |
127
|
|
|
|
|
|
|
|
128
|
|
|
|
|
|
|
=item * RT: CPAN's request tracker (report bugs here) |
129
|
|
|
|
|
|
|
|
130
|
|
|
|
|
|
|
L |
131
|
|
|
|
|
|
|
|
132
|
|
|
|
|
|
|
=item * AnnoCPAN: Annotated CPAN documentation |
133
|
|
|
|
|
|
|
|
134
|
|
|
|
|
|
|
L |
135
|
|
|
|
|
|
|
|
136
|
|
|
|
|
|
|
=item * CPAN Ratings |
137
|
|
|
|
|
|
|
|
138
|
|
|
|
|
|
|
L |
139
|
|
|
|
|
|
|
|
140
|
|
|
|
|
|
|
=item * Search CPAN |
141
|
|
|
|
|
|
|
|
142
|
|
|
|
|
|
|
L |
143
|
|
|
|
|
|
|
|
144
|
|
|
|
|
|
|
=back |
145
|
|
|
|
|
|
|
|
146
|
|
|
|
|
|
|
|
147
|
|
|
|
|
|
|
=head1 ACKNOWLEDGEMENTS |
148
|
|
|
|
|
|
|
|
149
|
|
|
|
|
|
|
|
150
|
|
|
|
|
|
|
=head1 LICENSE AND COPYRIGHT |
151
|
|
|
|
|
|
|
|
152
|
|
|
|
|
|
|
Copyright 2012 Priezt. |
153
|
|
|
|
|
|
|
|
154
|
|
|
|
|
|
|
This program is free software; you can redistribute it and/or modify it |
155
|
|
|
|
|
|
|
under the terms of the the Artistic License (2.0). You may obtain a |
156
|
|
|
|
|
|
|
copy of the full license at: |
157
|
|
|
|
|
|
|
|
158
|
|
|
|
|
|
|
L |
159
|
|
|
|
|
|
|
|
160
|
|
|
|
|
|
|
Any use, modification, and distribution of the Standard or Modified |
161
|
|
|
|
|
|
|
Versions is governed by this Artistic License. By using, modifying or |
162
|
|
|
|
|
|
|
distributing the Package, you accept this license. Do not use, modify, |
163
|
|
|
|
|
|
|
or distribute the Package, if you do not accept this license. |
164
|
|
|
|
|
|
|
|
165
|
|
|
|
|
|
|
If your Modified Version has been derived from a Modified Version made |
166
|
|
|
|
|
|
|
by someone other than you, you are nevertheless required to ensure that |
167
|
|
|
|
|
|
|
your Modified Version complies with the requirements of this license. |
168
|
|
|
|
|
|
|
|
169
|
|
|
|
|
|
|
This license does not grant you the right to use any trademark, service |
170
|
|
|
|
|
|
|
mark, tradename, or logo of the Copyright Holder. |
171
|
|
|
|
|
|
|
|
172
|
|
|
|
|
|
|
This license includes the non-exclusive, worldwide, free-of-charge |
173
|
|
|
|
|
|
|
patent license to make, have made, use, offer to sell, sell, import and |
174
|
|
|
|
|
|
|
otherwise transfer the Package with respect to any patent claims |
175
|
|
|
|
|
|
|
licensable by the Copyright Holder that are necessarily infringed by the |
176
|
|
|
|
|
|
|
Package. If you institute patent litigation (including a cross-claim or |
177
|
|
|
|
|
|
|
counterclaim) against any party alleging that the Package constitutes |
178
|
|
|
|
|
|
|
direct or contributory patent infringement, then this Artistic License |
179
|
|
|
|
|
|
|
to you shall terminate on the date that such litigation is filed. |
180
|
|
|
|
|
|
|
|
181
|
|
|
|
|
|
|
Disclaimer of Warranty: THE PACKAGE IS PROVIDED BY THE COPYRIGHT HOLDER |
182
|
|
|
|
|
|
|
AND CONTRIBUTORS "AS IS' AND WITHOUT ANY EXPRESS OR IMPLIED WARRANTIES. |
183
|
|
|
|
|
|
|
THE IMPLIED WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR |
184
|
|
|
|
|
|
|
PURPOSE, OR NON-INFRINGEMENT ARE DISCLAIMED TO THE EXTENT PERMITTED BY |
185
|
|
|
|
|
|
|
YOUR LOCAL LAW. UNLESS REQUIRED BY LAW, NO COPYRIGHT HOLDER OR |
186
|
|
|
|
|
|
|
CONTRIBUTOR WILL BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, OR |
187
|
|
|
|
|
|
|
CONSEQUENTIAL DAMAGES ARISING IN ANY WAY OUT OF THE USE OF THE PACKAGE, |
188
|
|
|
|
|
|
|
EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
189
|
|
|
|
|
|
|
|
190
|
|
|
|
|
|
|
|
191
|
|
|
|
|
|
|
=cut |
192
|
|
|
|
|
|
|
|
193
|
|
|
|
|
|
|
1; # End of SQL::Smart |