NAME
PDK::DBI::Mysql - A Moose-based wrapper for MySQL database operations using DBIx::Custom
SYNOPSIS
use PDK::DBI::Mysql;
my $db = PDK::DBI::Mysql->new(
dsn => 'DBI:mysql:database=netdisco;host=netdisco;port=3306',
user => 'username',
password => 'password'
);
my $result = $db->execute("SELECT * FROM users WHERE id = ?", [1])->all;
$db->disconnect;
DESCRIPTION
PDK::DBI::Mysql is a Moose-based module that provides a high-level interface for MySQL database operations. It uses DBIx::Custom to handle the underlying database connections and queries, and implements transaction management and connection pooling.
ATTRIBUTES
option
A read-only HashRef that stores database connection options.
METHODS
new(%params)
Constructor for creating a new PDK::DBI::Mysql object. It accepts the following parameters:
host - Database host
port - Database port
dbname - Database name
user - Database username
password - Database password
dsn - Data Source Name (optional, will be generated if not provided)
option - HashRef of additional database connection options
clone()
Creates a new instance of the current object, copying the connection information (dsn, user, password, and options).
Returns: A new PDK::DBI::Mysql object.
batchExecute($params, $sql)
Executes a batch SQL operation.
$params - ArrayRef of parameters for the SQL query
$sql - SQL query string
disconnect()
Closes the database connection.
reconnect()
Disconnects and then re-establishes the database connection.
INHERITED METHODS
The following methods are inherited from DBIx::Custom and can be called directly on the PDK::DBI::Mysql object:
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 be rolled back.
EXAMPLES
Connecting to a database
my $db = PDK::DBI::Mysql->new(
host => 'localhost',
port => 3306,
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 Doe', 'jane@example.com']
];
$db->batchExecute($params, "INSERT INTO users (name, email) VALUES (?, ?)");
SEE ALSO
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.