NAME

PDK::DBI::Pg - Moose-based wrapper for PostgreSQL database operations using DBIx::Custom

SYNOPSIS

use PDK::DBI::Pg;

my $db = PDK::DBI::Pg->new(
  host     => 'localhost',
  port     => 5432,
  dbname   => 'mydb',
  user     => 'username',
  password => 'password'
);

my $result = $db->execute("SELECT * FROM users WHERE id = ?", [1])->all;

$db->disconnect;

DESCRIPTION

PDK::DBI::Pg is a Moose-based module that provides a high-level interface for PostgreSQL database operations. It uses DBIx::Custom to handle the underlying database connections and queries, and implements transaction management and connection pooling.

ATTRIBUTES

option

Read-only HashRef for storing database connection options.

METHODS

new(%params)

Constructor for creating a new PDK::DBI::Pg object. Accepts the following parameters:

  • host - Database host

  • port - Database port

  • dbname - Database name

  • user - Database username

  • password - Database password

  • dsn - Data Source Name (optional; automatically generated if not provided)

  • option - HashRef for additional database connection options

clone()

Creates a new instance of the current object, copying connection information (dsn, user, password, and options).

Returns: A new PDK::DBI::Pg object.

batchExecute($params, $sql)

Executes a batch SQL operation.

  • $params - ArrayRef of SQL query parameters

  • $sql - SQL query string

disconnect()

Closes the database connection.

reconnect()

Disconnects and re-establishes the database connection.

INHERITED METHODS

The following methods are inherited from DBIx::Custom and can be called directly on PDK::DBI::Pg objects:

  • select

  • update

  • insert

  • delete

  • execute

  • user

TRANSACTION HANDLING

All database operations (execute, delete, update, insert, batchExecute) are automatically wrapped in transactions. If an error occurs during the operation, the transaction will roll back.

EXAMPLES

Connecting to the database

my $db = PDK::DBI::Pg->new(
  host     => 'localhost',
  port     => 5432,
  dbname   => 'mydb',
  user     => 'username',
  password => 'password'
);

Executing a SELECT query

my $results = $db->execute("SELECT * FROM users WHERE status = ?", ['active'])->all;

Inserting data

$db->insert('users', {name => 'John Doe', email => 'john@example.com'});

Updating data

$db->update('users', {status => 'inactive'}, {id => 1});

Deleting data

$db->delete('users', {id => 1});

Batch execution

my $params = [
  ['John Doe', 'john@example.com'],
  ['Jane Smith', 'jane@example.com']
];
$db->batchExecute($params, "INSERT INTO users (name, email) VALUES (?, ?)");

SEE ALSO

Moose, DBIx::Custom, DBI

AUTHOR

WENWU YAN <968828@gmail.com>

LICENSE AND COPYRIGHT

Copyright (C) 2024 WENWU YAN

This program is free software; you can redistribute it and/or modify it under the terms of either: the GNU General Public License as published by the Free Software Foundation; or the Artistic License.

See http://dev.perl.org/licenses/ for more information.