DB::Object::Tables - Database Table Object
v0.6.0
This is the table object package used to represent and manipulate table objects.
my $tbl = DB::Object::Tables->new( 'my_table' ) || die( DB::Object::Tables->error );
Creates a new DB::Object::Tables object.
A table name may be provided as first argument.
It may also take an hash of arguments, that also are method of the same name.
It will call "structure" to get the table structure from database and returns an error if it fails.
Possible arguments are:
Toggles debug mode on/off
This is a convenient wrapper around "alias" in DB::Object::Query
It takes a column name to alias hash and sets those aliases for the following query.
Get/set alias for table fields in SELECT queries. The hash provided thus contain a list of field => alias pairs.
Provided with an array or array reference of specification for the alter and this will prepare the proper query.
The specification array or array reference will be joined with a comma
If called in void context, the resulting statement handler will be executed immediately.
This returns the resulting statement handler.
Provided with a table alias and this will call "table_alias" in DB::Object::Query passing it whatever arguments were provided.
Takes a list of array reference of column to avoid in the next query.
This is a convenient wrapper around "avoid" in DB::Object::Query
Returns an array object of the table columns.
This information is provided by "fields", which is in turn provided by "structure"
Sets the query object constant for statement caching and return our current object.
This must be implemented by the driver package, so check "create" in DB::Object::Mysql::Tables, "create" in DB::Object::Postgres::Tables or "create" in DB::Object::SQLite::Tables
This must be implemented by the driver package, so check "create_info" in DB::Object::Mysql::Tables, "create_info" in DB::Object::Postgres::Tables or "create_info" in DB::Object::SQLite::Tables
Returns the name of the current database by calling "database" in DB::Object
Returns the database object (DB::Object)
Returns the database handler (DBI)
This calls "structure" which may return cached data.
Returns an hash in list context and an hash reference in scalar representing column to its default values pairs.
If nothing is found, it returns an empty list in list context and "undef" in perlfunc in scalar context.
"delete" will format a delete query based on previously set parameters, such as "where".
"delete" will refuse to execute a query without a where condition. To achieve this, one must prepare the delete query on his/her own by using the "do" method and passing the sql query directly.
$tbl->where( login => 'jack' ); $tbl->limit(1); my $rows_affected = $tbl->delete(); # or passing the where condition directly to delete my $sth = $tbl->delete( login => 'jack' );
This will prepare the query to drop the current table.
In void context, this will execute the resulting statement handler.
It returns the resulting statement handler
This must be implemented by the driver package, so check "exists" in DB::Object::Mysql::Tables, "exists" in DB::Object::Postgres::Tables or "exists" in DB::Object::SQLite::Tables
Returns an hash in list context and an hash reference in scalar representing column to its order (integer) in the table pairs.
my $tbl = $dbh->user || die( "No table \"user\" found in database\n" ); # get the field object for "name" my $name = $tbl->fields_object->name # Do something with it my $expr = $name == 'joe'; # Resulting in an DB::Object::Fields::Field::Overloaded object
This returns the cached object if there is one.
This will dynamically create a package based on the database and table name. For example a database Foo and a table Bar would result in the following dynamically created package: DB::Object::Tables::Foo::Bar
Foo
Bar
DB::Object::Tables::Foo::Bar
This new package will inherit from DB::Object::Fields, which enable the dynamic loading of column object using AUTOLOAD
AUTOLOAD
This will instantiate an object from this newly created package, cache it and return it.
This is a convenient shortcut for "fields_object"
my $tbl = $dbh->user || die( "No table \"user\" found in database\n" ); # get the field object for "name" my $name = $tbl->fo->name
This is a convenient wrapper around "format_statement" in DB::Object::Query
Format the sql statement for queries of types select, delete and insert
select
delete
insert
In list context, it returns 2 strings: one comma-separated list of fields and one comma-separated list of values. In scalar context, it only returns a comma-separated string of fields.
This is a convenient wrapper around "format_update" in DB::Object::Query
Formats update query based on the following arguments provided:
An array of key-value pairs to be used in the update query. This array can be provided as the prime argument as a reference to an array, an array, or as the data element of a hash or a reference to a hash provided.
Why an array if eventually we build a list of key-value pair? Because the order of the fields may be important, and if the key-value pair list is provided, "format_update" honors the order in which the fields are provided.
"format_update" will then iterate through each field-value pair, and perform some work:
If the field being reviewed was provided to from_unixtime, then "format_update" will enclose it in the function FROM_UNIXTIME() as in:
FROM_UNIXTIME(field_name)
If the the given value is a reference to a scalar, it will be used as-is, ie. it will not be enclosed in quotes or anything. This is useful if you want to control which function to use around that field.
If the given value is another field or looks like a function having parenthesis, or if the value is a question mark, the value will be used as-is.
If "bind" in DB::Object is off, the value will be escaped and the pair field='value' created.
If the field is a SET data type and the value is a number, the value will be used as-is without surrounding single quote.
If "bind" in DB::Object is enabled, a question mark will be used as the value and the original value will be saved as value to bind upon executing the query.
Finally, otherwise the value is escaped and surrounded by single quotes.
"format_update" returns a string representing the comma-separated list of fields that will be used.
Provided with an array or array reference of table columns and this will set the list of fields that are to be treated as unix time and converted accordingly after the sql query is executed.
It returns the list of fields in list context or a reference to an array in scalar context.
This is a convenient wrapper around "group" in DB::Object::Query
This is a convenient wrapper around "insert" in DB::Object::Query
This is a convenient wrapper around "limit" in DB::Object::Query
This is a convenient wrapper around "local" in DB::Object::Query
This must be implemented by the driver package, so check "lock" in DB::Object::Mysql::Tables, "lock" in DB::Object::Postgres::Tables or "lock" in DB::Object::SQLite::Tables
Returns the table name. This is read-only.
Returns an hash in list context and an hash reference in scalar representing column to its default null values pairs.
The SQL ON CONFLICT clause needs to be implemented by the driver and is currently supported only by DB::Object::Postgres and DB::Object::SQLite.
ON CONFLICT
This must be implemented by the driver package, so check "optimize" in DB::Object::Mysql::Tables, "optimize" in DB::Object::Postgres::Tables or "optimize" in DB::Object::SQLite::Tables
This is a convenient wrapper around "order" in DB::Object::Query
Prepares the ORDER BY clause and returns the value of the clause in list context or the ORDER BY clause in full in scalar context, ie. "ORDER BY $clause"
ORDER BY
Based on the prefix level, this will return a string with the database name if prefix is higher than 2, with the schema if the prefix level is higher than 1 and with the table name if the prefix level is higher than 0.
The resulting string is used as prefix to table columns when preparing queries.
Returns true if "prefixed" is higher than 2.
Returns true if "prefixed" is higher than 1.
Returns true if "prefixed" is higher than 0.
Sets or gets the prefix level. 0 being no prefix and 2 implying the use of the database name in prefix.
Returns an hash in list context and an hash reference in scalar representing column to primary keys pairs. If a column has no primary keys, its value would be empty.
Returns the query object (DB::Object::Query)
Reset the query object (DB::Object::Query)
This must be implemented by the driver package, so check "rename" in DB::Object::Mysql::Tables, "rename" in DB::Object::Postgres::Tables or "rename" in DB::Object::SQLite::Tables
This must be implemented by the driver package, so check "repair" in DB::Object::Mysql::Tables, "repair" in DB::Object::Postgres::Tables or "repair" in DB::Object::SQLite::Tables
Just like for the INSERT query, "replace" takes one optional argument representing a DB::Object::Statement SELECT object or a list of field-value pairs.
INSERT
SELECT
If a SELECT statement is provided, it will be used to construct a query of the type of REPLACE INTO mytable SELECT FROM other_table
REPLACE INTO mytable SELECT FROM other_table
Otherwise the query will be REPLACE INTO mytable (fields) VALUES(values)
REPLACE INTO mytable (fields) VALUES(values)
In scalar context, it execute the query and in list context it simply returns the statement handler.
This is used to reset a prepared query to its default values. If a field is a date/time type, its default value will be set to NOW()
It execute an update with the reseted value and return the number of affected rows.
The SQL RETURNING clause needs to be implemented by the driver and is currently supported only by and DB::Object::Postgres (see "returning" in DB::Object::Postgres::Query) and DB::Object::SQLite (see "returning" in DB::Object::SQLite::Query).
RETURNING
Get or set the reverse mode.
Returns the schema name, if any. For example, with PostgreSQL, the default schema name would be public.
public
Given an optional list of fields to fetch, "select" prepares a SELECT query.
If no field was provided, "select" will use default value where appropriate like the NOW() for date/time fields.
NOW()
"select" in DB::Object::Query calls upon "tie" in DB::Object::Query, "where" in DB::Object::Query, "group" in DB::Object::Query, "order" in DB::Object::Query, "limit" in DB::Object::Query, "local" in DB::Object::Query, and possibly more depending on the driver implementation, to build the query.
In scalar context, it execute the query and return it. In list context, it just returns the statement handler.
It toggles sort mode on and consequently disable reverse mode.
This must be implemented by the driver package, so check "stat" in DB::Object::Mysql::Tables, "stat" in DB::Object::Postgres::Tables or "stat" in DB::Object::SQLite::Tables
The implementation is driver specific.
This must be implemented by the driver package, so check "structure" in DB::Object::Mysql::Tables, "structure" in DB::Object::Postgres::Tables or "structure" in DB::Object::SQLite::Tables
This is a convenient wrapper around "tie" in DB::Object::Query
The table type
Returns an hash in list context and an hash reference in scalar representing column to data type.
Returns an hash in list context and an hash reference in scalar representing column to hash that defines the driver constant for this data type:
some_column => { constant => 17, name => 'PG_JSONB', type => 'jsonb' }
This is used to help manage binded value with the right type, or helps when converting an hash into json.
This is a convenient wrapper around "unix_timestamp" in DB::Object::Query
This must be implemented by the driver package, so check "unlock" in DB::Object::Mysql::Tables, "unlock" in DB::Object::Postgres::Tables or "unlock" in DB::Object::SQLite::Tables
Given a list of field-value pairs, "update" prepares a sql update query.
It calls upon "where" in DB::Object::Query and "limit" in DB::Object::Query as previously set.
It returns undef and sets an error if it failed to prepare the update statement. In scalar context, it execute the query. In list context, it simply return the statement handler.
This is a convenient wrapper around "where" in DB::Object::Query
Jacques Deguest <jack@deguest.jp>
DB::Object::Mysql::Tables, DB::Object::Postgres::Tables or DB::Object::SQLite::Tables
Copyright (c) 2019-2021 DEGUEST Pte. Ltd.
You can use, copy, modify and redistribute this package and associated files under the same terms as Perl itself.
To install DB::Object, copy and paste the appropriate command in to your terminal.
cpanm
cpanm DB::Object
CPAN shell
perl -MCPAN -e shell install DB::Object
For more information on module installation, please visit the detailed CPAN module installation guide.