]> wagnertech.de Git - mfinanz.git/blobdiff - SL/DBUtils.pm
GuV und BWA: Bei Projektfilter Zeitraum richtig setzen
[mfinanz.git] / SL / DBUtils.pm
index 2d9c472afb1ed78be574ff0bdbfe6f58bcfb2864..e8cd304eed043bd509481ac821e5e56980c491e5 100644 (file)
@@ -1,5 +1,7 @@
 package SL::DBUtils;
 
 package SL::DBUtils;
 
+use SL::Util qw(trim);
+
 require Exporter;
 our @ISA = qw(Exporter);
 
 require Exporter;
 our @ISA = qw(Exporter);
 
@@ -8,6 +10,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
              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);
              prepare_execute_query prepare_query
              create_sort_spec does_table_exist
              add_token);
@@ -29,7 +32,9 @@ sub conv_b {
 
 sub conv_date {
   my ($value) = @_;
 
 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 {
 }
 
 sub conv_dateq {
@@ -221,11 +226,11 @@ sub selectall_as_map {
   my %hash;
   if ('' eq ref $value_col) {
     while (my $ref = $sth->fetchrow_hashref()) {
   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()) {
     }
   } else {
     while (my $ref = $sth->fetchrow_hashref()) {
-      $hash{$ref->{$key_col}} = { map { $_ => $ref->{$_} } @{ $value_col } };
+      $hash{$ref->{$key_col} // ''} = { map { $_ => $ref->{$_} } @{ $value_col } };
     }
   }
 
     }
   }
 
@@ -236,6 +241,25 @@ sub selectall_as_map {
   return %hash;
 }
 
   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);
 
 sub create_sort_spec {
   $main::lxdebug->enter_sub(2);
 
@@ -369,7 +393,7 @@ __END__
 
 =head1 NAME
 
 
 =head1 NAME
 
-SL::DBUTils.pm: All about Databaseconections in Lx
+SL::DBUTils.pm: All about database connections in kivitendo
 
 =head1 SYNOPSIS
 
 
 =head1 SYNOPSIS
 
@@ -402,7 +426,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.
 
 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:
 
 
 Every function here should accomplish the follwing things: