| 1 | # -*- perl -*- | ||
| 2 | ##---------------------------------------------------------------------------- | ||
| 3 | ## Database Object Interface - ~/lib//mnt/src/perl/DB-Object/lib/DB/Object/SQLite.pm | ||
| 4 | ## Version v1.4.0 | ||
| 5 | ## Copyright(c) 2026 DEGUEST Pte. Ltd. | ||
| 6 | ## Author: Jacques Deguest <jack@deguest.jp> | ||
| 7 | ## Created 2017/07/19 | ||
| 8 | ## Modified 2026/09/23 | ||
| 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 | # This is the subclassable module for driver specific ones. | ||
| 15 | package DB::Object::SQLite; | ||
| 16 | BEGIN | ||
| 17 | { | ||
| 18 | use strict; | ||
| 19 | use warnings; | ||
| 20 | warnings::register_categories( 'DB::Object' ); | ||
| 21 | use vars qw( | ||
| 22 | $VERSION $CACHE_SIZE $CONNECT_VIA $ERROR $DEBUG | ||
| 23 | $USE_BIND $USE_CACHE $MOD_PERL | ||
| 24 | $PLACEHOLDER_REGEXP $DATATYPES_DICT $EXCEPTION_CLASS | ||
| 25 | ); | ||
| 26 | use DBI qw( :sql_types ); | ||
| 27 | eval { require DBD::SQLite; }; | ||
| 28 | die( $@ ) if( $@ ); | ||
| 29 | use parent qw( DB::Object ); | ||
| 30 | use POSIX (); | ||
| 31 | use DateTime::Lite; | ||
| 32 | use DateTime::Lite::TimeZone; | ||
| 33 | use Module::Generic::File qw( sys_tmpdir ); | ||
| 34 | # <https://metacpan.org/pod/DBD::SQLite::Constants> | ||
| 35 | # <https://www.sqlite.org/datatype3.html> | ||
| 36 | # <https://metacpan.org/pod/DBD::SQLite::Constants#datatypes-(fundamental_datatypes)> | ||
| 37 | # NULL, INTEGER, REAL, TEXT, BLOB | ||
| 38 | # Check DBD::SQLite::Constants | ||
| 39 | our $DATATYPES_DICT = | ||
| 40 | { | ||
| 41 | blob => { | ||
| 42 | constant => '', | ||
| 43 | name => 'SQLITE_BLOB', | ||
| 44 | re => qr/^BLOB/, | ||
| 45 | type => 'blob' | ||
| 46 | }, | ||
| 47 | bool => { | ||
| 48 | alias => [qw( boolean date datetime decimal numeric )], | ||
| 49 | constant => '', | ||
| 50 | name => 'SQLITE_NULL', | ||
| 51 | re => qr/^(NUMERIC|DECIMAL\(\d+,\d+\)|BOOLEAN|DATETIME|DATE)/, | ||
| 52 | type => 'bool' | ||
| 53 | }, | ||
| 54 | float => { | ||
| 55 | alias => [qw( double real ), 'double precision', ], | ||
| 56 | constant => '', | ||
| 57 | name => 'SQLITE_FLOAT', | ||
| 58 | re => qr/^(REAL|DOUBLE|DOUBLE\s+PRECISION|FLOAT)/, | ||
| 59 | type => 'float' | ||
| 60 | }, | ||
| 61 | integer => { | ||
| 62 | alias => [qw( int tinyint smallint mediumint bigint int2 int8 ), 'unsigned big int' ], | ||
| 63 | constant => '', | ||
| 64 | name => 'SQLITE_INTEGER', | ||
| 65 | re => qr/^(INT|INTEGER|TINYINT|SMALLINT|MEDIUMINT|BIGINT|UNSIGNED\s+BIG\s+INT|INT2|INT8)/, | ||
| 66 | type => 'integer', | ||
| 67 | }, | ||
| 68 | # Data type text is only available from version 1.71 onward | ||
| 69 | }; | ||
| 70 | if( $DBD::SQLite::VERSION >= 1.071 ) | ||
| 71 | { | ||
| 72 | $DATATYPES_DICT->{text} = | ||
| 73 | { | ||
| 74 | alias => [qw( character clob nchar nvarchar varchar ), 'native character', 'varying character' ], | ||
| 75 | constant => '', | ||
| 76 | name => 'SQLITE_TEXT', | ||
| 77 | re => qr/^(CHARACTER\(\d+\)|VARCHAR\(\d+\)|VARYING\s+CHARACTER\(\d+\)|NCHAR\(\d+\)|NATIVE\s+CHARACTER\(\d+\)|NVARCHAR\(\d+\)|TEXT|CLOB)/, | ||
| 78 | type => 'text' | ||
| 79 | }; | ||
| 80 | } | ||
| 81 | our $PLACEHOLDER_REGEXP = qr/(?:(?<![?\w])\?(?<index>[1-9]\d*)(?!\w))/; | ||
| 82 | our $EXCEPTION_CLASS = $DB::Object::EXCEPTION_CLASS; | ||
| 83 | our $VERSION = 'v1.4.0'; | ||
| 84 | }; | ||
| 85 | |||
| 86 | use strict; | ||
| 87 | use warnings; | ||
| 88 | # require DB::Object::SQLite::Statement; | ||
| 89 | # require DB::Object::SQLite::Tables; | ||
| 90 | our $DEBUG = 0; | ||
| 91 | our $CACHE_SIZE = 10; | ||
| 92 | our $USE_BIND = 0; | ||
| 93 | our $USE_CACHE = 0; | ||
| 94 | our $MOD_PERL = 0; | ||
| 95 | if( $INC{ 'Apache/DBI.pm' } && | ||
| 96 | substr( $ENV{GATEWAY_INTERFACE}|| '', 0, 8 ) eq 'CGI-Perl' ) | ||
| 97 | { | ||
| 98 | $CONNECT_VIA = "Apache::DBI::connect"; | ||
| 99 | $MOD_PERL++; | ||
| 100 | } | ||
| 101 | |||
| 102 | # Actually the one in DB::Object is used, because DBD::SQLite has no datatype constants of its own | ||
| 103 | # our $DATATYPES = {}; | ||
| 104 | |||
| 105 | our $PRIVATE_FUNCTIONS = | ||
| 106 | { | ||
| 107 | ceiling =>[1, \&_ceiling], | ||
| 108 | concat =>[-1, \&_concat], | ||
| 109 | curdate =>[0, \&_curdate], | ||
| 110 | curtime =>[0, \&_curtime], | ||
| 111 | dayname =>[1, \&_dayname], | ||
| 112 | dayofmonth =>[1, \&_dayofmonth], | ||
| 113 | dayofweek =>[1, \&_dayofweek], | ||
| 114 | dayofyear =>[1, \&_dayofyear], | ||
| 115 | distance_miles =>[4, \&_distance_miles], | ||
| 116 | # from_days =>[-1, \&_from_days], | ||
| 117 | from_unixtime =>[1, \&_from_unixtime], | ||
| 118 | hour =>[1, \&_hour], | ||
| 119 | lcase =>[1, \&_lcase], | ||
| 120 | left =>[2, \&_left], | ||
| 121 | locate =>[2, \&_locate], | ||
| 122 | log10 =>[1, \&_log10], | ||
| 123 | minute =>[1, \&_minute], | ||
| 124 | month =>[1, \&_month], | ||
| 125 | monthname =>[1, \&_monthname], | ||
| 126 | number_format =>[4, \&_number_format], | ||
| 127 | power =>[2, \&_power], | ||
| 128 | quarter =>[1, \&_quarter], | ||
| 129 | rand =>[0, \&_rand], | ||
| 130 | regexp =>[2, \&_regexp], | ||
| 131 | replace =>[3, \&_replace], | ||
| 132 | right =>[2, \&_right], | ||
| 133 | second =>[1, \&_second], | ||
| 134 | space =>[1, \&_space], | ||
| 135 | sprintf =>[-1, \&_sprintf], | ||
| 136 | to_days =>[1, \&_to_days], | ||
| 137 | # truncate =>[-1, \&_truncate], | ||
| 138 | ucase =>[1, \&_ucase], | ||
| 139 | unix_timestamp =>[1, \&_unix_timestamp], | ||
| 140 | week =>[1, \&_week], | ||
| 141 | weekday =>[1, \&_weekday], | ||
| 142 | year =>[1, \&_year], | ||
| 143 | }; | ||
| 144 | # See compile_options method | ||
| 145 | # This is very useful to know which features can be used | ||
| 146 | our $COMPILE_OPTIONS = []; | ||
| 147 | |||
| 148 | foreach my $type ( keys( %$DATATYPES_DICT ) ) | ||
| 149 | { | ||
| 150 | if( CORE::exists( $DATATYPES_DICT->{ $type }->{alias} ) && | ||
| 151 | ref( $DATATYPES_DICT->{ $type }->{alias} ) eq 'ARRAY' && | ||
| 152 | scalar( @{$DATATYPES_DICT->{ $type }->{alias}} ) ) | ||
| 153 | { | ||
| 154 | foreach my $alias ( @{$DATATYPES_DICT->{ $type }->{alias}} ) | ||
| 155 | { | ||
| 156 | next if( CORE::exists( $DATATYPES_DICT->{ $alias } ) ); | ||
| 157 | $DATATYPES_DICT->{ $alias } = $DATATYPES_DICT->{ $type }; | ||
| 158 | } | ||
| 159 | } | ||
| 160 | } | ||
| 161 | |||
| 162 | foreach my $type ( keys( %$DATATYPES_DICT ) ) | ||
| 163 | { | ||
| 164 | my $c = $DATATYPES_DICT->{ $type }->{name}; | ||
| 165 | my $code = \&{"DBD::SQLite::Constants::${c}"}; | ||
| 166 | my $val = eval | ||
| 167 | { | ||
| 168 | $code->(); | ||
| 169 | }; | ||
| 170 | if( $@ ) | ||
| 171 | { | ||
| 172 | warn( "Datatype \"DBD::SQLite::Constants::${c}\" is not defined in DBD::SQLite version $DBD::SQLite::VERSION: $@" ) if( warnings::enabled( 'DB::Object' ) ); | ||
| 173 | delete( $DATATYPES_DICT->{ $type } ); | ||
| 174 | } | ||
| 175 | else | ||
| 176 | { | ||
| 177 | $DATATYPES_DICT->{ $type }->{constant} = $val; | ||
| 178 | } | ||
| 179 | } | ||
| 180 | |||
| 181 | sub init | ||
| 182 | { | ||
| 183 | my $self = shift( @_ ); | ||
| 184 | $self->{_exception_class} = $EXCEPTION_CLASS; | ||
| 185 | $self->SUPER::init( @_ ) || return( $self->pass_error ); | ||
| 186 | $self->{driver} = 'SQLite'; | ||
| 187 | $self->{_func} = {}; | ||
| 188 | return( $self ); | ||
| 189 | } | ||
| 190 | |||
| 191 | # Get/set alias | ||
| 192 | # sub alias | ||
| 193 | |||
| 194 | # sub as_string | ||
| 195 | |||
| 196 | # sub avoid | ||
| 197 | |||
| 198 | sub attribute($;$@) | ||
| 199 | { | ||
| 200 | my $self = shift( @_ ); | ||
| 201 | # $h->{AttributeName} = ...; # set/write | ||
| 202 | # ... = $h->{AttributeName}; # get/read | ||
| 203 | # 1 means that the attribute may be modified | ||
| 204 | # 0 mneas that the attribute may only be read | ||
| 205 | my $name = shift( @_ ) if( @_ == 1 ); | ||
| 206 | my %arg = ( @_ ); | ||
| 207 | my %attr = | ||
| 208 | ( | ||
| 209 | ActiveKids => 0, | ||
| 210 | AutoCommit => 1, | ||
| 211 | AutoInactiveDestroy => 1, | ||
| 212 | CachedKids => 0, | ||
| 213 | ChildHandles => 0, | ||
| 214 | ChopBlanks => 1, | ||
| 215 | CursorName => 0, | ||
| 216 | Driver => 0, | ||
| 217 | ErrCount => 1, | ||
| 218 | Executed => 0, | ||
| 219 | FetchHashKeyName => 1, | ||
| 220 | HandleError => 1, | ||
| 221 | HandleSetErr => 1, | ||
| 222 | InactiveDestroy => 1, | ||
| 223 | Kids => 0, | ||
| 224 | NAME => 0, | ||
| 225 | NULLABLE => 0, | ||
| 226 | NUM_OF_FIELDS => 0, | ||
| 227 | NUM_OF_PARAMS => 0, | ||
| 228 | # Current database name | ||
| 229 | Name => 0, | ||
| 230 | PRECISION => 0, | ||
| 231 | PrintError => 1, | ||
| 232 | PrintWarn => 1, | ||
| 233 | Profile => 1, | ||
| 234 | RaiseError => 1, | ||
| 235 | RowCacheSize => 0, | ||
| 236 | RowsInCache => 0, | ||
| 237 | SCALE => 0, | ||
| 238 | ShowErrorStatement => 1, | ||
| 239 | Statement => 0, | ||
| 240 | TYPE => 0, | ||
| 241 | Taint => 1, | ||
| 242 | TaintIn => 1, | ||
| 243 | TaintOut => 1, | ||
| 244 | TraceLevel => 1, | ||
| 245 | Type => 1, | ||
| 246 | Username => 0, | ||
| 247 | Warn => 1, | ||
| 248 | # Not used | ||
| 249 | # LongReadLen => 1, | ||
| 250 | # LongTruncOk => 1, | ||
| 251 | # CompatMode => 1, | ||
| 252 | # If you set this to true, "do" method will process multiple statements at one go. | ||
| 253 | # This may be handy, but with performance penalty. See above for details. | ||
| 254 | sqlite_allow_multiple_statements => 1, | ||
| 255 | # If you set this to true, DBD::SQLite tries to see if the bind values are number or | ||
| 256 | # not, and does not quote if they are numbers. | ||
| 257 | sqlite_see_if_its_a_number => 1, | ||
| 258 | sqlite_unicode => 1, | ||
| 259 | # Returns an unprepared part of the statement you pass to "prepare". Typically this | ||
| 260 | # contains nothing but white spaces after a semicolon. | ||
| 261 | sqlite_unprepared_statements => 0, | ||
| 262 | # If you set this to true, DBD::SQLite tries to issue a "begin immediate transaction" | ||
| 263 | # (instead of "begin transaction") when necessary. | ||
| 264 | sqlite_use_immediate_transaction => 1, | ||
| 265 | sqlite_version => 0, | ||
| 266 | ); | ||
| 267 | # Only those attribute exist | ||
| 268 | # Using an a non existing attribute produce an exception, so we better avoid | ||
| 269 | if( $name ) | ||
| 270 | { | ||
| 271 | return( $self->{dbh}->{ $name } ) if( exists( $attr{ $name } ) ); | ||
| 272 | } | ||
| 273 | else | ||
| 274 | { | ||
| 275 | my $value; | ||
| 276 | while( ( $name, $value ) = each( %arg ) ) | ||
| 277 | { | ||
| 278 | # We intend to modifiy the value of an attribute | ||
| 279 | # we are allowed to modify this value if it is true | ||
| 280 | if( exists( $attr{ $name } ) && | ||
| 281 | defined( $value ) && | ||
| 282 | $attr{ $name } ) | ||
| 283 | { | ||
| 284 | $self->{dbh}->{ $name } = $value; | ||
| 285 | } | ||
| 286 | } | ||
| 287 | } | ||
| 288 | } | ||
| 289 | |||
| 290 | # sub available_drivers(@) | ||
| 291 | |||
| 292 | sub begin_work($;$@) | ||
| 293 | { | ||
| 294 | my $self = shift( @_ ); | ||
| 295 | local $@; | ||
| 296 | my $rv = eval | ||
| 297 | { | ||
| 298 | return( $self->{dbh}->begin_work( @_ ) ); | ||
| 299 | }; | ||
| 300 | if( $@ ) | ||
| 301 | { | ||
| 302 | return( $self->error( "Error calling begin_work(). Have you forgotten to turn on AutoCommit?: $@" ) ); | ||
| 303 | } | ||
| 304 | elsif( !$rv && $self->{dbh}->err ) | ||
| 305 | { | ||
| 306 | return( $self->error({ code => $self->{dbh}->err, message => $self->{dbh}->errstr }) ); | ||
| 307 | } | ||
| 308 | |||
| 309 | if( defined( $rv ) ) | ||
| 310 | { | ||
| 311 | $self->_transaction_started; | ||
| 312 | return( $rv || 1 ); | ||
| 313 | } | ||
| 314 | return; | ||
| 315 | } | ||
| 316 | |||
| 317 | # This method is common to DB::Object and DB::Object::Statement | ||
| 318 | # sub bind | ||
| 319 | |||
| 320 | # sub cache | ||
| 321 | |||
| 322 | sub can_update_delete_limit { return( shift->has_compile_option( 'ENABLE_UPDATE_DELETE_LIMIT' ) ); } | ||
| 323 | |||
| 324 | # sub check_driver(@;$@) | ||
| 325 | |||
| 326 | sub commit($;$@) | ||
| 327 | { | ||
| 328 | my $self = shift( @_ ); | ||
| 329 | local $@; | ||
| 330 | my $rv = eval | ||
| 331 | { | ||
| 332 | return( $self->{dbh}->commit( @_ ) ); | ||
| 333 | }; | ||
| 334 | if( $@ ) | ||
| 335 | { | ||
| 336 | return( $self->error( "Error calling commit(): $@" ) ); | ||
| 337 | } | ||
| 338 | elsif( !$rv && $self->{dbh}->err ) | ||
| 339 | { | ||
| 340 | return( $self->error({ code => $self->{dbh}->err, message => $self->{dbh}->errstr }) ); | ||
| 341 | } | ||
| 342 | |||
| 343 | if( defined( $rv ) ) | ||
| 344 | { | ||
| 345 | $self->_transaction_finished; | ||
| 346 | return( $rv || 1 ); | ||
| 347 | } | ||
| 348 | return; | ||
| 349 | } | ||
| 350 | |||
| 351 | sub compile_options | ||
| 352 | { | ||
| 353 | my $self = shift( @_ ); | ||
| 354 | return( [ @$COMPILE_OPTIONS ] ) if( scalar( @$COMPILE_OPTIONS ) ); | ||
| 355 | my $system_tmpdir = sys_tmpdir(); | ||
| 356 | my $compile_options_cache_file = $system_tmpdir->join( 'sql_sqlite_compile_options.cfg' ); | ||
| 357 | my @options = (); | ||
| 358 | # if( -e( $compile_options_cache_file ) && !-z( $compile_options_cache_file ) ) | ||
| 359 | if( $compile_options_cache_file->exists && !$compile_options_cache_file->is_empty ) | ||
| 360 | { | ||
| 361 | # my $fh = IO::File->new( "<$compile_options_cache_file" ) || return( $self->error( "Unable to read the sqlite compile options cache file \"$compile_options_cache_file\": $!" ) ); | ||
| 362 | my $fh = $compile_options_cache_file->open( '<' ) || return( $self->error( "Unable to read the sqlite compile options cache file \"$compile_options_cache_file\": ", $compile_options_cache_file->error ) ); | ||
| 363 | my @all = $fh->getlines; | ||
| 364 | # Remove any comments | ||
| 365 | @options = grep( !/^#/, @all ); | ||
| 366 | $fh->close; | ||
| 367 | if( scalar( @options ) ) | ||
| 368 | { | ||
| 369 | $COMPILE_OPTIONS = \@options; | ||
| 370 | # Return a copy only to be safe | ||
| 371 | return( [ @options ] ); | ||
| 372 | } | ||
| 373 | } | ||
| 374 | # If the cache file does not yet exists or there is no options, we do the query | ||
| 375 | my $dbh = $self->{dbh} || return( $self->error( "No active database handler available. You can only call this method once a database connection has been made." ) ); | ||
| 376 | my $all = $self->do( "PRAGMA compile_options" )->fetchall_arrayref; | ||
| 377 | @options = map( $_->[0], @$all ); | ||
| 378 | # my $fh = IO::File->new( ">$compile_options_cache_file" ) || return( $self->error( "Unable to write to sqlite compile options cache file \"$compile_options_cache_file\": $!" ) ); | ||
| 379 | my $fh = $compile_options_cache_file->open( '>' ) || return( $self->error( "Unable to write to sqlite compile options cache file \"$compile_options_cache_file\": ", $compile_options_cache_file->error ) ); | ||
| 380 | $fh->autoflush(1); | ||
| 381 | $fh->print( join( "\n", @options ), "\n" ) || return( $self->error( "Unable to write to the sqlite compile options cache file \"$compile_options_cache_file\": $!" ) ); | ||
| 382 | $fh->close; | ||
| 383 | $COMPILE_OPTIONS = \@options; | ||
| 384 | return( \@options ); | ||
| 385 | } | ||
| 386 | |||
| 387 | # connect() is inherited by DB::Object | ||
| 388 | |||
| 389 | # NOTE: sub constant_to_datatype is inherited | ||
| 390 | |||
| 391 | # sub copy | ||
| 392 | |||
| 393 | # sub create_table($;%) | ||
| 394 | |||
| 395 | # See DB::Object | ||
| 396 | # sub data_sources($;\%) | ||
| 397 | |||
| 398 | # sub data_type | ||
| 399 | |||
| 400 | # sub database | ||
| 401 | |||
| 402 | sub database_file | ||
| 403 | { | ||
| 404 | return( shift->{database_file} ); | ||
| 405 | } | ||
| 406 | |||
| 407 | sub databases | ||
| 408 | { | ||
| 409 | my $self = shift( @_ ); | ||
| 410 | # return( $self->error( "Not connected to PostgreSQL server yet. Issue $dbh->connect first." ) ) if( !$self->{dbh} ); | ||
| 411 | my $dbh; | ||
| 412 | # If there is no connection yet, then create one using the postgres login. | ||
| 413 | # There should not be a live user and database just to check what databases there are. | ||
| 414 | if( !$self->{dbh} ) | ||
| 415 | { | ||
| 416 | # try-catch | ||
| 417 | local $@; | ||
| 418 | $dbh = eval | ||
| 419 | { | ||
| 420 | $self->connect( @_ ); | ||
| 421 | }; | ||
| 422 | if( $@ ) | ||
| 423 | { | ||
| 424 | return( $self->error( "Error trying to connect to the SQLite database file: $@" ) ); | ||
| 425 | } | ||
| 426 | $dbh or return( $self->pass_error ); | ||
| 427 | } | ||
| 428 | else | ||
| 429 | { | ||
| 430 | $dbh = $self; | ||
| 431 | } | ||
| 432 | my $temp = $dbh->do( 'PRAGMA database_list' )->fetchall_arrayref( {} ); | ||
| 433 | my @dbases = map( $_->{name}, @$temp ); | ||
| 434 | return( @dbases ); | ||
| 435 | } | ||
| 436 | |||
| 437 | # NOTE: sub datatype_dict is inherited | ||
| 438 | |||
| 439 | # NOTE: sub datatype_to_constant is inherited | ||
| 440 | |||
| 441 | # NOTE: sub datatypes is in inherited | ||
| 442 | |||
| 443 | sub func | ||
| 444 | { | ||
| 445 | my $self = shift( @_ ); | ||
| 446 | my $table = shift( @_ ); | ||
| 447 | # e.g. table_attributes to get the detail information on table columns | ||
| 448 | my $func_name = shift( @_ ); | ||
| 449 | # Returns: | ||
| 450 | # NAME attribute name | ||
| 451 | # TYPE attribute type | ||
| 452 | # SIZE attribute size (-1 for variable size) | ||
| 453 | # NULLABLE flag nullable | ||
| 454 | # DEFAULT default value | ||
| 455 | # CONSTRAINT constraint | ||
| 456 | # PRIMARY_KEY flag is_primary_key | ||
| 457 | # REMARKS attribute description | ||
| 458 | return( $self->{dbh}->func( $table, $func_name ) ); | ||
| 459 | } | ||
| 460 | |||
| 461 | # <https://metacpan.org/pod/DBD::SQLite::Constants#datatypes-(fundamental_datatypes)> | ||
| 462 | # <https://www.sqlite.org/datatype3.html> | ||
| 463 | # In SQLite, there are only 4 types: | ||
| 464 | # SQLITE_INTEGER | ||
| 465 | # SQLITE_FLOAT | ||
| 466 | # SQLITE_BLOB | ||
| 467 | # SQLITE_NULL | ||
| 468 | sub get_sql_type | ||
| 469 | { | ||
| 470 | my $self = shift( @_ ); | ||
| 471 | my $type = shift( @_ ) || return( $self->error( "No sql type was provided to get its constant." ) ); | ||
| 472 | $type = lc( $type ); | ||
| 473 | if( CORE::exists( $DATATYPES_DICT->{ $type } ) && | ||
| 474 | $type ne $DATATYPES_DICT->{ $type }->{type} ) | ||
| 475 | { | ||
| 476 | $type = $DATATYPES_DICT->{ $type }->{type}; | ||
| 477 | } | ||
| 478 | my $const; | ||
| 479 | if( substr( $type, 0, 7 ) eq 'sqlite_' ) | ||
| 480 | { | ||
| 481 | $const = $self->{dbh}->can( "DBD::SQLite::\U${type}\E" ); | ||
| 482 | } | ||
| 483 | else | ||
| 484 | { | ||
| 485 | $const = $self->{dbh}->can( "DBD::SQLite::SQLITE_\U${type}\E" ); | ||
| 486 | } | ||
| 487 | return( '' ) if( !defined( $const ) ); | ||
| 488 | return( $const->() ); | ||
| 489 | } | ||
| 490 | |||
| 491 | sub having | ||
| 492 | { | ||
| 493 | my $self = shift( @_ ); | ||
| 494 | my $q = $self->_reset_query; | ||
| 495 | return( $q->having( @_ ) ); | ||
| 496 | } | ||
| 497 | |||
| 498 | # https://www.sqlite.org/compile.html | ||
| 499 | sub has_compile_option | ||
| 500 | { | ||
| 501 | my $self = shift( @_ ); | ||
| 502 | my $opt = shift( @_ ) || return( $self->error( "No compile option was provided to check" ) ); | ||
| 503 | $opt = uc( $opt ); | ||
| 504 | my $all = $self->compile_options; | ||
| 505 | my @found = grep( /^$opt/, @$all ); | ||
| 506 | return( $found[0] ) if( scalar( @found ) ); | ||
| 507 | return( '' ); | ||
| 508 | } | ||
| 509 | |||
| 510 | sub last_insert_id | ||
| 511 | { | ||
| 512 | my $self = shift( @_ ); | ||
| 513 | my $table = shift( @_ ) || $self->{table}; | ||
| 514 | return( $self->{dbh}->last_insert_id( undef, undef, $table, undef ) ); | ||
| 515 | } | ||
| 516 | |||
| 517 | sub lock { return( shift->error( "Table lock is unsupported in SQLite." ) ); } | ||
| 518 | |||
| 519 | sub pragma | ||
| 520 | { | ||
| 521 | my $self = shfit( @_ ); | ||
| 522 | my $key2val = | ||
| 523 | { | ||
| 524 | foreign_keys => [ qw( ON OFF ) ], | ||
| 525 | journal_mode => [ qw( DELETE TRUNCATE ) ], | ||
| 526 | legacy_file_format => [ qw( ON OFF ) ], | ||
| 527 | reverse_unordered_selects => [ qw( ON OFF ) ], | ||
| 528 | synchronous => [ qw( ON OFF ) ], | ||
| 529 | # To avoid corruption after BEGIN starts, DBD uses BEGIN IMMEDIATE. Default is TRUE | ||
| 530 | sqlite_use_immediate_transaction => [ qw( 1 0 ) ], | ||
| 531 | cache_size => qr/^\d+$/ | ||
| 532 | }; | ||
| 533 | } | ||
| 534 | |||
| 535 | sub query_object { return( shift->_set_get_object( 'query_object', 'DB::Object::SQLite::Query', @_ ) ); } | ||
| 536 | |||
| 537 | # https://www.sqlite.org/lang_replace.html | ||
| 538 | # https://www.sqlite.org/lang_conflict.html | ||
| 539 | # REPLACE is an alias for INSERT OR REPLACE | ||
| 540 | # https://www.sqlite.org/lang_insert.html | ||
| 541 | sub replace | ||
| 542 | { | ||
| 543 | my $self = shift( @_ ); | ||
| 544 | my $data = shift( @_ ) if( @_ == 1 && ref( $_[0] ) ); | ||
| 545 | my @arg = @_; | ||
| 546 | my %arg = (); | ||
| 547 | my $select = ''; | ||
| 548 | if( !%arg && $data && $self->_is_hash( $data => 'strict' ) ) | ||
| 549 | { | ||
| 550 | %arg = %$data; | ||
| 551 | } | ||
| 552 | elsif( $data && ref( $data ) eq 'DB::Object::Statement' ) | ||
| 553 | { | ||
| 554 | $select = $data->as_string(); | ||
| 555 | } | ||
| 556 | %arg = @arg if( @arg ); | ||
| 557 | my $table = $self->{table} || | ||
| 558 | return( $self->error( "No table was provided to replace data." ) ); | ||
| 559 | my $structure = $self->structure || return( $self->pass_error ); | ||
| 560 | my $null = $self->null(); | ||
| 561 | my @avoid = (); | ||
| 562 | foreach my $field ( keys( %$structure ) ) | ||
| 563 | { | ||
| 564 | # It is useless to insert a blank data in a field whose default value is NULL. | ||
| 565 | # Especially since a test on a NULL field may be made specifically. | ||
| 566 | push( @avoid, $field ) if( !CORE::exists( $arg{ $field } ) && $null->{ $field } ); | ||
| 567 | } | ||
| 568 | my $db_data = $self->getdefault({ | ||
| 569 | table => $table, | ||
| 570 | arg => \@arg, | ||
| 571 | avoid => \@avoid | ||
| 572 | }); | ||
| 573 | my( $fields, $values ) = $db_data->format_statement(); | ||
| 574 | $self->_reset_query(); | ||
| 575 | delete( $self->{query_reset} ); | ||
| 576 | $self->{binded_values} = $db_data->{binded_values}; | ||
| 577 | my $query = $self->{query} = $select ? "REPLACE INTO $table $select" : "REPLACE INTO $table ($fields) VALUES($values)"; | ||
| 578 | # Everything meaningfull lies within the object | ||
| 579 | # If no bind should be done _save_bind does nothing | ||
| 580 | $self->_save_bind(); | ||
| 581 | # Query string should lie within the object | ||
| 582 | # _cache_this sends back an object no matter what or unde() if an error occurs | ||
| 583 | my $sth = $self->_cache_this($query); | ||
| 584 | # STOP! No need to go further | ||
| 585 | if( !defined( $sth ) ) | ||
| 586 | { | ||
| 587 | return( $self->error( "Error while preparing query to replace data into table '$table':\n$query", $self->errstr() ) ); | ||
| 588 | } | ||
| 589 | if( !defined( wantarray() ) ) | ||
| 590 | { | ||
| 591 | $sth->execute() || | ||
| 592 | return( $self->error( "Error while executing query to replace data to table '$table':\n$query" ) ); | ||
| 593 | } | ||
| 594 | return( $sth ); | ||
| 595 | } | ||
| 596 | |||
| 597 | sub register_function | ||
| 598 | { | ||
| 599 | my $self = shift( @_ ); | ||
| 600 | return( $self->error( "I was expecting an hash reference of parameters as only argument." ) ) if( !$self->_is_hash( $_[0] => 'strict' ) ); | ||
| 601 | my $opts = shift( @_ ); | ||
| 602 | return( $self->error( "Parameter 'code' to register the private function must be a code reference." ) ) if( ref( $opts->{code} ) ne 'CODE' ); | ||
| 603 | $opts->{flags} = [] if( !CORE::exists( $opts->{flags} ) ); | ||
| 604 | return( $self->error( "Parameter 'flags' must be an array reference." ) ) if( !$self->_is_array( $opts->{flags} ) ); | ||
| 605 | $opts->{argc} = -1 if( !CORE::exists( $opts->{argc} ) ); | ||
| 606 | return( $self->error( "No name was provided for this private function." ) ) if( !CORE::exists( $opts->{name} ) ); | ||
| 607 | my $name = $opts->{name}; | ||
| 608 | return( $self->error( "Name provided for this private function \"$name\" contains illegal characters. Only alphabetical and numerical characters and underscore allowed with name starting with a letter." ) ) if( $name !~ /^[a-zA-Z][a-zA-Z0-9\_]+$/ ); | ||
| 609 | my $funcs = $self->{func}; | ||
| 610 | $funcs->{ lc( $opts->{name} ) } = $opts; | ||
| 611 | $self->sql_function_register( $opts ) if( $self->{dbh} ); | ||
| 612 | return( $opts ); | ||
| 613 | } | ||
| 614 | |||
| 615 | sub remove_function | ||
| 616 | { | ||
| 617 | my $self = shift( @_ ); | ||
| 618 | my $name = shift( @_ ) || return( $self->error( "No private function was provided to be removed." ) ); | ||
| 619 | my $funcs = $self->{func}; | ||
| 620 | $name = lc( $name ); | ||
| 621 | return( 0 ) if( !CORE::exists( $funcs->{ $name } ) ); | ||
| 622 | return( CORE::delete( $funcs->{ $name } ) ); | ||
| 623 | } | ||
| 624 | |||
| 625 | sub returning | ||
| 626 | { | ||
| 627 | my $self = shift( @_ ); | ||
| 628 | my $q = $self->_reset_query; | ||
| 629 | return( $q->returning( @_ ) ); | ||
| 630 | } | ||
| 631 | |||
| 632 | sub rollback | ||
| 633 | { | ||
| 634 | my $self = shift( @_ ); | ||
| 635 | local $@; | ||
| 636 | my $rv = eval | ||
| 637 | { | ||
| 638 | return( $self->{dbh}->rollback( @_ ) ); | ||
| 639 | }; | ||
| 640 | if( $@ ) | ||
| 641 | { | ||
| 642 | return( $self->error( "Error calling rollback: $@" ) ); | ||
| 643 | } | ||
| 644 | elsif( !$rv && $self->{dbh}->err ) | ||
| 645 | { | ||
| 646 | return( $self->error({ code => $self->{dbh}->err, message => $self->{dbh}->errstr }) ); | ||
| 647 | } | ||
| 648 | |||
| 649 | if( defined( $rv ) ) | ||
| 650 | { | ||
| 651 | $self->_transaction_finished; | ||
| 652 | return( $rv || 1 ); | ||
| 653 | } | ||
| 654 | return; | ||
| 655 | } | ||
| 656 | |||
| 657 | sub sql_function_register | ||
| 658 | { | ||
| 659 | my $self = shift( @_ ); | ||
| 660 | my $opts = shift( @_ ) || return( $self->error( "No private function hash reference provided." ) ); | ||
| 661 | my $dbh = $self->{dbh} || return( $self->error( "No active database handler available." ) ); | ||
| 662 | $opts->{flags} = [] if( !exists( $opts->{flags} ) || ref( $opts->{flags} ) ne 'ARRAY' ); | ||
| 663 | my $flag; | ||
| 664 | my $eval = join( '|', @{$opts->{flags}} ); | ||
| 665 | $flag = eval( $eval ); | ||
| 666 | my $code = $opts->{code}; | ||
| 667 | if( defined( $flag ) ) | ||
| 668 | { | ||
| 669 | $dbh->sqlite_create_function( $opts->{name}, $opts->{argc}, sub{ my @arg = @_; unshift( @arg, $self ); $code->( @arg ); }, $flag ); | ||
| 670 | } | ||
| 671 | else | ||
| 672 | { | ||
| 673 | $dbh->sqlite_create_function( $opts->{name}, $opts->{argc}, sub{ my @arg = @_; unshift( @arg, $self ); $code->( @arg ); } ); | ||
| 674 | } | ||
| 675 | $opts->{_registered_on} = time(); | ||
| 676 | } | ||
| 677 | |||
| 678 | # http://www.sqlite.org/c3ref/c_status_malloc_count.html | ||
| 679 | sub stat | ||
| 680 | { | ||
| 681 | my $self = shift( @_ ); | ||
| 682 | my $opt = $self->_get_args_as_hash( @_ ); | ||
| 683 | my $ref = $opt->{reset} ? DBD::SQLite::sqlite_status( 0 ) : DBD::SQLite::sqlite_status(); | ||
| 684 | if( $opt->{type} ) | ||
| 685 | { | ||
| 686 | return( exists( $ref->{ $opt->{type} } ) ? $ref->{ $opt->{type} } : undef() ); | ||
| 687 | } | ||
| 688 | else | ||
| 689 | { | ||
| 690 | return( wantarray() ? () : undef() ) if( !%$ref ); | ||
| 691 | return( wantarray() ? %$ref : $ref ); | ||
| 692 | } | ||
| 693 | } | ||
| 694 | |||
| 695 | # sub table_exists | ||
| 696 | # { | ||
| 697 | # my $self = shift( @_ ); | ||
| 698 | # my $table = shift( @_ ) || | ||
| 699 | # return( $self->error( "You must provide a table name to access the table methods." ) ); | ||
| 700 | # my $cache_tables = $self->cache_tables; | ||
| 701 | # my $tables_in_cache = $cache_tables->get({ | ||
| 702 | # host => 'localhost', | ||
| 703 | # driver => $self->driver, | ||
| 704 | # port => 0, | ||
| 705 | # database => $self->database, | ||
| 706 | # }); | ||
| 707 | # foreach my $ref ( @$tables_in_cache ) | ||
| 708 | # { | ||
| 709 | # return( 1 ) if( $ref->{name} eq $table ); | ||
| 710 | # } | ||
| 711 | # # We did not find it, so let's try by checking directly the database | ||
| 712 | # my $def = $self->table_info( $table ) || return; | ||
| 713 | # return(0) if( !scalar( @$def ) ); | ||
| 714 | # return(1); | ||
| 715 | # } | ||
| 716 | |||
| 717 | sub table_info | ||
| 718 | { | ||
| 719 | my $self = shift( @_ ); | ||
| 720 | my $table = shift( @_ ) || | ||
| 721 | return( $self->error( "You must provide a table name to access the table methods." ) ); | ||
| 722 | my $opts = $self->_get_args_as_hash( @_ ); | ||
| 723 | my $sql = <<'EOT'; | ||
| 724 | SELECT | ||
| 725 | name | ||
| 726 | ,type | ||
| 727 | FROM sqlite_master | ||
| 728 | WHERE type IN ('table', 'view') AND name = ? | ||
| 729 | EOT | ||
| 730 | my $dbh = $self->{dbh} || return( $self->error( "Could not find database handler." ) ); | ||
| 731 | my $sth = $dbh->prepare_cached( $sql ) || return( $self->error( "An error occured while preparing query to check if table \"$table\" exists in database \"", $self->database, "\": ", $dbh->errstr ) ); | ||
| 732 | $sth->execute( $table ) || return( $self->error( "An error occured while executing query to check if table \"$table\" exists in database \"", $self->database, "\: ", $sth->errstr ) ); | ||
| 733 | my $all = $sth->fetchall_arrayref( {} ); | ||
| 734 | $sth->finish; | ||
| 735 | return( {} ) if( !scalar( @$all ) ); | ||
| 736 | return( $all->[0] ); | ||
| 737 | } | ||
| 738 | |||
| 739 | sub tables | ||
| 740 | { | ||
| 741 | my $self = shift( @_ ); | ||
| 742 | my $db = shift( @_ ) || $self->{database}; | ||
| 743 | my $all = $self->tables_info || return; | ||
| 744 | my @tables = map( $_->{name}, @$all ); | ||
| 745 | # return( wantarray() ? () : undef() ) if( !@tables ); | ||
| 746 | # return( wantarray() ? @tables : \@tables ); | ||
| 747 | return( \@tables ); | ||
| 748 | } | ||
| 749 | |||
| 750 | sub tables_info | ||
| 751 | { | ||
| 752 | my $self = shift( @_ ); | ||
| 753 | my $db = shift( @_ ) || $self->{database}; | ||
| 754 | # Parameters are: ?, schema, table, and type | ||
| 755 | # my $sth = $self->{dbh}->table_info( undef, undef, $table, "TABLE,VIEW" ); | ||
| 756 | # The original query was fetched by connecting to Postgres with psql -E and executing the command \z | ||
| 757 | # This revised query will fetch only tables, views, materialised view and foreign tables, but will avoid the mysterious view called sequence_setvals | ||
| 758 | # https://stackoverflow.com/questions/82875/how-to-list-the-tables-in-a-sqlite-database-file-that-was-opened-with-attach | ||
| 759 | my $query = <<SQL; | ||
| 760 | SELECT name FROM sqlite_master | ||
| 761 | WHERE type IN ('table','view') AND name NOT LIKE 'sqlite_%' | ||
| 762 | UNION ALL | ||
| 763 | SELECT name FROM sqlite_temp_master | ||
| 764 | WHERE type IN ('table','view') | ||
| 765 | ORDER BY 1 | ||
| 766 | SQL | ||
| 767 | my $dbh = $self->{dbh} || return( $self->error( "Could not find database handler." ) ); | ||
| 768 | my $sth = $dbh->prepare_cached( $query ) || return( $self->error( sprintf( "Error while preparing query $query: %s", $dbh->errstr ) ) ); | ||
| 769 | $sth->execute() || return( $self->error( sprintf( "Error while executing query $query: %s", $sth->errstr ) ) ); | ||
| 770 | my $all = $sth->fetchall_arrayref( {} ); | ||
| 771 | return( $all ); | ||
| 772 | } | ||
| 773 | |||
| 774 | sub trace { return( shift->error( "Trace is unsupported on SQLite." ) ); } | ||
| 775 | |||
| 776 | sub unlock { return( shift->error( "unlock() does not work with SQLite." ) ); } | ||
| 777 | |||
| 778 | sub variables | ||
| 779 | { | ||
| 780 | return( shift->error( "variables is currently unsupported in Postgres" ) ); | ||
| 781 | } | ||
| 782 | |||
| 783 | # https://www.sqlite.org/versionnumbers.html | ||
| 784 | sub version | ||
| 785 | { | ||
| 786 | my $self = shift( @_ ); | ||
| 787 | # If we already have the information, let's use our cache instead of making a query | ||
| 788 | return( $self->{_db_version} ) if( length( $self->{_db_version} ) ); | ||
| 789 | my $sql = 'SELECT sqlite_version()'; | ||
| 790 | my $sth = $self->do( $sql ) || return( $self->error( "Unable to issue the sql statement '$sql' to get the server version: ", $self->errstr ) ); | ||
| 791 | my $ver = $sth->fetchrow; | ||
| 792 | $sth->finish; | ||
| 793 | # We cache it | ||
| 794 | $self->{_db_version} = version->parse( $ver ); | ||
| 795 | return( $ver ); | ||
| 796 | } | ||
| 797 | |||
| 798 | sub _check_connect_param | ||
| 799 | { | ||
| 800 | my $self = shift( @_ ); | ||
| 801 | my $param = $self->SUPER::_check_connect_param( @_ ); | ||
| 802 | if( !$param->{database_file} && $param->{database} ) | ||
| 803 | { | ||
| 804 | if( $param->{database} eq ':memory:' ) | ||
| 805 | { | ||
| 806 | $param->{database_file} = $self->{database_file} = ':memory:'; | ||
| 807 | } | ||
| 808 | else | ||
| 809 | { | ||
| 810 | my( $filename, $path, $ext ); | ||
| 811 | my $uri = CORE::exists( $param->{uri} ) ? $param->{uri} : ''; | ||
| 812 | my $db = $param->{database} ? $param->{database} : ( $uri->path_segments )[-1]; | ||
| 813 | $path = $uri ? $uri->path : $db; | ||
| 814 | # $db = Cwd::abs_path( $uri ? $uri->path : $db ); | ||
| 815 | # $db = File::Spec->rel2abs( $path ); | ||
| 816 | $db = $self->new_file( $path ); | ||
| 817 | # If we cannot find the file and it does not end with .sqlite, let's add the extension | ||
| 818 | # So the user can provide the database parameter just like database => 'test' or database => './test' | ||
| 819 | $db = $self->new_file( "$db.sqlite" ) if( !$db->exists && $db->extension->lc ne 'sqlite' ); | ||
| 820 | ( $filename, $path, $ext ) = $db->baseinfo( qr/\.[^\.]+$/ ); | ||
| 821 | $param->{database} = $filename; | ||
| 822 | $param->{database_file} = $self->{database_file} = $db; | ||
| 823 | } | ||
| 824 | } | ||
| 825 | $param->{host} = 'localhost' if( !length( $param->{host} ) ); | ||
| 826 | $param->{port} = 0 if( !CORE::exists( $param->{port} ) ); | ||
| 827 | return( $param ); | ||
| 828 | } | ||
| 829 | |||
| 830 | sub _check_default_option | ||
| 831 | { | ||
| 832 | my $self = shift( @_ ); | ||
| 833 | my $opts = $self->_get_args_as_hash( @_ ); | ||
| 834 | return( $self->error( "Provided option is not a hash reference." ) ) if( !$self->_is_hash( $opts => 'strict' ) ); | ||
| 835 | $opts->{sqlite_unicode} = 1 if( !CORE::exists( $opts->{sqlite_unicode} ) ); | ||
| 836 | return( $opts ); | ||
| 837 | } | ||
| 838 | |||
| 839 | sub _connection_options | ||
| 840 | { | ||
| 841 | my $self = shift( @_ ); | ||
| 842 | my $param = shift( @_ ); | ||
| 843 | my @sqlite_params = grep( /^sqlite_/, keys( %$param ) ); | ||
| 844 | my $opt = $self->SUPER::_connection_options( $param ); | ||
| 845 | @$opt{ @sqlite_params } = @$param{ @sqlite_params }; | ||
| 846 | return( $opt ); | ||
| 847 | } | ||
| 848 | |||
| 849 | sub _connection_params2hash_driver | ||
| 850 | { | ||
| 851 | my $self = shift( @_ ); | ||
| 852 | my $opts = $self->_get_args_as_hash( @_ ); | ||
| 853 | $opts->{sqlite_unicode} = 1 if( !length( $opts->{sqlite_unicode} ) ); | ||
| 854 | return( $opts ); | ||
| 855 | } | ||
| 856 | |||
| 857 | sub _connection_parameters | ||
| 858 | { | ||
| 859 | my $self = shift( @_ ); | ||
| 860 | my $param = shift( @_ ); | ||
| 861 | # Even though login, password, server, host are not used, I was hesitating, but decided to leave them as ok, and ignore them | ||
| 862 | # Or maybe should I issue an error when they are provided? | ||
| 863 | my $core = $self->new_array( [qw( db login passwd host port driver database server opt uri debug cache_connections cache_dir cache_query cache_table unknown_field use_cache id )] ); | ||
| 864 | my @sqlite_params = grep( /^sqlite_/, keys( %$param ) ); | ||
| 865 | # See DBD::SQLite for the list of valid parameters | ||
| 866 | # E.g.: sqlite_open_flags sqlite_busy_timeout sqlite_use_immediate_transaction sqlite_see_if_its_a_number sqlite_allow_multiple_statements sqlite_unprepared_statements sqlite_unicode sqlite_allow_multiple_statements sqlite_use_immediate_transaction | ||
| 867 | $core->push( @sqlite_params ); | ||
| 868 | my $core_fields = $self->{_core_fields} || []; | ||
| 869 | $core->push( @$core_fields ); | ||
| 870 | return( $core->unique(1) ); | ||
| 871 | } | ||
| 872 | |||
| 873 | sub _dbi_connect | ||
| 874 | { | ||
| 875 | my $self = shift( @_ ); | ||
| 876 | my $dbh = $self->{dbh} = $self->SUPER::_dbi_connect( @_ ); | ||
| 877 | # my $func = $self->{_func}; | ||
| 878 | my $func = $self->{_func}; | ||
| 879 | foreach my $k ( sort( keys( %$PRIVATE_FUNCTIONS ) ) ) | ||
| 880 | { | ||
| 881 | my $this = $PRIVATE_FUNCTIONS->{ $k }; | ||
| 882 | my $ref = | ||
| 883 | { | ||
| 884 | name => $k, | ||
| 885 | argc => $this->[0], | ||
| 886 | code => $this->[1], | ||
| 887 | }; | ||
| 888 | $func->{ $k } = $ref; | ||
| 889 | } | ||
| 890 | foreach my $name ( sort( keys( %$func ) ) ) | ||
| 891 | { | ||
| 892 | my $ref = $func->{ $name }; | ||
| 893 | if( $ref->{_registered_on} ) | ||
| 894 | { | ||
| 895 | next; | ||
| 896 | } | ||
| 897 | $self->sql_function_register( $ref ); | ||
| 898 | $ref->{_registered_on} = time(); | ||
| 899 | } | ||
| 900 | return( $dbh ); | ||
| 901 | } | ||
| 902 | |||
| 903 | sub _dsn | ||
| 904 | { | ||
| 905 | my $self = shift( @_ ); | ||
| 906 | my $db = $self->{database_file} || return( $self->error( "No database file was specified." ) ); | ||
| 907 | # return( $self->error( "Database file \"$db\" does not exist." ) ) if( !-e( $db ) ); | ||
| 908 | return( $self->error( "Database file \"$db\" is not writable." ) ) if( -e( $db ) && !-w( $db ) ); | ||
| 909 | my @params = ( sprintf( 'dbi:%s:', $self->{driver} ) ); | ||
| 910 | push( @params, sprintf( 'dbname=%s', $db ) ); | ||
| 911 | return( join( ';', @params ) ); | ||
| 912 | } | ||
| 913 | |||
| 914 | # Private function | ||
| 915 | sub _ceiling | ||
| 916 | { | ||
| 917 | my $self = shift( @_ ); | ||
| 918 | my @args = @_; | ||
| 919 | return( POSIX::ceil( $args[0] ) ); | ||
| 920 | } | ||
| 921 | |||
| 922 | sub _concat | ||
| 923 | { | ||
| 924 | my $self = shift( @_ ); | ||
| 925 | my @args = @_; | ||
| 926 | return( join( '', @args ) ); | ||
| 927 | } | ||
| 928 | |||
| 929 | sub _curdate | ||
| 930 | { | ||
| 931 | my $self = shift( @_ ); | ||
| 932 | my @args = @_; | ||
| 933 | |||
| 934 | # try-catch | ||
| 935 | local $@; | ||
| 936 | my $tz = eval | ||
| 937 | { | ||
| 938 | return( DateTime::Lite::TimeZone->new( name => 'local' ) ); | ||
| 939 | }; | ||
| 940 | if( $@ ) | ||
| 941 | { | ||
| 942 | $tz = DateTime::Lite::TimeZone->new( name => 'UTC' ); | ||
| 943 | } | ||
| 944 | my $d = DateTime::Lite->from_epoch( epoch => time(), time_zone => $tz->name ); | ||
| 945 | return( $d->ymd( '-' ) ); | ||
| 946 | } | ||
| 947 | |||
| 948 | sub _curtime | ||
| 949 | { | ||
| 950 | my $self = shift( @_ ); | ||
| 951 | my @args = @_; | ||
| 952 | # try-catch | ||
| 953 | local $@; | ||
| 954 | my $tz = eval | ||
| 955 | { | ||
| 956 | return( DateTime::Lite::TimeZone->new( name => 'local' ) ); | ||
| 957 | }; | ||
| 958 | if( $@ ) | ||
| 959 | { | ||
| 960 | $tz = DateTime::Lite::TimeZone->new( name => 'UTC' ); | ||
| 961 | } | ||
| 962 | my $d = DateTime::Lite->now( time_zone => $tz->name ); | ||
| 963 | return( $d->hms( ':' ) ); | ||
| 964 | } | ||
| 965 | |||
| 966 | # e.g. Monday | ||
| 967 | sub _dayname | ||
| 968 | { | ||
| 969 | my $self = shift( @_ ); | ||
| 970 | my @args = @_; | ||
| 971 | my $dt = $self->_parse_timestamp( $args[0] ) || return; | ||
| 972 | return( $dt->day_name ); | ||
| 973 | } | ||
| 974 | |||
| 975 | # E.g.: 17 | ||
| 976 | sub _dayofmonth | ||
| 977 | { | ||
| 978 | my $self = shift( @_ ); | ||
| 979 | my @args = @_; | ||
| 980 | my $dt = $self->_parse_timestamp( $args[0] ) || return; | ||
| 981 | return( $dt->day ); | ||
| 982 | } | ||
| 983 | |||
| 984 | # timestamp, [integer] 1 to 7 | ||
| 985 | sub _dayofweek | ||
| 986 | { | ||
| 987 | my $self = shift( @_ ); | ||
| 988 | my @args = @_; | ||
| 989 | my $dt = $self->_parse_timestamp( $args[0] ) || return; | ||
| 990 | return( $dt->day_of_week ); | ||
| 991 | } | ||
| 992 | |||
| 993 | # E.g.: 170 | ||
| 994 | sub _dayofyear | ||
| 995 | { | ||
| 996 | my $self = shift( @_ ); | ||
| 997 | my @args = @_; | ||
| 998 | my $dt = $self->_parse_timestamp( $args[0] ) || return; | ||
| 999 | return( $dt->day_of_year ); | ||
| 1000 | } | ||
| 1001 | |||
| 1002 | # http://stackoverflow.com/questions/10034636/postgres-longitude-longitude-query | ||
| 1003 | sub _distance_miles | ||
| 1004 | { | ||
| 1005 | my $self = shift( @_ ); | ||
| 1006 | my @args = @_; | ||
| 1007 | my( $lat1, $lon1, $lat2, $lon2 ) = @args; | ||
| 1008 | my $x = 69.1 * ( $lat2 - $lat1 ); | ||
| 1009 | my $y = 69.1 * ( $lon2 - $lon1 ) * cos( $lat1 / 57.3 ); | ||
| 1010 | return( sqrt( $x * $x + $y * $y ) ); | ||
| 1011 | } | ||
| 1012 | |||
| 1013 | sub _from_days | ||
| 1014 | { | ||
| 1015 | my $self = shift( @_ ); | ||
| 1016 | my @args = @_; | ||
| 1017 | my $from_days = $args[0]; | ||
| 1018 | # try-catch | ||
| 1019 | local $@; | ||
| 1020 | my $tz = eval | ||
| 1021 | { | ||
| 1022 | return( DateTime::Lite::TimeZone->new( name => 'local' ) ); | ||
| 1023 | }; | ||
| 1024 | if( $@ ) | ||
| 1025 | { | ||
| 1026 | $tz = DateTime::Lite::TimeZone->new( name => 'UTC' ); | ||
| 1027 | } | ||
| 1028 | my $origin = DateTime::Lite->new( | ||
| 1029 | year => 0, | ||
| 1030 | month => 1, | ||
| 1031 | day => 1, | ||
| 1032 | hour => 0, | ||
| 1033 | minute => 0, | ||
| 1034 | second => 0, | ||
| 1035 | time_zone => $tz, | ||
| 1036 | ); | ||
| 1037 | my $epoch = DateTime::Lite->from_epoch( epoch => 0, time_zone => $tz ); | ||
| 1038 | # https://stackoverflow.com/questions/821423/how-can-i-calculate-the-number-of-days-between-two-dates-in-perl#7111718 | ||
| 1039 | my $epoch_days = $epoch->delta_days( $origin )->delta_days(); | ||
| 1040 | my $days_since_epoch = $from_days - int( $epoch_days ); | ||
| 1041 | my $dt = DateTime::Lite->from_epoch( epoch => ( $days_since_epoch * 86400 ), time_zone => $tz ); | ||
| 1042 | return( $dt ); | ||
| 1043 | } | ||
| 1044 | |||
| 1045 | sub _from_unixtime | ||
| 1046 | { | ||
| 1047 | my $self = shift( @_ ); | ||
| 1048 | my @args = @_; | ||
| 1049 | return if( $args[0] !~ /^\d+$/ ); | ||
| 1050 | # try-catch | ||
| 1051 | local $@; | ||
| 1052 | my $tz = eval | ||
| 1053 | { | ||
| 1054 | return( DateTime::Lite::TimeZone->new( name => 'local' ) ); | ||
| 1055 | }; | ||
| 1056 | if( $@ ) | ||
| 1057 | { | ||
| 1058 | $tz = DateTime::Lite::TimeZone->new( name => 'UTC' ); | ||
| 1059 | } | ||
| 1060 | my $dt = DateTime::Lite->from_epoch( epoch => $args[0], time_zone => $tz->name ); | ||
| 1061 | return( $dt->strftime( '%Y-%m-%d %T%z' ) ); | ||
| 1062 | } | ||
| 1063 | |||
| 1064 | sub _hour | ||
| 1065 | { | ||
| 1066 | my $self = shift( @_ ); | ||
| 1067 | my @args = @_; | ||
| 1068 | my $dt = $self->_parse_timestamp( $args[0] ) || return; | ||
| 1069 | return( $dt->hour ); | ||
| 1070 | } | ||
| 1071 | |||
| 1072 | sub _lcase | ||
| 1073 | { | ||
| 1074 | my $self = shift( @_ ); | ||
| 1075 | my @args = @_; | ||
| 1076 | return( lc( $args[0] ) ); | ||
| 1077 | } | ||
| 1078 | |||
| 1079 | sub _left | ||
| 1080 | { | ||
| 1081 | my $self = shift( @_ ); | ||
| 1082 | my @args = @_; | ||
| 1083 | return( $args[0] ) if( $args[1] !~ /^\d+$/ ); | ||
| 1084 | return( substr( $args[0], 0, $args[1] ) ); | ||
| 1085 | } | ||
| 1086 | |||
| 1087 | sub _locate | ||
| 1088 | { | ||
| 1089 | my $self = shift( @_ ); | ||
| 1090 | my @args = @_; | ||
| 1091 | return( CORE::index( $args[0], $args[1] ) ); | ||
| 1092 | } | ||
| 1093 | |||
| 1094 | sub _log10 | ||
| 1095 | { | ||
| 1096 | my $self = shift( @_ ); | ||
| 1097 | my @args = @_; | ||
| 1098 | return( log( $args[0] ) / log( 10 ) ); | ||
| 1099 | } | ||
| 1100 | |||
| 1101 | sub _minute | ||
| 1102 | { | ||
| 1103 | my $self = shift( @_ ); | ||
| 1104 | my @args = @_; | ||
| 1105 | my $dt = $self->_parse_timestamp( $args[0] ) || return; | ||
| 1106 | return( $dt->minute ); | ||
| 1107 | } | ||
| 1108 | |||
| 1109 | sub _month | ||
| 1110 | { | ||
| 1111 | my $self = shift( @_ ); | ||
| 1112 | my @args = @_; | ||
| 1113 | my $dt = $self->_parse_timestamp( $args[0] ) || return; | ||
| 1114 | return( $dt->month ); | ||
| 1115 | } | ||
| 1116 | |||
| 1117 | sub _monthname | ||
| 1118 | { | ||
| 1119 | my $self = shift( @_ ); | ||
| 1120 | my @args = @_; | ||
| 1121 | my $dt = $self->_parse_timestamp( $args[0] ) || return; | ||
| 1122 | return( $dt->month_name ); | ||
| 1123 | } | ||
| 1124 | |||
| 1125 | sub _number_format | ||
| 1126 | { | ||
| 1127 | my $self = shift( @_ ); | ||
| 1128 | my @args = @_; | ||
| 1129 | my( $num, $tho, $dec, $prec ) = @args; | ||
| 1130 | $self->_load_class( 'Module::Generic::Number' ) || return( $self->pass_error ); | ||
| 1131 | my $fmt = Module::Generic::Number->new( $num ); | ||
| 1132 | # 1 means with trailing zeros | ||
| 1133 | return( $fmt->format( | ||
| 1134 | thousand => $tho, | ||
| 1135 | decimal => $dec, | ||
| 1136 | precision => $prec, | ||
| 1137 | decimal_fill => 1, | ||
| 1138 | )); | ||
| 1139 | } | ||
| 1140 | |||
| 1141 | sub _placeholder_regexp { return( $PLACEHOLDER_REGEXP ) } | ||
| 1142 | |||
| 1143 | sub _power | ||
| 1144 | { | ||
| 1145 | my $self = shift( @_ ); | ||
| 1146 | my @args = @_; | ||
| 1147 | return( CORE::exp( $args[ 1 ] * CORE::log( $args[ 0 ] ) ) ); | ||
| 1148 | } | ||
| 1149 | |||
| 1150 | sub _quarter | ||
| 1151 | { | ||
| 1152 | my $self = shift( @_ ); | ||
| 1153 | my @args = @_; | ||
| 1154 | my $dt = $self->_parse_timestamp( $args[0] ) || return; | ||
| 1155 | return( $dt->quarter ); | ||
| 1156 | } | ||
| 1157 | |||
| 1158 | sub _rand | ||
| 1159 | { | ||
| 1160 | my $self = shift( @_ ); | ||
| 1161 | my @args = @_; | ||
| 1162 | return( CORE::rand() ); | ||
| 1163 | } | ||
| 1164 | |||
| 1165 | sub _regexp | ||
| 1166 | { | ||
| 1167 | my $self = shift( @_ ); | ||
| 1168 | my @args = @_; | ||
| 1169 | my( $re, $what ) = @args; | ||
| 1170 | return( $what =~ /$re/ ); | ||
| 1171 | } | ||
| 1172 | |||
| 1173 | sub _replace | ||
| 1174 | { | ||
| 1175 | my $self = shift( @_ ); | ||
| 1176 | my @args = @_; | ||
| 1177 | my( $str, $from, $to ) = @args; | ||
| 1178 | $str =~ s/($from)/$to/gs; | ||
| 1179 | return( $str ); | ||
| 1180 | } | ||
| 1181 | |||
| 1182 | sub _right | ||
| 1183 | { | ||
| 1184 | my $self = shift( @_ ); | ||
| 1185 | my @args = @_; | ||
| 1186 | return( CORE::substr( $args[0], CORE::length( $args[0] ) - $args[1] ) ); | ||
| 1187 | } | ||
| 1188 | |||
| 1189 | sub _second | ||
| 1190 | { | ||
| 1191 | my $self = shift( @_ ); | ||
| 1192 | my @args = @_; | ||
| 1193 | my $dt = $self->_parse_timestamp( $args[0] ) || return; | ||
| 1194 | return( $dt->second ); | ||
| 1195 | } | ||
| 1196 | |||
| 1197 | sub _space | ||
| 1198 | { | ||
| 1199 | my $self = shift( @_ ); | ||
| 1200 | my @args = @_; | ||
| 1201 | return( ' ' x CORE::int( $args[0] ) ); | ||
| 1202 | } | ||
| 1203 | |||
| 1204 | sub _sprintf | ||
| 1205 | { | ||
| 1206 | my $self = shift( @_ ); | ||
| 1207 | my @args = @_; | ||
| 1208 | for( my $i = 0; $i < scalar( @args ); $i++ ) | ||
| 1209 | { | ||
| 1210 | $args[$i] =~ s/'/\\'/g; | ||
| 1211 | } | ||
| 1212 | my $eval = "CORE::sprintf( '" . join( "', '", @args ) . "' )"; | ||
| 1213 | my $res = eval( $eval ); | ||
| 1214 | # return( CORE::sprintf( @args ) ); | ||
| 1215 | return( $res ); | ||
| 1216 | } | ||
| 1217 | |||
| 1218 | sub _to_days | ||
| 1219 | { | ||
| 1220 | my $self = shift( @_ ); | ||
| 1221 | my @args = @_; | ||
| 1222 | my $dt = $self->_parse_timestamp( $args[0] ) || return; | ||
| 1223 | # try-catch | ||
| 1224 | local $@; | ||
| 1225 | my $tz = eval | ||
| 1226 | { | ||
| 1227 | return( DateTime::Lite::TimeZone->new( name => 'local' ) ); | ||
| 1228 | }; | ||
| 1229 | if( $@ ) | ||
| 1230 | { | ||
| 1231 | $tz = DateTime::Lite::TimeZone->new( name => 'UTC' ); | ||
| 1232 | } | ||
| 1233 | my $origin = DateTime::Lite->new( | ||
| 1234 | year => 0, | ||
| 1235 | month => 1, | ||
| 1236 | day => 1, | ||
| 1237 | hour => 0, | ||
| 1238 | minute => 0, | ||
| 1239 | second => 0, | ||
| 1240 | time_zone => $tz->name, | ||
| 1241 | ); | ||
| 1242 | # https://stackoverflow.com/questions/821423/how-can-i-calculate-the-number-of-days-between-two-dates-in-perl#7111718 | ||
| 1243 | my $days = $dt->delta_days( $origin )->delta_days(); | ||
| 1244 | return( $days ); | ||
| 1245 | } | ||
| 1246 | |||
| 1247 | # sub _truncate | ||
| 1248 | # { | ||
| 1249 | # my $self = shift( @_ ); | ||
| 1250 | # my @args = @_; | ||
| 1251 | # return if( $args[1] !~ /^\-?\d+$/ ); | ||
| 1252 | # return( CORE::substr( $args[0], 0, $args[1] ) ); | ||
| 1253 | # } | ||
| 1254 | |||
| 1255 | sub _ucase | ||
| 1256 | { | ||
| 1257 | my $self = shift( @_ ); | ||
| 1258 | my @args = @_; | ||
| 1259 | return( uc( $args[0] ) ); | ||
| 1260 | } | ||
| 1261 | |||
| 1262 | sub _unix_timestamp | ||
| 1263 | { | ||
| 1264 | my $self = shift( @_ ); | ||
| 1265 | my @args = @_; | ||
| 1266 | my $dt = $self->_parse_timestamp( $args[0] ) || return; | ||
| 1267 | return( $dt->epoch ); | ||
| 1268 | } | ||
| 1269 | |||
| 1270 | sub _week | ||
| 1271 | { | ||
| 1272 | my $self = shift( @_ ); | ||
| 1273 | my @args = @_; | ||
| 1274 | my $dt = $self->_parse_timestamp( $args[0] ) || return; | ||
| 1275 | return( $dt->week_number ); | ||
| 1276 | } | ||
| 1277 | |||
| 1278 | sub _weekday | ||
| 1279 | { | ||
| 1280 | my $self = shift( @_ ); | ||
| 1281 | my @args = @_; | ||
| 1282 | my $dt = $self->_parse_timestamp( $args[0] ) || return; | ||
| 1283 | return( $dt->day_of_week ); | ||
| 1284 | } | ||
| 1285 | |||
| 1286 | sub _year | ||
| 1287 | { | ||
| 1288 | my $self = shift( @_ ); | ||
| 1289 | my @args = @_; | ||
| 1290 | my $dt = $self->_parse_timestamp( $args[0] ) || return; | ||
| 1291 | return( $dt->year ); | ||
| 1292 | } | ||
| 1293 | |||
| 1294 | DESTROY | ||
| 1295 | { | ||
| 1296 | # <https://perldoc.perl.org/perlobj#Destructors> | ||
| 1297 | CORE::local( $., $@, $!, $^E, $? ); | ||
| 1298 | CORE::return if( ${^GLOBAL_PHASE} eq 'DESTRUCT' ); | ||
| 1299 | my $self = CORE::shift( @_ ); | ||
| 1300 | CORE::return if( !CORE::defined( $self ) ); | ||
| 1301 | my $class = ref( $self ) || $self; | ||
| 1302 | if( $self->{sth} ) | ||
| 1303 | { | ||
| 1304 | # print( STDERR "DESTROY(): Terminating sth '$self' for query:\n$self->{ 'query' }\n" ) if( $DEBUG ); | ||
| 1305 | $self->{sth}->finish(); | ||
| 1306 | } | ||
| 1307 | { | ||
| 1308 | # $self->{dbh}->disconnect(); | ||
| 1309 | # if( $DEBUG ) | ||
| 1310 | # { | ||
| 1311 | # my( $pack, $file, $line, $sub ) = ( caller( 0 ) )[ 0, 1, 2, 3 ]; | ||
| 1312 | # my( $pack2, $file2, $line2, $sub2 ) = ( caller( 1 ) ) [ 0, 1, 2, 3 ]; | ||
| 1313 | # print( STDERR "DESTROY database handle ($self) [$self->{ 'query' }]\ncalled within sub '$sub' ($sub2) from package '$pack' ($pack2) in file '$file' ($file2) at line '$line' ($line2).\n" ); | ||
| 1314 | # } | ||
| 1315 | $self->disconnect(); | ||
| 1316 | } | ||
| 1317 | my $locks = $self->{_locks}; | ||
| 1318 | if( $locks && $self->_is_array( $locks ) ) | ||
| 1319 | { | ||
| 1320 | foreach my $name ( @$locks ) | ||
| 1321 | { | ||
| 1322 | $self->unlock( $name ); | ||
| 1323 | } | ||
| 1324 | } | ||
| 1325 | } | ||
| 1326 | |||
| 1327 | 1; | ||
| 1328 | |||
| 1329 | # NOTE: POD | ||
| 1330 | __END__ |