1 package Koha::Persistant;
3 # Copyright (c) 2012 Dobrica Pavlinusic
4 # This file is part of Koha.
6 # Koha is free software; you can redistribute it and/or modify it under the
7 # terms of the GNU General Public License as published by the Free Software
8 # Foundation; either version 2 of the License, or (at your option) any later
11 # Koha is distributed in the hope that it will be useful, but WITHOUT ANY
12 # WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
13 # A PARTICULAR PURPOSE. See the GNU General Public License for more details.
15 # You should have received a copy of the GNU General Public License along with
16 # Koha; if not, write to the Free Software Foundation, Inc., 59 Temple Place,
17 # Suite 330, Boston, MA 02111-1307 USA
24 use Data::Dump qw(dump);
27 use version; our $VERSION = qv('1.0.0');
30 qw( sql_cache authorized_value )
35 Koha::Persistant - make data objects in Koha persistant
39 Koha needs nice centralized way to cache data for plack
41 Name of this module was choosen to be non-conflicting with possible
48 $row = sql_cache($sql, $value1 [, $value2, ... ]);
50 Takes C<SELECT col1,col2 FROM table WHERE value1 = ? AND value2 = ?>
51 SQL query and cache result returning cached row.
55 Syntax inside SQL query will override default cache key generation
56 which is simple normalization of SQL strings.
61 warn "## Koha::Persistent::DESTROY";
73 $key = $1 if $key =~ s/^.*\s*--\s*key:\s*(.+)//;
74 my $full = join(' ', $key, @var);
75 # FIXME make multi-dimensional hash out of this?
77 if ( exists $_sql_cache->{$full} ) {
78 warn "### _sql_cache HIT $key\n";
79 $stats->{$key}->[0]++;
80 return $_sql_cache->{$full};
82 warn "### _sql_cache MISS $key\n";
83 $stats->{$key}->[1]++;
84 my $dbh = C4::Context->dbh;
85 my $sth = $dbh->prepare( $sql );
86 $sth->execute( @var );
87 my $v = $sth->fetchrow_hashref;
88 $_sql_cache->{$key} = $v;
89 warn "# row $key = ",dump($v);
93 =head2 autorhised_value
95 my $row = authorised_value( category => $category, $value );
99 sub authorised_value {
100 shift if $_[0] eq 'category';
101 my ( $category, $value ) = @_;
102 my $row = _sql_cache("SELECT lib, lib_opac FROM authorised_values WHERE category = ? AND authorised_value = ? -- key:autorhised_value", $category, $value);