FaaPz\PDO\Database extends PDO

December 4, 2021 ยท View on GitHub

The Database class is an extension of PHP's PDO object that provides additional factory style methods for the query building statements supported by this library. Because the Database object extends PDO, all PDO methods like ::beginTransaction(), ::rollBack() and ::commit() are fully supported.

Constructor

__construct(string $dsn, ?string $username = null, ?string $password = null, array $options = [])

All the same constructor parameters you would use to create a PDO object. The Database class will apply the following default options unless explicitly overridden:

[
        PDO::ATTR_ERRMODE            => PDO::ERRMODE_EXCEPTION,
        PDO::ATTR_DEFAULT_FETCH_MODE => PDO::FETCH_ASSOC,
        PDO::ATTR_EMULATE_PREPARES   => false,
]
ParameterDescription
$dsnData source name
$usernameDatabase username
$passwordDatabase password
$optionsArray of key => value connection options

Example

use FaaPz\PDO\Database;

$database = new Database('mysql:host=localhost;dbname=test_db;charset=UTF8');

Methods

call(?MethodInterface $procedure = null): CallInterface

Creates a new Call statement that will use the optional METHOD parameter.

ParameterDescription
$procedureOptional procedure to call

Example

use FaaPz\PDO\Clause\Method;
use FaaPz\PDO\Database;

$database = new Database('mysql:host=localhost;dbname=test_db;charset=UTF8');

// CALL MyProcedure(?, ?)
$statement = $database->call(new Method("MyProcedure", 1, 2));

$statement->execute();

insert(array $columns = []): InsertInterface

Creates a new INSERT statement that will use the optional array of columns.

ParameterDescription
$columnsOptional columns to use

Example

use FaaPz\PDO\Clause\Method;
use FaaPz\PDO\Database;

$database = new Database('mysql:host=localhost;dbname=test_db;charset=UTF8');

// INSERT INTO table (col1, col2) VALUES (?, ?), (?, ?)
$statement = $database->insert(['col1', 'col2'])
                 ->into('table')
                 ->values(1, 2)
                 ->values(3, 4);

$statement->execute();

select(array $columns = ['*']): SelectInterface

Creates a new Select statement that will use the optional array of columns.

ParameterDescription
$columnsOptional columns to select

Example

use FaaPz\PDO\Clause\Method;
use FaaPz\PDO\Database;

$database = new Database('mysql:host=localhost;dbname=test_db;charset=UTF8');

// SELECT COUNT(id) AS cnt FROM table
$statement = $database->select(['cnt' => 'COUNT(id)'])
                      ->from('table');

$statement->execute();

update(array $pairs = []): UpdateInterface

Creates a new UPDATE statement that will use the optional array of column / value pairs.

ParameterDescription
$pairsOptional column / value pairs to update

Example

use FaaPz\PDO\Clause\Method;
use FaaPz\PDO\Database;

$database = new Database('mysql:host=localhost;dbname=test_db;charset=UTF8');

// UPDATE table SET col1 = ?, col2 = ?
$statement = $database->update(['col1' => 1, 'col2' => 2])
                      ->table('table');

$statement->execute();

delete($table = null): DeleteInterface

Creates a new DELETE statement that will use the optional table to delete from.

ParameterDescription
$tableOptional table to delete from

Example

use FaaPz\PDO\Clause\Method;
use FaaPz\PDO\Database;

$database = new Database('mysql:host=localhost;dbname=test_db;charset=UTF8');

// DELETE FROM table
$statement = $database->delete('table');

$statement->execute();