X-Git-Url: http://koha-dev.rot13.org:8081/gitweb/?a=blobdiff_plain;f=C4%2FSQLHelper.pm;h=21f5fa179b0799f05f3fe6f8cea79e1f80897d0e;hb=98d8e1609af181fdfc05d748e27ac24f984b22e8;hp=4c74df25999d101e0c495f2250094639b56dc227;hpb=bfc3ea8cc4f0746d4d5658d7c2e2353855bd2b29;p=koha_fer diff --git a/C4/SQLHelper.pm b/C4/SQLHelper.pm index 4c74df2599..21f5fa179b 100644 --- a/C4/SQLHelper.pm +++ b/C4/SQLHelper.pm @@ -27,9 +27,26 @@ use C4::Debug; require Exporter; use vars qw($VERSION @ISA @EXPORT_OK %EXPORT_TAGS); +eval { + my $servers = C4::Context->config('memcached_servers'); + if ($servers) { + require Memoize::Memcached; + import Memoize::Memcached qw(memoize_memcached); + + my $memcached = { + servers => [$servers], + key_prefix => C4::Context->config('memcached_namespace') || 'koha', + expire_time => 600 + }; # cache for 10 mins + + memoize_memcached( '_get_columns', memcached => $memcached ); + memoize_memcached( 'GetPrimaryKeys', memcached => $memcached ); + } +}; + BEGIN { # set the version for version checking - $VERSION = 0.5; + $VERSION = 3.07.00.049; require Exporter; @ISA = qw(Exporter); @EXPORT_OK=qw( @@ -38,11 +55,15 @@ BEGIN { SearchInTable UpdateInTable GetPrimaryKeys + clear_columns_cache ); %EXPORT_TAGS = ( all =>[qw( InsertInTable DeleteInTable SearchInTable UpdateInTable GetPrimaryKeys)] ); } +my $tablename; +my $hashref; + =head1 NAME C4::SQLHelper - Perl Module containing convenience functions for SQL Handling @@ -227,12 +248,29 @@ Get the Primary Key field names of the table =cut -sub GetPrimaryKeys($) { +sub GetPrimaryKeys { my $tablename=shift; my $hash_columns=_get_columns($tablename); return grep { $hash_columns->{$_}->{'Key'} =~/PRI/i} keys %$hash_columns; } + +=head2 clear_columns_cache + + C4::SQLHelper->clear_columns_cache(); + +cleans the internal cache of sysprefs. Please call this method if +you update a tables structure. Otherwise, your new changes +will not be seen by this process. + +=cut + +sub clear_columns_cache { + %$hashref = (); +} + + + =head2 _get_columns _get_columns($tablename) @@ -246,17 +284,25 @@ With =cut -sub _get_columns($) { - my ($tablename)=@_; - my $dbh=C4::Context->dbh; - my $sth=$dbh->prepare_cached(qq{SHOW COLUMNS FROM $tablename }); - $sth->execute; - my $columns= $sth->fetchall_hashref(qw(Field)); +sub _get_columns { + my ($tablename) = @_; + unless ( exists( $hashref->{$tablename} ) ) { + my $dbh = C4::Context->dbh; + my $sth = $dbh->prepare_cached(qq{SHOW COLUMNS FROM $tablename }); + $sth->execute; + my $columns = $sth->fetchall_hashref(qw(Field)); + $hashref->{$tablename} = $columns; + } + return $hashref->{$tablename}; } =head2 _filter_columns - _filter_columns($tablename,$research, $filtercolumns) +=over 4 + +_filter_columns($tablename,$research, $filtercolumns) + +=back Given - a tablename @@ -268,7 +314,7 @@ If it is not for research purpose, filter primary keys =cut -sub _filter_columns ($$;$) { +sub _filter_columns { my ($tablename,$research, $filtercolumns)=@_; if ($filtercolumns){ return (@$filtercolumns); @@ -360,7 +406,7 @@ sub _filter_hash{ ## supposed to be a hash of simple values, hashes of arrays could be implemented $filter_input->{$field}=format_date_in_iso($filter_input->{$field}) if $columns->{$field}{Type}=~/date/ && - $filter_input->{$field} && $filter_input->{$field} !~C4::Dates->regexp("iso"); + $filter_input->{$field} !~C4::Dates->regexp("iso"); my ($tmpkeys, $localvalues)=_Process_Operands($filter_input->{$field},"$tablename.$field",$searchtype,$columns); if (@$tmpkeys){ push @values, @$localvalues;