| 1 | package SQL::Smart; | ||||||||||
| 2 | |||||||||||
| 3 | 2 | ▶ | use 5.006; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 4 | #use strict; | ||||||||||
| 5 | 2 | ▶ | use warnings FATAL => 'all'; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 6 | 2 | ▶ | use DBI; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 7 | 2 | require Exporter; | |||||||||
| 8 | 2 | @ISA = qw(Exporter); | |||||||||
| 9 | 2 | @EXPORT = qw / for_dbh sql /; | |||||||||
| 10 | 2 | 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 | 2 | 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 | 0 | ▶ | sub get_dbh{ | ||||||||
|
Subroutine
get_dbh: not called CC=2 SCAR=17.9
Pod
undocumented
| |||||||||||
| 52 | 0 | ▶ | unless($dbh_cache){ | ||||||||
|
Branch: 1 branch
| |||||||||||
| 53 | 0 | die "Need to call for_dbh first!"; | |||||||||
| 54 | } | ||||||||||
| 55 | 0 | return $dbh_cache; | |||||||||
| 56 | } | ||||||||||
| 57 | |||||||||||
| 58 | 0 | ▶ | sub for_dbh { | ||||||||
|
Subroutine
for_dbh: not called CC=1 SCAR=6.9
Pod
documented
| |||||||||||
| 59 | 0 | $dbh_cache = shift; | |||||||||
| 60 | } | ||||||||||
| 61 | |||||||||||
| 62 | =head2 sql | ||||||||||
| 63 | |||||||||||
| 64 | =cut | ||||||||||
| 65 | |||||||||||
| 66 | 0 | ▶ | sub flatten_result{ | ||||||||
|
Subroutine
flatten_result: not called CC=3 SCAR=24.8
Pod
undocumented
| |||||||||||
| 67 | 0 | my @input = @_; | |||||||||
| 68 | 0 | my $has_more_than_one_key = (scalar(keys %{$input[0]}) > 1); | |||||||||
| 69 | my @output = map { | ||||||||||
| 70 | 0 | ▶ | $has_more_than_one_key ? $_ : ((values %$_)[0]) | ||||||||
|
Branch: 1 branch
| |||||||||||
| 71 | } @input; | ||||||||||
| 72 | 0 | ▶ | if(wantarray){ | ||||||||
|
Branch: 1 branch
| |||||||||||
| 73 | 0 | return @output; | |||||||||
| 74 | }else{ | ||||||||||
| 75 | 0 | return $output[0]; | |||||||||
| 76 | } | ||||||||||
| 77 | } | ||||||||||
| 78 | |||||||||||
| 79 | 0 | ▶ | sub sql{ | ||||||||
|
Subroutine
sql: not called CC=5 SCAR=34.0
Pod
documented
| |||||||||||
| 80 | 0 | my $sql_statement = shift; | |||||||||
| 81 | 0 | my @binds = @_; | |||||||||
| 82 | 0 | my $dbh = get_dbh(); | |||||||||
| 83 | 0 | ▶ | unless($sql_statement =~ /^\s*(?:select|describe)/i){ | ||||||||
|
Branch: 1 branch
| |||||||||||
| 84 | 0 | my $sth = $dbh->prepare($sql_statement); | |||||||||
| 85 | 0 | $sth->execute(@binds); | |||||||||
| 86 | #$dbh->commit(); | ||||||||||
| 87 | 0 | ▶ | if($sql_statement =~ /^\s*insert\b/i){ | ||||||||
|
Branch: 1 branch
| |||||||||||
| 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 | ▶ | if(wantarray){ | ||||||||
|
Branch: 1 branch
| |||||||||||
| 95 | 0 | return flatten_result(@$ra_result); | |||||||||
| 96 | }else{ | ||||||||||
| 97 | 0 | ▶ | return undef if scalar(@$ra_result) == 0; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 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<< <vegetabird at gmail.com> >> | ||||||||||
| 107 | |||||||||||
| 108 | =head1 BUGS | ||||||||||
| 109 | |||||||||||
| 110 | Please report any bugs or feature requests to C<bug-sql-smart at rt.cpan.org>, or through | ||||||||||
| 111 | the web interface at L<http://rt.cpan.org/NoAuth/ReportBug.html?Queue=SQL-Smart>. 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<http://rt.cpan.org/NoAuth/Bugs.html?Dist=SQL-Smart> | ||||||||||
| 131 | |||||||||||
| 132 | =item * AnnoCPAN: Annotated CPAN documentation | ||||||||||
| 133 | |||||||||||
| 134 | L<http://annocpan.org/dist/SQL-Smart> | ||||||||||
| 135 | |||||||||||
| 136 | =item * CPAN Ratings | ||||||||||
| 137 | |||||||||||
| 138 | L<http://cpanratings.perl.org/d/SQL-Smart> | ||||||||||
| 139 | |||||||||||
| 140 | =item * Search CPAN | ||||||||||
| 141 | |||||||||||
| 142 | L<http://search.cpan.org/dist/SQL-Smart/> | ||||||||||
| 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<http://www.perlfoundation.org/artistic_license_2_0> | ||||||||||
| 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 | 2 | 1; # End of SQL::Smart | |||||||||