blib/lib/DB/Object/Cache/Tables.pm untested

filter: Statementstmt 0.0% 0 / 112 Branchbran 0.0% 0 / 38 Conditioncond 0.0% 0 / 45 MC/DCmcdc 0.0% 0 / 24 Subroutinesub 0.0% 0 / 20 Podpod 0.0% 0 / 11 totaltotal 0.0% 0 / 250 CC 20 SCAR 29.5
CC
20
Cov
0%
set
56.0
get
30.0
cache_file
20.0
CRAP 19.1
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 {
17 use strict;
18 use warnings;
19 warnings::register_categories( 'DB::Object' );
20 use parent qw( Module::Generic );
21 use vars qw( $VERSION $EXCEPTION_CLASS );
22 use JSON;
23 use Fcntl qw( :flock );
24 use Module::Generic::File qw( sys_tmpdir );
25 our $EXCEPTION_CLASS = $DB::Object::EXCEPTION_CLASS;
26 our $VERSION = 'v0.101.1';
27 };
28  
29 use strict;
30 use warnings;
31  
32 sub init
33 {
34 my $self = shift( @_ );
35 $self->{cache} = {};
36 $self->{cache_dir} = sys_tmpdir();
37 $self->{cache_file} = "$self->{cache_dir}/sql_tables.json";
38 $self->{timeout} = 86400;
39 $self->{_exception_class} = $EXCEPTION_CLASS;
40 $self->SUPER::init( @_ ) || return( $self->pass_error );
41 $self->{updated} = '';
42 $self->cache_dir( $self->{cache_dir} ) if( $self->{cache_dir} );
43 $self->cache_file( $self->{cache_file} ) if( $self->{cache_file} );
44 return( $self );
45 }
46  
47 sub cache { return( shift->_set_get_hash( 'cache', @_ ) ); }
48  
49 sub cache_dir
50 {
51 my $self = shift( @_ );
52 if( @_ )
53 {
54 my $v = shift( @_ );
55 $self->{cache_dir} = $v;
56 $self->cache_file( "$v/sql_tables.json" );
57 }
58 return( $self->{cache_dir} );
59 }
60  
61 sub cache_file
62 {
63 my $self = shift( @_ );
64 if( @_ )
65 {
66 my $f = shift( @_ ) || return( $self->error( "No tables cache file path was provided." ) );
67 # No change
68 return( $f ) if( $f eq $self->{cache_file} );
69 if( -e( $f ) )
70 {
71 my $mtime = ( stat( $f ) )[9];
72 $self->updated( $mtime );
73 my $hash = $self->read( $f ) || return;
74 $self->cache( $hash );
75 }
76 $self->{cache_file} = $f;
77 }
78 return( $self->{cache_file} );
79 }
80  
81 sub get
82 {
83 my $self = shift( @_ );
84 my $opts = {};
85 $opts = shift( @_ ) if( @_ && $self->_is_hash( $_[0] => 'strict' ) );
86 foreach my $k ( qw( host port driver ) )
87 {
88 return( $self->error( "Parameter \"$k\" is missing." ) ) if( !length( $opts->{ $k } ) );
89 }
90 my $cache = $self->cache;
91 my $timeout = $self->timeout;
92 my $part = {};
93 return( [] ) if( !exists( $cache->{ $opts->{host} }->{ $opts->{driver} }->{ $opts->{port} }->{ $opts->{database} }->{tables} ) );
94 $part = $cache->{ $opts->{host} }->{ $opts->{driver} }->{ $opts->{port} }->{ $opts->{database} };
95 my $ts = $part->{timestamp};
96 return( $part->{tables} ) if( $opts->{ignore_ttl} || ( $ts && ( time() - $ts < $timeout ) ) );
97 return( [] );
98 }
99  
100 sub read
101 {
102 my $self = shift( @_ );
103 my $tables_cache_file = shift( @_ ) || $self->cache_file || return( {} );
104 $tables_cache_file = $self->new_file( $tables_cache_file );
105 my $hash = {};
106 my $j = JSON->new->relaxed;
107 if( $tables_cache_file->exists && !$tables_cache_file->is_empty )
108 {
109 $hash = $tables_cache_file->load_json || do
110 {
111 warn( "An error occured while decoding json data from the table cache file: ", $tables_cache_file->error ) if( $self->_is_warnings_enabled( 'DB::Object' ) );
112 };
113 }
114 return( $hash );
115 }
116  
117 sub set
118 {
119 my $self = shift( @_ );
120 my $hash = shift( @_ ) || return( $self->error( "No hash reference was provided to add to tables cache." ) );
121 return( $self->error( "Hash reference provided for tables cache ($hash) is not a hash reference." ) ) if( !$self->_is_hash( $hash => 'strict' ) );
122 foreach my $k ( qw( host port driver tables ) )
123 {
124 return( $self->error( "Tables cache provided is missing the \"$k\" key." ) ) if( !length( $hash->{ $k } ) );
125 }
126 return( $self->error( "\"tables\" property in cache data is not an array reference." ) ) if( !$self->_is_array( $hash->{tables} ) );
127 ## Possibly reload the cache if the modification date changed
128 my $cache = $self->cache;
129 my $f = $self->cache_file;
130 my $last_update = $self->updated;
131 if( -s( $f ) && $last_update && ( stat( $f ) )[9] != $last_update )
132 {
133 $cache = $self->read( $f ) || return;
134 }
135 $cache->{ $hash->{host} }->{ $hash->{driver} }->{ $hash->{port} }->{ $hash->{database} } = {} if( ref( $cache->{ $hash->{host} }->{ $hash->{driver} }->{ $hash->{port} }->{ $hash->{database} } ) ne 'HASH' );
136 $cache->{ $hash->{host} }->{ $hash->{driver} }->{ $hash->{port} }->{ $hash->{database} }->{tables} = $hash->{tables};
137 $cache->{ $hash->{host} }->{ $hash->{driver} }->{ $hash->{port} }->{ $hash->{database} }->{timestamp} = time();
138 if( !defined( $self->write( $cache ) ) )
139 {
140 return;
141 }
142 return( $self );
143 }
144  
145 sub timeout { return( shift->_set_get_number( 'timeout', @_ ) ); }
146  
147 sub updated { return( shift->_set_get_number( 'updated', @_ ) ); }
148  
149 sub write
150 {
151 my $self = shift( @_ );
152 my $hash = shift( @_ ) || return( $self->error( "No table cache data was provided to write to cache file \"", $self->cache_file, "\"." ) );
153 my $tables_cache_file = shift( @_ ) || $self->cache_file || return( $self->error( "No cache file was set to write data to it." ) );
154 $tables_cache_file = $self->new_file( $tables_cache_file );
155 return( $self->error( "Tables cache data provided is not an hash reference." ) ) if( ref( $hash ) ne 'HASH' );
156 $tables_cache_file->unload_json( $hash ) ||
157 return( $self->pass_error( $tables_cache_file->error ) );
158 $self->updated( $tables_cache_file->finfo->mtime );
159 return( -s( $tables_cache_file ) );
160 }
161  
162 1;
163 # NOTE: POD
164 __END__