X-Git-Url: http://wagnertech.de/git?a=blobdiff_plain;f=SL%2FDBUtils.pm;h=f0952440e4aaa6595fe621eb4e77eee48ca11b46;hb=577042c61c5e1fffb8747079b4f9826e51532ee8;hp=689340ac2299224b8e6ec2c6ec14fff0c5f78e42;hpb=188696a433d68ae7e7546086a4491cdd377d3264;p=kivitendo-erp.git diff --git a/SL/DBUtils.pm b/SL/DBUtils.pm index 689340ac2..f0952440e 100644 --- a/SL/DBUtils.pm +++ b/SL/DBUtils.pm @@ -1,17 +1,19 @@ 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_hashref_query selectall_array_query selectcol_array_query selectall_as_map selectall_ids prepare_execute_query prepare_query create_sort_spec does_table_exist - add_token); + add_token check_trgm); use strict; @@ -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 { @@ -88,7 +92,7 @@ sub dump_query { my $self_filename = 'SL/DBUtils.pm'; my $filename = $self_filename; my ($caller_level, $line, $subroutine); - while ($filename eq $self_filename) { + while ($filename =~ m{$self_filename$}) { (undef, $filename, $line, $subroutine) = caller $caller_level++; } @@ -167,16 +171,15 @@ sub selectall_hashref_query { return wantarray ? @{ $result } : $result; } -sub selectall_array_query { +sub selectall_array_query { goto &selectcol_array_query; } + +sub selectcol_array_query { $main::lxdebug->enter_sub(2); my ($form, $dbh, $query) = splice(@_, 0, 3); my $sth = prepare_execute_query($form, $dbh, $query, @_); - my @result; - while (my ($value) = $sth->fetchrow_array()) { - push(@result, $value); - } + my @result = @{ $dbh->selectcol_arrayref($sth) }; $sth->finish(); $main::lxdebug->leave_sub(2); @@ -222,11 +225,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 +347,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,14 +385,37 @@ sub add_token { return ($token, @vals); } +sub like { + my ($string) = @_; + + return "%" . SL::Util::trim($string // '') . "%"; +} + +sub role_is_superuser { + my ($dbh, $login) = @_; + my ($is_superuser) = $dbh->selectrow_array(qq|SELECT usesuper FROM pg_user WHERE usename = ?|, undef, $login); + + return $is_superuser; +} + +sub check_trgm { + my ($dbh) = @_; + + my $version = $dbh->selectrow_array(qq|SELECT installed_version FROM pg_available_extensions WHERE name = 'pg_trgm'|); + + return !!$version; +} + 1; __END__ +=encoding utf-8 + =head1 NAME -SL::DBUTils.pm: All about Databaseconections in Lx +SL::DBUtils.pm: All about database connections in kivitendo =head1 SYNOPSIS @@ -400,6 +426,8 @@ SL::DBUTils.pm: All about Databaseconections in Lx conv_dateq($str) quote_db_date($date) + my $dbh = SL::DB->client->dbh; + do_query($form, $dbh, $query) do_statement($form, $sth, $query) @@ -409,30 +437,107 @@ SL::DBUTils.pm: All about Databaseconections in Lx my $all_results_ref = selectall_hashref_query($form, $dbh, $query) my $first_result_hash_ref = selectfirst_hashref_query($form, $dbh, $query); - my @first_result = selectfirst_array_query($form, $dbh, $query); # == + my @first_result = selectfirst_array_query($form, $dbh, $query); my @first_result = selectrow_query($form, $dbh, $query); + my @values = selectcol_array_query($form, $dbh, $query); + my %sort_spec = create_sort_spec(%params); =head1 DESCRIPTION -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. +DBUtils provides wrapper functions for low level database retrieval. It saves +you the trouble of mucking around with statement handles for small database +queries and does exception handling in the common cases for you. + +Query and retrieval functions share the parameter scheme: + + query_or_retrieval(C