]> wagnertech.de Git - mfinanz.git/blobdiff - SL/DBUtils.pm
Wiederkerende Rechnungen: Konfiguration für automatischen Versand via E-Mail
[mfinanz.git] / SL / DBUtils.pm
index 689340ac2299224b8e6ec2c6ec14fff0c5f78e42..d060bcb8e01ef00fdd78ea7899d2132b042f5288 100644 (file)
@@ -1,10 +1,12 @@
 package SL::DBUtils;
 
+use SL::Util qw(trim);
+
 require Exporter;
 our @ISA = qw(Exporter);
 
 our @EXPORT = qw(conv_i conv_date conv_dateq do_query selectrow_query do_statement
-             dump_query quote_db_date
+             dump_query quote_db_date like
              selectfirst_hashref_query selectfirst_array_query
              selectall_hashref_query selectall_array_query
              selectall_as_map
@@ -30,7 +32,9 @@ sub conv_b {
 
 sub conv_date {
   my ($value) = @_;
-  return (defined($value) && "$value" ne "") ? $value : undef;
+  return undef if !defined $value;
+  $value = trim($value);
+  return $value eq "" ? undef : $value;
 }
 
 sub conv_dateq {
@@ -222,11 +226,11 @@ sub selectall_as_map {
   my %hash;
   if ('' eq ref $value_col) {
     while (my $ref = $sth->fetchrow_hashref()) {
-      $hash{$ref->{$key_col}} = $ref->{$value_col};
+      $hash{$ref->{$key_col} // ''} = $ref->{$value_col};
     }
   } else {
     while (my $ref = $sth->fetchrow_hashref()) {
-      $hash{$ref->{$key_col}} = { map { $_ => $ref->{$_} } @{ $value_col } };
+      $hash{$ref->{$key_col} // ''} = { map { $_ => $ref->{$_} } @{ $value_col } };
     }
   }
 
@@ -344,9 +348,9 @@ sub add_token {
     id     => \&conv_i,
     bool   => \&conv_b,
     date   => \&conv_date,
-    start  => sub { $_[0] . '%' },
-    end    => sub { '%' . $_[0] },
-    substr => sub { '%' . $_[0] . '%' },
+    start  => sub { trim($_[0]) . '%' },
+    end    => sub { '%' . trim($_[0]) },
+    substr => sub { like($_[0]) },
   );
 
   my $_long_token = sub {
@@ -382,6 +386,12 @@ sub add_token {
   return ($token, @vals);
 }
 
+sub like {
+  my ($string) = @_;
+
+  return "%" . SL::Util::trim($string // '') . "%";
+}
+
 1;
 
 
@@ -389,7 +399,7 @@ __END__
 
 =head1 NAME
 
-SL::DBUTils.pm: All about Databaseconections in Lx
+SL::DBUTils.pm: All about database connections in kivitendo
 
 =head1 SYNOPSIS
 
@@ -422,7 +432,7 @@ Using DBUtils most database procedures can be reduced to defining the query, exe
 
 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:
@@ -455,6 +465,12 @@ Database version of conv_date. Quotes STR before returning. Returns 'NULL' if ST
 Treats STR as a database date, quoting it. If STR equals current_date returns an escaped version which is treated as the current date by Postgres.
 Returns 'NULL' if STR is empty.
 
+=item like STR
+
+Turns C<STR> into an argument suitable for SQL's C<LIKE> and C<ILIKE>
+operators by Trimming the string C<STR> (removes leading and trailing
+whitespaces) and prepending and appending C<%>.
+
 =back
 
 =head2 QUERY FUNCTIONS