DB-Handle Caches: DATESTYLE korrekt setzen
[kivitendo-erp.git] / SL / DBUtils.pm
index e0a362f..8e1ad45 100644 (file)
@@ -8,6 +8,7 @@ our @EXPORT = qw(conv_i conv_date conv_dateq do_query selectrow_query do_stateme
              selectfirst_hashref_query selectfirst_array_query
              selectall_hashref_query selectall_array_query
              selectall_as_map
+             selectall_ids
              prepare_execute_query prepare_query
              create_sort_spec does_table_exist
              add_token);
@@ -19,6 +20,14 @@ sub conv_i {
   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;
@@ -84,7 +93,7 @@ sub dump_query {
   }
 
   while ($query =~ /\?/) {
-    my $value = shift(@_);
+    my $value = shift || '';
     $value =~ s/\'/\\\'/g;
     $value = "'${value}'";
     $query =~ s/\?/$value/;
@@ -228,6 +237,25 @@ sub selectall_as_map {
   return %hash;
 }
 
+sub selectall_ids {
+  $main::lxdebug->enter_sub(2);
+
+  my ($form, $dbh, $query, $key_col) = splice(@_, 0, 4);
+
+  my $sth = prepare_execute_query($form, $dbh, $query, @_);
+
+  my @ids;
+  while (my $ref = $sth->fetchrow_arrayref()) {
+    push @ids, $ref->[$key_col];
+  }
+
+  $sth->finish;
+
+  $main::lxdebug->leave_sub(2);
+
+  return @ids;
+}
+
 sub create_sort_spec {
   $main::lxdebug->enter_sub(2);
 
@@ -307,16 +335,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;
@@ -324,6 +365,7 @@ sub add_token {
            : scalar @_ == 1 ? sprintf '%s = ?',     $col
            :                  undef;
     },
+    map({ $_ => $_long_token->($_) } qw(LIKE ILIKE >= <= > <)),
   );
 
   $method = $methods{$method} || $method;
@@ -347,7 +389,7 @@ __END__
 
 =head1 NAME
 
-SL::DBUTils.pm: All about Databaseconections in Lx
+SL::DBUTils.pm: All about database connections in kivitendo
 
 =head1 SYNOPSIS
 
@@ -374,13 +416,13 @@ 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.
 
 DBUtils relies heavily on two parameters which have to be passed to almost every function: $form and $dbh.
   - $form is used for error handling only. It can be omitted in theory, but should not.
-  - $dbh is a handle to the databe, as returned by the DBI::connect routine. If you don't have an active connectiong, you can query $form->get_standard_dbh() to get a generic no_auto connection. Don't forget to commit in this case!
+  - $dbh is a handle to the database, as returned by the DBI::connect routine. If you don't have an active connection, you can query $form->get_standard_dbh() to get a generic no_auto connection. Don't forget to commit in this case!
 
 
 Every function here should accomplish the follwing things:
@@ -569,7 +611,7 @@ Sven Schoeling E<lt>s.schoeling@linet-services.deE<gt>
 
 =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 +625,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