# -*- perl -*- ##---------------------------------------------------------------------------- ## Database Object Interface - ~/lib/DB/Object.pm ## Version v1.7.0 ## Copyright(c) 2025 DEGUEST Pte. Ltd. ## Author: Jacques Deguest ## Created 2017/07/19 ## Modified 2025/03/09 ## All rights reserved ## ## ## This program is free software; you can redistribute it and/or modify it ## under the same terms as Perl itself. ##---------------------------------------------------------------------------- ## This is the subclassable module for driver specific ones. package DB::Object; BEGIN { require 5.16.0; use strict; use warnings; use warnings::register; use parent qw( Module::Generic DBI ); use vars qw( $VERSION $AUTOLOAD @AVAILABLE_DATABASES $CACHE_DIR $CACHE_QUERIES $CACHE_SIZE $CACHE_TABLE $CONNECT_VIA $CONSTANT_QUERIES_CACHE $DB_ERRSTR @DBH $DRIVER2PACK $ERROR $DEBUG $MOD_PERL $QUERIES_CACHE $USE_BIND $USE_CACHE $PLACEHOLDER_REGEXP ); use Regexp::Common; use Scalar::Util qw( blessed ); use DateTime; use DateTime::TimeZone; use DateTime::Format::Strptime; use DB::Object::Cache::Tables; use DBI; use JSON; use Module::Generic::File qw( sys_tmpdir ); use Module::Generic::DateTime; use POSIX (); use Want; our $PLACEHOLDER_REGEXP = qr/\b\?\b/; our $VERSION = 'v1.7.0'; }; use strict; use warnings; our $DB_ERRSTR = ''; our $DEBUG = 0; # This is our system cache queries our $CACHE_QUERIES = []; our $CACHE_SIZE = 10; our $CACHE_TABLE = {}; our $USE_BIND = 0; our $USE_CACHE = 0; our $MOD_PERL = 0; our @DBH = (); our $CACHE_DIR = ''; our $CONSTANT_QUERIES_CACHE = {}; # This is for the user convenience our $QUERIES_CACHE = {}; if( $INC{ 'Apache/DBI.pm' } && substr( $ENV{GATEWAY_INTERFACE}|| '', 0, 8 ) eq 'CGI-Perl' ) { $CONNECT_VIA = "Apache::DBI::connect"; $MOD_PERL++; } our $DRIVER2PACK = { mysql => 'DB::Object::Mysql', Pg => 'DB::Object::Postgres', SQLite => 'DB::Object::SQLite', }; sub new { my $that = shift( @_ ); my $class = ref( $that ) || $that; my $self = {}; bless( $self, $class ); return( $self->init( @_ ) ); } sub init { my $self = shift( @_ ); $self->{cache_connections} = 1; $self->{cache_dir} = sys_tmpdir(); $self->{cache_table} = 0; $self->{driver} = ''; # Auto-decode json data into perl hash $self->{auto_decode_json} = 1; $self->{auto_convert_datetime_to_object} = 0; $self->{allow_bulk_delete} = 0; $self->{allow_bulk_update} = 0; $self->{unknown_field} = 'ignore'; $self->{_init_strict_use_sub} = 1; $self->Module::Generic::init( @_ ) || return( $self->pass_error ); # $self->{constant_queries_cache} = $DB::Object::CONSTANT_QUERIES_CACHE; return( $self ); } sub allow_bulk_delete { return( shift->_set_get_scalar( 'allow_bulk_delete', @_ ) ); } sub allow_bulk_update { return( shift->_set_get_scalar( 'allow_bulk_update', @_ ) ); } sub ALL { return( shift->_operator_object_create( 'DB::Object::ALL', @_ ) ); } sub AND { return( shift->_operator_object_create( 'DB::Object::AND', @_ ) ); } sub ANY { return( shift->_operator_object_create( 'DB::Object::ANY', @_ ) ); } sub auto_convert_datetime_to_object { return( shift->_set_get_scalar( 'auto_convert_datetime_to_object', @_ ) ); } sub auto_decode_json { return( shift->_set_get_scalar( 'auto_decode_json', @_ ) ); } sub attribute($;$@) { my $self = shift( @_ ); # $h->{AttributeName} = ...; # set/write # ... = $h->{AttributeName}; # get/read # 1 means that the attribute may be modified # 0 mneas that the attribute may only be read my $name = shift( @_ ) if( @_ == 1 ); my %arg = ( @_ ); my %attr = ( Active => 0, ActiveKids => 0, AutoCommit => 1, AutoInactiveDestroy => 1, CachedKids => 0, Callbacks => 1, ChildHandles => 0, ChopBlanks => 1, CompatMode => 1, CursorName => 0, ErrCount => 0, Executed => 0, FetchHashKeyName => 0, HandleError => 1, HandleSetErr => 1, InactiveDestroy => 1, Kids => 0, LongReadLen => 1, LongTruncOk => 1, NAME => 0, NULLABLE => 0, NUM_OF_FIELDS => 0, NUM_OF_PARAMS => 0, Name => 0, PRECISION => 0, PrintError => 1, PrintWarn => 1, Profile => 0, RaiseError => 1, ReadOnly => 1, RowCacheSize => 0, RowsInCache => 0, SCALE => 0, ShowErrorStatement => 1, Statement => 0, TYPE => 0, Taint => 1, TaintIn => 1, TaintOut => 1, TraceLevel => 1, Type => 0, Warn => 1, ); # Only those attribute exist # Using an a non existing attribute produce an exception, so we better avoid if( $name ) { return( $self->{dbh}->{ $name } ) if( exists( $attr{ $name } ) ); } else { my $value; while( ( $name, $value ) = each( %arg ) ) { # We intend to modifiy the value of an attribute # we are allowed to modify this value if it is true if( exists( $attr{ $name } ) && defined( $value ) && $attr{ $name } ) { $self->{dbh}->{ $name } = $value; } } } } sub available_drivers(@) { my $self = shift( @_ ); my $class = ref( $self ) || $self; # @ary = DBI->available_drivers( $quiet ); return( $class->SUPER::available_drivers( 1 ) ); } sub base_class { my $self = shift( @_ ); my @supported_classes = $self->supported_class; push( @supported_classes, 'DB::Object' ); my $ok_classes = join( '|', @supported_classes ); my $class = ref( $self ) ? ref( $self ) : $self; my $base_class = ( $class =~ /^($ok_classes)/ )[0]; return( $base_class ); } # This method is common to DB::Object and DB::Object::Statement sub bind { my $self = shift( @_ ); # Usage: # This activate the binding stuff # $dbh->bind() or $dbh->bind->where( "something" ) or $dbh->bind->select->fetchrow_hashref(); # Later, $dbh->bind( 'thingy' )->select->fetchrow_hashref() # When used like $table->bind; this means the user is setting the use bind option as a setting for all transactions, but # when used like $table->bind->select then the use bind option is only used for this transaction only and is reset after $self->{bind} = Want::want('VOID') ? 2 # Otherwise is it already set maybe? : $self->{bind} # Then use it ? $self->{bind} : 1; if( @_ ) { # If we are using the cache system, we search the object of this query my $obj = ''; # Ensure that we have something to look for at the least # my $queries = $self->{queries}; my $queries = $self->_cache_queries; my $base_class = $self->base_class; if( $self->isa( "${base_class}::Statement" ) ) { $obj = $self; } elsif( $self->{cache} && @$queries ) { $obj = $queries->[0]; } # Otherwise, our object is the statement object to use else { $obj = $self; } $obj->{binded} = [ @_ ]; # Since new binded parameters have been passed, since mean a new request to the # same statement is pending, so we need to re-execute the statement # and since most of the fetch method rely on AUTOLOAD that call # execute() automatically *IF* the statement was not already executed.... # we need to delete 'executed' value or set it to false, so the statement gets re-executed $obj->{executed} = 0; return( $obj ); } return( $self ); } sub cache { my $self = shift( @_ ); # activate cache # So we may be called as: $tbl->cache->select->fetchrow_hashref(); $self->{cache}++; return( $self ); } sub cache_connections { return( shift->_set_get_boolean( 'cache_connections', @_ ) ); } # { # my $self = shift( @_ ); # $self->{_cache_connections} = shift( @_ ) if( @_ ); # return( $self->{_cache_connections} ); # } sub cache_dir { return( shift->_set_get_scalar( 'cache_dir', @_ ) ); } sub cache_query_get { my $self = shift( @_ ); my $name = shift( @_ ) || return( $self->error( "No name for this query cache was provided." ) ); return( $QUERIES_CACHE->{ $name } ); } sub cache_query_set { my $self = shift( @_ ); my $name = shift( @_ ) || return( $self->error( "No name for this query cache was provided." ) ); my $sth = shift( @_ ) || return( $self->error( "No statement handler was provided." ) ); return( $QUERIES_CACHE->{ $name } = $sth ); } sub cache_table { return( shift->_set_get_boolean( 'cache_table', @_ ) ); } sub cache_tables { return( shift->_set_get_object( 'cache_tables', 'DB::Object::Cache::Tables', @_ ) ); } sub check_driver() { my $self = shift( @_ ); my $driver = shift( @_ ) || return( $self->error( "No SQL driver provided to check" ) ); my $ok = undef(); local $_; my @drivers = $self->available_drivers(); foreach( @drivers ) { if( m/$driver/s ) { $ok++; last; } } return( $ok ); } sub connect { my $this = shift( @_ ); my $class = ref( $this ) || $this; my $opts = $this->_get_args_as_hash( @_ ); # We pass the arguments so that debug and other init parameters can be set early my $that = ref( $this ) ? $this : $this->Module::Generic::new( debug => $opts->{debug} ); my $param = $that->_connection_params2hash( $opts ) || return( $this->error( "No valid connection parameters found" ) ); my $driver2pack = { mysql => 'DB::Object::Mysql', Pg => 'DB::Object::Postgres', SQLite => 'DB::Object::SQLite', }; return( $that->error( "No driver was provided." ) ) if( !exists( $param->{driver} ) ); if( !exists( $driver2pack->{ $param->{driver} } ) ) { return( $that->error( "Driver $param->{driver} is not supported." ) ); } # For example, will make this object a DB::ObjectD::Postgres object my $driver_class = $driver2pack->{ $param->{driver} }; my $driver_module = $driver_class; $driver_module =~ s|::|/|g; $driver_module .= '.pm'; eval { local $DEBUG; require $driver_module; }; return( $that->error( "Unable to load module $driver_class ($driver_module): $@" ) ) if( $@ ); my $self = $driver_class->new || return( $this->error( "Cannot get object from package $driver_class" ) ); $self->debug( CORE::exists( $param->{debug} ) ? CORE::delete( $param->{debug} ) : CORE::exists( $param->{Debug} ) ? CORE::delete( $param->{Debug} ) : $DEBUG ); $self->cache_dir( CORE::exists( $param->{cache_dir} ) ? CORE::delete( $param->{cache_dir} ) : CORE::exists( $that->{cache_dir} ) ? $that->{cache_dir} : $CACHE_DIR ); $self->{unknown_field} = CORE::delete( $param->{unknown_field} ) if( CORE::exists( $param->{unknown_field} ) ); $param = $self->_check_connect_param( $param ) || return( $self->pass_error ); my $opt = {}; if( exists( $param->{opt} ) ) { $opt = CORE::delete( $param->{opt} ); $opt = $self->_check_default_option( $opt ); } # print( STDERR ref( $self ), "::connect(): \$param is: ", $self->dumper( $param ), "\n" ); $self->{database} = CORE::exists( $param->{database} ) ? CORE::delete( $param->{database} ) : CORE::exists( $param->{db} ) ? CORE::delete( $param->{db} ) : undef(); $self->{host} = CORE::exists( $param->{host} ) ? CORE::delete( $param->{host} ) : CORE::exists( $param->{server} ) ? CORE::delete( $param->{server} ) : undef(); $self->{port} = CORE::delete( $param->{port} ); # $self->{database} = CORE::delete( $param->{ 'db' } ); $self->{login} = CORE::delete( $param->{login} ); $self->{passwd} = CORE::delete( $param->{passwd} ); $self->{driver} = CORE::delete( $param->{driver} ); $self->{cache} = CORE::exists( $param->{use_cache} ) ? CORE::delete( $param->{use_cache} ) : $USE_CACHE; $self->{bind} = CORE::exists( $param->{use_bind} ) ? CORE::delete( $param->{use_bind} ) : $USE_BIND; # Needed to be specified if the user does not want to cache connections # Will be used in _dbi_connect() $self->{cache_connections} = CORE::delete( $param->{cache_connections} ) if( CORE::exists( $param->{cache_connections} ) ); $self->{cache_table} = CORE::delete( $param->{cache_table} ) if( CORE::exists( $param->{cache_table} ) ); # If parameters starting with an upper case are provided, they are DBI database parameters #my @dbi_opts = grep( /^[A-Z][a-zA-Z]+/, keys( %$param ) ); #@$opt{ @dbi_opts } = @$param{ @dbi_opts }; if( my $driver = $self->driver ) { my $drh = $self->SUPER::install_driver( $driver ) || return( $that->pass_error( $self->error ) ); $self->{drh} = $drh; } $opt->{RaiseError} = 0 if( !CORE::exists( $opt->{RaiseError} ) ); $opt->{AutoCommit} = 1 if( !CORE::exists( $opt->{AutoCommit} ) ); $opt->{PrintError} = 0 if( !CORE::exists( $opt->{PrintError} ) ); $self->{opt} = $opt; my $dbh = $self->_dbi_connect || return( $that->pass_error( $self->error ) ); $self->{dbh} = $dbh; # If we are not running under mod_perl, cleanup the database object handle in case it was not shutdown # using the DESTROY, but also the END block push( @DBH, $dbh ) if( !$MOD_PERL ); #$self->param( # ## Do not allow SELECT that will take too long or too much resource, i.e. over 2Gb of data # ## This is idiot proof mode # 'SQL_BIG_SELECTS' => 0, # ## SQL will abort if a DELETE or UPDATE is being executed w/o LIMIT nor WHERE clause # 'SQL_SAFE_MODE' => 1, #); local $/ = "\n"; my $tables = []; # 1 day # my $tbl_cache_timeout = 86400; my $host = $self->host || 'localhost'; my $port = $self->port || 0; my $driver = $self->driver; my $database = $self->database; my $cache_params = {}; if( my $cache_dir = $self->cache_dir ) { $cache_params->{cache_dir} = $cache_dir; } $cache_params->{debug} = $self->debug; my $cache_tables = DB::Object::Cache::Tables->new( $cache_params ); $self->cache_tables( $cache_tables ); $tables = $self->tables_info; my $cache = { host => $host, driver => $driver, port => $port, database => $database, tables => $tables, }; if( !defined( $cache_tables->set( $cache ) ) ) { warn( "Unable to write to tables cache: ", $cache_tables->error, "\n" ); } return( $self ); } # sub constant_queries_cache { return( shift->_set_get_hash( 'constant_queries_cache', @_ ) ); } sub constant_queries_cache { return( $CONSTANT_QUERIES_CACHE ); } sub constant_queries_cache_get { my( $self, $def ) = @_; my $hash = $self->constant_queries_cache; return( $self->error( "Parameter provided must be a hash, but I got '$def'." ) ) if( ref( $def ) ne 'HASH' ); foreach my $k ( qw( pack file line ) ) { return( $self->error( "Parameter \"$k\" is missing from the hash." ) ) if( !CORE::length( $def->{ $k } ) ); } my $key = CORE::join( '|', @$def{qw( pack file line )} ); my $ref = $hash->{ $key }; # $ts is thee timestamp of the file recorded at the time my $ts = $ref->{ts}; # A DB::Object::Statement object my $qo = $ref->query_object; return if( !CORE::length( $def->{file} ) ); return if( !-e( $def->{file} ) ); return if( ( CORE::stat( $def->{file} ) )[9] != $ts ); return( $self->error( "Query object retrieved from constant query cache is void!" ) ) if( !$qo ); return( $self->error( "Query object retrieved from constant query cache is not a DB::Object::Query object or one of its sub classes." ) ) if( !$self->_is_object( $qo ) || !$qo->isa( 'DB::Object::Query' ) ); return if( $self->database ne $qo->database_object->database ); return( $self->_cache_this( $qo ) ); } sub constant_queries_cache_set { my( $self, $def ) = @_; my $hash = $self->constant_queries_cache; foreach my $k ( qw( pack file line query_object ) ) { return( $self->error( "Parameter \"$k\" is missing from the hash." ) ) if( !CORE::length( $def->{ $k } ) ); } return( $self->error( "Provided query object is not a DB::Object::Query." ) ) if( !$self->_is_object( $def->{query_object} ) || !$def->{query_object}->isa( 'DB::Object::Query' ) ); $def->{ts} = ( CORE::stat( $def->{file} ) )[9]; my $key = CORE::join( '|', @$def{qw( pack file line )} ); $hash->{ $key } = $def; return( $def ); } # See also datatype_to_constant() sub constant_to_datatype { my $self = shift( @_ ); return unless( scalar( @_ ) && defined( $_[0] ) ); my $this = shift( @_ ); my $ref = $self->datatypes; foreach my $t ( keys( %$ref ) ) { return( $t ) if( $ref->{ $t } eq $this ); } return; } sub copy { my $self = shift( @_ ); my $opts = $self->_get_args_as_hash( @_ ); my $ref = $self->select->fetchrow_hashref(); my $keys = keys( %$opts ); @$ref{ @$keys } = @$opts{ @$keys }; return(0) if( !scalar( keys( %$ref ) ) ); $self->insert( $ref ); return(1); } sub create_db { return( shift->error( "The driver has not implemented the create database method create_db." ) ); } sub create_table { return( shift->error( "The driver has not implemented the create table method create_table." ) ); } sub data_sources($;\%) { my $self = shift( @_ ); my $class = ref( $self ) || $self; my $opt; $opt = shift( @_ ) if( @_ ); my $driver = $self->{driver} || return( $self->error( "No driver to to use to check for data sources." ) ); return( $class->SUPER::data_sources( $driver, $opt ) ); } sub data_type { my $self = shift( @_ ); my $type = @_ == 1 ? shift( @_ ) : [ @_ ] if( @_ ); my $ref = eval { local $SIG{__DIE__} = sub{ }; local $SIG{__WARN__} = sub{ }; $self->{dbh}->type_info_all(); }; return( $self->error( "type_info_all() is unsupported by vendor '$self->{ 'driver' }'." ) ) if( $@ ); # First item is a reference to hash containing the order of the header my $header = shift( @$ref ); my $hash = {}; my $name_idx = $header->{TYPE_NAME}; my @found = (); if( $type ) { my @types = ref( $type ) ? @$type : ( $type ); foreach my $requested ( @types ) { push( @found, grep{ uc( $requested ) eq $_->[ $name_idx ] } @$ref ); } } else { @found = @$ref; } # Stop. No need to go further return( wantarray() ? () : undef() ) if( !@found ); my @names = map{ lc( $_ ) } keys( %$header ); my $len = scalar( keys( %$header ) ); my @order = values( %$header ); map { next if( @$_ != $len ); my %data; @data{ @names } = @{ $_ }[ @order ]; $hash->{ lc( $_->[ $name_idx ] ) } = \%data; } @found; return( wantarray() ? () : undef() ) if( !%$hash ); return( wantarray() ? %$hash : $hash ); } sub database { # Read only return( shift->{database} ); } sub databases { return( shift->error( "Method databases() is not implemented by driver." ) ); } sub datatype_dict { my $self = shift( @_ ); my $class = ( ref( $self ) || $self ); no strict 'refs'; my $sym = $self->_get_symbol( $class => '$DATATYPES_DICT' ); if( !defined( $sym ) ) { return( $self->pass_error ) if( $self->error ); return( $self->error( "No global variable \$DATATYPES_DICT defined in class $class" ) ); } my $dict = $$sym; return( $self->error( "Symbol found for \$DATATYPES_DICT in class $class did not resolve into an expecxted hash reference." ) ) if( ref( $dict ) ne 'HASH' ); if( @_ ) { return( $dict->{ lc( shift( @_ ) ) } ); } return( $dict ); } # See also constant_to_datatype() sub datatype_to_constant { my $self = shift( @_ ); my $ref = $self->datatypes || return( $self->pass_error ); return unless( scalar( @_ ) && defined( $_[0] ) ); return( $ref->{ lc( shift( @_ ) ) } ); } # Returns an hash of SQL constant name to its value sub datatypes { my $self = shift( @_ ); my $dict = $self->datatype_dict || return( $self->pass_error ); my $ref = +{ map{ $_ => $dict->{ $_ }->{constant} } keys( %$dict ) }; return( $ref ); } sub disconnect($) { my $self = shift( @_ ); # my( $pack, $file, $line ) = caller(); # print( STDERR "disconnect() called from package '$pack' in file '$file' at line '$line'.\n" ); my $rc = $self->{dbh}->disconnect( @_ ); return( $rc ); } sub do($;$@) { my $self = shift( @_ ); # $rc = $dbh->do( $statement ) || die( $dbh->errstr ); # $rc = $dbh->do( $statement, \%attr ) || die( $dbh->errstr ); # $rv = $dbh->do( $statement, \%attr, @bind_values ) || ... # my( $rows_deleted ) = $dbh->do( # q{ # DELETE FROM table WHERE status = ? # }, undef(), 'DONE' ) || die( $dbh->errstr ); my $query = shift( @_ ); my $opt_ref = shift( @_ ) || undef(); my $param_ref = shift( @_ ) || []; my $dbh = $self->can( 'database_object' ) ? $self->database_object : $self; $dbh or return( $self->error( "Could not find database handler." ) ); my $sth = $dbh->prepare( $query, $opt_ref ) || return( $self->error( "Error while preparing do query:\n$query", $dbh->errstr() ) ); $sth->execute( @$param_ref ) || return( $self->error( "Error while executing do query:\n$query", $sth->errstr() ) ); # my $rows = $sth->rows(); # return( ( $rows == 0 ) ? "0E0" : $rows ); return( $sth ); } sub driver { return( shift->_set_get( 'driver' ) ); } sub enhance { my $self = shift( @_ ); my $prev = $self->{enhance}; $self->{enhance} = shift( @_ ) if( @_ ); return( $prev ); } sub err(@) { my $self = shift( @_ ); # $rv = $h->err; if( defined( $self->{sth} ) ) { return( $self->{sth}->err() ); } elsif( $self->{dbh} ) { return( $self->{dbh}->err() ); } #else #{ # return( $self->{ 'drh' }->err() ); # return( DBI::err(); #} } sub errno { goto( &err ); } sub errmesg { goto( &errstr ); } sub errstr(@) { my $self = shift( @_ ); if( !ref( $self ) ) { return( $DBI::errstr || $DB_ERRSTR ); } elsif( defined( $self->{sth} ) && $self->{sth}->errstr() ) { return( $self->{sth}->errstr() ); } elsif( defined( $self->{dbh} ) && $self->{dbh}->errstr() ) { return( $self->{dbh}->errstr() ); } else { return( $self->{errstr} ); } } sub FALSE { return( 'FALSE' ); } sub fatal { my $self = shift( @_ ); if( @_ ) { $self->{fatal} = int( shift( @_ ) ); } return( $self->{fatal} ); } sub get_sql_type { return( shift->error( "The driver has not provided support for this method get_sql_type()" ) ); } sub host { return( shift->_set_get_scalar( 'host', @_ ) ); } sub IN { return( shift->_operator_object_create( 'DB::Object::IN', @_ ) ); } # $rv = $dbh->last_insert_id($catalog, $schema, $table, $field, \%attr); sub last_insert_id { my $self = shift( @_ ); return( $self->error( "Method \"last_insert_id\" has not been implemented by driver $self->{driver} (object = $self)." ) ); } sub LIKE { return( shift->_operator_object_create( 'DB::Object::LIKE', @_ ) ); } sub lock { my $self = shift( @_ ); return( $self->error( "Method \"lock\" has not been implemented by driver $self->{driver} (object = $self)." ) ); } sub login { return( shift->_set_get_scalar( 'login', @_ ) ); } # This value will be passed on to newly instantiated DB::Object::Tables objects sub no_bind { return( shift->_set_get_boolean( 'no_bind', @_ ) ); } sub no_cache { my $self = shift( @_ ); $self->{cache} = 0; return( $self ); } sub NOT { return( shift->_operator_object_create( 'DB::Object::NOT', @_ ) ); } sub NULL { return( 'NULL' ); } sub OR { return( shift->_operator_object_create( 'DB::Object::OR', @_ ) ); } sub P { return( shift->placeholder( @_ ) ); } sub param { my $self = shift( @_ ); return if( !@_ ); my @supported = qw( SQL_AUTO_IS_NULL AUTOCOMMIT SQL_BIG_TABLES SQL_BIG_SELECTS SQL_BUFFER_RESULT SQL_LOW_PRIORITY_UPDATES SQL_MAX_JOIN_SIZE SQL_SAFE_MODE SQL_SELECT_LIMIT SQL_LOG_OFF SQL_LOG_UPDATE TIMESTAMP INSERT_ID LAST_INSERT_ID ); my $params = $self->{params} ||= {}; if( @_ == 1 ) { my $type = shift( @_ ); $type = uc( $type ) if( scalar( grep{ /^$_[ 0 ]$/i } @supported ) ); return( $params->{ $type } ); } else { my %arg = ( @_ ); my( $type, $value ); my @query = (); while( ( $type, $value ) = each( %arg ) ) { my @found = grep{ /^(SQL_)?$type$/i } @supported; # SQL parameter if( scalar( @found ) ) { $type = uc( $type ); $value = 0 if( !defined( $value ) || $value eq '' ); $params->{ $type } = $value; if( $type eq 'AUTOCOMMIT' && $self->{dbh} && $value =~ /^(?:1|0)$/ ) { $self->{dbh}->{AutoCommit} = $value; } push( @query, "$type = $value" ); } # Private parameter - May be anything else { $params->{ $type } = $value; } } return( $self ) if( !scalar( @query ) ); my $dbh = $self->{dbh} || return( $self->error( "Could not find database handler." ) ); my $query = 'SET ' . CORE::join( ', ', @query ); my $sth = $dbh->prepare( $query ) || return( $self->error( "Unable to set options '", CORE::join( ', ', @query ), "'" ) ); $sth->execute(); $sth->finish(); return( $self ); } } sub passwd { return( shift->_set_get_scalar( 'passwd', @_ ) ); } sub ping(@) { #return( shift->{ 'dbh' }->ping ); my $self = shift( @_ ); return( $self->{dbh}->ping ); } sub ping_select(@) { my $self = shift( @_ ); # $rc = $dbh->ping; # Some new ping method replacement.... See Apache::DBI # my( $dbh ) = @_; my $ret = 0; eval { local( $SIG{__DIE__} ) = sub{ return( 0 ); }; local( $SIG{__WARN__} ) = sub{ return( 0 ); }; # adapt the select statement to your database: my $sth = $self->prepare( "SELECT 1" ); $ret = $sth && ( $sth->execute() ); $sth->finish(); }; return( ($@) ? 0 : $ret ); } sub placeholder { my $self = shift( @_ ); $self->_load_class( 'DB::Object::Placeholder' ) || return( $self->pass_error ); my $opts = $self->_get_args_as_hash( @_ ); $opts->{debug} = $self->debug if( !exists( $opts->{debug} ) ); my $obj = DB::Object::Placeholder->new( %$opts ) || return( $self->pass_error( DB::Object::Placeholder->error ) ); return( $obj ); } sub port { return( shift->_set_get_number( 'port', @_ ) ); } # Gateway to DB::Object::Statement sub prepare($;$) { my $self = shift( @_ ); my $class = ref( $self ) || $self; my $query = shift( @_ ); my $opt_ref = shift( @_ ) || undef(); my $base_class = $self->base_class; my $q; if( $self->_is_a( $query => 'DB::Object::Query' ) ) { $q = $query; $query = $q->as_string; } elsif( $self->_is_a( $self => 'DB::Object::Tables' ) ) { $q = $self->query_object; } $self->_clean_statement( \$query ); # Wether we are called from DB::Object or DB::Object::Tables object my $dbo = $self->{dbo} || $self; if( !$dbo->ping ) { my $dbh = $dbo->_dbi_connect || return; $self->{dbh} = $dbo->{dbh} = $dbh; } local $@; my $sth = eval { local( $SIG{__DIE__} ) = sub{ }; local( $SIG{__WARN__} ) = sub{ }; $dbo->{dbh}->prepare( $query, $opt_ref ); }; if( $sth ) { # my $data = { 'sth' => $sth, 'query' => $query }; my( $query_values, $sel_fields ); if( $q ) { $query_values = $q->query_values; $sel_fields = $q->selected_fields; } my $data = { sth => $sth, query => $query, # query_values => $q->query_values, # selected_fields => $q->selected_fields, ( defined( $query_values ) ? ( query_values => $query_values ) : () ), ( defined( $sel_fields ) ? ( selected_fields => $sel_fields ) : () ), query_object => $q, }; return( $self->_make_sth( "${base_class}::Statement", $data ) ); } else { my $err = $@ || $self->{dbh}->errstr() || 'Unknown error while cache preparing query.'; $self->{query} = $query; return( $self->error( $err ) ); } } sub prepare_cached { my $self = shift( @_ ); my $class = ref( $self ) || $self; my $query = shift( @_ ); my $opt_ref = shift( @_ ) || undef(); my $base_class = $self->base_class; my $q; if( ref( $q ) && $q->isa( 'DB::Object::Query' ) ) { $q = $query; $query = $q->as_string; } $self->_clean_statement( \$query ); # Wether we are called from DB::Object or DB::Object::Tables object my $dbo = $self->{dbo} || $self; if( !$dbo->ping ) { my $dbh = $dbo->_dbi_connect || return; $self->{dbh} = $dbo->{dbh} = $dbh; } my $sth = eval { local( $SIG{__DIE__} ) = sub{ }; local( $SIG{__WARN__} ) = sub{ }; $dbo->{dbh}->prepare_cached( $query, $opt_ref ); }; if( $sth ) { # my $data = { %$self, 'sth' => $sth, 'query' => $query }; # my $data = { 'sth' => $sth, 'query' => $query }; my $data = { sth => $sth, query => $query, query_values => $self->{query_values}, selected_fields => $self->{selected_fields}, query_object => $q, }; # CORE::delete( $data->{ 'executed' } ); # This is an inner package # bless( $data, "DB::Object::Statement" ); # return( $data ); return( $self->_make_sth( "${base_class}::Statement", $data ) ); } else { my $err = $@ || $self->{dbh}->errstr() || 'Unknown error while cache preparing query.'; $self->{query} = $query; return( $self->error( $err ) ); } } sub query($$) { my $self = shift( @_ ); my $sth = $self->prepare( @_ ); my $result; if( $sth && !( $result = $sth->execute() ) ) { return; } else { # bless( $sth, ref( $self ) ); return( $sth ); } } sub query_object { return( shift->_set_get_object_without_init( 'query_object', 'DB::Object::Query', @_ ) ); } sub quote { my $self = shift( @_ ); # my $dbh = $self->{dbh} || return( $self->error( "No database handler was set." ) ); my $dbh; unless( $dbh = $self->{dbh} ) { # This is a fallback in case we need to use quote, but do not have a database connection yet. my $str = shift( @_ ); # print( STDERR ref( $self ), "::quote -> \$str is '$str' (without surrounding quote\n" ); return( $self->NULL ) if( !defined( $str ) || uc( $str ) eq 'NULL' ); if( $str =~ /^$RE{num}{real}$/ ) { return( $str ); } else { $str =~ s/'/''/g; # iso SQL 2 return( "'$str'" ); } } return( $dbh->quote( @_ ) ); } sub set { my $self = shift( @_ ); my $vars = ''; $vars = shift( @_ ); $vars ||= $self->local(); # Are there any variable declaration? if( $vars ) { my $query = "SET $vars"; eval { local( $SIG{__DIE__} ) = sub{ }; local( $SIG{__WARN__} ) = sub{ }; local( $SIG{ALRM} ) = sub{ die( "Timeout while processing query to set variables:\n$query\n" ) }; $self->do( $query ); }; if( $@ ) { my $err = '*** ' . join( "\n*** ", split( /\n/, $@ ) ); if( $self->fatal() ) { die( "Error occured while setting SQL variables before executing query:\n$self->{sth}->{Statement}\n$err\n" ); } else { return( $self->error( $@ ) ); } } } return(1); } # To also consider: # $sth = $dbh->statistics_info( undef, $schema, $table, $unique_only, $quick ); sub stat { my $self = shift( @_ ); my $type = lc( shift( @_ ) ); my $sth = $self->prepare( "SHOW STATUS" ); $sth->execute(); my @data = (); my $ref = {}; while( @data = $sth->fetchrow() ) { $ref->{ lc( $data[ 0 ] ) } = $data[ 1 ]; } $sth->finish(); if( $type ) { return( exists( $ref->{ $type } ) ? $ref->{ $type } : undef() ); } else { return( wantarray() ? () : undef() ) if( !%$ref ); return( wantarray() ? %$ref : $ref ); } } sub state(@) { my $self = shift( @_ ); # $str = $h->state; if( !ref( $self ) ) { return( $DBI::state ); } else { return( $self->SUPER::state() ); } } sub supported_class { my $self = shift( @_ ); my @classes = values( %$DRIVER2PACK ); return( @classes ); } sub supported_drivers { my $self = shift( @_ ); my @drivers = keys( %$DRIVER2PACK ); return( @drivers ); } sub table { my $self = shift( @_ ); my $base_class = $self->base_class; return( $self->error( "You must use the database object to access this method." ) ) if( ref( $self ) ne $base_class ); my $table = shift( @_ ) || return( $self->error( "You must provide a table name to access the table methods." ) ); my $table_class = "${base_class}::Tables"; $self->messagec( 4, "Getting table {green}${table}{/} object with class {green}${table_class}{/}" ); $self->_load_class( $table_class ) || return( $self->pass_error ); my $host = $self->{server} // ''; my $db = $self->{database} // ''; my $tbl; if( $self->cache_table ) { $tbl = $self->_cache_table( database => $self->database, table => $table, ); return( $self->pass_error ) if( !defined( $tbl ) ); $tbl->reset if( $tbl ); } else { # cache_table is not enabled. } if( !$tbl ) { $tbl = $table_class->new( $table, dbh => $self->{dbh}, dbo => $self, debug => $self->debug, ) || return( $self->pass_error( $table_class->error ) ); $self->messagec( 6, "Object for table {green}${table}{/} created." ); $tbl->reset; if( $self->cache_table ) { $self->messagec( 6, "Caching table {green}${table}{/}" ); $self->_cache_table( database => $self->database, table => $tbl, ) || return( $self->pass_error ); } } $tbl->no_bind( $self->no_bind ); # We set debug and again here in case it changed since the table object was instantiated $tbl->debug( $self->debug ); $self->messagec( 6, "Returning table {green}${table}{/} object." ); return( $tbl ); } sub table_exists { my $self = shift( @_ ); my $table = shift( @_ ) || return( $self->error( "You must provide a table name to access the table methods." ) ); my $cache_tables = $self->cache_tables; my $tables_in_cache = []; if( defined( $cache_tables ) ) { $tables_in_cache = $cache_tables->get({ host => $self->host, driver => $self->driver, port => $self->port, database => $self->database, }); } foreach my $ref ( @$tables_in_cache ) { return( 1 ) if( $ref->{name} eq $table ); } # We did not find it, so let's try by checking directly the database my $def = $self->table_info( $table ); return( $self->pass_error ) if( !defined( $def ) ); return(0) if( $self->_is_empty( $def ) ); return(1); } sub table_info { my $self = shift( @_ ); return( $self->error( "table_info() has not been implemented by driver \"$self->{driver}\" (object = $self)." ) ); } sub table_push { my $self = shift( @_ ); my $table = shift( @_ ) || return( $self->error( "No table provided to add to our cache." ) ); my $def = $self->tables_info || return; my $hash = { host => $self->host, driver => $self->driver, port => $self->port, database => $self->database, tables => $def, }; my $cache_tables = $self->cache_tables; if( !defined( $cache_tables->set( $hash ) ) ) { return( $self->pass_error( $cache_tables->error ) ); } return( $table ); } sub tables { my $self = shift( @_ ); my $db = shift( @_ ) || $self->database; my $opts = $self->_get_args_as_hash( @_ ); $db = $opts->{database} if( $opts->{database} ); my $all = []; if( !$opts->{no_cache} && !$opts->{live} ) { if( my $cache_tables = $self->cache_tables ) { $all = $cache_tables->get({ host => $self->host, driver => $self->driver, port => $self->port, database => $db, }) || do { $self->error( "Warning only: an error occured while trying to fetch the tables cache for host '", $self->host, "', driver '", $self->driver, "', port '", $self->port, "' and database '", $self->database, "': ", $cache_tables->error, "\n" ); }; } else { $self->error( "Warning only: no cache tables object found in our self ($self)! Current keys are: '", join( "', '", sort( keys( %$self ) ) ), "'." ); } } if( $opts->{no_cache} || $opts->{live} || !scalar( @$all ) ) { $all = $self->tables_info || return; } my @tables = (); @tables = map( $_->{name}, @$all ) if( scalar( @$all ) ); return( \@tables ); } sub tables_cache { my $self = shift( @_ ); my $opts = $self->_get_args_as_hash( @_ ); my $cache_tables = $self->cache_tables; my $cache = $cache_tables->get({ host => $self->host, driver => $self->driver, port => $self->port, database => $self->database, }); return( $cache ); } sub tables_info { return( shift->error( "tables_info() has not been implemented by driver." ) ); } sub tables_refresh { my $self = shift( @_ ); my $db = shift( @_ ) || $self->database; my $tables = $self->tables_info || return; my $hash = { host => $self->host, driver => $self->driver, port => $self->port, database => $self->database, tables => $tables, }; my $cache_tables = $self->cache_tables; if( !defined( $cache_tables->set( $hash ) ) ) { return( $self->pass_error( $cache_tables->error ) ); } return( wantarray() ? @$tables : $tables ); } # Used to flag this as a transaction when begin_work is triggered sub transaction { return( shift->_set_get_boolean( 'transaction', @_ ) ); } sub TRUE { return( 'TRUE' ); } sub unknown_field { return( shift->_set_get_scalar( 'unknown_field', @_ ) ); } sub unlock { my $self = shift( @_ ); return( $self->error( "Method \"unlock\" has not been implemented by driver $self->{driver} (object $self)." ) ); } sub use { my $self = shift( @_ ); my $base_class = $self->base_class; return( $self->error( "You must use the the database object to switch database." ) ) if( ref( $self ) ne $base_class ); my $db = shift( @_ ); # No need to go further return( $self ) if( $db eq $self->{database} ); if( !@AVAILABLE_DATABASES ) { @AVAILABLE_DATABASES = $self->databases(); } if( !scalar( grep{ /^$db$/ } @AVAILABLE_DATABASES ) ) { return( $self->error( "The database '$db' does not exist." ) ); } my $dbh = $base_class->connect( $db ) || return( $self->error( "Unable to connect to database '$db'." ) ); $self->param( 'multi_db' => 1 ); $dbh->param( 'multi_db' => 1 ); return( $dbh ); } sub use_cache { return( shift->_set_get_boolean( 'cache', @_ ) ) } sub use_bind { return( shift->_set_get_boolean( 'bind', @_ ) ) } sub variables { my $self = shift( @_ ); my $type = shift( @_ ); $self->error( "Variable '$type' is a read-only value." ) if( @_ ); my $vars = $self->{variables} ||= {}; if( !%$vars ) { my $sth = $self->prepare( "SHOW VARIABLES" ) || return( $self->error( "SHOW VARIABLES is not supported." ) ); $sth->execute(); my $ref = $self->fetchall_arrayref(); my %vars = map{ lc( $_->[ 0 ] ) => $_->[ 1 ] } @$ref; $vars = \%vars if( %vars ); $sth->finish(); } my @found = grep{ /$type/i } keys( %$vars ); return( '' ) if( !scalar( @found ) ); return( $vars->{ $found[ 0 ] } ); } sub version { return( shift->error( "This driver has not set the version() method." ) ); } sub _cache_queries { my $self = shift( @_ ); my $base_class = $self->base_class; # DB::Object::CACHE_QUERIES, DB::Object::Postgres::CACHE_QUERIES, etc no strict 'refs'; my $cachedb = ${"${base_class}\::CACHE_QUERIES"}; return( $cachedb ); } # When the table schema caching is enabled, we store the table field objects in an hash reference # database->table => table_object sub _cache_table { my $self = shift( @_ ); my $opts = $self->_get_args_as_hash( @_ ); my $class = ( ref( $self ) || $self ); my $sym = $self->_get_symbol( '$TABLE_CACHE' ); if( !defined( $sym ) ) { return( $self->pass_error ) if( $self->error ); return( $self->error( "No variable ${class}::TABLE_CACHE could be found!" ) ); } ref( $$sym ) eq 'HASH' or return( $self->error( "Variable ${class}::TABLE_CACHE is not an hash reference." ) ); my $cache = $$sym; if( CORE::exists( $opts->{database} ) ) { my( $database, $table ) = @$opts{qw( database table )}; return( $self->error( "A database name was provided, but is undefined or empty." ) ) if( !defined( $database ) || !CORE::length( $database // '' ) ); if( !defined( $cache->{ $database } ) ) { $cache->{ $database } = {} if( !CORE::exists( $cache->{ $database } ) || ref( $cache->{ $database } ) ne 'HASH' ); } $self->messagec( 6, "There are {green}", scalar( keys( %{$cache->{ $database }} ) ), "{/} eements in hash reference \$cache->\{ $database \}" ); return( $cache->{ $database } ) if( !defined( $table ) ); if( $self->_is_a( $table => 'DB::Object::Tables' ) ) { my $name = $table->name || return( $self->error( "No table name associated with this table object." ) ); my $dbo = $table->database_object; $table->database_object( undef ); my $clone = $table->clone; $table->database_object( $dbo ); $cache->{ $database }->{ "$name" } = $clone; } elsif( !ref( $table ) || ( ref( $table ) && $self->_can_overload( $table => '""' ) ) ) { return( '' ) if( !CORE::exists( $cache->{ $database }->{ "$table" } ) || !defined( $cache->{ $database }->{ "$table" } ) ); my $tbl = $cache->{ $database }->{ "$table" }; my $clone = $tbl->clone; $clone->database_object( $self ); return( $clone ); } else { return( $self->error( "I do not understand the value provided for the 'table' option -> ", overload::StrVal( $table ) ) ); } } return( $cache // {} ); } sub _cache_this { my $self = shift( @_ ); # When this method is accessed by method from package DB::Object::Statement, they CAN NOT # implicitly pass the statement string or they would risk to modify the previous stored # query object they represent. # For instance: # $obj->select->join( 'some_table', { 'parameter', 'list' } )->fetchrow_hashref() # here the first query is prepared and cached and its resulting object is passed on to join # here join will rebuild the query, but will search first if there was one already cached # if join passes implictly the statement string, this means it will modify the cached query select() # has just previously stored... This is why method such as join must pass explicitly the query string my $q = shift( @_ ); my $query = ( ref( $q ) && $q->isa( 'DB::Object::Query' ) ) ? $q->as_string : $q; my $base_class = $self->base_class; my $cache = $self->{cache}; my $bind = $self->{bind}; my $queries = ''; my @saved = (); # my $cachedb = ${"${base_class}\::CACHE_QUERIES"}; my $cachedb = $self->_cache_queries; return( $self->error( "CACHE_QUERIES is not set in class $base_class" ) ) if( !$self->_is_array( $cachedb ) ); my $cache_size = scalar( @$cachedb ); my $cached_sth = ''; # If database object exists, this means this is a DB::Object::Tables object, otherwise a DB::Object object # my $dbo = $self->{ 'dbo' } || $self; if( $cache ) { if( $CACHE_SIZE > 0 && $cache_size > $CACHE_SIZE ) { # Take 20% off of the cache my $truncate_limit = int( ( $cache_size * 20 ) / 100 ); splice( @$cachedb, ( $cache_size - $truncate_limit ) ); } foreach my $obj ( @$cachedb ) { # print( STDERR ref( $self ) . "::_cache_this(): Is query:\n\t'$query'\nthe same than:\n\t'$obj->{ 'query' }'\n" ); if( $query && $obj->{query} && $obj->{query} eq $query ) { $cached_sth = $obj; last; } } } my $sth; # We found a previous query exactly the same if( $cached_sth ) { my $data = { sth => $cached_sth->{sth}, query => $cached_sth->{query} }; # This is an inner package $sth = $self->_make_sth( "${base_class}::Statement", $data ); } else { # Maybe we ought to write: # $prepare = $cache ? \&prepare_cached : \prepare; # $sth = $prepare->( $self, $self->{ 'query' } ) || # $sth = $self->prepare_cached( $query ) || my $prepare_options = {}; if( $q && $self->_is_a( $q, 'DB::Object::Query' ) ) { $prepare_options = $q->prepare_options->as_hash; } if( scalar( keys( %$prepare_options ) ) ) { $sth = $self->prepare( $query, $prepare_options ) || return( $self->pass_error ); } else { $sth = $self->prepare( $query ) || return( $self->pass_error ); } # $sth = $self->prepare( $self->{ 'query' } ) || # return( $self->error( "Error while preparing the query on table '$self->{ 'table' }':\n$self->{ 'query' }\n", $self->errstr() ) ); # Let the proper method set its error text # If caching of queries is turned on, cache the request if( $cache ) { unshift( @$cachedb, $sth ); } # If caching is off, but the query is a binded parameters' one, # make the current object hold the statement object elsif( $bind ) { $self->{sth} = $sth; } } #$sth->{query_object} = ( ref( $q ) && $q->isa( 'DB::Object::Query' ) ) ? $q : ''; $sth->query_object( $q ) if( $self->_is_a( $q, 'DB::Object::Query' ) ); # print( STDERR ref( $self ) . "::_cache_this(): prepared statement was ", $cached_sth ? 'cached' : 'not cached.', "\n" ); # Caching the query as a constant if( $q && $self->_is_object( $q ) && $q->isa( 'DB::Object::Query' ) ) { my $constant = $q->constant; if( scalar( keys( %$constant ) ) ) { foreach my $k (qw( pack file line )) { return( $self->error( "Could not find the parameter \"$k\" in the constant query hash reference." ) ) if( !$constant->{ $k } ); } $constant->{query_object} = $q; $self->constant_queries_cache_set( $constant ); } } return( $sth ); } sub _check_connect_param { my $self = shift( @_ ); my $param = shift( @_ ); my $valid = $self->_connection_parameters( $param ); my $opts = $self->_connection_options( $param ); foreach my $k ( keys( %$param ) ) { # If it is not in the list and it does not start with an upper case; those are like RaiseError, AutoCommit, etc if( CORE::length( $param->{ $k } ) && !grep( /^$k$/, @$valid ) && !CORE::exists( $opts->{ $k } ) ) { return( $self->error( "Invalid parameter '$k'." ) ); } } my @opts_to_remove = keys( %$opts ); CORE::delete( @$param{ @opts_to_remove } ) if( scalar( @opts_to_remove ) ); $param->{opt} = $opts; $param->{database} = CORE::delete( $param->{db} ) if( !length( $param->{database} ) && $param->{db} ); return( $param ); } sub _check_default_option { my $self = shift( @_ ); my $opts = $self->_get_args_as_hash( @_ ); # return( $self->error( "Provided option is not a hash reference." ) ) if( !$self->_is_hash( $opts ) ); # This method should be superseded by an inherited class return( $opts ); } # Simplistic clone by design. We do not want a recursive clone like Clone::clone() sub _clone { my( $self, $this ) = @_; if( $self->_is_array( $this ) ) { my @clone = @$this; return( \@clone ); } elsif( ref( $this ) eq 'HASH' ) { my @keys = keys( %$this ); my $clone = {}; @$clone{ @keys } = @$this{ @keys }; return( $clone ); } else { return( $self->error( "Unsupported data type to clone. This method only support hash or array reference." ) ); } } sub _connection_options { my $self = shift( @_ ); my $param = shift( @_ ); my @dbi_opts = grep( /^[A-Z][a-zA-Z]+/, keys( %$param ) ); my $opt = {}; $opt = CORE::delete( $param->{opt} ) if( $param->{opt} && $self->_is_hash( $param->{opt} => 'strict' ) ); @$opt{ @dbi_opts } = @$param{ @dbi_opts }; return( $opt ); } sub _connection_parameters { my $self = shift( @_ ); my $param = shift( @_ ); return( [qw( db login passwd host port driver database server opt uri debug cache_connections cache_dir cache_table unknown_field )] ); } sub _connection_params2hash { my $self = shift( @_ ); my $argv = [@_]; my $param = {}; if( !( @_ % 2 ) ) { $param = { @_ }; } elsif( @_ == 1 && ( ref( $_[0] ) // '' ) eq 'HASH' ) { $param = shift( @_ ); } else { my @keys = qw( database login passwd host driver schema ); # Only add in the $param hash the keys value we were given, so we don't create keys entry when not needed for( my $i = 0; $i < scalar( @_ ); $i++ ) { $param->{ $keys[ $i ] } = $_[ $i ]; } } my $equi = { database => 'DB_NAME', login => 'DB_LOGIN', passwd => 'DB_PASSWD', host => 'DB_HOST', port => 'DB_PORT', driver => 'DB_DRIVER', schema => 'DB_SCHEMA', }; foreach my $prop ( keys( %$equi ) ) { $param->{ $prop } = $ENV{ $equi->{ $prop } } if( $ENV{ $equi->{ $prop } } && !length( $param->{ $prop } ) ); } # A simple json file # An URI could be http://localhost:5432?database=somedb etc... # or it could also be file:/foo/bar?opt={"RaiseError":true} if( $param->{uri} || $ENV{DB_CON_URI} ) { my $uri; eval { require URI; $uri = URI->new( $param->{uri} || $ENV{DB_CON_URI} ); }; if( !$@ && $uri ) { # Make sure our parameter is a valid URI object $param->{uri} = $uri; if( $uri->can( 'port' ) ) { $param->{host} = $uri->host; $param->{port} = $uri->port if( $uri->port ); } # file:/ elsif( length( $uri->path ) ) { $param->{database} = ( $uri->path_segments )[-1]; } my( %q ) = $uri->query_form; $param->{host} = $q{host} if( $q{host} ); $param->{port} = $q{port} if( $q{port} ); $param->{database} = $q{database} if( $q{database} ); $param->{schema} = $q{schema} if( $q{schema} ); $param->{user} = $q{user} if( $q{user} ); $param->{login} = $q{login} if( $q{login} ); $param->{password} = $q{password} if( $q{password} ); $param->{opt} = $q{opt} if( $q{opt} ); $param->{login} = CORE::delete( $param->{user} ) if( !$param->{login} && $param->{user} ); if( $q{opt} ) { my $jdata = {}; eval { require JSON; if( defined( *{ "JSON::" } ) ) { my $j = JSON->new->allow_nonref; $jdata = $j->decode( $q{opt} ); } }; if( $@ ) { warn( "Found the database connection opt parameter provided in the connection uri \"$uri\", but could not decode its json value: $@\n" ); } $param->{opt} = $jdata if( scalar( keys( %$jdata ) ) ); } } } if( $param->{conf_file} || $param->{config_file} || $ENV{DB_CON_FILE} ) { my $db_con_file = $self->new_file( CORE::delete( $param->{conf_file} ) || CORE::delete( $param->{config_file} ) || $ENV{DB_CON_FILE} ); my $db_con_file_ok = 0; if( !$db_con_file->exists ) { warn( "Database connection parameter file \"$db_con_file\" was provided but does not exist.\n" ); } elsif( $db_con_file->is_empty ) { warn( "Database connection parameter file \"$db_con_file\" was provided but the file is empty.\n" ); } elsif( !$db_con_file->can_read ) { warn( "Database connection parameter file \"$db_con_file\" was provided but the file lacks privileges to be read.\n" ); } else { $db_con_file_ok++; } my $json = {}; # try-catch local $@; eval { require JSON; if( defined( *{ "JSON::" } ) ) { my $j = JSON->new->allow_nonref; if( my $io = $db_con_file->open_utf8( '<' ) ) { my $data = $db_con_file->load; $json = $j->decode( $data ); } else { warn( "Unable to open database connection parameter file \"$db_con_file\": ", $db_con_file->error ); } } }; if( $@ ) { warn( "Database connection parameter file \"$db_con_file\" was provided, but I encountered the following error while trying to read its json data: $@\n" ); } $json = {} if( !$self->_is_hash( $json => 'strict' ) ); my $ref = {}; if( exists( $json->{databases} ) ) { return( $self->error( "Found a property 'databases' in the connections configuration file \"$db_con_file\". I was expecting this property to be an array reference and instead I found this: '$json->{databases}'" ) ) if( !$self->_is_array( $json->{databases} ) ); # When called from sub classes, this is set my $driver = $self->driver; # We take the first one matching our driver if any, or else we just take the first one foreach my $this ( @{$json->{databases}} ) { if( !$param->{database} && ( !$driver || $this->{driver} eq $driver ) ) { $ref = $this; last; } elsif( $param->{database} && $this->{database} eq $param->{database} && ( !$param->{host} || $param->{host} eq $this->{host} ) && ( !$param->{port} || $param->{port} eq $this->{port} ) && ( !$param->{login} || $param->{login} eq $this->{login} ) ) { $ref = $this; last; } } } else { $ref = $json; } if( scalar( keys( %$ref ) ) ) { foreach my $k ( qw( database login passwd host port driver schema opt ) ) { $param->{ $k } = $ref->{ $k } if( !length( $param->{ $k } ) && length( $ref->{ $k } ) ); } } $param->{cache_table} = $json->{cache_table} if( CORE::exists( $json->{cache_table} ) ); $param->{cache_dir} = $json->{cache_dir} if( CORE::exists( $json->{cache_dir} ) ); } if( CORE::exists( $param->{host} ) && index( $param->{host}, ':' ) != -1 ) { @$param{ qw( host port ) } = split( /:/, $param->{host}, 2 ); } if( !$param->{opt} && $ENV{DB_OPT} ) { my $jdata = {}; eval { require JSON; if( defined( *{ "JSON::" } ) ) { my $j = JSON->new->allow_nonref; $jdata = $j->decode( $ENV{DB_OPT} ); } }; if( $@ ) { warn( "Found the database connection opt parameter provided in the envionment variable DB_OPT, but could not decode its json value: $@\n" ); } $param->{opt} = $jdata if( scalar( keys( %$jdata ) ) ); } return( $param ); } sub _clean_statement { my $self = shift( @_ ); my $data = shift( @_ ); my $query = ref( $data ) ? $data : \$data; $$query = CORE::join( "\n", map{ s/^\s+|\s+$//gs; $_ } split( /\n/, $$query ) ); return( $$query ) if( !ref( $data ) ); } sub _dbi_connect { my $self = shift( @_ ); my $dbh; my $dsn = $self->_dsn; # print( STDERR ref( $self ) . "::_dbi_connect() Options are: ", $self->dumper( $self->{opt} ), "\n" ); # See: # signals to mask in the handler my $mask = POSIX::SigSet->new( POSIX::SIGALRM ); my $action = POSIX::SigAction->new( # the handler code ref sub{ die( "connect timeout\n" ) }, $mask, # not using (perl 5.8.2 and later) 'safe' switch or sa_flags ); my $oldaction = POSIX::SigAction->new(); POSIX::sigaction( POSIX::SIGALRM, $action, $oldaction ); my $failed; local $@; eval { eval { # seconds before time out alarm(5); if( $self->{cache_connections} ) { $dbh = DBI->connect_cached( $dsn, $self->{login}, $self->{passwd}, $self->{opt}, undef(), $CONNECT_VIA, ); } else { $dbh = DBI->connect( $dsn, $self->{login}, $self->{passwd}, $self->{opt}, undef(), $CONNECT_VIA, ); } 1; } or $failed = 1; # cancel alarm (if connect worked fast) alarm(0); # connect died die( "$@\n" ) if( $failed ); 1; } or $failed = 1; # restore original signal handler POSIX::sigaction( POSIX::SIGALRM, $oldaction ); if( defined( $failed ) && $failed ) { if( defined( $@ ) && $@ =~ /^connect timeout/ ) { return( $self->error( "Connection timed out for dsn '", ( $dsn // 'undef' ), " and login '", ( $self->{login} // 'undef' ), "'" ) ); } else { return( $self->error( "Error connecting with dsn '", ( $dsn // 'undef' ), " and login '", ( $self->{login} // 'undef' ), "': $@" ) ); } } return( $self->error( $DBI::errstr ) ) if( !$dbh ); return( $dbh ); } sub _decode_json { my $self = shift( @_ ); my $json = shift( @_ ); return if( !CORE::length( $json ) ); my $j = JSON->new->allow_nonref; my $hash = eval { $j->decode( $json ); }; return if( $@ ); return( $hash ); } sub _dsn { my $self = shift( @_ ); my $class = ref( $self ) || $self; die( "Method _dsn is not implemented in class $class\n" ); } sub _encode_json { my $self = shift( @_ ); return if( !scalar( @_ ) || ( scalar( @_ ) == 1 && !defined( $_[0] ) ) ); my $this = shift( @_ ); return( $self->error( "Value provided is not a hash reference. I was expecting a hash reference to encode data into json." ) ) if( !$self->_is_hash( $this => 'strict' ) ); my $j = JSON->new; my $json = eval { $j->encode( $this ); }; return( $self->error( "An error occurred while trying to encode hash reference provided: $@" ) ) if( $@ ); return( $json ); } sub _make_sth { my $self = shift( @_ ); my $pkg = shift( @_ ); my $data = shift( @_ ) || {}; my $base_class = $self->base_class; $self->_load_class( $pkg ) || return( $self->pass_error ); # map{ $data->{ $_ } = $self->{ $_ } } # qw( # dbh drh server login passwd database driver # table debug bind cache params selected_fields # local where limit group_by order_by reverse from_table left_join # tie tie_order # ); map{ $data->{ $_ } = $self->{ $_ } } qw( table debug bind cache params from_table left_join ); $data->{dbh} = $self->{dbh}; $data->{dbo} = $self->{dbo} ? $self->{dbo} : ref( $self ) eq $self->base_class ? $self : ''; # $data->{ 'binded' } = $self->{ 'binded' } if( $self->{ 'binded' } && ref( $self ) ne $base_class ); # In any case suppress the binded parameter from our parent object to avoid polluting the next queries # If needed, the binded parameter will be rebuilt using the data stored in 'where', 'group', 'order' and 'limit' # CORE::delete( $self->{ 'binded' } ); # Binded parameters are now either in the DB::Object::Query package or one of its descendant OR passed as arguments to execute $data->{errstr} = ''; CORE::delete( $data->{executed} ); $data->{query_time} = time(); $data->{selected_fields} = '' if( !exists( $data->{selected_fields} ) ); $data->{table_object} = $self; my $this = bless( $data, $pkg ); $this->debug( $self->debug ); return( $this ); } sub _operator_object_create { my $self = shift( @_ ); my $class = shift( @_ ) || return( $self->error( "No operator class was provided." ) ); my $q = $self->_reset_query; my $args = ( scalar( @_ ) == 1 && ref( $_[0] ) eq 'ARRAY' ) ? [ @{$_[0]} ] : [ @_ ]; $self->messagec( 5, "Creating an operator object for class {green}${class}{/} and values '{green}" . join( "{/}', '{green}", map( overload::StrVal( $_ ), @$args ) ) . "{/}'" ); return( $class->new( debug => $self->debug, query_object => $q, value => $args, ) ); } sub _param2hash { my $self = shift( @_ ); my $opts = {}; if( scalar( @_ ) ) { if( $self->_is_hash( $_[0] => 'strict' ) ) { $opts = shift( @_ ); } elsif( !( scalar( @_ ) % 2 ) ) { $opts = { @_ }; } else { return( $self->error( "Uneven number of parameters. I was expecting a hash or a hash reference." ) ); } } return( $opts ); } sub _placeholder_regexp { return( $PLACEHOLDER_REGEXP ) } # NOTE: _query_object_add needs to reside in DB::Object (called indirectly by no_bind) sub _query_object_add { my $self = shift( @_ ); my $obj = shift( @_ ) || return( $self->error( "No query object was provided" ) ); my $base = $self->base_class; return( $self->error( "Object provided is not a query object class" ) ) if( ref( $obj ) !~ /^${base}\::Query$/ ); $self->query_object( $obj ); return( $obj ); } # NOTE: _query_object_create needs to reside in DB::Object (called indirectly by no_bind) sub _query_object_create { my $self = shift( @_ ); my $base = $self->base_class; my $query_class = "${base}::Query"; $self->messagec( 6, "Loading query class {green}${query_class}{/}" ); $self->_load_class( $query_class ) || return( $self->error( "Unable to load Query builder module $query_class: ", $self->error->message ) ); $self->messagec( 5, "Creating new query object with class $query_class for table '{green}", ( $self->isa( 'DB::Object::Tables' ) ? $self->name : '' ), "{/}' and alias {green}", ( $self->isa( 'DB::Object::Tables' ) ? $self->as : '' ), "{/}." ); $self->messagec( 6, "Instantiating new object for query class {green}${query_class}{/}" ); my $o = $query_class->new; $o->debug( $self->debug ); $o->enhance( $self->{enhance} ) if( CORE::length( $self->{enhance} ) ); if( $self->isa( 'DB::Object::Tables' ) ) { $o->table_object( $self ) || return( $self->pass_error( $o->error ) ); $o->database_object( $self->database_object ) || return( $self->pass_error( $o->error ) ); # Set the table alias if any has been set if( my $table_alias = $self->as ) { $o->table_alias( $table_alias ) if( defined( $table_alias ) ); } } elsif( $self->isa( 'DB::Object' ) ) { $o->database_object( $self ) || return( $self->pass_error( $o->error ) ); } return( $o ); } # NOTE: _query_object_current needs to reside in DB::Object (called indirectly by no_bind) sub _query_object_current { return( shift->{query_object} ); } # NOTE: _query_object_get_or_create needs to reside in DB::Object (called indirectly by no_bind) # If the stack is empty, we create an object, add it and resend it sub _query_object_get_or_create { my $self = shift( @_ ); my $obj = $self->query_object; if( !$obj ) { $self->messagec( 5, "Query object for table '{green}", ( $self->isa( 'DB::Object::Tables' ) ? $self->name : '' ), "{/}' does not exist yet, instantiating one." ); $obj = $self->_query_object_create || return( $self->pass_error ); $self->query_object( $obj ); } return( $obj ); } # NOTE: _query_object_remove needs to reside in DB::Object (called indirectly by no_bind) sub _query_object_remove { my $self = shift( @_ ); my $obj = shift( @_ ) || return( $self->error( "No query object was provided" ) ); my $base = $self->base_class; # return( $self->error( "Object provided is not a query object class" ) ) if( ref( $obj ) !~ /^${base}\::Query$/ ); return( $self->error( "Object provided is not a query object class" ) ) if( !$obj->isa( "DB::Object::Query" ) ); $self->query_object( undef ); return( $obj ); } sub _reset_query { my $self = shift( @_ ); if( !$self->{query_reset} ) { $self->{query_reset}++; $self->{enhance} = 1; my $obj = $self->query_object; $self->_query_object_remove( $obj ) if( $obj ); if( $obj && $obj->join_tables->length > 0 ) { $obj->join_tables->foreach(sub{ my $tbl = shift( @_ ); return if( $tbl->name eq $self->name ); my $this_query_object = $tbl->query_object; $tbl->_query_object_remove( $this_query_object ) if( $this_query_object ); $tbl->use_bind(0) unless( $tbl->use_bind > 1 ); $tbl->use_cache(0) unless( $tbl->use_cache > 1 ); $tbl->query_reset(1); return( $tbl->_query_object_get_or_create ); }); } $self->{bind} = 0 unless( defined( $self->{bind} ) && $self->{bind} > 1 ); $self->{cache} = 0 unless( defined( $self->{cache} ) && $self->{cache} > 1 ); return( $self->_query_object_get_or_create ); } return( $self->_query_object_current ); } # NOTE: AUTOLOAD AUTOLOAD { my $self; $self = shift( @_ ) if( blessed( $_[ 0 ] ) || index( $_[0], '::' ) != -1 ); my( $class, $meth ); if( $self ) { $class = ref( $self ) || $self; } $meth = $AUTOLOAD; if( CORE::index( $meth, '::' ) != -1 ) { my $idx = rindex( $meth, '::' ); $class = substr( $meth, 0, $idx ); $meth = substr( $meth, $idx + 2 ); } my @supported_class = DB::Object->supported_class; push( @supported_class, 'DB::Object' ); my $ok_classes = join( '|', @supported_class ); my $base_class = ( $class =~ /^($ok_classes)/ )[0]; # Is it a table object that is being requested? # if( $self && scalar( grep{ /^$meth$/ } @$tables ) ) # Getting table object take NO argument. # If the user wants to access a method, and somehow the table name is identical to one of our methods, # it is likely it will take an argument if( $class eq $base_class && !scalar( @_ ) && $self->table_exists( $meth ) ) { return( $self->table( $meth ) ); } elsif( $self && $self->can( $meth ) && defined( &{ "$class\::$meth" } ) ) { return( $self->$meth( @_ ) ); } # For imported subs elsif( defined( &$meth ) ) { no strict 'refs'; *{"${class}\::${meth}"} = \&$meth; unshift( @_, $self ) if( $self ); return( &$meth( @_ ) ); } # Taken from AutoLoader.pm elsif( $class =~ /^(?:$ok_classes)$/ ) { my $filename; my $pkg = $class; $pkg =~ s/::/\//g; if( defined( $filename = $INC{ "$pkg.pm" } ) ) { $filename =~ s%^(.*)$pkg\.pm\z%$1auto/${pkg}/${meth}.al%s; if( -r( $filename ) ) { unless( $filename =~ m|^/|s ) { $filename = "./$filename"; } } else { $filename = undef(); } } if( !defined( $filename ) ) { $filename = "auto/${meth}.al"; $filename =~ s/::/\//g; } my $save = $@; eval { local $SIG{__DIE__} = sub{ }; local $SIG{__WARN__} = sub{ }; require $filename; }; if( $@ ) { if( substr( $AUTOLOAD, -9 ) eq '::DESTROY' ) { no strict 'refs'; *$meth = sub {}; } else { # The load might just have failed because the filename was too # long for some old SVR3 systems which treat long names as errors. # If we can succesfully truncate a long name then it's worth a go. # There is a slight risk that we could pick up the wrong file here # but autosplit should have warned about that when splitting. if( $filename =~ s/(\w{12,})\.al$/substr( $1, 0, 11 ) . ".al"/e ) { eval { local $SIG{__DIE__} = sub{ }; local $SIG{__WARN__} = sub{ }; require $filename }; } } } unless( $@ ) { $@ = $save; unshift( @_, $self ) if( $self ); goto &$meth; } $@ = $save; } if( $self && exists( $self->{sth} ) ) { # e.g. $sth->pg_server_prepare => $self->{sth}->{pg_server_prepare} if( CORE::exists( $self->{sth}->{ $meth } ) ) { $self->{sth}->{ $meth } = shift( @_ ) if( scalar( @_ ) ); return( $self->{sth}->{ $meth } ); } if( !$self->executed() ) { $self->execute() || return( $self->error( $self->{sth}->errstr() ) ); } # $self->_cleanup(); # print( STDERR "Calling DBI method $meth with sth '$self->{sth}' arguments: '", join( "', '", @_ ), "'\n" ) if( $DEBUG ); # *{ "${class}\::$meth" } = sub{ return( shift->{ 'sth' }->$meth( @_ ) ); }; return( $self->{sth}->$meth( @_ ) ); } # e.g. $dbh->pg_notifies elsif( $self && ( ( $self->{dbh} && $self->{dbh}->can( $meth ) ) || defined( &{ "DBI::db::" . $meth } ) ) ) { return( $self->{dbh}->$meth( @_ ) ); } # e.g. $dbh->pg_enable_utf8 becomes $self->{dbh}->{pg_enable_utf8] elsif( $self && $self->{dbh} && CORE::exists( $self->{dbh}->{ $meth } ) ) { $self->{dbh}->{ $meth } = shift( @_ ) if( scalar( @_ ) ); return( $self->{dbh}->{ $meth } ); } elsif( defined( &{ "DBI::" . $meth } ) ) { my $h = &{ "DBI::" . $meth }( @_ ); if( defined( $h ) ) { bless( $h, $class ); return( $h ); } else { return; } } my $what = $self ? $self : $class; return( $what->error( "${class}::AUTOLOAD: Not defined in $class and not autoloadable (last try $meth)" ) ); } # NOTE: DESTROY DESTROY { my $self = shift( @_ ); my $class = ref( $self ) || $self; if( $self->{sth} ) { print( STDERR "DESTROY(): Terminating sth '$self' for query:\n$self->{query}\n" ) if( $DEBUG ); $self->{sth}->finish(); } elsif( $self->{dbh} && $class =~ /^AI\:\:DB(?:\:\:(?:Postgres|Mysql|SQLite))?$/ ) { local( $SIG{__WARN__} ) = sub { }; # $self->{ 'dbh' }->disconnect(); if( $DEBUG ) { my( $pack, $file, $line, $sub ) = ( caller( 0 ) )[ 0, 1, 2, 3 ]; my( $pack2, $file2, $line2, $sub2 ) = ( caller( 1 ) ) [ 0, 1, 2, 3 ]; 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" ); } $self->disconnect(); } my $locks = $self->{_locks}; if( $locks && $self->_is_array( $locks ) ) { foreach my $name ( @$locks ) { $self->unlock( $name ); } } } END { # foreach my $dbh ( @DBH ) # { # $dbh->disconnect(); # } }; # NOTE: package DB::Object::Operator package DB::Object::Operator; BEGIN { use strict; use parent qw( Module::Generic ); }; sub init { my $self = shift( @_ ); $self->{operator} = undef; $self->{query_object} = undef; $self->{value} = []; $self->{_init_strict_use_sub} = 1; $self->SUPER::init( @_ ) || return( $self->pass_error ); return( $self ); } sub operator { return( '' ); } sub query_object { return( shift->_set_get_object_without_init( 'query_object', 'DB::Object::Query', @_ ) ); } # sub value { return( wantarray() ? @{$_[0]->{value}} : $_[0]->{value} ); } # wantlist option instructs this method to return a list in list context instead of the default array object sub value { return( shift->_set_get_array_as_object( { field => 'value', wantlist => 1 }, @_ ) ); } # For arguments that have a query object like statements, we coy their binded_types and binded_values to our current query object. # This is used by ALL, ANY and IN sub _copy_binded_elements { my $self = shift( @_ ); if( my $qo = $self->query_object ) { $self->value->foreach(sub { my $this = shift( @_ ); my $q; if( $self->_is_a( $this => 'DB::Object::Statement' ) && ( $q = $this->query_object ) ) { $qo->binded_types->push( $q->binded_types->list ); $qo->binded_values->push( $q->binded_values->list ); } }); } return( $self ); } # Ref: # # NOTE: package DB::Object::ALL package DB::Object::ALL; BEGIN { use strict; use warnings; use parent -norequire, qw( DB::Object::Operator ); use Scalar::Util (); use overload ( '""' => 'as_string', 'bool' => sub{1}, '==' => sub{ &_opt_overload( @_, '==' ) }, '!=' => sub{ &_opt_overload( @_, '!=' ) }, fallback => 1, ); }; sub init { my $self = shift( @_ ); $self->SUPER::init( @_ ) || return( $self->pass_error ); # $self->_copy_binded_elements || return( $self->pass_error ); return( $self ); } sub as_string { my $self = shift( @_ ); my $vals = $self->value; my @list = (); foreach my $elem ( @$vals ) { next unless( defined( $elem ) ); if( Scalar::Util::blessed( $elem ) && $elem->isa( 'DB::Object::Statement' ) ) { push( @list, $elem->as_string ); } else { push( @list, $elem ); } } local $" = ','; my $sql = "ALL (@list)"; return( $sql ); } sub operator { return( 'ALL' ); } sub _opt_overload { my( $self, $val, $swap, $op ) = @_; my $map = { '!=' => '!= ', '<>' => '!= ', '==' => '= ', }; my $not = $map->{ $op }; if( !defined( $not ) ) { warn( "Unknown operator '${op}'. Falling back to '='." ); $not = $map->{ '==' }; } my $in = $self->as_string; my $placeholder_re = $self->query_object->database_object->_placeholder_regexp; my $lval = ( Scalar::Util::blessed( $val ) && $val->isa( 'DB::Object::Fields::Field' ) ) ? $val->name : ( $val =~ /^$placeholder_re$/ || $self->_is_number( $val ) ) ? $val : qq{'${val}'}; return( DB::Object::Expression->new( "${lval} ${not}${in}" ) ); } # NOTE: package DB::Object::AND package DB::Object::AND; BEGIN { use strict; use parent -norequire, qw( DB::Object::Operator ); }; sub operator { return( 'AND' ); } # Ref: # # NOTE: package DB::Object::ANY package DB::Object::ANY; BEGIN { use strict; use warnings; use parent -norequire, qw( DB::Object::Operator ); use Scalar::Util (); use overload ( '""' => 'as_string', 'bool' => sub{1}, '==' => sub{ &_opt_overload( @_, '==' ) }, '!=' => sub{ &_opt_overload( @_, '!=' ) }, fallback => 1, ); }; sub init { my $self = shift( @_ ); $self->SUPER::init( @_ ) || return( $self->pass_error ); # $self->_copy_binded_elements || return( $self->pass_error ); return( $self ); } sub as_string { my $self = shift( @_ ); my $vals = $self->value; my @list = (); foreach my $elem ( @$vals ) { next unless( defined( $elem ) ); if( Scalar::Util::blessed( $elem ) && $elem->isa( 'DB::Object::Statement' ) ) { push( @list, $elem->as_string ); } else { push( @list, $elem ); } } local $" = ','; my $sql = "ANY (@list)"; return( $sql ); } sub operator { return( 'ANY' ); } sub _opt_overload { my( $self, $val, $swap, $op ) = @_; my $map = { '!=' => '!= ', '==' => '= ', }; my $not = $map->{ $op }; if( !defined( $not ) ) { warn( "Unknown operator '${op}'. Falling back to '='." ); $not = $map->{ '==' }; } my $in = $self->as_string; my $placeholder_re = $self->query_object->database_object->_placeholder_regexp; my $lval = ( Scalar::Util::blessed( $val ) && $val->isa( 'DB::Object::Fields::Field' ) ) ? $val->name : ( $val =~ /^$placeholder_re$/ || $self->_is_number( $val ) ) ? $val : qq{'${val}'}; return( DB::Object::Expression->new( "${lval} ${not}${in}" ) ); } # NOTE: package DB::Object::Expression package DB::Object::Expression; BEGIN { use strict; use warnings; use overload ( '""' => 'as_string', 'bool' => sub{1}, fallback => 1, ); }; sub new { my $that = shift( @_ ); my $val = ( scalar( @_ ) == 1 && ref( $_[0] ) eq 'ARRAY' ) ? [ @{$_[0]} ] : [ @_ ]; return( bless( { value => $val } => ( ref( $that ) || $that ) ) ); } sub as_string { my $self = shift( @_ ); my $vals = $self->components; return( join( ' ', @$vals ) ); } sub components { return( wantarray() ? @{$_[0]->{value}} : $_[0]->{value} ); } # Ref: # # # # # NOTE: package DB::Object::IN package DB::Object::IN; BEGIN { use strict; use warnings; use parent -norequire, qw( DB::Object::Operator ); use Scalar::Util (); use overload ( '""' => 'as_string', 'bool' => sub{1}, '==' => sub{ &_opt_overload( @_, '==' ) }, '!=' => sub{ &_opt_overload( @_, '!=' ) }, fallback => 1, ); }; sub init { my $self = shift( @_ ); $self->SUPER::init( @_ ) || return( $self->pass_error ); # $self->_copy_binded_elements || return( $self->pass_error ); return( $self ); } sub as_string { my $self = shift( @_ ); my $vals = $self->value; my @list = (); foreach my $elem ( @$vals ) { next unless( defined( $elem ) ); if( Scalar::Util::blessed( $elem ) && $elem->isa( 'DB::Object::Statement' ) ) { push( @list, $elem->as_string ); } else { push( @list, $elem ); } } local $" = ','; my $sql = "IN (@list)"; return( $sql ); } sub operator { return( 'IN' ); } sub _opt_overload { my( $self, $val, $swap, $op ) = @_; my $map = { '!=' => 'NOT ', '<>' => 'NOT ', '==' => '', }; my $not = $map->{ $op } // ''; my $in = $self->as_string; my $placeholder_re = $self->query_object->database_object->_placeholder_regexp; my $lval = ( Scalar::Util::blessed( $val ) && $val->isa( 'DB::Object::Fields::Field' ) ) ? $val->name : ( $val =~ /^$placeholder_re$/ || $self->_is_number( $val ) ) ? $val : qq{'${val}'}; return( DB::Object::Expression->new( "${lval} ${not}${in}" ) ); } # Ref: # # # # # NOTE: package DB::Object::LIKE # TODO: Finish the class DB::Object::LIKE package DB::Object::LIKE; BEGIN { use strict; use warnings; use parent -norequire, qw( DB::Object::Operator ); use Scalar::Util (); use overload ( '""' => 'as_string', 'bool' => sub{1}, '==' => sub{ &_opt_overload( @_, '==' ) }, '!=' => sub{ &_opt_overload( @_, '!=' ) }, fallback => 1, ); }; sub init { my $self = shift( @_ ); $self->SUPER::init( @_ ) || return( $self->pass_error ); # $self->_copy_binded_elements || return( $self->pass_error ); return( $self ); } sub as_string { my $self = shift( @_ ); my $vals = $self->value; # Parameters can be given as an array for convenience, and they will concatenated my $sql = "LIKE '" . join( '', @$vals ) . "'"; return( $sql ); } sub _opt_overload { my( $self, $val, $swap, $op ) = @_; my $map = { '!=' => 'NOT ', '<>' => 'NOT ', '==' => '', }; my $not = $map->{ $op } // ''; my $like = $self->as_string; my $placeholder_re = $self->query_object->database_object->_placeholder_regexp; my $lval = ( Scalar::Util::blessed( $val ) && $val->isa( 'DB::Object::Fields::Field' ) ) ? $val->name : ( $val =~ /^$placeholder_re$/ || $self->_is_number( $val ) ) ? $val : qq{'${val}'}; return( DB::Object::Expression->new( "${lval} ${not}${like}" ) ); } # NOTE: package DB::Object::NOT package DB::Object::NOT; BEGIN { use strict; use parent -norequire, qw( DB::Object::Operator ); }; sub operator { return( 'NOT' ); } # NOTE: package DB::Object::OR package DB::Object::OR; BEGIN { use strict; use parent -norequire, qw( DB::Object::Operator ); }; sub operator { return( 'OR' ); } 1; # NOTE: POD __END__ =encoding utf8 =head1 NAME DB::Object - SQL API =head1 SYNOPSIS use DB::Object; my $dbh = DB::Object->connect({ driver => 'Pg', conf_file => 'db-settings.json', database => 'webstore', host => 'localhost', login => 'store-admin', schema => 'auth', debug => 3, }) || bailout( "Unable to connect to sql server on host localhost: ", DB::Object->error ); # Legacy regular query my $sth = $dbh->prepare( "SELECT login,name FROM login WHERE login='jack'" ) || die( $dbh->errstr() ); $sth->execute() || die( $sth->errstr() ); my $ref = $sth->fetchrow_hashref(); $sth->finish(); # Get a list of databases; my @databases = $dbh->databases; # Doesn't exist? Create it: my $dbh2 = $dbh->create_db( 'webstore' ); # Load some sql into it my $rv = $dbh2->do( $sql ) || die( $dbh->error ); # Check a table exists $dbh->table_exists( 'customers' ) || die( "Cannot find the customers table!\n" ); # Get list of tables, as array reference: my $tables = $dbh->tables; my $cust = $dbh->customers || die( "Cannot get customers object." ); $cust->where( email => 'john@example.org' ); my $str = $cust->delete->as_string; # Becomes: DELETE FROM customers WHERE email='john\@example.org' # Do some insert with transaction $dbh->begin_work; # Making some other inserts and updates here... my $cust_sth_ins = $cust->insert( first_name => 'Paul', last_name => 'Goldman', email => 'paul@example.org', active => 0, ) || do { # Rollback everything since the begin_work $dbh->rollback; die( "Error while create query to add data to table customers: " . $cust->error ); }; $result = $cust_sth_ins->as_string; # INSERT INTO customers (first_name, last_name, email, active) VALUES('Paul', 'Goldman', 'paul\@example.org', '0') $dbh->commit; # Get the last used insert id my $id = $dbh->last_insert_id(); $cust->where( email => 'john@example.org' ); $cust->order( 'last_name' ); $cust->having( email => qr/\@example/ ); $cust->limit( 10 ); my $cust_sth_sel = $cust->select || die( "An error occurred while creating a query to select data frm table customers: " . $cust->error ); # Becomes: # 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 $cust->reset; $cust->where( email => 'john@example.org' ); my $cust_sth_upd = $cust->update( active => 0 ) # Would become: # UPDATE ONLY customers SET active='0' WHERE email='john\@example.org' # Lets' dump the result of our query # First to STDERR $login->where( "login='jack'" ); $login->select->dump(); # Now dump the result to a file $login->select->dump( "my_file.txt" ); Using L $cust->where( $dbh->OR( $cust->fo->email == 'john@example.org', $cust->fo->id == 2 ) ); my $ref = $cust->select->fetchrow_hashref; Doing some left join my $geo_tbl = $dbh->geoip || return( $self->error( "Unable to get the database object \"geoip\"." ) ); my $name_tbl = $dbh->geoname || return( $self->error( "Unable to get the database object \"geoname\"." ) ); $geo_tbl->as( 'i' ); $name_tbl->as( 'l' ); $geo_tbl->where( "INET '?'" << $geo_tbl->fo->network ); $geo_tbl->alias( id => 'ip_id' ); $name_tbl->alias( country_iso_code => 'code' ); my $sth = $geo_tbl->select->join( $name_tbl, $geo_tbl->fo->geoname_id == $name_tbl->fo->geoname_id ); # SELECT # -- tables fields # FROM # geoip AS i # LEFT JOIN geoname AS l ON i.geoname_id = l.geoname_id # WHERE # INET '?' << i.network Using a promise (L) to execute an asynchronous query: my $sth = $dbh->prepare( "SELECT some_slow_function(?)" ) || die( $dbh->error ); my $p = $sth->promise(10)->then(sub { my $st = shift( @_ ); my $ref = $st->fetchrow_hashref; my $obj = My::Module->new( %$ref ); })->catch(sub { $log->warn( "Failed to execute query: ", @_ ); }); # Do other regular processing here # Get the My::Module object my( $obj ) = await( $p ); Sometimes, having placeholders in expression makes it difficult to work, so you can use placeholder objects to make it work: my $P = $dbh->placeholder( type => 'inet' ); $orders_tbl->where( $dbh->OR( $orders_tbl->fo->ip_addr == "inet $P", "inet $P" << $orders_tbl->fo->ip_addr ) ); my $order_ip_sth = $orders_tbl->select( 'id' ) || fail( "An error has occurred while trying to create a select by ip query for table orders: " . $orders_tbl->error ); # SELECT id FROM orders WHERE ip_addr = inet ? OR inet ? << ip_addr Be careful though, when using L, not to do this: my $tbl = $dbh->some_table; $tbl->where( $tbl->fo->some_field => '?', $tbl->fo->other_field => '?' ); my $sth = $tbl->select || die( $tbl->error ); Because the L are overloaded, instead do this: my $tbl = $dbh->some_table; $tbl->where( $tbl->fo->some_field == '?', $tbl->fo->other_field == '?' ); my $sth = $tbl->select || die( $tbl->error ); Accessing a property in a C or C field: my $tbl = $dbh->some_table; $tbl->where( metadata => { is_system => 'true' } ); my $sth = $tbl->select; say $sth->as_string; # SELECT * FROM some_table WHERE metadata->>is_system = 'true'; In future release, other operators than C<=> will be implemented for C and C fields. =head1 VERSION v1.7.0 =head1 DESCRIPTION L is a SQL API much alike C, but with the added benefits that it formats queries in a simple object oriented, chaining way. So why use a private module instead of using that great C package? At first, I started to inherit from C to conform to C perl manual page and to general perl coding guidlines. It became very quickly a real hassle. Barely impossible to inherit, difficulty to handle error, too much dependent from an API that changes its behaviour with new versions. In short, I wanted a better, more accurate control over the SQL connection and an easy way to format sql statement using an object oriented approach. So, L acts as a convenient, modifiable wrapper that provides the programmer with an intuitive, user-friendly, object oriented and hassle free interface. However, if you use the power of this interface to prepare queries conveniently, you should cache the resulting statement handler object, because there is an obvious real cost penalty in preparing queries and they absolutely do not need to be prepared each time. So you can do something like: my $sth; unless( $sth = $dbh->cache_query_get( 'some_arbitrary_identifier' ) ) { # prepare the query my $tbl = $dbh->some_table || die( $dbh->error ); $tbl->where( id => '?' ); $sth = $tbl->select || die( $tbl->error ); $dbh->cache_query_set( some_arbitrary_identifier => $sth ); } $sth->exec(12) || die( $sth->error ); my $ref = $sth->fetchrow_hashref; This will provide you with the convenience and power of L while keeping execution fast. =head1 CONSTRUCTOR =head2 new Create a new instance of L. Nothing much to say. =head2 connect Provided with a C, C, C, C:[C], C, C, and optional hash or hash reference of parameters and this will issue a, possibly cached, database connection and return the resulting database handler. Create a new instance of L, but also attempts a connection to SQL server. It can take either an array of value in the order database name, login, password, host, driver and optionally schema, or it can take a has or hash reference. The hash or hash reference attributes are as follow. Note that if you provide connection options that are not among the followings, this will return an error. =over 4 =item * C Defaults to true. If true, this will instruct L to use L instead of just L Beware that using cached connections can have some drawbacks, such as if you open a cached connection, enters into a transaction using L, then somewhere else in your code a call to a cached connection using the same parameters, which L will provide, but will reset the database handler parameters, including the C that will have been temporarily set to false when you called L, and then you close your transaction by calling L or L, but it will trigger an error, because C will have been reset on this cached connection to a true value. L and L require that C be disabled, which L normally do. Thus, if you want to avoid using a cached connection, set this to false. More on this issue at L =item * C or I The database name you wish to connect to =item * C or I The login used to access that database =item * C or I The password that goes along =item * C or I The server, that is hostname of the machine serving a SQL server. =item * C or I The port to connect to =item * C or I The driver you want to use. It needs to be of the same type than the server you want to connect to. If you are connecting to a MySQL server, you would use C, if you would connecto to an Oracle server, you would use C. You need to make sure that those driver are properly installed in the system before attempting to connect. To install the required driver, you could start with the command line: perl -MCPAN -e shell which will provide you a special shell to install modules in a convenient way. =item * C or I The schema to use to access the tables. Currently only used by PostgreSQL =item * C This takes a hash reference and contains the standard C options such as I, I, I, etc =item * C or C This is used to specify a json connection configuration file. It can also provided via the environment variable I. It has the following structure: { "database": "some_database", "host": "db.example.com", "login": "sql_joe", "passwd": "some password", "driver": "Pg", "schema": "warehouse", "opt": { "RaiseError": false, "PrintError": true, "AutoCommit": true } } Alternatively, it can contain connections parameters for multiple databases and drivers, such as: { "databases": [ { "database": "some_database", "host": "db.example.com", "port": 5432, "login": "sql_joe", "passwd": "some password", "driver": "Pg", "schema": "warehouse", "opt": { "RaiseError": false, "PrintError": true, "AutoCommit": true } }, { "database": "other_database", "host": "db.example2.com", "login": "sql_bob", "passwd": "other password", "driver": "mysql", }, { "database": "/path/to/my/database.sqlite", "driver": "SQLite", } ] } =item * C or I This is used to specify an uri to contain all the connection parameters for one database connection. It can also provided via the environment variable I. For example: http://db.example.com:5432?database=some_database&login=sql_joe&passwd=some%020password&driver=Pg&schema=warehouse&&opt=%7B%22RaiseError%22%3A+false%2C+%22PrintError%22%3Atrue%2C+%22AutoCommit%22%3Atrue%7D Here the I parameter is passed as a json string, for example: {"RaiseError": false, "PrintError":true, "AutoCommit":true} =back =head1 METHODS =head2 alias See L =head2 allow_bulk_delete Sets/gets the boolean value for whether to allow unsafe bulk delete. This means query without any C clause. Default is false. =head2 allow_bulk_update Sets/gets the boolean value for whether to allow unsafe bulk update. This means query without any C clause. Default is false. =head2 AND Takes any arguments and wrap them into a C clause. $tbl->where( $dbh->AND( $tbl->fo->id == ?, $tbl->fo->frequency >= .30 ) ); =head2 as_string See L =head2 auto_convert_datetime_to_object Sets or gets the boolean value. If true, then this api will automatically transcode datetime value into their equivalent L object. Default is false. =head2 auto_decode_json Sets or gets the boolean value. If true, then this api will automatically transcode json data into perl hash reference. Default is true. =head2 avoid See L =head2 attribute Sets or get the value of database connection parameters. If only one argument is provided, returns its value. If multiple arguments in a form of pair => value are provided, it sets the corresponding database parameters. The authorised parameters are: =over 4 =item I Is read-only. =item I Is read-only. =item I Can be changed. =item I Can be changed. =item I Is read-only. =item I Can be changed. =item I Is read-only. =item I Can be changed. =item I Can be changed. =item I Is read-only. =item I Is read-only. =item I Is read-only. =item I Is read-only. =item I Can be changed. =item I Can be changed. =item I Can be changed. =item I Is read-only. =item I Can be changed. =item I Can be changed. =item I Is read-only. =item I Is read-only. =item I Is read-only. =item I Is read-only. =item I Is read-only. =item I Is read-only. =item I Can be changed. =item I Can be changed. =item I Is read-only. =item I Can be changed. =item I Can be changed. =item I Is read-only. =item I Is read-only. =item I Is read-only. =item I Can be changed. =item I Is read-only. =item I Is read-only. =item I Can be changed. =item I Can be changed. =item I Can be changed. =item I Can be changed. =item I Is read-only. =item I Can be changed. =back =head2 available_drivers Return the list of available drivers. =head2 base_class Returns the base class. =head2 bind If no values to bind to the underlying query is provided, L simply activate the bind value feature. If values are provided, they are allocated to the statement object and will be applied when the query will be executed. Example: $dbh->bind() # or $dbh->bind->where( "something" ) # or $dbh->bind->select->fetchrow_hashref() # and then later $dbh->bind( 'thingy' )->select->fetchrow_hashref() =head2 cache Activate caching. $tbl->cache->select->fetchrow_hashref(); =head2 cache_connections Sets/get the cached database connection. =head2 cache_dir Sets or gets the directory on the file system used for caching data. =head2 cache_query_get my $sth; unless( $sth = $dbh->cache_query_get( 'some_arbitrary_identifier' ) ) { # prepare the query my $tbl = $dbh->some_table || die( $dbh->error ); $tbl->where( id => '?' ); $sth = $tbl->select || die( $tbl->error ); $dbh->cache_query_set( some_arbitrary_identifier => $sth ); } $sth->exec(12) || die( $sth->error ); my $ref = $sth->fetchrow_hashref; Provided with a unique name, and this will return a cached statement object if it exists already, otherwise it will return undef =head2 cache_query_set my $sth; unless( $sth = $dbh->cache_query_get( 'some_arbitrary_identifier' ) ) { # prepare the query my $tbl = $dbh->some_table || die( $dbh->error ); $tbl->where( id => '?' ); $sth = $tbl->select || die( $tbl->error ); $dbh->cache_query_set( some_arbitrary_identifier => $sth ); } $sth->exec(12) || die( $sth->error ); my $ref = $sth->fetchrow_hashref; Provided with a unique name and a statement object (L), and this will cache it. What this does simply is store the statement object in a global C<$QUERIES_CACHE> hash reference of identifier-statement object pairs. It returns the statement object cached. =head2 cache_table Sets or gets a boolean value whether to cache the table fields object. When this is enabled, the second time a database table is accessed, it will retrieve its field objects from the cache rather than recreating them after reading the structure from the database. This is much faster. By default, this is set to false. This can be specified in the configuration file passed when instantiating a new C object with the property C =head2 cache_table_fields my $all_dbs = $dbh->cache_table_fields; my $all_tables = $dbh->cache_table_fields( database => $some_database ); my $all_fields = $dbh->cache_table_fields( database => $some_database, table => $some_table, ); $dbh->cache_table_fields( database => $some_database, table => $some_table, fields => $some_hash_reference, ); Sets or gets the hash reference of database table field name to their L. If no parameter is provided, it will return the entire cache for all databases for a given driver. If only a database name is provided, it will return the cache hash reference for all the tables in the given database. If a database and a table name is provided, this will return an hash reference of field name to their L. If a database and a table name and an hash reference of field names to their L is provided, it will set this hash as the cache for the given database and table. =head2 cache_tables Sets or gets the L object. =head2 check_driver Check that the driver set in I<$SQL_DRIVER> in ~/etc/common.cfg is indeed available. It does this by calling L. =head2 connect This will attempt a database server connection. It called L to get the necessary connection parameters, which is superseded in each driver package. Then, it will call L to get the right parameters for connection. It will also call L to get some driver specific default options unless the previous call to _check_connect_param returned an has with a property I. It will then set the following current object properties: L, L, L, L, L, L, L, L, L Unless specified in the connection options retrieved with L, it sets some basic default value: =over 4 =item I 1 =item I 0 =item I 0 =back Finally it tries to connect by calling the, possibly superseded, method L It instantiate a L object to cache database tables and return the current object. =head2 constant_queries_cache Returns the global value for C<$CONSTANT_QUERIES_CACHE> This is used by L and L =head2 constant_queries_cache_get Provided with some hash reference with properties C, C and C that are together used as a key in the cache and this will use an existing entry in the cache if available. =head2 constant_queries_cache_set Provided with some hash reference with properties C, C and C that are together used as a key in the cache and C and this will set an entry in the cache. it returns the hash reference initially provided. =head2 constant_to_datatype Provided with a data type constant value and this returns its equivalent data type as a string in upper case. This constant is set by the driver, or by default by L. For example C is C<12> and its data type is C See also L =head2 copy Provided with either a reference to an hash or an hash of key => value pairs, L will first execute a select statement on the table object, then fetch the row of data, then replace the key-value pair in the result by the ones provided, and finally will perform an insert. Return false if no data to copy were provided, otherwise it always returns true. =head2 create_db This is a method that must be implemented by the driver package. =head2 create_table This is a method that must be implemented by the driver package. =head2 data_sources Given an optional list of options as hash, this return the data source of the database handler. =head2 data_type Given a reference to an array or an array of data type, L will check their availability in the database driver. If nothing found, it return an empty list in list context, or undef in scalar context. If something was found, it returns a hash in list context or a reference to a hash in list context. =head2 database Return the name of the current database. =head2 databases This returns the list of available databases. This is a method that must be implemented by the driver package. =head2 datatype_dict Returns an hash reference of each data type with their equivalent C, regular expression (C), constant C and C name. Each data type is an hash with the following properties for each type: C, C, C, C The data returned is dependent on each driver. =head2 datatype_to_constant my $type = $dbh->datatype_to_constant( 'varchar' ); # 12 # Below achieves the same result use DBI ':sql_types'; say SQL_VARCHAR; # 12 Provided with a data type as a string and this returns its equivalent driver value if any, or by default the one of set by L. The data type provided is case insensitive. If no matching data type exists, it returns C in scalar context, or an empty list in list context. As pointed out by L: "just because the DBI defines a named constant for a given data type doesn't mean that drivers will support that data type." See also L =head2 datatypes my $types = $dbh->datatypes; Returns an hash reference of data types to their respective values. If the driver has its own, it will return the driver's constants, otherwise, this will return an hash reference of L. As pointed out by L: "just because the DBI defines a named constant for a given data type doesn't mean that drivers will support that data type." =head2 delete See L =head2 disconnect Disconnect from database. Returns the return code. my $rc = $dbh->disconnect; =head2 do Provided with a string representing a sql query, some hash reference of attributes and some optional values to bind and this will execute the query and return the statement handler. The attributes list will be used to B the query and the bind values will be used when executing the query. Example: $rc = $dbh->do( $statement ) || die( $dbh->errstr ); $rc = $dbh->do( $statement, \%attr ) || die( $dbh->errstr ); $rv = $dbh->do( $statement, \%attr, @bind_values ) || die( $dbh->errstr ); my $rows_deleted = $dbh->do( q{ DELETE FROM table WHERE status = ? }, undef(), 'DONE' ) || die( $dbh->errstr ); =head2 driver Return the name of the driver for the current object. =head2 enhance Toggle the enhance mode on/off. When on, the functions L and L will be used on date/time field to translate from and to unix time seamlessly. =head2 err Get the currently set error. =head2 errno Is just an alias for L. =head2 errmesg Is just an alias for L. =head2 errstr Get the currently set error string. =head2 FALSE This return the keyword C to be used in queries. =head2 fatal Provided a boolean value and this toggles fatal mode on/off. =head2 format_statement See L =head2 format_update See L =head2 from_unixtime See L =head2 get_sql_type Provided with a sql type, irrespective of the character case, and this will return the driver equivalent constant value. =head2 group See L =head2 host Sets or gets the C property for this database object. =head2 insert See L =head2 last_insert_id Get the id of the primary key from the last insert. =head2 LIKE Returns a new L object, passing it whatever arguments were provided. =head2 limit See L =head2 local See L =head2 lock This method must be implemented by the driver package. =head2 login Sets or gets the C property for this database object. =head2 no_bind When invoked, L will change any preparation made so far for caching the query with bind parameters, and instead substitute the value in lieu of the question mark placeholder. =head2 no_cache Disable caching of queries. =head2 NOT Returns a new L object, passing it whatever arguments were provided. =head2 NULL Returns a C string to be used in queries. =head2 on_conflict See L =head2 OR Returns a new L object, passing it whatever arguments were provided. =head2 order See L =head2 P Returns a L object, passing it whatever arguments was provided. =head2 param If only a single parameter is provided, its value is return. If a list of parameters is provided they are set accordingly using the C sql command. Supported parameters are: =over 4 =item AUTOCOMMIT =item INSERT_ID =item LAST_INSERT_ID =item SQL_AUTO_IS_NULL =item SQL_BIG_SELECTS =item SQL_BIG_TABLES =item SQL_BUFFER_RESULT =item SQL_LOG_OFF =item SQL_LOW_PRIORITY_UPDATES =item SQL_MAX_JOIN_SIZE =item SQL_SAFE_MODE =item SQL_SELECT_LIMIT =item SQL_LOG_UPDATE =item TIMESTAMP =back If unsupported parameters are provided, they are considered to be private and not passed to the database handler. It then execute the query and return L in case of error. Otherwise, it returns the current object used to call the method. =head2 passwd Sets or gets the C property for this database object. =head2 ping Evals a SELECT 1 statement and returns 0 if errors occurred or the return value. =head2 ping_select Will prepare and execute a simple C