| 1 | # -*- perl -*- | ||
| 2 | ##---------------------------------------------------------------------------- | ||
| 3 | ## Database Object Interface - ~/lib/DB/Object/SQLite.pm | ||
| 4 | ## Version v1.3.1 | ||
| 5 | ## Copyright(c) 2026 DEGUEST Pte. Ltd. | ||
| 6 | ## Author: Jacques Deguest <jack@deguest.jp> | ||
| 7 | ## Created 2017/07/19 | ||
| 8 | ## Modified 2026/03/26 | ||
| 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 | # This is the subclassable module for driver specific ones. | ||
| 16 | package DB::Object::SQLite; | ||
| 17 | BEGIN | ||
| 18 | { | ||
| 19 | use strict; | ||
| 20 | use warnings; | ||
| 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; | ||
| 32 | use DateTime::TimeZone; | ||
| 33 | use DateTime::Format::Strptime; | ||
| 34 | use Module::Generic::File qw( sys_tmpdir ); | ||
| 35 | # <https://metacpan.org/pod/DBD::SQLite::Constants> | ||
| 36 | # <https://www.sqlite.org/datatype3.html> | ||
| 37 | # <https://metacpan.org/pod/DBD::SQLite::Constants#datatypes-(fundamental_datatypes)> | ||
| 38 | # NULL, INTEGER, REAL, TEXT, BLOB | ||
| 39 | # Check DBD::SQLite::Constants | ||
| 40 | our $DATATYPES_DICT = | ||
| 41 | { | ||
| 42 | blob => { | ||
| 43 | constant => '', | ||
| 44 | name => 'SQLITE_BLOB', | ||
| 45 | re => qr/^BLOB/, | ||
| 46 | type => 'blob' | ||
| 47 | }, | ||
| 48 | bool => { | ||
| 49 | alias => [qw( boolean date datetime decimal numeric )], | ||
| 50 | constant => '', | ||
| 51 | name => 'SQLITE_NULL', | ||
| 52 | re => qr/^(NUMERIC|DECIMAL\(\d+,\d+\)|BOOLEAN|DATETIME|DATE)/, | ||
| 53 | type => 'bool' | ||
| 54 | }, | ||
| 55 | float => { | ||
| 56 | alias => [qw( double real ), 'double precision', ], | ||
| 57 | constant => '', | ||
| 58 | name => 'SQLITE_FLOAT', | ||
| 59 | re => qr/^(REAL|DOUBLE|DOUBLE\s+PRECISION|FLOAT)/, | ||
| 60 | type => 'float' | ||
| 61 | }, | ||
| 62 | integer => { | ||
| 63 | alias => [qw( int tinyint smallint mediumint bigint int2 int8 ), 'unsigned big int' ], | ||
| 64 | constant => '', | ||
| 65 | name => 'SQLITE_INTEGER', | ||
| 66 | re => qr/^(INT|INTEGER|TINYINT|SMALLINT|MEDIUMINT|BIGINT|UNSIGNED\s+BIG\s+INT|INT2|INT8)/, | ||
| 67 | type => 'integer', | ||
| 68 | }, | ||
| 69 | # Data type text is only available from version 1.71 onward | ||
| 70 | }; | ||
| 71 | if( $DBD::SQLite::VERSION >= 1.071 ) | ||
| 72 | { | ||
| 73 | $DATATYPES_DICT->{text} = | ||
| 74 | { | ||
| 75 | alias => [qw( character clob nchar nvarchar varchar ), 'native character', 'varying character' ], | ||
| 76 | constant => '', | ||
| 77 | name => 'SQLITE_TEXT', | ||
| 78 | re => qr/^(CHARACTER\(\d+\)|VARCHAR\(\d+\)|VARYING\s+CHARACTER\(\d+\)|NCHAR\(\d+\)|NATIVE\s+CHARACTER\(\d+\)|NVARCHAR\(\d+\)|TEXT|CLOB)/, | ||
| 79 | type => 'text' | ||
| 80 | }; | ||
| 81 | } | ||
| 82 | our $PLACEHOLDER_REGEXP = qr/(?:(?<![?\w])\?(?<index>[1-9]\d*)(?!\w))/; | ||
| 83 | our $EXCEPTION_CLASS = $DB::Object::EXCEPTION_CLASS; | ||
| 84 | our $VERSION = 'v1.3.1'; | ||
| 85 | }; | ||
| 86 | |||
| 87 | use strict; | ||
| 88 | use warnings; | ||
| 89 | # require DB::Object::SQLite::Statement; | ||
| 90 | # require DB::Object::SQLite::Tables; | ||
| 91 | our $DEBUG = 0; | ||
| 92 | our $CACHE_SIZE = 10; | ||
| 93 | our $USE_BIND = 0; | ||
| 94 | our $USE_CACHE = 0; | ||
| 95 | our $MOD_PERL = 0; | ||
| 96 | if( $INC{ 'Apache/DBI.pm' } && | ||
| 97 | substr( $ENV{GATEWAY_INTERFACE}|| '', 0, 8 ) eq 'CGI-Perl' ) | ||
| 98 | { | ||
| 99 | $CONNECT_VIA = "Apache::DBI::connect"; | ||
| 100 | $MOD_PERL++; | ||
| 101 | } | ||
| 102 | |||
| 103 | # Actually the one in DB::Object is used, because DBD::SQLite has no datatype constants of its own | ||
| 104 | # our $DATATYPES = {}; | ||
| 105 | |||
| 106 | our $PRIVATE_FUNCTIONS = | ||
| 107 | { | ||
| 108 | ceiling =>[1, \&_ceiling], | ||
| 109 | concat =>[-1, \&_concat], | ||
| 110 | curdate =>[0, \&_curdate], | ||
| 111 | curtime =>[0, \&_curtime], | ||
| 112 | dayname =>[1, \&_dayname], | ||
| 113 | dayofmonth =>[1, \&_dayofmonth], | ||
| 114 | dayofweek =>[1, \&_dayofweek], | ||
| 115 | dayofyear =>[1, \&_dayofyear], | ||
| 116 | distance_miles =>[4, \&_distance_miles], | ||
| 117 | # from_days =>[-1, \&_from_days], | ||
| 118 | from_unixtime =>[1, \&_from_unixtime], | ||
| 119 | hour =>[1, \&_hour], | ||
| 120 | lcase =>[1, \&_lcase], | ||
| 121 | left =>[2, \&_left], | ||
| 122 | locate =>[2, \&_locate], | ||
| 123 | log10 =>[1, \&_log10], | ||
| 124 | minute =>[1, \&_minute], | ||
| 125 | month =>[1, \&_month], | ||
| 126 | monthname =>[1, \&_monthname], | ||
| 127 | number_format =>[4, \&_number_format], | ||
| 128 | power =>[2, \&_power], | ||
| 129 | quarter =>[1, \&_quarter], | ||
| 130 | rand =>[0, \&_rand], | ||
| 131 | regexp =>[2, \&_regexp], | ||
| 132 | replace =>[3, \&_replace], | ||
| 133 | right =>[2, \&_right], | ||
| 134 | second =>[1, \&_second], | ||
| 135 | space =>[1, \&_space], | ||
| 136 | sprintf =>[-1, \&_sprintf], | ||
| 137 | to_days =>[1, \&_to_days], | ||
| 138 | # truncate =>[-1, \&_truncate], | ||
| 139 | ucase =>[1, \&_ucase], | ||
| 140 | unix_timestamp =>[1, \&_unix_timestamp], | ||
| 141 | week =>[1, \&_week], | ||
| 142 | weekday =>[1, \&_weekday], | ||
| 143 | year =>[1, \&_year], | ||
| 144 | }; | ||
| 145 | # See compile_options method | ||
| 146 | # This is very useful to know which features can be used | ||
| 147 | our $COMPILE_OPTIONS = []; | ||
| 148 | |||
| 149 | foreach my $type ( keys( %$DATATYPES_DICT ) ) | ||
| 150 | { | ||
| 151 | if( CORE::exists( $DATATYPES_DICT->{ $type }->{alias} ) && | ||
| 152 | ref( $DATATYPES_DICT->{ $type }->{alias} ) eq 'ARRAY' && | ||
| 153 | scalar( @{$DATATYPES_DICT->{ $type }->{alias}} ) ) | ||
| 154 | { | ||
| 155 | foreach my $alias ( @{$DATATYPES_DICT->{ $type }->{alias}} ) | ||
| 156 | { | ||
| 157 | next if( CORE::exists( $DATATYPES_DICT->{ $alias } ) ); | ||
| 158 | $DATATYPES_DICT->{ $alias } = $DATATYPES_DICT->{ $type }; | ||
| 159 | } | ||
| 160 | } | ||
| 161 | } | ||
| 162 | |||
| 163 | foreach my $type ( keys( %$DATATYPES_DICT ) ) | ||
| 164 | { | ||
| 165 | my $c = $DATATYPES_DICT->{ $type }->{name}; | ||
| 166 | my $code = \&{"DBD::SQLite::Constants::${c}"}; | ||
| 167 | my $val = eval | ||
| 168 | { | ||
| 169 | $code->(); | ||
| 170 | }; | ||
| 171 | if( $@ ) | ||
| 172 | { | ||
| 173 | warn( "Datatype \"DBD::SQLite::Constants::${c}\" is not defined in DBD::SQLite version $DBD::SQLite::VERSION: $@" ); | ||
| 174 | delete( $DATATYPES_DICT->{ $type } ); | ||
| 175 | } | ||
| 176 | else | ||
| 177 | { | ||
| 178 | $DATATYPES_DICT->{ $type }->{constant} = $val; | ||
| 179 | } | ||
| 180 | } | ||
| 181 | |||
| 182 | sub init | ||
| 183 | { | ||
| 184 | my $self = shift( @_ ); | ||
| 185 | $self->{_exception_class} = $EXCEPTION_CLASS; | ||
| 186 | $self->SUPER::init( @_ ) || return( $self->pass_error ); | ||
| 187 | $self->{driver} = 'SQLite'; | ||
| 188 | $self->{_func} = {}; | ||
| 189 | return( $self ); | ||
| 190 | } | ||
| 191 | |||
| 192 | # Get/set alias | ||
| 193 | # sub alias | ||
| 194 | |||
| 195 | # sub as_string | ||
| 196 | |||
| 197 | # sub avoid | ||
| 198 | |||
| 199 | sub attribute($;$@) | ||
| 200 | { | ||
| 201 | my $self = shift( @_ ); | ||
| 202 | # $h->{AttributeName} = ...; # set/write | ||
| 203 | # ... = $h->{AttributeName}; # get/read | ||
| 204 | # 1 means that the attribute may be modified | ||
| 205 | # 0 mneas that the attribute may only be read | ||
| 206 | my $name = shift( @_ ) if( @_ == 1 ); | ||
| 207 | my %arg = ( @_ ); | ||
| 208 | my %attr = | ||
| 209 | ( | ||
| 210 | ActiveKids => 0, | ||
| 211 | AutoCommit => 1, | ||
| 212 | AutoInactiveDestroy => 1, | ||
| 213 | CachedKids => 0, | ||
| 214 | ChildHandles => 0, | ||
| 215 | ChopBlanks => 1, | ||
| 216 | CursorName => 0, | ||
| 217 | Driver => 0, | ||
| 218 | ErrCount => 1, | ||
| 219 | Executed => 0, | ||
| 220 | FetchHashKeyName => 1, | ||
| 221 | HandleError => 1, | ||
| 222 | HandleSetErr => 1, | ||
| 223 | InactiveDestroy => 1, | ||
| 224 | Kids => 0, | ||
| 225 | NAME => 0, | ||
| 226 | NULLABLE => 0, | ||
| 227 | NUM_OF_FIELDS => 0, | ||
| 228 | NUM_OF_PARAMS => 0, | ||
| 229 | # Current database name | ||
| 230 | Name => 0, | ||
| 231 | PRECISION => 0, | ||
| 232 | PrintError => 1, | ||
| 233 | PrintWarn => 1, | ||
| 234 | Profile => 1, | ||
| 235 | RaiseError => 1, | ||
| 236 | RowCacheSize => 0, | ||
| 237 | RowsInCache => 0, | ||
| 238 | SCALE => 0, | ||
| 239 | ShowErrorStatement => 1, | ||
| 240 | Statement => 0, | ||
| 241 | TYPE => 0, | ||
| 242 | Taint => 1, | ||
| 243 | TaintIn => 1, | ||
| 244 | TaintOut => 1, | ||
| 245 | TraceLevel => 1, | ||
| 246 | Type => 1, | ||
| 247 | Username => 0, | ||
| 248 | Warn => 1, | ||
| 249 | # Not used | ||
| 250 | # LongReadLen => 1, | ||
| 251 | # LongTruncOk => 1, | ||
| 252 | # CompatMode => 1, | ||
| 253 | # If you set this to true, "do" method will process multiple statements at one go. | ||
| 254 | # This may be handy, but with performance penalty. See above for details. | ||
| 255 | sqlite_allow_multiple_statements => 1, | ||
| 256 | # If you set this to true, DBD::SQLite tries to see if the bind values are number or | ||
| 257 | # not, and does not quote if they are numbers. | ||
| 258 | sqlite_see_if_its_a_number => 1, | ||
| 259 | sqlite_unicode => 1, | ||
| 260 | # Returns an unprepared part of the statement you pass to "prepare". Typically this | ||
| 261 | # contains nothing but white spaces after a semicolon. | ||
| 262 | sqlite_unprepared_statements => 0, | ||
| 263 | # If you set this to true, DBD::SQLite tries to issue a "begin immediate transaction" | ||
| 264 | # (instead of "begin transaction") when necessary. | ||
| 265 | sqlite_use_immediate_transaction => 1, | ||
| 266 | sqlite_version => 0, | ||
| 267 | ); | ||
| 268 | # Only those attribute exist | ||
| 269 | # Using an a non existing attribute produce an exception, so we better avoid | ||
| 270 | if( $name ) | ||
| 271 | { | ||
| 272 | return( $self->{dbh}->{ $name } ) if( exists( $attr{ $name } ) ); | ||
| 273 | } | ||
| 274 | else | ||
| 275 | { | ||
| 276 | my $value; | ||
| 277 | while( ( $name, $value ) = each( %arg ) ) | ||
| 278 | { | ||
| 279 | # We intend to modifiy the value of an attribute | ||
| 280 | # we are allowed to modify this value if it is true | ||
| 281 | if( exists( $attr{ $name } ) && | ||
| 282 | defined( $value ) && | ||
| 283 | $attr{ $name } ) | ||
| 284 | { | ||
| 285 | $self->{dbh}->{ $name } = $value; | ||
| 286 | } | ||
| 287 | } | ||
| 288 | } | ||
| 289 | } | ||
| 290 | |||
| 291 | # sub available_drivers(@) | ||
| 292 | |||
| 293 | sub begin_work($;$@) | ||
| 294 | { | ||
| 295 | my $self = shift( @_ ); | ||
| 296 | $self->{transaction} = 1; | ||
| 297 | return( $self->{dbh}->begin_work( @_ ) ); | ||
| 298 | } | ||
| 299 | |||
| 300 | # This method is common to DB::Object and DB::Object::Statement | ||
| 301 | # sub bind | ||
| 302 | |||
| 303 | # sub cache | ||
| 304 | |||
| 305 | sub can_update_delete_limit { return( shift->has_compile_option( 'ENABLE_UPDATE_DELETE_LIMIT' ) ); } | ||
| 306 | |||
| 307 | # sub check_driver(@;$@) | ||
| 308 | |||
| 309 | sub commit($;$@) | ||
| 310 | { | ||
| 311 | my $self = shift( @_ ); | ||
| 312 | $self->{transaction} = 0; | ||
| 313 | return( $self->{dbh}->commit( @_ ) ); | ||
| 314 | } | ||
| 315 | |||
| 316 | sub compile_options | ||
| 317 | { | ||
| 318 | my $self = shift( @_ ); | ||
| 319 | return( [ @$COMPILE_OPTIONS ] ) if( scalar( @$COMPILE_OPTIONS ) ); | ||
| 320 | my $system_tmpdir = sys_tmpdir(); | ||
| 321 | my $compile_options_cache_file = $system_tmpdir->join( 'sql_sqlite_compile_options.cfg' ); | ||
| 322 | my @options = (); | ||
| 323 | # if( -e( $compile_options_cache_file ) && !-z( $compile_options_cache_file ) ) | ||
| 324 | if( $compile_options_cache_file->exists && !$compile_options_cache_file->is_empty ) | ||
| 325 | { | ||
| 326 | # 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\": $!" ) ); | ||
| 327 | 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 ) ); | ||
| 328 | my @all = $fh->getlines; | ||
| 329 | # Remove any comments | ||
| 330 | @options = grep( !/^#/, @all ); | ||
| 331 | $fh->close; | ||
| 332 | if( scalar( @options ) ) | ||
| 333 | { | ||
| 334 | $COMPILE_OPTIONS = \@options; | ||
| 335 | # Return a copy only to be safe | ||
| 336 | return( [ @options ] ); | ||
| 337 | } | ||
| 338 | } | ||
| 339 | # If the cache file does not yet exists or there is no options, we do the query | ||
| 340 | 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." ) ); | ||
| 341 | my $all = $self->do( "PRAGMA compile_options" )->fetchall_arrayref; | ||
| 342 | @options = map( $_->[0], @$all ); | ||
| 343 | # 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\": $!" ) ); | ||
| 344 | 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 ) ); | ||
| 345 | $fh->autoflush(1); | ||
| 346 | $fh->print( join( "\n", @options ), "\n" ) || return( $self->error( "Unable to write to the sqlite compile options cache file \"$compile_options_cache_file\": $!" ) ); | ||
| 347 | $fh->close; | ||
| 348 | $COMPILE_OPTIONS = \@options; | ||
| 349 | return( \@options ); | ||
| 350 | } | ||
| 351 | |||
| 352 | # connect() is inherited by DB::Object | ||
| 353 | |||
| 354 | # NOTE: sub constant_to_datatype is inherited | ||
| 355 | |||
| 356 | # sub copy | ||
| 357 | |||
| 358 | # sub create_table($;%) | ||
| 359 | |||
| 360 | # See DB::Object | ||
| 361 | # sub data_sources($;\%) | ||
| 362 | |||
| 363 | # sub data_type | ||
| 364 | |||
| 365 | # sub database | ||
| 366 | |||
| 367 | sub database_file | ||
| 368 | { | ||
| 369 | return( shift->{database_file} ); | ||
| 370 | } | ||
| 371 | |||
| 372 | sub databases | ||
| 373 | { | ||
| 374 | my $self = shift( @_ ); | ||
| 375 | # return( $self->error( "Not connected to PostgreSQL server yet. Issue $dbh->connect first." ) ) if( !$self->{dbh} ); | ||
| 376 | my $dbh; | ||
| 377 | # If there is no connection yet, then create one using the postgres login. | ||
| 378 | # There should not be a live user and database just to check what databases there are. | ||
| 379 | if( !$self->{dbh} ) | ||
| 380 | { | ||
| 381 | # try-catch | ||
| 382 | local $@; | ||
| 383 | $dbh = eval | ||
| 384 | { | ||
| 385 | $self->connect( @_ ); | ||
| 386 | }; | ||
| 387 | if( $@ ) | ||
| 388 | { | ||
| 389 | return( $self->error( "Error trying to connect to the SQLite database file: $@" ) ); | ||
| 390 | } | ||
| 391 | $dbh or return( $self->pass_error ); | ||
| 392 | } | ||
| 393 | else | ||
| 394 | { | ||
| 395 | $dbh = $self; | ||
| 396 | } | ||
| 397 | my $temp = $dbh->do( 'PRAGMA database_list' )->fetchall_arrayref( {} ); | ||
| 398 | my @dbases = map( $_->{name}, @$temp ); | ||
| 399 | return( @dbases ); | ||
| 400 | } | ||
| 401 | |||
| 402 | # NOTE: sub datatype_dict is inherited | ||
| 403 | |||
| 404 | # NOTE: sub datatype_to_constant is inherited | ||
| 405 | |||
| 406 | # NOTE: sub datatypes is in inherited | ||
| 407 | |||
| 408 | sub func | ||
| 409 | { | ||
| 410 | my $self = shift( @_ ); | ||
| 411 | my $table = shift( @_ ); | ||
| 412 | # e.g. table_attributes to get the detail information on table columns | ||
| 413 | my $func_name = shift( @_ ); | ||
| 414 | # Returns: | ||
| 415 | # NAME attribute name | ||
| 416 | # TYPE attribute type | ||
| 417 | # SIZE attribute size (-1 for variable size) | ||
| 418 | # NULLABLE flag nullable | ||
| 419 | # DEFAULT default value | ||
| 420 | # CONSTRAINT constraint | ||
| 421 | # PRIMARY_KEY flag is_primary_key | ||
| 422 | # REMARKS attribute description | ||
| 423 | return( $self->{dbh}->func( $table, $func_name ) ); | ||
| 424 | } | ||
| 425 | |||
| 426 | # <https://metacpan.org/pod/DBD::SQLite::Constants#datatypes-(fundamental_datatypes)> | ||
| 427 | # <https://www.sqlite.org/datatype3.html> | ||
| 428 | # In SQLite, there are only 4 types: | ||
| 429 | # SQLITE_INTEGER | ||
| 430 | # SQLITE_FLOAT | ||
| 431 | # SQLITE_BLOB | ||
| 432 | # SQLITE_NULL | ||
| 433 | sub get_sql_type | ||
| 434 | { | ||
| 435 | my $self = shift( @_ ); | ||
| 436 | my $type = shift( @_ ) || return( $self->error( "No sql type was provided to get its constant." ) ); | ||
| 437 | $type = lc( $type ); | ||
| 438 | if( CORE::exists( $DATATYPES_DICT->{ $type } ) && | ||
| 439 | $type ne $DATATYPES_DICT->{ $type }->{type} ) | ||
| 440 | { | ||
| 441 | $type = $DATATYPES_DICT->{ $type }->{type}; | ||
| 442 | } | ||
| 443 | my $const; | ||
| 444 | if( substr( $type, 0, 7 ) eq 'sqlite_' ) | ||
| 445 | { | ||
| 446 | $const = $self->{dbh}->can( "DBD::SQLite::\U${type}\E" ); | ||
| 447 | } | ||
| 448 | else | ||
| 449 | { | ||
| 450 | $const = $self->{dbh}->can( "DBD::SQLite::SQLITE_\U${type}\E" ); | ||
| 451 | } | ||
| 452 | return( '' ) if( !defined( $const ) ); | ||
| 453 | return( $const->() ); | ||
| 454 | } | ||
| 455 | |||
| 456 | sub having | ||
| 457 | { | ||
| 458 | my $self = shift( @_ ); | ||
| 459 | my $q = $self->_reset_query; | ||
| 460 | return( $q->having( @_ ) ); | ||
| 461 | } | ||
| 462 | |||
| 463 | # https://www.sqlite.org/compile.html | ||
| 464 | sub has_compile_option | ||
| 465 | { | ||
| 466 | my $self = shift( @_ ); | ||
| 467 | my $opt = shift( @_ ) || return( $self->error( "No compile option was provided to check" ) ); | ||
| 468 | $opt = uc( $opt ); | ||
| 469 | my $all = $self->compile_options; | ||
| 470 | my @found = grep( /^$opt/, @$all ); | ||
| 471 | return( $found[0] ) if( scalar( @found ) ); | ||
| 472 | return( '' ); | ||
| 473 | } | ||
| 474 | |||
| 475 | sub last_insert_id | ||
| 476 | { | ||
| 477 | my $self = shift( @_ ); | ||
| 478 | my $table = shift( @_ ) || $self->{table}; | ||
| 479 | return( $self->{dbh}->last_insert_id( undef, undef, $table, undef ) ); | ||
| 480 | } | ||
| 481 | |||
| 482 | sub lock { return( shift->error( "Table lock is unsupported in SQLite." ) ); } | ||
| 483 | |||
| 484 | sub pragma | ||
| 485 | { | ||
| 486 | my $self = shfit( @_ ); | ||
| 487 | my $key2val = | ||
| 488 | { | ||
| 489 | foreign_keys => [ qw( ON OFF ) ], | ||
| 490 | journal_mode => [ qw( DELETE TRUNCATE ) ], | ||
| 491 | legacy_file_format => [ qw( ON OFF ) ], | ||
| 492 | reverse_unordered_selects => [ qw( ON OFF ) ], | ||
| 493 | synchronous => [ qw( ON OFF ) ], | ||
| 494 | # To avoid corruption after BEGIN starts, DBD uses BEGIN IMMEDIATE. Default is TRUE | ||
| 495 | sqlite_use_immediate_transaction => [ qw( 1 0 ) ], | ||
| 496 | cache_size => qr/^\d+$/ | ||
| 497 | }; | ||
| 498 | } | ||
| 499 | |||
| 500 | sub query_object { return( shift->_set_get_object( 'query_object', 'DB::Object::SQLite::Query', @_ ) ); } | ||
| 501 | |||
| 502 | # https://www.sqlite.org/lang_replace.html | ||
| 503 | # https://www.sqlite.org/lang_conflict.html | ||
| 504 | # REPLACE is an alias for INSERT OR REPLACE | ||
| 505 | # https://www.sqlite.org/lang_insert.html | ||
| 506 | sub replace | ||
| 507 | { | ||
| 508 | my $self = shift( @_ ); | ||
| 509 | my $data = shift( @_ ) if( @_ == 1 && ref( $_[0] ) ); | ||
| 510 | my @arg = @_; | ||
| 511 | my %arg = (); | ||
| 512 | my $select = ''; | ||
| 513 | if( !%arg && $data && $self->_is_hash( $data => 'strict' ) ) | ||
| 514 | { | ||
| 515 | %arg = %$data; | ||
| 516 | } | ||
| 517 | elsif( $data && ref( $data ) eq 'DB::Object::Statement' ) | ||
| 518 | { | ||
| 519 | $select = $data->as_string(); | ||
| 520 | } | ||
| 521 | %arg = @arg if( @arg ); | ||
| 522 | my $table = $self->{table} || | ||
| 523 | return( $self->error( "No table was provided to replace data." ) ); | ||
| 524 | my $structure = $self->structure || return( $self->pass_error ); | ||
| 525 | my $null = $self->null(); | ||
| 526 | my @avoid = (); | ||
| 527 | foreach my $field ( keys( %$structure ) ) | ||
| 528 | { | ||
| 529 | # It is useless to insert a blank data in a field whose default value is NULL. | ||
| 530 | # Especially since a test on a NULL field may be made specifically. | ||
| 531 | push( @avoid, $field ) if( !CORE::exists( $arg{ $field } ) && $null->{ $field } ); | ||
| 532 | } | ||
| 533 | my $db_data = $self->getdefault({ | ||
| 534 | table => $table, | ||
| 535 | arg => \@arg, | ||
| 536 | avoid => \@avoid | ||
| 537 | }); | ||
| 538 | my( $fields, $values ) = $db_data->format_statement(); | ||
| 539 | $self->_reset_query(); | ||
| 540 | delete( $self->{query_reset} ); | ||
| 541 | $self->{binded_values} = $db_data->{binded_values}; | ||
| 542 | my $query = $self->{query} = $select ? "REPLACE INTO $table $select" : "REPLACE INTO $table ($fields) VALUES($values)"; | ||
| 543 | # Everything meaningfull lies within the object | ||
| 544 | # If no bind should be done _save_bind does nothing | ||
| 545 | $self->_save_bind(); | ||
| 546 | # Query string should lie within the object | ||
| 547 | # _cache_this sends back an object no matter what or unde() if an error occurs | ||
| 548 | my $sth = $self->_cache_this($query); | ||
| 549 | # STOP! No need to go further | ||
| 550 | if( !defined( $sth ) ) | ||
| 551 | { | ||
| 552 | return( $self->error( "Error while preparing query to replace data into table '$table':\n$query", $self->errstr() ) ); | ||
| 553 | } | ||
| 554 | if( !defined( wantarray() ) ) | ||
| 555 | { | ||
| 556 | $sth->execute() || | ||
| 557 | return( $self->error( "Error while executing query to replace data to table '$table':\n$query" ) ); | ||
| 558 | } | ||
| 559 | return( $sth ); | ||
| 560 | } | ||
| 561 | |||
| 562 | sub register_function | ||
| 563 | { | ||
| 564 | my $self = shift( @_ ); | ||
| 565 | return( $self->error( "I was expecting an hash reference of parameters as only argument." ) ) if( !$self->_is_hash( $_[0] => 'strict' ) ); | ||
| 566 | my $opts = shift( @_ ); | ||
| 567 | return( $self->error( "Parameter 'code' to register the private function must be a code reference." ) ) if( ref( $opts->{code} ) ne 'CODE' ); | ||
| 568 | $opts->{flags} = [] if( !CORE::exists( $opts->{flags} ) ); | ||
| 569 | return( $self->error( "Parameter 'flags' must be an array reference." ) ) if( !$self->_is_array( $opts->{flags} ) ); | ||
| 570 | $opts->{argc} = -1 if( !CORE::exists( $opts->{argc} ) ); | ||
| 571 | return( $self->error( "No name was provided for this private function." ) ) if( !CORE::exists( $opts->{name} ) ); | ||
| 572 | my $name = $opts->{name}; | ||
| 573 | 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\_]+$/ ); | ||
| 574 | my $funcs = $self->{func}; | ||
| 575 | $funcs->{ lc( $opts->{name} ) } = $opts; | ||
| 576 | $self->sql_function_register( $opts ) if( $self->{dbh} ); | ||
| 577 | return( $opts ); | ||
| 578 | } | ||
| 579 | |||
| 580 | sub remove_function | ||
| 581 | { | ||
| 582 | my $self = shift( @_ ); | ||
| 583 | my $name = shift( @_ ) || return( $self->error( "No private function was provided to be removed." ) ); | ||
| 584 | my $funcs = $self->{func}; | ||
| 585 | $name = lc( $name ); | ||
| 586 | return( 0 ) if( !CORE::exists( $funcs->{ $name } ) ); | ||
| 587 | return( CORE::delete( $funcs->{ $name } ) ); | ||
| 588 | } | ||
| 589 | |||
| 590 | sub returning | ||
| 591 | { | ||
| 592 | my $self = shift( @_ ); | ||
| 593 | my $q = $self->_reset_query; | ||
| 594 | return( $q->returning( @_ ) ); | ||
| 595 | } | ||
| 596 | |||
| 597 | sub rollback | ||
| 598 | { | ||
| 599 | return( shift->{dbh}->rollback() ); | ||
| 600 | } | ||
| 601 | |||
| 602 | sub sql_function_register | ||
| 603 | { | ||
| 604 | my $self = shift( @_ ); | ||
| 605 | my $opts = shift( @_ ) || return( $self->error( "No private function hash reference provided." ) ); | ||
| 606 | my $dbh = $self->{dbh} || return( $self->error( "No active database handler available." ) ); | ||
| 607 | $opts->{flags} = [] if( !exists( $opts->{flags} ) || ref( $opts->{flags} ) ne 'ARRAY' ); | ||
| 608 | my $flag; | ||
| 609 | my $eval = join( '|', @{$opts->{flags}} ); | ||
| 610 | $flag = eval( $eval ); | ||
| 611 | my $code = $opts->{code}; | ||
| 612 | if( defined( $flag ) ) | ||
| 613 | { | ||
| 614 | $dbh->sqlite_create_function( $opts->{name}, $opts->{argc}, sub{ my @arg = @_; unshift( @arg, $self ); $code->( @arg ); }, $flag ); | ||
| 615 | } | ||
| 616 | else | ||
| 617 | { | ||
| 618 | $dbh->sqlite_create_function( $opts->{name}, $opts->{argc}, sub{ my @arg = @_; unshift( @arg, $self ); $code->( @arg ); } ); | ||
| 619 | } | ||
| 620 | $opts->{_registered_on} = time(); | ||
| 621 | } | ||
| 622 | |||
| 623 | # http://www.sqlite.org/c3ref/c_status_malloc_count.html | ||
| 624 | sub stat | ||
| 625 | { | ||
| 626 | my $self = shift( @_ ); | ||
| 627 | my $opt = $self->_get_args_as_hash( @_ ); | ||
| 628 | my $ref = $opt->{reset} ? DBD::SQLite::sqlite_status( 0 ) : DBD::SQLite::sqlite_status(); | ||
| 629 | if( $opt->{type} ) | ||
| 630 | { | ||
| 631 | return( exists( $ref->{ $opt->{type} } ) ? $ref->{ $opt->{type} } : undef() ); | ||
| 632 | } | ||
| 633 | else | ||
| 634 | { | ||
| 635 | return( wantarray() ? () : undef() ) if( !%$ref ); | ||
| 636 | return( wantarray() ? %$ref : $ref ); | ||
| 637 | } | ||
| 638 | } | ||
| 639 | |||
| 640 | # sub table_exists | ||
| 641 | # { | ||
| 642 | # my $self = shift( @_ ); | ||
| 643 | # my $table = shift( @_ ) || | ||
| 644 | # return( $self->error( "You must provide a table name to access the table methods." ) ); | ||
| 645 | # my $cache_tables = $self->cache_tables; | ||
| 646 | # my $tables_in_cache = $cache_tables->get({ | ||
| 647 | # host => 'localhost', | ||
| 648 | # driver => $self->driver, | ||
| 649 | # port => 0, | ||
| 650 | # database => $self->database, | ||
| 651 | # }); | ||
| 652 | # foreach my $ref ( @$tables_in_cache ) | ||
| 653 | # { | ||
| 654 | # return( 1 ) if( $ref->{name} eq $table ); | ||
| 655 | # } | ||
| 656 | # # We did not find it, so let's try by checking directly the database | ||
| 657 | # my $def = $self->table_info( $table ) || return; | ||
| 658 | # return(0) if( !scalar( @$def ) ); | ||
| 659 | # return(1); | ||
| 660 | # } | ||
| 661 | |||
| 662 | sub table_info | ||
| 663 | { | ||
| 664 | my $self = shift( @_ ); | ||
| 665 | my $table = shift( @_ ) || | ||
| 666 | return( $self->error( "You must provide a table name to access the table methods." ) ); | ||
| 667 | my $opts = $self->_get_args_as_hash( @_ ); | ||
| 668 | my $sql = <<'EOT'; | ||
| 669 | SELECT | ||
| 670 | name | ||
| 671 | ,type | ||
| 672 | FROM sqlite_master | ||
| 673 | WHERE type IN ('table', 'view') AND name = ? | ||
| 674 | EOT | ||
| 675 | my $dbh = $self->{dbh} || return( $self->error( "Could not find database handler." ) ); | ||
| 676 | 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 ) ); | ||
| 677 | $sth->execute( $table ) || return( $self->error( "An error occured while executing query to check if table \"$table\" exists in database \"", $self->database, "\: ", $sth->errstr ) ); | ||
| 678 | my $all = $sth->fetchall_arrayref( {} ); | ||
| 679 | $sth->finish; | ||
| 680 | return( {} ) if( !scalar( @$all ) ); | ||
| 681 | return( $all->[0] ); | ||
| 682 | } | ||
| 683 | |||
| 684 | sub tables | ||
| 685 | { | ||
| 686 | my $self = shift( @_ ); | ||
| 687 | my $db = shift( @_ ) || $self->{database}; | ||
| 688 | my $all = $self->tables_info || return; | ||
| 689 | my @tables = map( $_->{name}, @$all ); | ||
| 690 | # return( wantarray() ? () : undef() ) if( !@tables ); | ||
| 691 | # return( wantarray() ? @tables : \@tables ); | ||
| 692 | return( \@tables ); | ||
| 693 | } | ||
| 694 | |||
| 695 | sub tables_info | ||
| 696 | { | ||
| 697 | my $self = shift( @_ ); | ||
| 698 | my $db = shift( @_ ) || $self->{database}; | ||
| 699 | # Parameters are: ?, schema, table, and type | ||
| 700 | # my $sth = $self->{dbh}->table_info( undef, undef, $table, "TABLE,VIEW" ); | ||
| 701 | # The original query was fetched by connecting to Postgres with psql -E and executing the command \z | ||
| 702 | # This revised query will fetch only tables, views, materialised view and foreign tables, but will avoid the mysterious view called sequence_setvals | ||
| 703 | # https://stackoverflow.com/questions/82875/how-to-list-the-tables-in-a-sqlite-database-file-that-was-opened-with-attach | ||
| 704 | my $query = <<SQL; | ||
| 705 | SELECT name FROM sqlite_master | ||
| 706 | WHERE type IN ('table','view') AND name NOT LIKE 'sqlite_%' | ||
| 707 | UNION ALL | ||
| 708 | SELECT name FROM sqlite_temp_master | ||
| 709 | WHERE type IN ('table','view') | ||
| 710 | ORDER BY 1 | ||
| 711 | SQL | ||
| 712 | my $dbh = $self->{dbh} || return( $self->error( "Could not find database handler." ) ); | ||
| 713 | my $sth = $dbh->prepare_cached( $query ) || return( $self->error( sprintf( "Error while preparing query $query: %s", $dbh->errstr ) ) ); | ||
| 714 | $sth->execute() || return( $self->error( sprintf( "Error while executing query $query: %s", $sth->errstr ) ) ); | ||
| 715 | my $all = $sth->fetchall_arrayref( {} ); | ||
| 716 | return( $all ); | ||
| 717 | } | ||
| 718 | |||
| 719 | sub trace { return( shift->error( "Trace is unsupported on SQLite." ) ); } | ||
| 720 | |||
| 721 | sub unlock { return( shift->error( "unlock() does not work with SQLite." ) ); } | ||
| 722 | |||
| 723 | sub variables | ||
| 724 | { | ||
| 725 | return( shift->error( "variables is currently unsupported in Postgres" ) ); | ||
| 726 | } | ||
| 727 | |||
| 728 | # https://www.sqlite.org/versionnumbers.html | ||
| 729 | sub version | ||
| 730 | { | ||
| 731 | my $self = shift( @_ ); | ||
| 732 | # If we already have the information, let's use our cache instead of making a query | ||
| 733 | return( $self->{_db_version} ) if( length( $self->{_db_version} ) ); | ||
| 734 | my $sql = 'SELECT sqlite_version()'; | ||
| 735 | my $sth = $self->do( $sql ) || return( $self->error( "Unable to issue the sql statement '$sql' to get the server version: ", $self->errstr ) ); | ||
| 736 | my $ver = $sth->fetchrow; | ||
| 737 | $sth->finish; | ||
| 738 | # We cache it | ||
| 739 | $self->{_db_version} = version->parse( $ver ); | ||
| 740 | return( $ver ); | ||
| 741 | } | ||
| 742 | |||
| 743 | sub _check_connect_param | ||
| 744 | { | ||
| 745 | my $self = shift( @_ ); | ||
| 746 | my $param = $self->SUPER::_check_connect_param( @_ ); | ||
| 747 | if( !$param->{database_file} && $param->{database} ) | ||
| 748 | { | ||
| 749 | my( $filename, $path, $ext ); | ||
| 750 | my $uri = CORE::exists( $param->{uri} ) ? $param->{uri} : ''; | ||
| 751 | my $db = $param->{database} ? $param->{database} : ( $uri->path_segments )[-1]; | ||
| 752 | $path = $uri ? $uri->path : $db; | ||
| 753 | # $db = Cwd::abs_path( $uri ? $uri->path : $db ); | ||
| 754 | # $db = File::Spec->rel2abs( $path ); | ||
| 755 | $db = $self->new_file( $path ); | ||
| 756 | # If we cannot find the file and it does not end with .sqlite, let's add the extension | ||
| 757 | # So the user can provide the database parameter just like database => 'test' or database => './test' | ||
| 758 | $db = "$db.sqlite" if( !-e( $db ) && $db !~ /\.sqlite$/i ); | ||
| 759 | # ( $filename, $path, $ext ) = File::Basename::fileparse( $db, qr/\.[^\.]+$/ ); | ||
| 760 | ( $filename, $path, $ext ) = $db->baseinfo( qr/\.[^\.]+$/ ); | ||
| 761 | $param->{database} = $filename; | ||
| 762 | $param->{database_file} = $self->{database_file} = $db; | ||
| 763 | } | ||
| 764 | $param->{host} = 'localhost' if( !length( $param->{host} ) ); | ||
| 765 | $param->{port} = 0 if( !CORE::exists( $param->{port} ) ); | ||
| 766 | return( $param ); | ||
| 767 | } | ||
| 768 | |||
| 769 | sub _check_default_option | ||
| 770 | { | ||
| 771 | my $self = shift( @_ ); | ||
| 772 | my $opts = $self->_get_args_as_hash( @_ ); | ||
| 773 | return( $self->error( "Provided option is not a hash reference." ) ) if( !$self->_is_hash( $opts => 'strict' ) ); | ||
| 774 | $opts->{sqlite_unicode} = 1 if( !CORE::exists( $opts->{sqlite_unicode} ) ); | ||
| 775 | return( $opts ); | ||
| 776 | } | ||
| 777 | |||
| 778 | sub _connection_options | ||
| 779 | { | ||
| 780 | my $self = shift( @_ ); | ||
| 781 | my $param = shift( @_ ); | ||
| 782 | my @sqlite_params = grep( /^sqlite_/, keys( %$param ) ); | ||
| 783 | my $opt = $self->SUPER::_connection_options( $param ); | ||
| 784 | @$opt{ @sqlite_params } = @$param{ @sqlite_params }; | ||
| 785 | return( $opt ); | ||
| 786 | } | ||
| 787 | |||
| 788 | sub _connection_params2hash_driver | ||
| 789 | { | ||
| 790 | my $self = shift( @_ ); | ||
| 791 | my $opts = $self->_get_args_as_hash( @_ ); | ||
| 792 | $opts->{sqlite_unicode} = 1 if( !length( $opts->{sqlite_unicode} ) ); | ||
| 793 | return( $opts ); | ||
| 794 | } | ||
| 795 | |||
| 796 | sub _connection_parameters | ||
| 797 | { | ||
| 798 | my $self = shift( @_ ); | ||
| 799 | my $param = shift( @_ ); | ||
| 800 | # Even though login, password, server, host are not used, I was hesitating, but decided to leave them as ok, and ignore them | ||
| 801 | # Or maybe should I issue an error when they are provided? | ||
| 802 | my $core = [qw( db login passwd host port driver database server opt uri debug cache_connections cache_dir cache_query cache_table unknown_field use_cache )]; | ||
| 803 | my @sqlite_params = grep( /^sqlite_/, keys( %$param ) ); | ||
| 804 | # See DBD::SQLite for the list of valid parameters | ||
| 805 | # 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 | ||
| 806 | push( @$core, @sqlite_params ); | ||
| 807 | return( $core ); | ||
| 808 | } | ||
| 809 | |||
| 810 | sub _dbi_connect | ||
| 811 | { | ||
| 812 | my $self = shift( @_ ); | ||
| 813 | my $dbh = $self->{dbh} = $self->SUPER::_dbi_connect( @_ ); | ||
| 814 | # my $func = $self->{_func}; | ||
| 815 | my $func = $self->{_func}; | ||
| 816 | foreach my $k ( sort( keys( %$PRIVATE_FUNCTIONS ) ) ) | ||
| 817 | { | ||
| 818 | my $this = $PRIVATE_FUNCTIONS->{ $k }; | ||
| 819 | my $ref = | ||
| 820 | { | ||
| 821 | name => $k, | ||
| 822 | argc => $this->[0], | ||
| 823 | code => $this->[1], | ||
| 824 | }; | ||
| 825 | $func->{ $k } = $ref; | ||
| 826 | } | ||
| 827 | foreach my $name ( sort( keys( %$func ) ) ) | ||
| 828 | { | ||
| 829 | my $ref = $func->{ $name }; | ||
| 830 | if( $ref->{_registered_on} ) | ||
| 831 | { | ||
| 832 | next; | ||
| 833 | } | ||
| 834 | $self->sql_function_register( $ref ); | ||
| 835 | $ref->{_registered_on} = time(); | ||
| 836 | } | ||
| 837 | return( $dbh ); | ||
| 838 | } | ||
| 839 | |||
| 840 | sub _dsn | ||
| 841 | { | ||
| 842 | my $self = shift( @_ ); | ||
| 843 | my $db = $self->{database_file} || return( $self->error( "No database file was specified." ) ); | ||
| 844 | # return( $self->error( "Database file \"$db\" does not exist." ) ) if( !-e( $db ) ); | ||
| 845 | return( $self->error( "Database file \"$db\" is not writable." ) ) if( -e( $db ) && !-w( $db ) ); | ||
| 846 | my @params = ( sprintf( 'dbi:%s:', $self->{driver} ) ); | ||
| 847 | push( @params, sprintf( 'dbname=%s', $db ) ); | ||
| 848 | return( join( ';', @params ) ); | ||
| 849 | } | ||
| 850 | |||
| 851 | sub _parse_timestamp | ||
| 852 | { | ||
| 853 | my $self = shift( @_ ); | ||
| 854 | my $str = shift( @_ ); | ||
| 855 | # No value was actually provided | ||
| 856 | return if( !length( $str ) ); | ||
| 857 | # try-catch | ||
| 858 | local $@; | ||
| 859 | my $tz = eval | ||
| 860 | { | ||
| 861 | return( DateTime::TimeZone->new( name => 'local' ) ); | ||
| 862 | }; | ||
| 863 | if( $@ ) | ||
| 864 | { | ||
| 865 | $tz = DateTime::TimeZone->new( name => 'UTC' ); | ||
| 866 | } | ||
| 867 | my $error = 0; | ||
| 868 | my $opt = | ||
| 869 | { | ||
| 870 | pattern => '%Y-%m-%d %T', | ||
| 871 | locale => 'en_GB', | ||
| 872 | time_zone => $tz->name, | ||
| 873 | on_error => sub{ $error++ }, | ||
| 874 | }; | ||
| 875 | # 2019-06-19 23:23:57.000000000+0900 | ||
| 876 | # From PostgreSQL: 2019-06-20 11:02:36.306917+09 | ||
| 877 | # ISO 8601: 2019-06-20T11:08:27 | ||
| 878 | if( $str =~ /(\d{4})[-|\/](\d{1,2})[-|\/](\d{1,2})(?:[[:blank:]]+|T)(\d{1,2}:\d{1,2}:\d{1,2})(?:\.\d+)?((?:\+|\-)\d{2,4})?/ ) | ||
| 879 | { | ||
| 880 | my( $date, $time, $zone ) = ( "$1-$2-$3", $4, $5 ); | ||
| 881 | if( !length( $zone ) ) | ||
| 882 | { | ||
| 883 | my $dt = DateTime->now( time_zone => $tz ); | ||
| 884 | my $offset = $dt->offset; | ||
| 885 | # e.g. 9 or possibly 9.5 | ||
| 886 | my $offset_hour = ( $offset / 3600 ); | ||
| 887 | # e.g. 9.5 => 0.5 * 60 = 30 | ||
| 888 | my $offset_min = ( $offset_hour - CORE::int( $offset_hour ) ) * 60; | ||
| 889 | $zone = sprintf( '%+03d%02d', $offset_hour, $offset_min ); | ||
| 890 | } | ||
| 891 | $date =~ tr/\//-/; | ||
| 892 | $zone .= '00' if( length( $zone ) == 3 ); | ||
| 893 | $str = "$date $time$zone"; | ||
| 894 | $opt->{pattern} = '%Y-%m-%d %T%z'; | ||
| 895 | } | ||
| 896 | # From SQLite: 2019-06-20 02:03:14 | ||
| 897 | # From MySQL: 2019-06-20 11:04:01 | ||
| 898 | elsif( $str =~ /(\d{4})[-|\/](\d{1,2})[-|\/](\d{1,2})(?:[[:blank:]]+|T)(\d{1,2}:\d{1,2}:\d{1,2})/ ) | ||
| 899 | { | ||
| 900 | my( $date, $time ) = ( "$1-$2-$3", $4 ); | ||
| 901 | my $dt = DateTime->now( time_zone => $tz ); | ||
| 902 | my $offset = $dt->offset; | ||
| 903 | # e.g. 9 or possibly 9.5 | ||
| 904 | my $offset_hour = ( $offset / 3600 ); | ||
| 905 | # e.g. 9.5 => 0.5 * 60 = 30 | ||
| 906 | my $offset_min = ( $offset_hour - CORE::int( $offset_hour ) ) * 60; | ||
| 907 | my $offset_str = sprintf( '%+03d%02d', $offset_hour, $offset_min ); | ||
| 908 | $date =~ tr/\//-/; | ||
| 909 | $str = "$date $time$offset_str"; | ||
| 910 | $opt->{pattern} = '%Y-%m-%d %T%z'; | ||
| 911 | } | ||
| 912 | elsif( $str =~ /^(\d{4})[-|\/](\d{1,2})[-|\/](\d{1,2})$/ ) | ||
| 913 | { | ||
| 914 | $str = "$1-$2-$3"; | ||
| 915 | $opt->{pattern} = '%Y-%m-%d'; | ||
| 916 | } | ||
| 917 | my $strp = DateTime::Format::Strptime->new( %$opt ); | ||
| 918 | my $dt = $strp->parse_datetime( $str ); | ||
| 919 | return( $dt ); | ||
| 920 | } | ||
| 921 | |||
| 922 | # Private function | ||
| 923 | sub _ceiling | ||
| 924 | { | ||
| 925 | my $self = shift( @_ ); | ||
| 926 | my @args = @_; | ||
| 927 | return( POSIX::ceil( $args[0] ) ); | ||
| 928 | } | ||
| 929 | |||
| 930 | sub _concat | ||
| 931 | { | ||
| 932 | my $self = shift( @_ ); | ||
| 933 | my @args = @_; | ||
| 934 | return( join( '', @args ) ); | ||
| 935 | } | ||
| 936 | |||
| 937 | sub _curdate | ||
| 938 | { | ||
| 939 | my $self = shift( @_ ); | ||
| 940 | my @args = @_; | ||
| 941 | |||
| 942 | # try-catch | ||
| 943 | local $@; | ||
| 944 | my $tz = eval | ||
| 945 | { | ||
| 946 | return( DateTime::TimeZone->new( name => 'local' ) ); | ||
| 947 | }; | ||
| 948 | if( $@ ) | ||
| 949 | { | ||
| 950 | $tz = DateTime::TimeZone->new( name => 'UTC' ); | ||
| 951 | } | ||
| 952 | my $d = DateTime->from_epoch( epoch => time(), time_zone => $tz->name ); | ||
| 953 | return( $d->ymd( '-' ) ); | ||
| 954 | } | ||
| 955 | |||
| 956 | sub _curtime | ||
| 957 | { | ||
| 958 | my $self = shift( @_ ); | ||
| 959 | my @args = @_; | ||
| 960 | # try-catch | ||
| 961 | local $@; | ||
| 962 | my $tz = eval | ||
| 963 | { | ||
| 964 | return( DateTime::TimeZone->new( name => 'local' ) ); | ||
| 965 | }; | ||
| 966 | if( $@ ) | ||
| 967 | { | ||
| 968 | $tz = DateTime::TimeZone->new( name => 'UTC' ); | ||
| 969 | } | ||
| 970 | my $d = DateTime->now( time_zone => $tz->name ); | ||
| 971 | return( $d->hms( ':' ) ); | ||
| 972 | } | ||
| 973 | |||
| 974 | # e.g. Monday | ||
| 975 | sub _dayname | ||
| 976 | { | ||
| 977 | my $self = shift( @_ ); | ||
| 978 | my @args = @_; | ||
| 979 | my $dt = $self->_parse_timestamp( $args[0] ) || return; | ||
| 980 | return( $dt->day_name ); | ||
| 981 | } | ||
| 982 | |||
| 983 | # E.g.: 17 | ||
| 984 | sub _dayofmonth | ||
| 985 | { | ||
| 986 | my $self = shift( @_ ); | ||
| 987 | my @args = @_; | ||
| 988 | my $dt = $self->_parse_timestamp( $args[0] ) || return; | ||
| 989 | return( $dt->day ); | ||
| 990 | } | ||
| 991 | |||
| 992 | # timestamp, [integer] 1 to 7 | ||
| 993 | sub _dayofweek | ||
| 994 | { | ||
| 995 | my $self = shift( @_ ); | ||
| 996 | my @args = @_; | ||
| 997 | my $dt = $self->_parse_timestamp( $args[0] ) || return; | ||
| 998 | return( $dt->day_of_week ); | ||
| 999 | } | ||
| 1000 | |||
| 1001 | # E.g.: 170 | ||
| 1002 | sub _dayofyear | ||
| 1003 | { | ||
| 1004 | my $self = shift( @_ ); | ||
| 1005 | my @args = @_; | ||
| 1006 | my $dt = $self->_parse_timestamp( $args[0] ) || return; | ||
| 1007 | return( $dt->day_of_year ); | ||
| 1008 | } | ||
| 1009 | |||
| 1010 | # http://stackoverflow.com/questions/10034636/postgres-longitude-longitude-query | ||
| 1011 | sub _distance_miles | ||
| 1012 | { | ||
| 1013 | my $self = shift( @_ ); | ||
| 1014 | my @args = @_; | ||
| 1015 | my( $lat1, $lon1, $lat2, $lon2 ) = @args; | ||
| 1016 | my $x = 69.1 * ( $lat2 - $lat1 ); | ||
| 1017 | my $y = 69.1 * ( $lon2 - $lon1 ) * cos( $lat1 / 57.3 ); | ||
| 1018 | return( sqrt( $x * $x + $y * $y ) ); | ||
| 1019 | } | ||
| 1020 | |||
| 1021 | sub _from_days | ||
| 1022 | { | ||
| 1023 | my $self = shift( @_ ); | ||
| 1024 | my @args = @_; | ||
| 1025 | my $from_days = $args[0]; | ||
| 1026 | # try-catch | ||
| 1027 | local $@; | ||
| 1028 | my $tz = eval | ||
| 1029 | { | ||
| 1030 | return( DateTime::TimeZone->new( name => 'local' ) ); | ||
| 1031 | }; | ||
| 1032 | if( $@ ) | ||
| 1033 | { | ||
| 1034 | $tz = DateTime::TimeZone->new( name => 'UTC' ); | ||
| 1035 | } | ||
| 1036 | my $origin = DateTime->new( | ||
| 1037 | year => 0, | ||
| 1038 | month => 1, | ||
| 1039 | day => 1, | ||
| 1040 | hour => 0, | ||
| 1041 | minute => 0, | ||
| 1042 | second => 0, | ||
| 1043 | time_zone => $tz, | ||
| 1044 | ); | ||
| 1045 | my $epoch = DateTime->from_epoch( epoch => 0, time_zone => $tz ); | ||
| 1046 | # https://stackoverflow.com/questions/821423/how-can-i-calculate-the-number-of-days-between-two-dates-in-perl#7111718 | ||
| 1047 | my $epoch_days = $epoch->delta_days( $origin )->delta_days(); | ||
| 1048 | my $days_since_epoch = $from_days - int( $epoch_days ); | ||
| 1049 | my $dt = DateTime->from_epoch( epoch => ( $days_since_epoch * 86400 ), time_zone => $tz ); | ||
| 1050 | return( $dt ); | ||
| 1051 | } | ||
| 1052 | |||
| 1053 | sub _from_unixtime | ||
| 1054 | { | ||
| 1055 | my $self = shift( @_ ); | ||
| 1056 | my @args = @_; | ||
| 1057 | return if( $args[0] !~ /^\d+$/ ); | ||
| 1058 | # try-catch | ||
| 1059 | local $@; | ||
| 1060 | my $tz = eval | ||
| 1061 | { | ||
| 1062 | return( DateTime::TimeZone->new( name => 'local' ) ); | ||
| 1063 | }; | ||
| 1064 | if( $@ ) | ||
| 1065 | { | ||
| 1066 | $tz = DateTime::TimeZone->new( name => 'UTC' ); | ||
| 1067 | } | ||
| 1068 | my $dt = DateTime->from_epoch( epoch => $args[0], time_zone => $tz->name ); | ||
| 1069 | return( $dt->strftime( '%Y-%m-%d %T%z' ) ); | ||
| 1070 | } | ||
| 1071 | |||
| 1072 | sub _hour | ||
| 1073 | { | ||
| 1074 | my $self = shift( @_ ); | ||
| 1075 | my @args = @_; | ||
| 1076 | my $dt = $self->_parse_timestamp( $args[0] ) || return; | ||
| 1077 | return( $dt->hour ); | ||
| 1078 | } | ||
| 1079 | |||
| 1080 | sub _lcase | ||
| 1081 | { | ||
| 1082 | my $self = shift( @_ ); | ||
| 1083 | my @args = @_; | ||
| 1084 | return( lc( $args[0] ) ); | ||
| 1085 | } | ||
| 1086 | |||
| 1087 | sub _left | ||
| 1088 | { | ||
| 1089 | my $self = shift( @_ ); | ||
| 1090 | my @args = @_; | ||
| 1091 | return( $args[0] ) if( $args[1] !~ /^\d+$/ ); | ||
| 1092 | return( substr( $args[0], 0, $args[1] ) ); | ||
| 1093 | } | ||
| 1094 | |||
| 1095 | sub _locate | ||
| 1096 | { | ||
| 1097 | my $self = shift( @_ ); | ||
| 1098 | my @args = @_; | ||
| 1099 | return( CORE::index( $args[0], $args[1] ) ); | ||
| 1100 | } | ||
| 1101 | |||
| 1102 | sub _log10 | ||
| 1103 | { | ||
| 1104 | my $self = shift( @_ ); | ||
| 1105 | my @args = @_; | ||
| 1106 | return( log( $args[0] ) / log( 10 ) ); | ||
| 1107 | } | ||
| 1108 | |||
| 1109 | sub _minute | ||
| 1110 | { | ||
| 1111 | my $self = shift( @_ ); | ||
| 1112 | my @args = @_; | ||
| 1113 | my $dt = $self->_parse_timestamp( $args[0] ) || return; | ||
| 1114 | return( $dt->minute ); | ||
| 1115 | } | ||
| 1116 | |||
| 1117 | sub _month | ||
| 1118 | { | ||
| 1119 | my $self = shift( @_ ); | ||
| 1120 | my @args = @_; | ||
| 1121 | my $dt = $self->_parse_timestamp( $args[0] ) || return; | ||
| 1122 | return( $dt->month ); | ||
| 1123 | } | ||
| 1124 | |||
| 1125 | sub _monthname | ||
| 1126 | { | ||
| 1127 | my $self = shift( @_ ); | ||
| 1128 | my @args = @_; | ||
| 1129 | my $dt = $self->_parse_timestamp( $args[0] ) || return; | ||
| 1130 | return( $dt->month_name ); | ||
| 1131 | } | ||
| 1132 | |||
| 1133 | sub _number_format | ||
| 1134 | { | ||
| 1135 | my $self = shift( @_ ); | ||
| 1136 | my @args = @_; | ||
| 1137 | my( $num, $tho, $dec, $prec ) = @args; | ||
| 1138 | $self->_load_class( 'Module::Generic::Number' ) || return( $self->pass_error ); | ||
| 1139 | my $fmt = Module::Generic::Number->new( $num ); | ||
| 1140 | # 1 means with trailing zeros | ||
| 1141 | return( $fmt->format( | ||
| 1142 | thousand => $tho, | ||
| 1143 | decimal => $dec, | ||
| 1144 | precision => $prec, | ||
| 1145 | decimal_fill => 1, | ||
| 1146 | )); | ||
| 1147 | } | ||
| 1148 | |||
| 1149 | sub _placeholder_regexp { return( $PLACEHOLDER_REGEXP ) } | ||
| 1150 | |||
| 1151 | sub _power | ||
| 1152 | { | ||
| 1153 | my $self = shift( @_ ); | ||
| 1154 | my @args = @_; | ||
| 1155 | return( CORE::exp( $args[ 1 ] * CORE::log( $args[ 0 ] ) ) ); | ||
| 1156 | } | ||
| 1157 | |||
| 1158 | sub _quarter | ||
| 1159 | { | ||
| 1160 | my $self = shift( @_ ); | ||
| 1161 | my @args = @_; | ||
| 1162 | my $dt = $self->_parse_timestamp( $args[0] ) || return; | ||
| 1163 | return( $dt->quarter ); | ||
| 1164 | } | ||
| 1165 | |||
| 1166 | sub _rand | ||
| 1167 | { | ||
| 1168 | my $self = shift( @_ ); | ||
| 1169 | my @args = @_; | ||
| 1170 | return( CORE::rand() ); | ||
| 1171 | } | ||
| 1172 | |||
| 1173 | sub _regexp | ||
| 1174 | { | ||
| 1175 | my $self = shift( @_ ); | ||
| 1176 | my @args = @_; | ||
| 1177 | my( $re, $what ) = @args; | ||
| 1178 | return( $what =~ /$re/ ); | ||
| 1179 | } | ||
| 1180 | |||
| 1181 | sub _replace | ||
| 1182 | { | ||
| 1183 | my $self = shift( @_ ); | ||
| 1184 | my @args = @_; | ||
| 1185 | my( $str, $from, $to ) = @args; | ||
| 1186 | $str =~ s/($from)/$to/gs; | ||
| 1187 | return( $str ); | ||
| 1188 | } | ||
| 1189 | |||
| 1190 | sub _right | ||
| 1191 | { | ||
| 1192 | my $self = shift( @_ ); | ||
| 1193 | my @args = @_; | ||
| 1194 | return( CORE::substr( $args[0], CORE::length( $args[0] ) - $args[1] ) ); | ||
| 1195 | } | ||
| 1196 | |||
| 1197 | sub _second | ||
| 1198 | { | ||
| 1199 | my $self = shift( @_ ); | ||
| 1200 | my @args = @_; | ||
| 1201 | my $dt = $self->_parse_timestamp( $args[0] ) || return; | ||
| 1202 | return( $dt->second ); | ||
| 1203 | } | ||
| 1204 | |||
| 1205 | sub _space | ||
| 1206 | { | ||
| 1207 | my $self = shift( @_ ); | ||
| 1208 | my @args = @_; | ||
| 1209 | return( ' ' x CORE::int( $args[0] ) ); | ||
| 1210 | } | ||
| 1211 | |||
| 1212 | sub _sprintf | ||
| 1213 | { | ||
| 1214 | my $self = shift( @_ ); | ||
| 1215 | my @args = @_; | ||
| 1216 | for( my $i = 0; $i < scalar( @args ); $i++ ) | ||
| 1217 | { | ||
| 1218 | $args[$i] =~ s/'/\\'/g; | ||
| 1219 | } | ||
| 1220 | my $eval = "CORE::sprintf( '" . join( "', '", @args ) . "' )"; | ||
| 1221 | my $res = eval( $eval ); | ||
| 1222 | # return( CORE::sprintf( @args ) ); | ||
| 1223 | return( $res ); | ||
| 1224 | } | ||
| 1225 | |||
| 1226 | sub _to_days | ||
| 1227 | { | ||
| 1228 | my $self = shift( @_ ); | ||
| 1229 | my @args = @_; | ||
| 1230 | my $dt = $self->_parse_timestamp( $args[0] ) || return; | ||
| 1231 | # try-catch | ||
| 1232 | local $@; | ||
| 1233 | my $tz = eval | ||
| 1234 | { | ||
| 1235 | return( DateTime::TimeZone->new( name => 'local' ) ); | ||
| 1236 | }; | ||
| 1237 | if( $@ ) | ||
| 1238 | { | ||
| 1239 | $tz = DateTime::TimeZone->new( name => 'UTC' ); | ||
| 1240 | } | ||
| 1241 | my $origin = DateTime->new( | ||
| 1242 | year => 0, | ||
| 1243 | month => 1, | ||
| 1244 | day => 1, | ||
| 1245 | hour => 0, | ||
| 1246 | minute => 0, | ||
| 1247 | second => 0, | ||
| 1248 | time_zone => $tz->name, | ||
| 1249 | ); | ||
| 1250 | # https://stackoverflow.com/questions/821423/how-can-i-calculate-the-number-of-days-between-two-dates-in-perl#7111718 | ||
| 1251 | my $days = $dt->delta_days( $origin )->delta_days(); | ||
| 1252 | return( $days ); | ||
| 1253 | } | ||
| 1254 | |||
| 1255 | # sub _truncate | ||
| 1256 | # { | ||
| 1257 | # my $self = shift( @_ ); | ||
| 1258 | # my @args = @_; | ||
| 1259 | # return if( $args[1] !~ /^\-?\d+$/ ); | ||
| 1260 | # return( CORE::substr( $args[0], 0, $args[1] ) ); | ||
| 1261 | # } | ||
| 1262 | |||
| 1263 | sub _ucase | ||
| 1264 | { | ||
| 1265 | my $self = shift( @_ ); | ||
| 1266 | my @args = @_; | ||
| 1267 | return( uc( $args[0] ) ); | ||
| 1268 | } | ||
| 1269 | |||
| 1270 | sub _unix_timestamp | ||
| 1271 | { | ||
| 1272 | my $self = shift( @_ ); | ||
| 1273 | my @args = @_; | ||
| 1274 | my $dt = $self->_parse_timestamp( $args[0] ) || return; | ||
| 1275 | return( $dt->epoch ); | ||
| 1276 | } | ||
| 1277 | |||
| 1278 | sub _week | ||
| 1279 | { | ||
| 1280 | my $self = shift( @_ ); | ||
| 1281 | my @args = @_; | ||
| 1282 | my $dt = $self->_parse_timestamp( $args[0] ) || return; | ||
| 1283 | return( $dt->week_number ); | ||
| 1284 | } | ||
| 1285 | |||
| 1286 | sub _weekday | ||
| 1287 | { | ||
| 1288 | my $self = shift( @_ ); | ||
| 1289 | my @args = @_; | ||
| 1290 | my $dt = $self->_parse_timestamp( $args[0] ) || return; | ||
| 1291 | return( $dt->day_of_week ); | ||
| 1292 | } | ||
| 1293 | |||
| 1294 | sub _year | ||
| 1295 | { | ||
| 1296 | my $self = shift( @_ ); | ||
| 1297 | my @args = @_; | ||
| 1298 | my $dt = $self->_parse_timestamp( $args[0] ) || return; | ||
| 1299 | return( $dt->year ); | ||
| 1300 | } | ||
| 1301 | |||
| 1302 | DESTROY | ||
| 1303 | { | ||
| 1304 | # <https://perldoc.perl.org/perlobj#Destructors> | ||
| 1305 | CORE::local( $., $@, $!, $^E, $? ); | ||
| 1306 | CORE::return if( ${^GLOBAL_PHASE} eq 'DESTRUCT' ); | ||
| 1307 | my $self = CORE::shift( @_ ); | ||
| 1308 | CORE::return if( !CORE::defined( $self ) ); | ||
| 1309 | my $class = ref( $self ) || $self; | ||
| 1310 | if( $self->{sth} ) | ||
| 1311 | { | ||
| 1312 | # print( STDERR "DESTROY(): Terminating sth '$self' for query:\n$self->{ 'query' }\n" ) if( $DEBUG ); | ||
| 1313 | $self->{sth}->finish(); | ||
| 1314 | } | ||
| 1315 | elsif( $self->{dbh} && $class =~ /^AI\:\:DB\:\:Postgres$/ ) | ||
| 1316 | { | ||
| 1317 | local( $SIG{__WARN__} ) = sub { }; | ||
| 1318 | # $self->{dbh}->disconnect(); | ||
| 1319 | # if( $DEBUG ) | ||
| 1320 | # { | ||
| 1321 | # my( $pack, $file, $line, $sub ) = ( caller( 0 ) )[ 0, 1, 2, 3 ]; | ||
| 1322 | # my( $pack2, $file2, $line2, $sub2 ) = ( caller( 1 ) ) [ 0, 1, 2, 3 ]; | ||
| 1323 | # 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" ); | ||
| 1324 | # } | ||
| 1325 | $self->disconnect(); | ||
| 1326 | } | ||
| 1327 | my $locks = $self->{_locks}; | ||
| 1328 | if( $locks && $self->_is_array( $locks ) ) | ||
| 1329 | { | ||
| 1330 | foreach my $name ( @$locks ) | ||
| 1331 | { | ||
| 1332 | $self->unlock( $name ); | ||
| 1333 | } | ||
| 1334 | } | ||
| 1335 | } | ||
| 1336 | |||
| 1337 | 1; | ||
| 1338 | |||
| 1339 | # NOTE: POD | ||
| 1340 | __END__ | ||
| 1341 | |||
| 1342 | =encoding utf8 | ||
| 1343 | |||
| 1344 | =head1 NAME | ||
| 1345 | |||
| 1346 | DB::Object::SQLite - DB Object SQLite Driver | ||
| 1347 | |||
| 1348 | =head1 SYNOPSIS | ||
| 1349 | |||
| 1350 | use DB::Object; | ||
| 1351 | |||
| 1352 | my $dbh = DB::Object->connect({ | ||
| 1353 | driver => 'SQLite', | ||
| 1354 | conf_file => 'db-settings.json', | ||
| 1355 | database => 'webstore', | ||
| 1356 | host => 'localhost', | ||
| 1357 | login => 'store-admin', | ||
| 1358 | schema => 'auth', | ||
| 1359 | debug => 3, | ||
| 1360 | }) || bailout( "Unable to connect to sql server on host localhost: ", DB::Object->error ); | ||
| 1361 | |||
| 1362 | # Legacy regular query | ||
| 1363 | my $sth = $dbh->prepare( "SELECT login,name FROM login WHERE login='jack'" ) || | ||
| 1364 | die( $dbh->errstr() ); | ||
| 1365 | $sth->execute() || die( $sth->errstr() ); | ||
| 1366 | my $ref = $sth->fetchrow_hashref(); | ||
| 1367 | $sth->finish(); | ||
| 1368 | |||
| 1369 | # Get a list of databases; | ||
| 1370 | my @databases = $dbh->databases; | ||
| 1371 | # Doesn't exist? Create it: | ||
| 1372 | my $dbh2 = $dbh->create_db( 'webstore' ); | ||
| 1373 | # Load some sql into it | ||
| 1374 | my $rv = $dbh2->do( $sql ) || die( $dbh->error ); | ||
| 1375 | |||
| 1376 | # Check a table exists | ||
| 1377 | $dbh->table_exists( 'customers' ) || die( "Cannot find the customers table!\n" ); | ||
| 1378 | |||
| 1379 | # Get list of tables, as array reference: | ||
| 1380 | my $tables = $dbh->tables; | ||
| 1381 | |||
| 1382 | my $cust = $dbh->customers || die( "Cannot get customers object." ); | ||
| 1383 | $cust->where( email => 'john@example.org' ); | ||
| 1384 | my $str = $cust->delete->as_string; | ||
| 1385 | # Becomes: DELETE FROM customers WHERE email='john\@example.org' | ||
| 1386 | |||
| 1387 | # Do some insert with transaction | ||
| 1388 | $dbh->begin_work; | ||
| 1389 | # Making some other inserts and updates here... | ||
| 1390 | my $cust_sth_ins = $cust->insert( | ||
| 1391 | first_name => 'Paul', | ||
| 1392 | last_name => 'Goldman', | ||
| 1393 | email => 'paul@example.org', | ||
| 1394 | active => 0, | ||
| 1395 | ) || do | ||
| 1396 | { | ||
| 1397 | # Rollback everything since the begin_work | ||
| 1398 | $dbh->rollback; | ||
| 1399 | die( "Error while create query to add data to table customers: " . $cust->error ); | ||
| 1400 | }; | ||
| 1401 | $result = $cust_sth_ins->as_string; | ||
| 1402 | # INSERT INTO customers (first_name, last_name, email, active) VALUES('Paul', 'Goldman', 'paul\@example.org', '0') | ||
| 1403 | $dbh->commit; | ||
| 1404 | # Get the last used insert id | ||
| 1405 | my $id = $dbh->last_insert_id(); | ||
| 1406 | |||
| 1407 | $cust->where( email => 'john@example.org' ); | ||
| 1408 | $cust->order( 'last_name' ); | ||
| 1409 | $cust->having( email => qr/\@example/ ); | ||
| 1410 | $cust->limit( 10 ); | ||
| 1411 | my $cust_sth_sel = $cust->select || die( "An error occurred while creating a query to select data frm table customers: " . $cust->error ); | ||
| 1412 | # Becomes: | ||
| 1413 | # SELECT id, first_name, last_name, email, created, modified, active, created::ABSTIME::INTEGER AS created_unixtime, modified::ABSTIME::INTEGER AS modified_unixtime, CONCAT(first_name, ' ', last_name) AS name FROM customers WHERE email='john\@example.org' HAVING email ~ '\@example' ORDER BY last_name LIMIT 10 | ||
| 1414 | |||
| 1415 | $cust->reset; | ||
| 1416 | $cust->where( email => 'john@example.org' ); | ||
| 1417 | my $cust_sth_upd = $cust->update( active => 0 ) | ||
| 1418 | # Would become: | ||
| 1419 | # UPDATE ONLY customers SET active='0' WHERE email='john\@example.org' | ||
| 1420 | |||
| 1421 | # Lets' dump the result of our query | ||
| 1422 | # First to STDERR | ||
| 1423 | $login->where( "login='jack'" ); | ||
| 1424 | $login->select->dump(); | ||
| 1425 | # Now dump the result to a file | ||
| 1426 | $login->select->dump( "my_file.txt" ); | ||
| 1427 | |||
| 1428 | =head1 VERSION | ||
| 1429 | |||
| 1430 | v1.3.1 | ||
| 1431 | |||
| 1432 | =head1 DESCRIPTION | ||
| 1433 | |||
| 1434 | This package inherits from L<DB::Object>, so any method not here, but there you can use. | ||
| 1435 | |||
| 1436 | L<DB::Object::SQLite> is a SQL API much alike L<DBD::SQLite>. | ||
| 1437 | So why use a private module instead of using that great L<DBD::SQLite> package? | ||
| 1438 | |||
| 1439 | At first, I started to inherit from C<DBI> to conform to C<perlmod> perl | ||
| 1440 | manual page and to general perl coding guidlines. It became very quickly a | ||
| 1441 | real hassle. Barely impossible to inherit, difficulty to handle error, too | ||
| 1442 | much dependent from an API that change its behaviour with new versions. | ||
| 1443 | In short, I wanted a better, more accurate control over the SQL connection. | ||
| 1444 | |||
| 1445 | So, L<DB::Object::SQLite> acts as a convenient, modifiable wrapper that provide the | ||
| 1446 | programmer with an intuitive, user-friendly and hassle free interface. | ||
| 1447 | |||
| 1448 | =head1 CONSTRUCTOR | ||
| 1449 | |||
| 1450 | =head2 new | ||
| 1451 | |||
| 1452 | Create a new instance of L<DB::Object::SQLite>. Nothing much to say. | ||
| 1453 | |||
| 1454 | =head2 connect | ||
| 1455 | |||
| 1456 | Same as L<DB::Object/connect>, only specific to SQLite. | ||
| 1457 | |||
| 1458 | See L</_connection_params2hash> | ||
| 1459 | |||
| 1460 | =head1 METHODS | ||
| 1461 | |||
| 1462 | =head2 alias | ||
| 1463 | |||
| 1464 | This is inherited from L<DB::Object/alias> | ||
| 1465 | |||
| 1466 | =head2 as_string | ||
| 1467 | |||
| 1468 | This is inherited from L<DB::Object/as_string> | ||
| 1469 | |||
| 1470 | =head2 avoid | ||
| 1471 | |||
| 1472 | This is inherited from L<DB::Object/avoid> | ||
| 1473 | |||
| 1474 | =head2 attribute | ||
| 1475 | |||
| 1476 | Sets or get the value of database connection parameters. | ||
| 1477 | |||
| 1478 | If only one argument is provided, returns its value. | ||
| 1479 | If multiple arguments in a form of pair => value are provided, it sets the corresponding database parameters. | ||
| 1480 | |||
| 1481 | The authorised parameters are: | ||
| 1482 | |||
| 1483 | =over 4 | ||
| 1484 | |||
| 1485 | =item * C<ActiveKids> | ||
| 1486 | |||
| 1487 | Is read-only. | ||
| 1488 | |||
| 1489 | =item * C<AutoCommit> | ||
| 1490 | |||
| 1491 | Can be changed. | ||
| 1492 | |||
| 1493 | =item * C<AutoInactiveDestroy> | ||
| 1494 | |||
| 1495 | Can be changed. | ||
| 1496 | |||
| 1497 | =item * C<CachedKids> | ||
| 1498 | |||
| 1499 | Is read-only. | ||
| 1500 | |||
| 1501 | =item * C<ChildHandles> | ||
| 1502 | |||
| 1503 | Is read-only. | ||
| 1504 | |||
| 1505 | =item * C<ChopBlanks> | ||
| 1506 | |||
| 1507 | Can be changed. | ||
| 1508 | |||
| 1509 | =item * C<CursorName> | ||
| 1510 | |||
| 1511 | Is read-only. | ||
| 1512 | |||
| 1513 | =item * C<Driver> | ||
| 1514 | |||
| 1515 | Is read-only. | ||
| 1516 | |||
| 1517 | =item * C<ErrCount> | ||
| 1518 | |||
| 1519 | Can be changed. | ||
| 1520 | |||
| 1521 | =item * C<Executed> | ||
| 1522 | |||
| 1523 | Is read-only. | ||
| 1524 | |||
| 1525 | =item * C<FetchHashKeyName> | ||
| 1526 | |||
| 1527 | Can be changed. | ||
| 1528 | |||
| 1529 | =item * C<HandleError> | ||
| 1530 | |||
| 1531 | Can be changed. | ||
| 1532 | |||
| 1533 | =item * C<HandleSetErr> | ||
| 1534 | |||
| 1535 | Can be changed. | ||
| 1536 | |||
| 1537 | =item * C<InactiveDestroy> | ||
| 1538 | |||
| 1539 | Can be changed. | ||
| 1540 | |||
| 1541 | =item * C<Kids> | ||
| 1542 | |||
| 1543 | Is read-only. | ||
| 1544 | |||
| 1545 | =item * C<NAME> | ||
| 1546 | |||
| 1547 | Is read-only. | ||
| 1548 | |||
| 1549 | =item * C<NULLABLE> | ||
| 1550 | |||
| 1551 | Is read-only. | ||
| 1552 | |||
| 1553 | =item * C<NUM_OF_FIELDS> | ||
| 1554 | |||
| 1555 | Is read-only. | ||
| 1556 | |||
| 1557 | =item * C<NUM_OF_PARAMS> | ||
| 1558 | |||
| 1559 | Is read-only. | ||
| 1560 | |||
| 1561 | =item * C<Name> | ||
| 1562 | |||
| 1563 | Is read-only. | ||
| 1564 | |||
| 1565 | =item * C<PRECISION> | ||
| 1566 | |||
| 1567 | Is read-only. | ||
| 1568 | |||
| 1569 | =item * C<PrintError> | ||
| 1570 | |||
| 1571 | Can be changed. | ||
| 1572 | |||
| 1573 | =item * C<PrintWarn> | ||
| 1574 | |||
| 1575 | Can be changed. | ||
| 1576 | |||
| 1577 | =item * C<Profile> | ||
| 1578 | |||
| 1579 | Can be changed. | ||
| 1580 | |||
| 1581 | =item * C<RaiseError> | ||
| 1582 | |||
| 1583 | Can be changed. | ||
| 1584 | |||
| 1585 | =item * C<RowCacheSize> | ||
| 1586 | |||
| 1587 | Is read-only. | ||
| 1588 | |||
| 1589 | =item * C<RowsInCache> | ||
| 1590 | |||
| 1591 | Is read-only. | ||
| 1592 | |||
| 1593 | =item * C<SCALE> | ||
| 1594 | |||
| 1595 | Is read-only. | ||
| 1596 | |||
| 1597 | =item * C<ShowErrorStatement> | ||
| 1598 | |||
| 1599 | Can be changed. | ||
| 1600 | |||
| 1601 | =item * C<Statement> | ||
| 1602 | |||
| 1603 | Is read-only. | ||
| 1604 | |||
| 1605 | =item * C<TYPE> | ||
| 1606 | |||
| 1607 | Is read-only. | ||
| 1608 | |||
| 1609 | =item * C<Taint> | ||
| 1610 | |||
| 1611 | Can be changed. | ||
| 1612 | |||
| 1613 | =item * C<TaintIn> | ||
| 1614 | |||
| 1615 | Can be changed. | ||
| 1616 | |||
| 1617 | =item * C<TaintOut> | ||
| 1618 | |||
| 1619 | Can be changed. | ||
| 1620 | |||
| 1621 | =item * C<TraceLevel> | ||
| 1622 | |||
| 1623 | Can be changed. | ||
| 1624 | |||
| 1625 | =item * C<Type> | ||
| 1626 | |||
| 1627 | Can be changed. | ||
| 1628 | |||
| 1629 | =item * C<Username> | ||
| 1630 | |||
| 1631 | Is read-only. | ||
| 1632 | |||
| 1633 | =item * C<Warn> | ||
| 1634 | |||
| 1635 | Can be changed. | ||
| 1636 | |||
| 1637 | =item * C<sqlite_allow_multiple_statements> | ||
| 1638 | |||
| 1639 | Can be changed. | ||
| 1640 | |||
| 1641 | =item * C<sqlite_see_if_its_a_number> | ||
| 1642 | |||
| 1643 | Can be changed. | ||
| 1644 | |||
| 1645 | =item * C<sqlite_unicode> | ||
| 1646 | |||
| 1647 | Can be changed. | ||
| 1648 | |||
| 1649 | =item * C<sqlite_unprepared_statements> | ||
| 1650 | |||
| 1651 | Is read-only. | ||
| 1652 | |||
| 1653 | =item * C<sqlite_use_immediate_transaction> | ||
| 1654 | |||
| 1655 | Can be changed. | ||
| 1656 | |||
| 1657 | =item * C<sqlite_version> | ||
| 1658 | |||
| 1659 | Is read-only. | ||
| 1660 | |||
| 1661 | =back | ||
| 1662 | |||
| 1663 | =head2 available_drivers | ||
| 1664 | |||
| 1665 | Return the list of available drivers. | ||
| 1666 | |||
| 1667 | This is an inherited method from L<DB::Object/available_drivers> | ||
| 1668 | |||
| 1669 | =head2 begin_work | ||
| 1670 | |||
| 1671 | Mark the beginning of a transaction. | ||
| 1672 | |||
| 1673 | Any arguments provided are passed along to L<DBD::SQLite/begin_work> | ||
| 1674 | |||
| 1675 | =head2 bind | ||
| 1676 | |||
| 1677 | This is an inherited method from L<DB::Object/bind> | ||
| 1678 | |||
| 1679 | =head2 cache | ||
| 1680 | |||
| 1681 | This is an inherited method from L<DB::Object/cache> | ||
| 1682 | |||
| 1683 | =head2 can_update_delete_limit | ||
| 1684 | |||
| 1685 | Returns the boolean value for the SQLite compiled option C<ENABLE_UPDATE_DELETE_LIMIT> by calling L</has_compile_option> | ||
| 1686 | |||
| 1687 | =head2 check_driver | ||
| 1688 | |||
| 1689 | This is an inherited method from L<DB::Object/check_driver> | ||
| 1690 | |||
| 1691 | =head2 commit | ||
| 1692 | |||
| 1693 | Make any change to the database irreversible. | ||
| 1694 | |||
| 1695 | This must be used only after having called L</begin_work> | ||
| 1696 | |||
| 1697 | Any arguments provided are passed along to L<DBD::SQLite/commit> | ||
| 1698 | |||
| 1699 | =head2 compile_options | ||
| 1700 | |||
| 1701 | Returns the cached list of SQLite compiled options. The cached file is in the file C<sql_sqlite_compile_options.cfg> in the sytem directory. | ||
| 1702 | |||
| 1703 | =head2 connect | ||
| 1704 | |||
| 1705 | Same as L<DB::Object/connect>, only specific to SQLite. | ||
| 1706 | |||
| 1707 | It sets C<sqlite_unicode> to a true value in the connection parameters returned by L</_connection_params2hash> | ||
| 1708 | |||
| 1709 | See L</_connection_params2hash> | ||
| 1710 | |||
| 1711 | =head2 copy | ||
| 1712 | |||
| 1713 | This is an inherited method from L<DB::Object/copy> | ||
| 1714 | |||
| 1715 | =head2 create_table | ||
| 1716 | |||
| 1717 | This is an inherited method from L<DB::Object/create_table> | ||
| 1718 | |||
| 1719 | =head2 data_sources | ||
| 1720 | |||
| 1721 | This is an inherited method from L<DB::Object/data_sources> | ||
| 1722 | |||
| 1723 | =head2 data_type | ||
| 1724 | |||
| 1725 | This is an inherited method from L<DB::Object/data_type> | ||
| 1726 | |||
| 1727 | =head2 database | ||
| 1728 | |||
| 1729 | This is an inherited method from L<DB::Object/database> | ||
| 1730 | |||
| 1731 | =head2 database_file | ||
| 1732 | |||
| 1733 | Returns the file path to the database file. | ||
| 1734 | |||
| 1735 | =head2 databases | ||
| 1736 | |||
| 1737 | Returns a list of databases, which in SQLite, means a list of opened sqlite database files. | ||
| 1738 | |||
| 1739 | =head2 datatype_dict | ||
| 1740 | |||
| 1741 | Returns an hash reference of each data type with their equivalent C<constant>, regular expression (C<re>), constant C<name> and C<type> name. | ||
| 1742 | |||
| 1743 | Each data type is an hash with the following properties for each type: C<constant>, C<name>, C<re>, C<type> | ||
| 1744 | |||
| 1745 | =head2 delete | ||
| 1746 | |||
| 1747 | This is an inherited method from L<DB::Object/database> | ||
| 1748 | |||
| 1749 | =head2 disconnect | ||
| 1750 | |||
| 1751 | This is an inherited method from L<DB::Object/disconnect> | ||
| 1752 | |||
| 1753 | =head2 do | ||
| 1754 | |||
| 1755 | This is an inherited method from L<DB::Object/do> | ||
| 1756 | |||
| 1757 | =head2 enhance | ||
| 1758 | |||
| 1759 | This is an inherited method from L<DB::Object/enhance> | ||
| 1760 | |||
| 1761 | =head2 func | ||
| 1762 | |||
| 1763 | Provided with a table name and a function name and this will call L<DB::SQLite> passing it the table name and the function name. | ||
| 1764 | |||
| 1765 | It returns the value received from the function call. | ||
| 1766 | |||
| 1767 | =head2 get_sql_type | ||
| 1768 | |||
| 1769 | Provided with a data type as a string and this returns a SQLite constant suitable to be passed to L<DBI/bind_param> | ||
| 1770 | |||
| 1771 | =head2 having | ||
| 1772 | |||
| 1773 | A convenient wrapper to L<DB::Object::SQLite::Query/having> | ||
| 1774 | |||
| 1775 | =head2 has_compile_option | ||
| 1776 | |||
| 1777 | Provided with a compile option (the character case is irrelevant) and this will check if it exists or not. | ||
| 1778 | |||
| 1779 | =head2 last_insert_id | ||
| 1780 | |||
| 1781 | =head2 lock | ||
| 1782 | |||
| 1783 | This is an unsupported feature in SQLIte | ||
| 1784 | |||
| 1785 | =head2 on_conflict | ||
| 1786 | |||
| 1787 | See L<DB::Object::SQLite::Tables/on_conflict> | ||
| 1788 | |||
| 1789 | =head2 pragma | ||
| 1790 | |||
| 1791 | This is still a work in progress. | ||
| 1792 | |||
| 1793 | =head2 replace | ||
| 1794 | |||
| 1795 | Just like for the INSERT query, L</replace> takes one optional argument representing a L<DB::Object::SQLite::Statement> SELECT object or a list of field-value pairs. | ||
| 1796 | |||
| 1797 | If a SELECT statement is provided, it will be used to construct a query of the type of REPLACE INTO mytable SELECT FROM other_table | ||
| 1798 | |||
| 1799 | Otherwise the query will be REPLACE INTO mytable (fields) VALUES(values) | ||
| 1800 | |||
| 1801 | In scalar context, it execute the query and in list context it simply returns the statement handler. | ||
| 1802 | |||
| 1803 | =head2 register_function | ||
| 1804 | |||
| 1805 | This takes an hash reference of parameters and will register a new function by calling L<DBD::SQLite/sql_function_register> | ||
| 1806 | |||
| 1807 | Possible options are: | ||
| 1808 | |||
| 1809 | =over 4 | ||
| 1810 | |||
| 1811 | =item * C<code> | ||
| 1812 | |||
| 1813 | Anonymous code to be executed when the function is called. | ||
| 1814 | |||
| 1815 | =item * C<func> | ||
| 1816 | |||
| 1817 | This is an hash reference representing registry of functions. The value for each key is the option hash reference. | ||
| 1818 | |||
| 1819 | =item * C<flags> | ||
| 1820 | |||
| 1821 | An array reference of flags | ||
| 1822 | |||
| 1823 | =item * C<name> | ||
| 1824 | |||
| 1825 | The function name | ||
| 1826 | |||
| 1827 | =back | ||
| 1828 | |||
| 1829 | =head2 remove_function | ||
| 1830 | |||
| 1831 | Provided with a function name and this will remove it. | ||
| 1832 | |||
| 1833 | It returns false if there is no function, or returns the options hash reference originally set for the function removed. | ||
| 1834 | |||
| 1835 | =head2 returning | ||
| 1836 | |||
| 1837 | A convenient wrapper to L<DB::Object::Postgres::Query/returning> | ||
| 1838 | |||
| 1839 | =head2 rollback | ||
| 1840 | |||
| 1841 | Will roll back any changes made to the database since the last transaction point marked with L</begin_work> | ||
| 1842 | |||
| 1843 | =head2 sql_function_register | ||
| 1844 | |||
| 1845 | Provided with an hash reference of options and this will register a sql function by calling L<DBD::SQLite/sqlite_create_function> | ||
| 1846 | |||
| 1847 | Possible options are: | ||
| 1848 | |||
| 1849 | =over 4 | ||
| 1850 | |||
| 1851 | =item * C<argc> | ||
| 1852 | |||
| 1853 | The function arguments | ||
| 1854 | |||
| 1855 | =item * C<code> | ||
| 1856 | |||
| 1857 | Anonymous perl code to be executed when the function is called | ||
| 1858 | |||
| 1859 | =item * C<flags> | ||
| 1860 | |||
| 1861 | An array reference of flags. Those flags are joined with C<|> and L<perlfunc/eval>'ed | ||
| 1862 | |||
| 1863 | =item * C<name> | ||
| 1864 | |||
| 1865 | The function name | ||
| 1866 | |||
| 1867 | =back | ||
| 1868 | |||
| 1869 | =head2 stat | ||
| 1870 | |||
| 1871 | Provided with an hash or hash reference of parameters and this will call L<DBD::SQLite/sqlite_status> and get the hash reference of values returned. | ||
| 1872 | |||
| 1873 | If the option I<reset> is set, then this will call L<DBD::SQLite/sqlite_status> passing it 0 to reset it instead. | ||
| 1874 | |||
| 1875 | If the option I<type> is specified, this will return the equivalent property from the stat hash reference returned by L<DBD::SQLite/sqlite_status>, otherwise, it will return the hash in list context and the hash reference of stat properties in scalar context. | ||
| 1876 | |||
| 1877 | =head2 table_info | ||
| 1878 | |||
| 1879 | Provided with a table name and this will retrieve the table information as an hash reference. | ||
| 1880 | |||
| 1881 | Otherwise, if nothing can be found, it returns an empty hash reference. | ||
| 1882 | |||
| 1883 | It takes no optional parameters. | ||
| 1884 | |||
| 1885 | Information retrieved are: | ||
| 1886 | |||
| 1887 | =over 4 | ||
| 1888 | |||
| 1889 | =item * C<name> | ||
| 1890 | |||
| 1891 | The table name | ||
| 1892 | |||
| 1893 | =item * C<type> | ||
| 1894 | |||
| 1895 | The object type, which may be one of: C<table>, C<view>, C<materialized view>, C<special>, C<foreign table> | ||
| 1896 | |||
| 1897 | =back | ||
| 1898 | |||
| 1899 | =head2 tables | ||
| 1900 | |||
| 1901 | Connects to the database and finds out the list of all available tables. | ||
| 1902 | |||
| 1903 | Returns undef or empty list in scalar or list context respectively if no table found. | ||
| 1904 | |||
| 1905 | Otherwise, it returns the list of table in list context or a reference of it in scalar context. | ||
| 1906 | |||
| 1907 | =head2 tables_info | ||
| 1908 | |||
| 1909 | Provided with a database or using by default the current database and this will issue a query to get an array reference of all tables. | ||
| 1910 | |||
| 1911 | It returns the array reference. | ||
| 1912 | |||
| 1913 | =head2 trace | ||
| 1914 | |||
| 1915 | Trace is unsupported on SQLite. | ||
| 1916 | |||
| 1917 | =head2 unlock | ||
| 1918 | |||
| 1919 | Unlock is unsupported on SQLite. | ||
| 1920 | |||
| 1921 | =head2 variables | ||
| 1922 | |||
| 1923 | Variables are unsupported on SQLite. | ||
| 1924 | |||
| 1925 | =head2 version | ||
| 1926 | |||
| 1927 | This returns the, possibly cached, SQLite server version as a L<version> object. | ||
| 1928 | |||
| 1929 | =head2 _check_connect_param | ||
| 1930 | |||
| 1931 | This returns an hash reference of connection parameters. | ||
| 1932 | |||
| 1933 | If there is no L</database_file> currently set, it will use the property I<uri>. | ||
| 1934 | |||
| 1935 | The database is taken from the property I<database>, or derived from the last path segment of the I<uri>. | ||
| 1936 | |||
| 1937 | The database file is made absolute and is et as the I<database_file> property | ||
| 1938 | |||
| 1939 | The database is name, if not set, is derived from the base path of the I<database_file> | ||
| 1940 | |||
| 1941 | The I<host> property is set to C<localhost> and I<port> property to C<0> | ||
| 1942 | |||
| 1943 | It returns the hash reference of parameters thus processed. | ||
| 1944 | |||
| 1945 | =head2 _check_default_option | ||
| 1946 | |||
| 1947 | Provided with an hash or hash reference of options and this will check it and set some default value. | ||
| 1948 | |||
| 1949 | The only default property this sets is C<sqlite_unicode> to true. | ||
| 1950 | |||
| 1951 | It returns the hash reference of options. | ||
| 1952 | |||
| 1953 | =head2 _connection_options | ||
| 1954 | |||
| 1955 | Provided with an hash reference of parameters and this will check them and returns an hash reference of options who name start with C<sqlite_> | ||
| 1956 | |||
| 1957 | =head2 _connection_parameters | ||
| 1958 | |||
| 1959 | Provided with an hash or hash reference of connection parameters and this will extra all the properties that start with C<sqlite_/> and add them to an array of core properties: db login passwd host port driver database server opt uri debug | ||
| 1960 | |||
| 1961 | It returns those properties as an array reference. | ||
| 1962 | |||
| 1963 | =head2 _dbi_connect | ||
| 1964 | |||
| 1965 | This calls L<DB::Connect/_dbi_connect> and do more driver specific processing. | ||
| 1966 | |||
| 1967 | It will register all the functions set in the global hash reference C<$PRIVATE_FUNCTIONS> which is a function name to code reference pairs. For each of those function, they will be added by calling L</sql_function_register> | ||
| 1968 | |||
| 1969 | It returns the database handler object (L<DBD::Object::SQLite>) | ||
| 1970 | |||
| 1971 | =head2 _dsn | ||
| 1972 | |||
| 1973 | Using the L</database_file> set and this will issue a connection to the SQLite database file. | ||
| 1974 | |||
| 1975 | If the file does not exist or is not writable, this will return an error, otherwise this will return the string representing the dsn, which are connection parameters separated by C<;> | ||
| 1976 | |||
| 1977 | =head2 _parse_timestamp | ||
| 1978 | |||
| 1979 | Provided a string and this will parse it to return a L<DateTime> object. | ||
| 1980 | |||
| 1981 | =head1 SQLITE FUNCTIONS AVAILABLE | ||
| 1982 | |||
| 1983 | =head2 ceiling | ||
| 1984 | |||
| 1985 | This is a sql function to be registered automatically upon connection to the SQLite database file. | ||
| 1986 | |||
| 1987 | It leverages L<POSIX/ceil> | ||
| 1988 | |||
| 1989 | =head2 concat | ||
| 1990 | |||
| 1991 | This returns the arguments provided concatenated as a string. | ||
| 1992 | |||
| 1993 | =head2 curdate | ||
| 1994 | |||
| 1995 | Returns a string representing the year, month and date separated by a C<-> | ||
| 1996 | |||
| 1997 | This is computed using L<DateTime> | ||
| 1998 | |||
| 1999 | =head2 curtime | ||
| 2000 | |||
| 2001 | Returns a string representing the hours, minutes and seconds separated by a C<:> | ||
| 2002 | |||
| 2003 | This is computed using L<DateTime> | ||
| 2004 | |||
| 2005 | =head2 dayname | ||
| 2006 | |||
| 2007 | Based on a datetime that is parsed using L</_parse_timestamp>, this returns the day name of the week, such as C<Monday> | ||
| 2008 | |||
| 2009 | =head2 dayofmonth | ||
| 2010 | |||
| 2011 | Based on a datetime that is parsed using L</_parse_timestamp>, this returns the day of the month, such as 17. | ||
| 2012 | |||
| 2013 | =head2 dayofweek | ||
| 2014 | |||
| 2015 | Based on a datetime that is parsed using L</_parse_timestamp>, this returns the day of the week as a number from 1 to 7 with 1 being Monday | ||
| 2016 | |||
| 2017 | =head2 dayofyear | ||
| 2018 | |||
| 2019 | Based on a datetime that is parsed using L</_parse_timestamp>, this returns the day of the year, such as a number from 1 to 365, or possibly 366 depending on the year. | ||
| 2020 | |||
| 2021 | =head2 distance_miles | ||
| 2022 | |||
| 2023 | Provided with an original latitude, longitude and a target latitude and longitude and this will calculate the distance between the 2. | ||
| 2024 | |||
| 2025 | See the source L<StackOverflow post on which this function is based|http://stackoverflow.com/questions/10034636/postgres-longitude-longitude-query>. | ||
| 2026 | |||
| 2027 | =head2 from_days | ||
| 2028 | |||
| 2029 | Calculate the number of days since January 1st of year 0 and returns a L<DateTime> object. | ||
| 2030 | |||
| 2031 | =head2 from_unixtime | ||
| 2032 | |||
| 2033 | Provided with a unix timestamp, and this will return a datetime string such as C<YYYY-mm-dd HH:MM:SS> | ||
| 2034 | |||
| 2035 | =head2 hour | ||
| 2036 | |||
| 2037 | Provided with a date time, and this will parse it and return the hour. | ||
| 2038 | |||
| 2039 | =head2 lcase | ||
| 2040 | |||
| 2041 | Provided with a string and this returns its lower case value. | ||
| 2042 | |||
| 2043 | =head2 left | ||
| 2044 | |||
| 2045 | Provided with a string and an integer C<n> and this will return a substring capturing the nth first characters. | ||
| 2046 | |||
| 2047 | =head2 locate | ||
| 2048 | |||
| 2049 | This essentially does the same as L<perlfunc/index> | ||
| 2050 | |||
| 2051 | =head2 log10 | ||
| 2052 | |||
| 2053 | Provided with a number and this returns its logarithm base 10. | ||
| 2054 | |||
| 2055 | =head2 minute | ||
| 2056 | |||
| 2057 | Provided with a date time, and this will parse it and return the minutes. | ||
| 2058 | |||
| 2059 | =head2 month | ||
| 2060 | |||
| 2061 | Provided with a date time, and this will parse it and return the month. | ||
| 2062 | |||
| 2063 | =head2 monthname | ||
| 2064 | |||
| 2065 | Provided with a date time, and this will parse it and return the month name. | ||
| 2066 | |||
| 2067 | =head2 number_format | ||
| 2068 | |||
| 2069 | Provided with a number, a thousand separator, a decimal separator and a decimal precision and this will format the number accordingly and return it as a string. | ||
| 2070 | |||
| 2071 | =head2 power | ||
| 2072 | |||
| 2073 | Provided with a number and a power, and this will return the number powered | ||
| 2074 | |||
| 2075 | =head2 quarter | ||
| 2076 | |||
| 2077 | Provided with a date time, and this will parse it and return the quarter. | ||
| 2078 | |||
| 2079 | =head2 query_object | ||
| 2080 | |||
| 2081 | Set or gets the SQLite query object (L<DB::Object::SQLite::Query>) used to process and format queries. | ||
| 2082 | |||
| 2083 | =head2 rand | ||
| 2084 | |||
| 2085 | This takes no argument and simply returns a random number using L<perlfunc/rand> | ||
| 2086 | |||
| 2087 | =head2 regexp | ||
| 2088 | |||
| 2089 | Provided with a regular expression and the string to test, and this will test the regular expression and return true if it matches or false otherwise. | ||
| 2090 | |||
| 2091 | =head2 replace | ||
| 2092 | |||
| 2093 | Provided with a string, some term to replace and a replacement string and this will do a perl substitution and return the resulting string. | ||
| 2094 | |||
| 2095 | =head2 right | ||
| 2096 | |||
| 2097 | Provided with a string and an integer n and this will return the nth right most characters. | ||
| 2098 | |||
| 2099 | =head2 second | ||
| 2100 | |||
| 2101 | Provided with a date time, and this will parse it and return the seconds. | ||
| 2102 | |||
| 2103 | =head2 space | ||
| 2104 | |||
| 2105 | Provided with an integer and this will return as much spaces. | ||
| 2106 | |||
| 2107 | =head2 sprintf | ||
| 2108 | |||
| 2109 | This behaves like L<perlfunc/sprintf> and provided with a template and some arguments, it will return a formatted string. | ||
| 2110 | |||
| 2111 | =head2 to_days | ||
| 2112 | |||
| 2113 | Provided with a date time, and this will return the number of days since January 1st of year 0. | ||
| 2114 | |||
| 2115 | =head2 ucase | ||
| 2116 | |||
| 2117 | This returns the string provided with all its characters in upper case. | ||
| 2118 | |||
| 2119 | =head2 unix_timestamp | ||
| 2120 | |||
| 2121 | Provided with a date time and this will returns its unix timestamp representation. | ||
| 2122 | |||
| 2123 | =head2 week | ||
| 2124 | |||
| 2125 | Provided with a date time, and this will parse it and return the week. | ||
| 2126 | |||
| 2127 | =head2 weekday | ||
| 2128 | |||
| 2129 | Provided with a date time, and this will parse it and return the day of the week. | ||
| 2130 | |||
| 2131 | =head2 year | ||
| 2132 | |||
| 2133 | Provided with a date time, and this will parse it and return the day of the year. | ||
| 2134 | |||
| 2135 | =head1 SEE ALSO | ||
| 2136 | |||
| 2137 | L<DBI>, L<Apache::DBI> | ||
| 2138 | |||
| 2139 | =head1 AUTHOR | ||
| 2140 | |||
| 2141 | Jacques Deguest E<lt>F<jack@deguest.jp>E<gt> | ||
| 2142 | |||
| 2143 | =head1 COPYRIGHT & LICENSE | ||
| 2144 | |||
| 2145 | Copyright (c) 2019-2021 DEGUEST Pte. Ltd. | ||
| 2146 | |||
| 2147 | You can use, copy, modify and redistribute this package and associated | ||
| 2148 | files under the same terms as Perl itself. | ||
| 2149 | |||
| 2150 | =cut |