X-Git-Url: http://wagnertech.de/git?a=blobdiff_plain;f=SL%2FDBUtils.pm;h=2d9c472afb1ed78be574ff0bdbfe6f58bcfb2864;hb=fee5532a132c44dcfc1743393cba00c8e3397176;hp=f2e99a8e398794f98df950241f63b218ff13bb9c;hpb=451843cf750b655eb2678de12c369af74bca9867;p=kivitendo-erp.git diff --git a/SL/DBUtils.pm b/SL/DBUtils.pm index f2e99a8e3..2d9c472af 100644 --- a/SL/DBUtils.pm +++ b/SL/DBUtils.pm @@ -1,9 +1,9 @@ package SL::DBUtils; require Exporter; -@ISA = qw(Exporter); +our @ISA = qw(Exporter); -@EXPORT = qw(conv_i conv_date conv_dateq do_query selectrow_query do_statement +our @EXPORT = qw(conv_i conv_date conv_dateq do_query selectrow_query do_statement dump_query quote_db_date selectfirst_hashref_query selectfirst_array_query selectall_hashref_query selectall_array_query @@ -12,11 +12,21 @@ require Exporter; create_sort_spec does_table_exist add_token); +use strict; + sub conv_i { my ($value, $default) = @_; return (defined($value) && "$value" ne "") ? $value * 1 : $default; } +# boolean escape +sub conv_b { + my ($value, $default) = @_; + return !defined $value && defined $default ? $default + : $value ? 't' + : 'f'; +} + sub conv_date { my ($value) = @_; return (defined($value) && "$value" ne "") ? $value : undef; @@ -36,7 +46,7 @@ sub do_query { my ($form, $dbh, $query) = splice(@_, 0, 3); - dump_query(LXDebug::QUERY, '', $query, @_); + dump_query(LXDebug->QUERY(), '', $query, @_); my $result; if (0 == scalar(@_)) { @@ -57,7 +67,7 @@ sub do_statement { my ($form, $sth, $query) = splice(@_, 0, 3); - dump_query(LXDebug::QUERY, '', $query, @_); + dump_query(LXDebug->QUERY(), '', $query, @_); my $result; if (0 == scalar(@_)) { @@ -74,14 +84,15 @@ sub do_statement { sub dump_query { my ($level, $msg, $query) = splice(@_, 0, 3); - my $filename = $self_filename = 'SL/DBUtils.pm'; - my $caller_level; + my $self_filename = 'SL/DBUtils.pm'; + my $filename = $self_filename; + my ($caller_level, $line, $subroutine); while ($filename eq $self_filename) { (undef, $filename, $line, $subroutine) = caller $caller_level++; } while ($query =~ /\?/) { - my $value = shift(@_); + my $value = shift || ''; $value =~ s/\'/\\\'/g; $value = "'${value}'"; $query =~ s/\?/$value/; @@ -111,7 +122,7 @@ sub prepare_query { my ($form, $dbh, $query) = splice(@_, 0, 3); - dump_query(LXDebug::QUERY, '', $query, @_); + dump_query(LXDebug->QUERY(), '', $query, @_); my $sth = $dbh->prepare($query) || $form->dberror($query); @@ -125,7 +136,7 @@ sub prepare_execute_query { my ($form, $dbh, $query) = splice(@_, 0, 3); - dump_query(LXDebug::QUERY, '', $query, @_); + dump_query(LXDebug->QUERY(), '', $query, @_); my $sth = $dbh->prepare($query) || $form->dberror($query); if (scalar(@_) != 0) { @@ -144,12 +155,11 @@ sub selectall_hashref_query { my ($form, $dbh, $query) = splice(@_, 0, 3); - my $sth = prepare_execute_query($form, $dbh, $query, @_); - my $result = []; - while (my $ref = $sth->fetchrow_hashref()) { - push(@{ $result }, $ref); - } - $sth->finish(); + dump_query(LXDebug->QUERY(), '', $query, @_); + + # this works back 'til at least DBI 1.46 on perl 5.8.4 on Debian Sarge (2004) + my $result = $dbh->selectall_arrayref($query, { Slice => {} }, @_) + or $form->dberror($query . (@_ ? " (" . join(", ", @_) . ")" : '')); $main::lxdebug->leave_sub(2); @@ -305,16 +315,29 @@ sub add_token { my %params = @_; my $col = $params{col}; my $val = $params{val}; - my $method = $params{method} || '='; my $escape = $params{esc} || sub { $_ }; + my $method = $params{esc} =~ /^start|end|substr$/ ? 'ILIKE' : $params{method} || '='; $val = [ $val ] unless ref $val eq 'ARRAY'; my %escapes = ( id => \&conv_i, + bool => \&conv_b, date => \&conv_date, + start => sub { $_[0] . '%' }, + end => sub { '%' . $_[0] }, + substr => sub { '%' . $_[0] . '%' }, ); + my $_long_token = sub { + my $op = shift; + sub { + my $col = shift; + return scalar @_ ? join ' OR ', ("$col $op ?") x scalar @_, + : undef; + } + }; + my %methods = ( '=' => sub { my $col = shift; @@ -322,6 +345,7 @@ sub add_token { : scalar @_ == 1 ? sprintf '%s = ?', $col : undef; }, + map({ $_ => $_long_token->($_) } qw(LIKE ILIKE >= <= > <)), ); $method = $methods{$method} || $method; @@ -372,7 +396,7 @@ SL::DBUTils.pm: All about Databaseconections in Lx =head1 DESCRIPTION -DBUtils is the attempt to reduce the amount of overhead it takes to retrieve information from the database in Lx-Office. Previously it would take about 15 lines of code just to get one single integer out of the database, including failure procedures and importing the necessary packages. Debugging would take even more. +DBUtils is the attempt to reduce the amount of overhead it takes to retrieve information from the database in kivitendo. Previously it would take about 15 lines of code just to get one single integer out of the database, including failure procedures and importing the necessary packages. Debugging would take even more. Using DBUtils most database procedures can be reduced to defining the query, executing it, and retrieving the result. Let DBUtils handle the rest. Whenever there is a database operation not covered in DBUtils, add it here, rather than working around it in the backend code. @@ -555,8 +579,6 @@ Dumps a query using LXDebug->message, using LEVEL for the debug-level of LXDebug =back -=head1 SEE ALSO - =head1 MODULE AUTHORS Moritz Bunkus Em.bunkus@linet-services.deE @@ -569,7 +591,7 @@ Sven Schoeling Es.schoeling@linet-services.deE =head1 COPYRIGHT AND LICENSE -Copyright 2007 by Lx-Office Community +Copyright 2007 by kivitendo Community This program is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by @@ -583,4 +605,5 @@ GNU General Public License for more details. You should have received a copy of the GNU General Public License along with this program; if not, write to the Free Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. + =cut