lib/DB/Object/Cache/Tables.pm

filter: Statementstmt 84.1% 90 / 107 Branchbran 47.5% 19 / 40 Conditioncond 27.5% 11 / 40 MC/DCmcdc 0.0% 0 / 20 Subroutinesub 95.0% 19 / 20 Podpod 100% 10 / 10 totaltotal 62.8% 149 / 237 CC 39 SCAR 25.5
CC
39
Cov
64%
read
42.0
set
18.7
get
13.0
CRAP 12.9
« blib/lib/DB/Object/Cache/Tables.pm ↑ Summary blib/lib/DB/Object/Query/Elements.pm »
1 # -*- perl -*-
2 ##----------------------------------------------------------------------------
3 ## Database Object Interface - ~/lib/DB/Object/Cache/Tables.pm
4 ## Version v0.101.0
5 ## Copyright(c) 2024 DEGUEST Pte. Ltd.
6 ## Author: Jacques Deguest <jack@deguest.jp>
7 ## Created 2017/07/19
8 ## Modified 2026/03/22
9 ## All rights reserved
10 ##
11 ##
12 ## This program is free software; you can redistribute it and/or modify it
13 ## under the same terms as Perl itself.
14 ##----------------------------------------------------------------------------
15 package DB::Object::Cache::Tables;
16 BEGIN
17 {
18 5 ▶ use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
19 5 ▶ use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
20 5 ▶ use parent qw( Module::Generic );
Subroutine BEGIN: called CC=1 SCAR=0.0
21 5 ▶ use vars qw( $VERSION $EXCEPTION_CLASS );
Subroutine BEGIN: called CC=1 SCAR=0.0
22 5 ▶ use JSON;
Subroutine BEGIN: called CC=1 SCAR=0.0
23 5 ▶ use Fcntl qw( :flock );
Subroutine BEGIN: called CC=1 SCAR=0.0
24 5 ▶ use Module::Generic::File qw( sys_tmpdir );
Subroutine BEGIN: called CC=1 SCAR=0.0
25 5 ▶ our $EXCEPTION_CLASS = $DB::Object::EXCEPTION_CLASS;
Subroutine BEGIN: called CC=1 SCAR=0.0
26 5 our $VERSION = 'v0.101.0';
27 };
28  
29 5 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
30 5 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
31  
32 sub init
33 {
34 2 ▶ my $self = shift( @_ );
Subroutine init: called CC=4 SCAR=14.1
Pod documented
35 2 $self->{cache} = {};
36 2 $self->{cache_dir} = sys_tmpdir();
37 2 $self->{cache_file} = "$self->{cache_dir}/sql_tables.json";
38 2 $self->{timeout} = 86400;
39 2 $self->{_exception_class} = $EXCEPTION_CLASS;
40 2 ▶ $self->SUPER::init( @_ ) || return( $self->pass_error );
Branch: 1 branch
BranchTrueFalseTotal
$self->SUPER::init(@_) or return $self->pass_error 0 2 2
41 2 $self->{updated} = '';
42 2 ▶ $self->cache_dir( $self->{cache_dir} ) if( $self->{cache_dir} );
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"cache_dir"} 2 0 2
43 2 ▶ $self->cache_file( $self->{cache_file} ) if( $self->{cache_file} );
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"cache_file"} 2 0 2
44 2 return( $self );
45 }
46  
47 6 ▶sub cache { return( shift->_set_get_hash( 'cache', @_ ) ); }
Subroutine cache: called CC=1 SCAR=0.0
Pod documented
48  
49 sub cache_dir
50 {
51 4 ▶ my $self = shift( @_ );
Subroutine cache_dir: called CC=2 SCAR=7.0
Pod documented
52 4 ▶ if( @_ )
Branch: 1 branch
BranchTrueFalseTotal
if (@_) 4 0 4
53 {
54 4 my $v = shift( @_ );
55 4 $self->{cache_dir} = $v;
56 4 $self->cache_file( "$v/sql_tables.json" );
57 }
58 4 return( $self->{cache_dir} );
59 }
60  
61 sub cache_file
62 {
63 10 ▶ my $self = shift( @_ );
Subroutine cache_file: called CC=6 SCAR=25.4
Pod documented
64 10 ▶ if( @_ )
Branch: 1 branch
BranchTrueFalseTotal
if (@_) 6 4 10
65 {
66 6 ▶ my $f = shift( @_ ) || return( $self->error( "No tables cache file path was provided." ) );
Condition50%1/2
shift @_ || (return $self->error('No tables cache file path was provided.'))
l!l
60
MC/DC0%0/1
shift @_ || (return $self->error('No tables cache file path was provided.'))
shift @_
Truth table50%1/2
A
A= shift @_
Aresult
11
00
67 # No change
68 6 ▶ return( $f ) if( $f eq $self->{cache_file} );
Branch: 1 branch
BranchTrueFalseTotal
if $f eq $self->{"cache_file"} 6 0 6
69 0 ▶ if( -e( $f ) )
Branch: 1 branch
BranchTrueFalseTotal
if (-e $f) 0 0 0
70 {
71 0 my $mtime = ( stat( $f ) )[9];
72 0 $self->updated( $mtime );
73 0 ▶ my $hash = $self->read( $f ) || return;
Condition0%0/2
$self->read($f) || (return)
l!l
00
MC/DC0%0/1
$self->read($f) || (return)
$self->read($f)
Truth table0%0/2
A
A= $self->read($f)
Aresult
11
00
74 0 $self->cache( $hash );
75 }
76 0 $self->{cache_file} = $f;
77 }
78 4 return( $self->{cache_file} );
79 }
80  
81 sub get
82 {
83 4 ▶ my $self = shift( @_ );
Subroutine get: called CC=9 SCAR=25.6
Pod documented
84 4 my $opts = {};
85 4 ▶ $opts = shift( @_ ) if( @_ && $self->_is_hash( $_[0] => 'strict' ) );
Branch: 1 branch
BranchTrueFalseTotal
if @_ and $self->_is_hash($_[0], "strict") 4 0 4
Condition33%1/3
@_ and $self->_is_hash($_[0], "strict")
!ll&&!rl&&r
004
MC/DC0%0/2
@_ and $self->_is_hash($_[0], "strict")
@_ $self->_is_hash($_[0], "strict")
Truth table33%1/3
A and B
A= @_B= $self->_is_hash($_[0], "strict")
ABresult
0X0
100
111
86 4 foreach my $k ( qw( host port driver ) )
87 {
88 12 ▶ return( $self->error( "Parameter \"$k\" is missing." ) ) if( !length( $opts->{ $k } ) );
Branch: 1 branch
BranchTrueFalseTotal
unless length $opts->{$k} 0 12 12
89 }
90 4 my $cache = $self->cache;
91 4 my $timeout = $self->timeout;
92 4 my $part = {};
93 4 ▶ return( [] ) if( !exists( $cache->{ $opts->{host} }->{ $opts->{driver} }->{ $opts->{port} }->{ $opts->{database} }->{tables} ) );
Branch: 1 branch
BranchTrueFalseTotal
unless exists $$cache{$opts->{"host"}}{$opts->{"driver"}}{$opts->{"port"}}{$opts->{"database"}}{"tables"} 0 4 4
94 4 $part = $cache->{ $opts->{host} }->{ $opts->{driver} }->{ $opts->{port} }->{ $opts->{database} };
95 4 my $ts = $part->{timestamp};
96 4 ▶ return( $part->{tables} ) if( $opts->{ignore_ttl} || ( $ts && ( time() - $ts < $timeout ) ) );
Branch: 1 branch
BranchTrueFalseTotal
if $opts->{"ignore_ttl"} or $ts and time - $ts < $timeout 4 0 4
Condition33%2/6
$opts->{"ignore_ttl"} or $ts and time - $ts < $timeout
l!l&&r!l&&!r
040
$ts and time - $ts < $timeout
!ll&&!rl&&r
004
MC/DC0%0/3
$opts->{"ignore_ttl"} or $ts and time - $ts < $timeout
$opts->{"ignore_ttl"} $ts time - $ts < $timeout
Truth table25%1/4
A or B and C
A= $opts->{"ignore_ttl"}B= $tsC= time - $ts < $timeout
ABCresult
1XX1
0111
00X0
0100
97 0 return( [] );
98 }
99  
100 sub read
101 {
102 0 ▶ my $self = shift( @_ );
Subroutine read: not called CC=6 SCAR=37.4
Pod documented
103 0 ▶ my $tables_cache_file = shift( @_ ) || $self->cache_file || return( {} );
Condition0%0/5
shift @_ || $self->cache_file || (return {})
l!l
00
shift @_ or $self->cache_file
l!l&&r!l&&!r
000
MC/DC0%0/2
shift @_ || $self->cache_file || (return {})
shift @_ $self->cache_file
Truth table0%0/3
A or B
A= shift @_B= $self->cache_file
ABresult
1X1
011
000
104 0 $tables_cache_file = $self->new_file( $tables_cache_file );
105 0 my $hash = {};
106 0 my $j = JSON->new->relaxed;
107 0 ▶ if( $tables_cache_file->exists && !$tables_cache_file->is_empty )
Branch: 1 branch
BranchTrueFalseTotal
if ($tables_cache_file->exists and not $tables_cache_file->is_empty) 0 0 0
Condition0%0/3
$tables_cache_file->exists and not $tables_cache_file->is_empty
!ll&&!rl&&r
000
MC/DC0%0/2
$tables_cache_file->exists and not $tables_cache_file->is_empty
$tables_cache_file->exists not $tables_cache_file->is_empty
Truth table0%0/3
A and not(B)
A= $tables_cache_file->existsB= not $tables_cache_file->is_empty
ABresult
0X0
100
111
108 {
109 0 ▶ $hash = $tables_cache_file->load_json ||
Condition0%0/2
$tables_cache_file->load_json || warn('An error occured while decoding json data from the table cache file: ', $tables_cache_file->error)
l!l
00
MC/DC0%0/1
$tables_cache_file->load_json || warn('An error occured while decoding json data from the table cache file: ', $tables_cache_file->error)
$tables_cache_file->load_json
Truth table0%0/2
A
A= $tables_cache_file->load_json
Aresult
11
00
110 warn( "An error occured while decoding json data from the table cache file: ", $tables_cache_file->error );
111 }
112 0 return( $hash );
113 }
114  
115 sub set
116 {
117 2 ▶ my $self = shift( @_ );
Subroutine set: called CC=12 SCAR=29.3
Pod documented
118 2 ▶ my $hash = shift( @_ ) || return( $self->error( "No hash reference was provided to add to tables cache." ) );
Condition50%1/2
shift @_ || (return $self->error('No hash reference was provided to add to tables cache.'))
l!l
20
MC/DC0%0/1
shift @_ || (return $self->error('No hash reference was provided to add to tables cache.'))
shift @_
Truth table50%1/2
A
A= shift @_
Aresult
11
00
119 2 ▶ return( $self->error( "Hash reference provided for tables cache ($hash) is not a hash reference." ) ) if( !$self->_is_hash( $hash => 'strict' ) );
Branch: 1 branch
BranchTrueFalseTotal
unless $self->_is_hash($hash, "strict") 0 2 2
120 2 foreach my $k ( qw( host port driver tables ) )
121 {
122 8 ▶ return( $self->error( "Tables cache provided is missing the \"$k\" key." ) ) if( !length( $hash->{ $k } ) );
Branch: 1 branch
BranchTrueFalseTotal
unless length $hash->{$k} 0 8 8
123 }
124 2 ▶ return( $self->error( "\"tables\" property in cache data is not an array reference." ) ) if( !$self->_is_array( $hash->{tables} ) );
Branch: 1 branch
BranchTrueFalseTotal
unless $self->_is_array($hash->{"tables"}) 0 2 2
125 ## Possibly reload the cache if the modification date changed
126 2 my $cache = $self->cache;
127 2 my $f = $self->cache_file;
128 2 my $last_update = $self->updated;
129 2 ▶ if( -s( $f ) && $last_update && ( stat( $f ) )[9] != $last_update )
Branch: 1 branch
BranchTrueFalseTotal
if (-s $f and $last_update and (stat $f)[9] != $last_update) 0 2 2
Condition50%3/6
-s $f and $last_update and (stat $f)[9] != $last_update
!ll&&!rl&&r
200
-s $f and $last_update
!ll&&!rl&&r
110
MC/DC0%0/3
-s $f and $last_update and (stat $f)[9] != $last_update
-s $f $last_update (stat $f)[9] != $last_update
Truth table50%2/4
A and B and C
A= -s $fB= $last_updateC= (stat $f)[9] != $last_update
ABCresult
0XX0
10X0
1100
1111
130 {
131 0 ▶ $cache = $self->read( $f ) || return;
Condition0%0/2
$self->read($f) || (return)
l!l
00
MC/DC0%0/1
$self->read($f) || (return)
$self->read($f)
Truth table0%0/2
A
A= $self->read($f)
Aresult
11
00
132 }
133 2 ▶ $cache->{ $hash->{host} }->{ $hash->{driver} }->{ $hash->{port} }->{ $hash->{database} } = {} if( ref( $cache->{ $hash->{host} }->{ $hash->{driver} }->{ $hash->{port} }->{ $hash->{database} } ) ne 'HASH' );
Branch: 1 branch
BranchTrueFalseTotal
if ref $$cache{$hash->{"host"}}{$hash->{"driver"}}{$hash->{"port"}}{$hash->{"database"}} ne "HASH" 2 0 2
134 2 $cache->{ $hash->{host} }->{ $hash->{driver} }->{ $hash->{port} }->{ $hash->{database} }->{tables} = $hash->{tables};
135 2 $cache->{ $hash->{host} }->{ $hash->{driver} }->{ $hash->{port} }->{ $hash->{database} }->{timestamp} = time();
136 2 ▶ if( !defined( $self->write( $cache ) ) )
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $self->write($cache)) 0 2 2
137 {
138 0 return;
139 }
140 2 return( $self );
141 }
142  
143 4 ▶sub timeout { return( shift->_set_get_number( 'timeout', @_ ) ); }
Subroutine timeout: called CC=1 SCAR=0.0
Pod documented
144  
145 4 ▶sub updated { return( shift->_set_get_number( 'updated', @_ ) ); }
Subroutine updated: called CC=1 SCAR=0.0
Pod documented
146  
147 sub write
148 {
149 2 ▶ my $self = shift( @_ );
Subroutine write: called CC=6 SCAR=19.6
Pod documented
150 2 ▶ my $hash = shift( @_ ) || return( $self->error( "No table cache data was provided to write to cache file \"", $self->cache_file, "\"." ) );
Condition50%1/2
shift @_ || (return $self->error('No table cache data was provided to write to cache file "', $self->cache_file, '".'))
l!l
20
MC/DC0%0/1
shift @_ || (return $self->error('No table cache data was provided to write to cache file "', $self->cache_file, '".'))
shift @_
Truth table50%1/2
A
A= shift @_
Aresult
11
00
151 2 ▶ my $tables_cache_file = shift( @_ ) || $self->cache_file || return( $self->error( "No cache file was set to write data to it." ) );
Condition40%2/5
shift @_ || $self->cache_file || (return $self->error('No cache file was set to write data to it.'))
l!l
20
shift @_ or $self->cache_file
l!l&&r!l&&!r
020
MC/DC0%0/2
shift @_ || $self->cache_file || (return $self->error('No cache file was set to write data to it.'))
shift @_ $self->cache_file
Truth table33%1/3
A or B
A= shift @_B= $self->cache_file
ABresult
1X1
011
000
152 2 $tables_cache_file = $self->new_file( $tables_cache_file );
153 2 ▶ return( $self->error( "Tables cache data provided is not an hash reference." ) ) if( ref( $hash ) ne 'HASH' );
Branch: 1 branch
BranchTrueFalseTotal
if ref $hash ne "HASH" 0 2 2
154 2 ▶ $tables_cache_file->unload_json( $hash ) ||
Branch: 1 branch
BranchTrueFalseTotal
$tables_cache_file->unload_json($hash) or return $self->pass_error($tables_cache_file->error) 0 2 2
155 return( $self->pass_error( $tables_cache_file->error ) );
156 2 $self->updated( $tables_cache_file->finfo->mtime );
157 2 return( -s( $tables_cache_file ) );
158 }
159  
160 5 1;
161 # NOTE: POD
162 __END__
163  
164 =encoding utf-8
165  
166 =head1 NAME
167  
168 DB::Object::Cache::Tables - Table Cache
169  
170 =head1 SYNOPSIS
171  
172 my $cache = DB::Object::Cache::Tables->new({
173 timeout => 86400,
174 # This is automatically set
175 # cache_file => '/some/dir/sql_tables.json',
176 });
177 $dbh->cache_tables( $cache_tables );
178 $tables = $dbh->tables_info;
179 my $cache =
180 {
181 host => $host,
182 driver => $driver,
183 port => $port,
184 database => $database,
185 tables => $tables,
186 };
187 if( !defined( $cache->set( $cache ) ) )
188 {
189 warn( "Unable to write to tables cache: ", $cache->error, "\n" );
190 }
191  
192 # Returning an array reference of tables hash reference definition
193 $all = $cache_tables->get({
194 host => $self->host,
195 driver => $self->driver,
196 port => $self->port,
197 database => $db,
198 }) || do
199  
200 =head1 VERSION
201  
202 v0.101.0
203  
204 =head1 DESCRIPTION
205  
206 This is a simple way to maintain a cache of database tables in a session. When a connection object is created, it will issue a query to get the list of all tables and views in the database and pass it to L<DB::Object::Cache::Tables>, and save its object. It is then used later several times such as when instantiating table objects.
207  
208 =head1 METHODS
209  
210 =head2 init
211  
212 Possible parameters:
213  
214 =over 4
215  
216 =item I<cache_dir>
217  
218 An absolute path to a directory that will contain the json cache file. Beware that if you run your script from the web, this directory must be writable by the http server user.
219  
220 =item I<cache_file>
221  
222 Alternatively to I<cache_dir>, you can provide an absolute path to the json cache file.
223  
224 =item I<timeout>
225  
226 An amount of time in second until the cache file becomes obsolete.
227  
228 =back
229  
230 =head2 cache
231  
232 Returns the hash reference structure of the cache
233  
234 =head2 cache_dir
235  
236 Set or get the cache dir.
237  
238 When set, this will also set the cache file calling L</"cache_file">
239  
240 =head2 cache_file
241  
242 Set or get the cache file.
243  
244 When set, this will store the cache file modification time to check later if it has become obsolete and load its json data into the L</"cache">
245  
246 =head2 get
247  
248 Given an hash reference of parameters, this will return an array reference of table hash reference.
249  
250 Parameters are:
251  
252 =over 4
253  
254 =item I<host>
255  
256 =item I<driver>
257  
258 =item I<port>
259  
260 =item I<database>
261  
262 =back
263  
264 =head2 read
265  
266 Given a full path to a json cache file, this will read the file and return its data as a hash reference.
267  
268 If an error occurs while reading the json cache file, it will issue a warning using B<warn> and return an empty hash reference.
269  
270 =head2 set
271  
272 Provided with an hash reference of parameters, this will add it to the cache data and write it to the file.
273  
274 Parameters are:
275  
276 =over 4
277  
278 =item I<host>
279  
280 =item I<driver>
281  
282 =item I<port>
283  
284 =item I<database>
285  
286 =item I<tables>
287  
288 An array reference of hash reference containing table definition as returned by L<DB::Object::table_info>
289  
290 =back
291  
292 =head2 timeout
293  
294 Set/get the cache file timeout.
295  
296 If the current unix timestamp minus the cache file timestamp is higher than the timeout, the cache file has expired.
297  
298 =head2 updated
299  
300 Set/get the cache file last modified unix timestamp
301  
302 =head2 write
303  
304 Provided with a cache data, which is a hash reference and optionally the full path to the cache file, and B<write> will write the hash data as a json to the cache file.
305  
306 If no cache file is provided as a second argument, it will use the default one set up when the object was instantiated.
307  
308 It returns the size of the cache file or return undef and set the B<error>
309  
310 =head1 COPYRIGHT
311  
312 Copyright (c) 2000-2019 DEGUEST Pte. Ltd.
313  
314 =head1 SEE ALSO
315  
316 L<DB::Object>
317  
318 =head1 AUTHOR
319  
320 Jacques Deguest E<lt>F<jack@deguest.jp>E<gt>
321  
322 =head1 COPYRIGHT & LICENSE
323  
324 Copyright (c) 2018-2021 DEGUEST Pte. Ltd.
325  
326 You can use, copy, modify and redistribute this package and associated
327 files under the same terms as Perl itself.
328  
329 =cut
« blib/lib/DB/Object/Cache/Tables.pm ↑ Summary blib/lib/DB/Object/Query/Elements.pm »