| 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 | use strict; | ||
| 19 | use warnings; | ||
| 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.0'; | ||
| 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 || | ||
| 110 | warn( "An error occured while decoding json data from the table cache file: ", $tables_cache_file->error ); | ||
| 111 | } | ||
| 112 | return( $hash ); | ||
| 113 | } | ||
| 114 | |||
| 115 | sub set | ||
| 116 | { | ||
| 117 | my $self = shift( @_ ); | ||
| 118 | my $hash = shift( @_ ) || return( $self->error( "No hash reference was provided to add to tables cache." ) ); | ||
| 119 | return( $self->error( "Hash reference provided for tables cache ($hash) is not a hash reference." ) ) if( !$self->_is_hash( $hash => 'strict' ) ); | ||
| 120 | foreach my $k ( qw( host port driver tables ) ) | ||
| 121 | { | ||
| 122 | return( $self->error( "Tables cache provided is missing the \"$k\" key." ) ) if( !length( $hash->{ $k } ) ); | ||
| 123 | } | ||
| 124 | return( $self->error( "\"tables\" property in cache data is not an array reference." ) ) if( !$self->_is_array( $hash->{tables} ) ); | ||
| 125 | ## Possibly reload the cache if the modification date changed | ||
| 126 | my $cache = $self->cache; | ||
| 127 | my $f = $self->cache_file; | ||
| 128 | my $last_update = $self->updated; | ||
| 129 | if( -s( $f ) && $last_update && ( stat( $f ) )[9] != $last_update ) | ||
| 130 | { | ||
| 131 | $cache = $self->read( $f ) || return; | ||
| 132 | } | ||
| 133 | $cache->{ $hash->{host} }->{ $hash->{driver} }->{ $hash->{port} }->{ $hash->{database} } = {} if( ref( $cache->{ $hash->{host} }->{ $hash->{driver} }->{ $hash->{port} }->{ $hash->{database} } ) ne 'HASH' ); | ||
| 134 | $cache->{ $hash->{host} }->{ $hash->{driver} }->{ $hash->{port} }->{ $hash->{database} }->{tables} = $hash->{tables}; | ||
| 135 | $cache->{ $hash->{host} }->{ $hash->{driver} }->{ $hash->{port} }->{ $hash->{database} }->{timestamp} = time(); | ||
| 136 | if( !defined( $self->write( $cache ) ) ) | ||
| 137 | { | ||
| 138 | return; | ||
| 139 | } | ||
| 140 | return( $self ); | ||
| 141 | } | ||
| 142 | |||
| 143 | sub timeout { return( shift->_set_get_number( 'timeout', @_ ) ); } | ||
| 144 | |||
| 145 | sub updated { return( shift->_set_get_number( 'updated', @_ ) ); } | ||
| 146 | |||
| 147 | sub write | ||
| 148 | { | ||
| 149 | my $self = shift( @_ ); | ||
| 150 | my $hash = shift( @_ ) || return( $self->error( "No table cache data was provided to write to cache file \"", $self->cache_file, "\"." ) ); | ||
| 151 | my $tables_cache_file = shift( @_ ) || $self->cache_file || return( $self->error( "No cache file was set to write data to it." ) ); | ||
| 152 | $tables_cache_file = $self->new_file( $tables_cache_file ); | ||
| 153 | return( $self->error( "Tables cache data provided is not an hash reference." ) ) if( ref( $hash ) ne 'HASH' ); | ||
| 154 | $tables_cache_file->unload_json( $hash ) || | ||
| 155 | return( $self->pass_error( $tables_cache_file->error ) ); | ||
| 156 | $self->updated( $tables_cache_file->finfo->mtime ); | ||
| 157 | return( -s( $tables_cache_file ) ); | ||
| 158 | } | ||
| 159 | |||
| 160 | 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 |