DB-Object
view release on metacpan or search on metacpan
lib/DB/Object/Postgres.pm view on Meta::CPAN
use DB::Object::Postgres;
my $dbh = DB::Object::Postgres->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;
# and more elaborate:
# Ref: https://www.postgresql.org/docs/10/sql-insert.html#SQL-ON-CONFLICT
$login->on_conflict({
# mandatory, can be a constraint name or a field name or array of fields
target => 'on constraint idx_prefs_unique',
action => 'update',
# where => '',
# which fields to update. It can also be more specific by providing a hash ref like fields => { val => 'plop' }
fields => [qw( val )],
});
# would become:
insert into login (..) values(...) on conflict on constraint idx_prefs_unique do update set val = EXCLUDED.val;
# 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\@exa...
$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" );
=head1 VERSION
v1.5.1
=head1 DESCRIPTION
This package inherits from L<DB::Object>, so any method not here, but there you can use.
L<DB::Object::Postgres> is a SQL API much alike L<DBD::Pg>.
So why use a private module instead of using that great L<DBD::Pg> package?
At first, I started to inherit from C<DBI> to conform to C<perlmod> 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 change its behaviour with new versions.
In short, I wanted a better, more accurate control over the SQL connection.
So, L<DB::Object::Postgres> acts as a convenient, modifiable wrapper that provide the
programmer with an intuitive, user-friendly and hassle free interface.
=head1 CONSTRUCTOR
=head2 new
Create a new instance of L<DB::Object::Postgres>. Nothing much to say.
=head2 connect
Provided with a database, login, password, server, driver, and this will attempt a database connection.
Create a new instance of L<DB::Object::Postgres>, but also attempts a connection to SQL server.
You can specify the following parameters:
=over 4
( run in 0.983 second using v1.01-cache-2.11-cpan-0bb4e1dffa6 )