lib/DB/Object/Cache/Tables.pm

filter: Statementstmt 71.5% 78 / 109 Branchbran 35.7% 15 / 42 Conditioncond 19.5% 8 / 41 MC/DCmcdc 0.0% 0 / 21 Subroutinesub 85.0% 17 / 20 Podpod 100% 10 / 10 totaltotal 52.6% 128 / 243 CC 40 SCAR 32.9
CC
40
Cov
53%
get
90.0
read
56.0
set
18.7
CRAP 26.8
« lib/DB/Object/Fields/Field.pm ↑ Summary blib/lib/DB/Object/Postgres/Lo.pm »
1 # -*- perl -*-
2 ##----------------------------------------------------------------------------
3 ## Database Object Interface - ~/lib/DB/Object/Cache/Tables.pm
4 ## Version v0.101.1
5 ## Copyright(c) 2026 DEGUEST Pte. Ltd.
6 ## Author: Jacques Deguest <jack@deguest.jp>
7 ## Created 2017/07/19
8 ## Modified 2026/08/05
9 ## All rights reserved
10 ##
11 ## This program is free software; you can redistribute it and/or modify it
12 ## under the same terms as Perl itself.##
13 ##----------------------------------------------------------------------------##
14 package DB::Object::Cache::Tables;
15 BEGIN
16 8 ▶{
Subroutine BEGIN: called CC=1 SCAR=0.0
17 8 ▶ use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
18 8 ▶ use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
19 8 warnings::register_categories( 'DB::Object' );
20 8 ▶ use parent qw( Module::Generic );
Subroutine BEGIN: called CC=1 SCAR=0.0
21 8 ▶ use vars qw( $VERSION $EXCEPTION_CLASS );
Subroutine BEGIN: called CC=1 SCAR=0.0
22 8 ▶ use JSON;
Subroutine BEGIN: called CC=1 SCAR=0.0
23 8 ▶ use Fcntl qw( :flock );
Subroutine BEGIN: called CC=1 SCAR=0.0
24 8 ▶ use Module::Generic::File qw( sys_tmpdir );
Subroutine BEGIN: called CC=1 SCAR=0.0
25 8 our $EXCEPTION_CLASS = $DB::Object::EXCEPTION_CLASS;
26 8 our $VERSION = 'v0.101.1';
27 };
28  
29 8 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
30 8 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
31  
32 sub init
33 3 ▶{
Subroutine init: called CC=4 SCAR=14.1
Pod documented
34 3 my $self = shift( @_ );
35 3 $self->{cache} = {};
36 3 $self->{cache_dir} = sys_tmpdir();
37 3 $self->{cache_file} = "$self->{cache_dir}/sql_tables.json";
38 3 $self->{timeout} = 86400;
39 3 $self->{_exception_class} = $EXCEPTION_CLASS;
40 3 ▶ $self->SUPER::init( @_ ) || return( $self->pass_error );
Branch: 1 branch
BranchTrueFalseTotal
$self->SUPER::init(@_) or return $self->pass_error 3 0 3
41 3 $self->{updated} = '';
42 3 ▶ $self->cache_dir( $self->{cache_dir} ) if( $self->{cache_dir} );
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"cache_dir"} 3 0 3
43 3 ▶ $self->cache_file( $self->{cache_file} ) if( $self->{cache_file} );
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"cache_file"} 3 0 3
44 3 return( $self );
45 }
46  
47 3 ▶sub cache { return( shift->_set_get_hash( 'cache', @_ ) ); }
Subroutine cache: called CC=1 SCAR=0.0
Pod documented
48  
49 sub cache_dir
50 6 ▶{
Subroutine cache_dir: called CC=2 SCAR=7.0
Pod documented
51 6 my $self = shift( @_ );
52 6 ▶ if( @_ )
Branch: 1 branch
BranchTrueFalseTotal
if (@_) 6 0 6
53 {
54 6 my $v = shift( @_ );
55 6 $self->{cache_dir} = $v;
56 6 $self->cache_file( "$v/sql_tables.json" );
57 }
58 6 return( $self->{cache_dir} );
59 }
60  
61 sub cache_file
62 15 ▶{
Subroutine cache_file: called CC=6 SCAR=25.4
Pod documented
63 15 my $self = shift( @_ );
64 15 ▶ if( @_ )
Branch: 1 branch
BranchTrueFalseTotal
if (@_) 9 6 15
65 {
66 9 ▶ 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
90
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 9 ▶ return( $f ) if( $f eq $self->{cache_file} );
Branch: 1 branch
BranchTrueFalseTotal
if $f eq $self->{"cache_file"} 9 0 9
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 6 return( $self->{cache_file} );
79 }
80  
81 sub get
82 0 ▶{
Subroutine get: not called CC=9 SCAR=45.0
Pod documented
83 0 my $self = shift( @_ );
84 0 my $opts = {};
85 0 ▶ $opts = shift( @_ ) if( @_ && $self->_is_hash( $_[0] => 'strict' ) );
Branch: 1 branch
BranchTrueFalseTotal
if @_ and $self->_is_hash($_[0], "strict") 0 0 0
Condition0%0/3
@_ and $self->_is_hash($_[0], "strict")
!ll&&!rl&&r
000
MC/DC0%0/2
@_ and $self->_is_hash($_[0], "strict")
@_ $self->_is_hash($_[0], "strict")
Truth table0%0/3
A and B
A= @_B= $self->_is_hash($_[0], "strict")
ABresult
0X0
100
111
86 0 foreach my $k ( qw( host port driver ) )
87 {
88 0 ▶ return( $self->error( "Parameter \"$k\" is missing." ) ) if( !length( $opts->{ $k } ) );
Branch: 1 branch
BranchTrueFalseTotal
unless length $opts->{$k} 0 0 0
89 }
90 0 my $cache = $self->cache;
91 0 my $timeout = $self->timeout;
92 0 my $part = {};
93 0 ▶ 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 0 0
94 0 $part = $cache->{ $opts->{host} }->{ $opts->{driver} }->{ $opts->{port} }->{ $opts->{database} };
95 0 my $ts = $part->{timestamp};
96 0 ▶ 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 0 0 0
Condition0%0/6
$opts->{"ignore_ttl"} or $ts and time - $ts < $timeout
l!l&&r!l&&!r
000
$ts and time - $ts < $timeout
!ll&&!rl&&r
000
MC/DC0%0/3
$opts->{"ignore_ttl"} or $ts and time - $ts < $timeout
$opts->{"ignore_ttl"} $ts time - $ts < $timeout
Truth table0%0/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 0 ▶{
Subroutine read: not called CC=7 SCAR=40.3
Pod documented
102 0 my $self = shift( @_ );
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 $hash = $tables_cache_file->load_json || do
110 0 ▶ {
Condition0%0/3
$tables_cache_file->load_json || do { warn 'An error occured while decoding json data from the table cache file: ', $tables_cache_file->error if $self->_is_warnings_enabled('DB::Object') }
l!l&&r!l&&!r
000
MC/DC0%0/2
$tables_cache_file->load_json || do { warn 'An error occured while decoding json data from the table cache file: ', $tables_cache_file->error if $self->_is_warnings_enabled('DB::Object') }
$tables_cache_file->load_json do { warn 'An error occured while decoding json data from the table cache file: ', $tables_cache_file->error if $self->_is_warnings_enabled('DB::Object') }
Truth table0%0/3
A || B
A= $tables_cache_file->load_jsonB= do { warn 'An error occured while decoding json data from the table cache file: ', $tables_cache_file->error if $self->_is_warnings_enabled('DB::Object') }
ABresult
1X1
011
000
111 0 ▶ warn( "An error occured while decoding json data from the table cache file: ", $tables_cache_file->error ) if( $self->_is_warnings_enabled( 'DB::Object' ) );
Branch: 1 branch
BranchTrueFalseTotal
if $self->_is_warnings_enabled("DB::Object") 0 0 0
112 };
113 }
114 0 return( $hash );
115 }
116  
117 sub set
118 3 ▶{
Subroutine set: called CC=12 SCAR=29.3
Pod documented
119 3 my $self = shift( @_ );
120 3 ▶ 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
30
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
121 3 ▶ 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 3 3
122 3 foreach my $k ( qw( host port driver tables ) )
123 {
124 12 ▶ return( $self->error( "Tables cache provided is missing the \"$k\" key." ) ) if( !length( $hash->{ $k } ) );
Branch: 1 branch
BranchTrueFalseTotal
unless length $hash->{$k} 0 12 12
125 }
126 3 ▶ 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 3 3
127 ## Possibly reload the cache if the modification date changed
128 3 my $cache = $self->cache;
129 3 my $f = $self->cache_file;
130 3 my $last_update = $self->updated;
131 3 ▶ 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 3 3
Condition50%3/6
-s $f and $last_update and (stat $f)[9] != $last_update
!ll&&!rl&&r
300
-s $f and $last_update
!ll&&!rl&&r
120
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
132 {
133 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
134 }
135 3 ▶ $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" 3 0 3
136 3 $cache->{ $hash->{host} }->{ $hash->{driver} }->{ $hash->{port} }->{ $hash->{database} }->{tables} = $hash->{tables};
137 3 $cache->{ $hash->{host} }->{ $hash->{driver} }->{ $hash->{port} }->{ $hash->{database} }->{timestamp} = time();
138 3 ▶ if( !defined( $self->write( $cache ) ) )
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $self->write($cache)) 0 3 3
139 {
140 0 return;
141 }
142 3 return( $self );
143 }
144  
145 0 ▶sub timeout { return( shift->_set_get_number( 'timeout', @_ ) ); }
Subroutine timeout: not called CC=1 SCAR=6.9
Pod documented
146  
147 6 ▶sub updated { return( shift->_set_get_number( 'updated', @_ ) ); }
Subroutine updated: called CC=1 SCAR=0.0
Pod documented
148  
149 sub write
150 3 ▶{
Subroutine write: called CC=6 SCAR=19.6
Pod documented
151 3 my $self = shift( @_ );
152 3 ▶ 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
30
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
153 3 ▶ 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
30
shift @_ or $self->cache_file
l!l&&r!l&&!r
030
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
154 3 $tables_cache_file = $self->new_file( $tables_cache_file );
155 3 ▶ 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 3 3
156 3 ▶ $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) 3 0 3
157 return( $self->pass_error( $tables_cache_file->error ) );
158 3 $self->updated( $tables_cache_file->finfo->mtime );
159 3 return( -s( $tables_cache_file ) );
160 }
161  
162 8 1;
163 # NOTE: POD
164 __END__
« lib/DB/Object/Fields/Field.pm ↑ Summary blib/lib/DB/Object/Postgres/Lo.pm »