PHP Class PDOWrapper

Author: xuanyan ([email protected])
Inheritance: extends DatabaseAbstract, implements DatabaseWrapper
Show file Open project: mikehenrty/thin-pdo-wrapper Class Usage Examples

Public Properties

Property Type Description
$LOG_ERRORS boolean Write all errors to error log
$TIMESTAMP_WRITES boolean Automatically add/update created/updated fields

Protected Properties

Property Type Description
$config_master Dynamic master config creds
$config_slaves Dynamic slave config creds
$instance PDOWrapper A reference to the singleton instance
$pdo_exception We will cache any PDO errors in case we want to get out them externally
$pdo_master The PDO objects for the master connection
$pdo_slave The PDO objects for the slave connection

Public Methods

Method Description
__destruct ( ) Destructor.
configMaster ( $host, $name, $user, $password, $port = null, $driver = 'mysql' ) method configMaster - configure connection credentials to the master db server
configSlave ( $host, $name, $user, $password, $port = null, $driver = 'mysql' ) method configSlave - configure a connection to a slave (can be called multiple times)
delete ( $table, $params = [] ) : boolean method delete.
execute ( string $query, $params = [] ) : mixed method execute.
getErrorMessage ( ) method getErrorMessage.
getPDOException ( ) method getError.
insert ( string $table, array $params = [], boolean $timestamp_this = null ) : mixed method insert.
insertMultiple ( string $table, array $columns = [], $rows = [], boolean $timestamp_these = null ) : mixed method insertMultiple.
instance ( ) : - method instance.
query ( string $query, array $params = [], boolean $use_master = false ) : mixed method query.
queryFirst ( string $query, array $params = [], boolean $use_master = false ) : mixed method queryFirst.
select ( string $table, array $params = null, integer $limit = null, integer $start = null, array $order_by = null, boolean $use_master = false ) : mixed method select.
selectFirst ( $table, $params = [], array $order_by = null ) : mixed method selectFirst.
selectFirstMaster ( $table, $params = [], array $order_by = null ) : mixed method selectFirstMaster.
selectMaster ( $table, $params = [], integer $limit = null, integer $start = null, array $order_by = null ) : mixed method selectMaster.
update ( string $table, array $params, array $wheres = [], boolean $timestamp_this = null ) : integer | boolean method update.

Protected Methods

Method Description
__construct ( ) Constructor.
createConnection ( $driver, $host, $name, $user, $password, $port = null ) : PDO method createConnection.
getMaster ( ) method getMaster.
getSlave ( ) method getSlave.

Private Methods

Method Description
validateDriver ( string $driver ) : boolean Validate the database in question is supported by your installation of PHP.

Method Details

__construct() protected method

- make protected so only subclasses and self can create this object (singleton)
protected __construct ( )

__destruct() public method

- release the PDO db connections
public __destruct ( )

configMaster() public method

method configMaster - configure connection credentials to the master db server
public configMaster ( $host, $name, $user, $password, $port = null, $driver = 'mysql' )

configSlave() public method

method configSlave - configure a connection to a slave (can be called multiple times)
public configSlave ( $host, $name, $user, $password, $port = null, $driver = 'mysql' )

createConnection() protected method

- create a PDO connection using the credentials provided
protected createConnection ( $driver, $host, $name, $user, $password, $port = null ) : PDO
return PDO object with a connection to the database specified

delete() public method

- deletes rows from a table based on the parameters
public delete ( $table, $params = [] ) : boolean
return boolean - associate representing the fetched table row, false on failure

execute() public method

- executes a query that modifies the database
public execute ( string $query, $params = [] ) : mixed
$query string - the SQL query we are executing
return mixed - the affected rows, false on failure

getErrorMessage() public method

- returns the last error message caught
public getErrorMessage ( )

getMaster() protected method

- grab the PDO connection to the master DB
protected getMaster ( )

getPDOException() public method

- returns the actual PDO exception
public getPDOException ( )

getSlave() protected method

- grab the PDO connection to the slave DB, create it if not there
protected getSlave ( )

insert() public method

- adds a row to the specified table
public insert ( string $table, array $params = [], boolean $timestamp_this = null ) : mixed
$table string - the name of the db table we are adding row to
$params array - associative array representing the columns and their respective values
$timestamp_this boolean (Optional), if true we set date_created and date_modified values to now
return mixed - new primary key of inserted table, false on failure

insertMultiple() public method

- adds multiple rows to a table with a single query
public insertMultiple ( string $table, array $columns = [], $rows = [], boolean $timestamp_these = null ) : mixed
$table string - the name of the db table we are adding row to
$columns array - contains the column names
$timestamp_these boolean (Optional), if true we set date_created and date_modified values to NOW() for each row
return mixed - new primary key of inserted table, false on failure

instance() public static method

- static, for singleton, for creating a global instance of this object
public static instance ( ) : -
return -

query() public method

- returns data from a free form select query
public query ( string $query, array $params = [], boolean $use_master = false ) : mixed
$query string - the SQL query we are executing
$params array - a list of bind parameters
$use_master boolean (Optional) - whether or not to use the master connection
return mixed - the affected rows, false on failure

queryFirst() public method

- returns the first record from a free form select query
public queryFirst ( string $query, array $params = [], boolean $use_master = false ) : mixed
$query string - the SQL query we are executing
$params array - a list of bind parameters
$use_master boolean (Optional) - whether or not to use the master connection
return mixed - the affected rows, false on failure

select() public method

- retrieve information from the database, as an array
public select ( string $table, array $params = null, integer $limit = null, integer $start = null, array $order_by = null, boolean $use_master = false ) : mixed
$table string - the name of the db table we are retreiving the rows from
$params array - associative array representing the WHERE clause filters
$limit integer (optional) - the amount of rows to return
$start integer (optional) - the row to start on, indexed by zero
$order_by array (optional) - an array with order by clause
$use_master boolean (optional) - use the master db for this read
return mixed - associate representing the fetched table row, false on failure

selectFirst() public method

- retrieve the first row returned from a select statement
public selectFirst ( $table, $params = [], array $order_by = null ) : mixed
$order_by array (optional) - an array with order by clause
return mixed - associate representing the fetched table row, false on failure

selectFirstMaster() public method

- retrieve the first row returned from a select statement using the master database
public selectFirstMaster ( $table, $params = [], array $order_by = null ) : mixed
$order_by array (optional) - an array with order by clause
return mixed - associate representing the fetched table row, false on failure

selectMaster() public method

- retrieve information from the master database, as an array
public selectMaster ( $table, $params = [], integer $limit = null, integer $start = null, array $order_by = null ) : mixed
$limit integer (optional) - the amount of rows to return
$start integer (optional) - the row to start on, indexed by zero
$order_by array (optional) - an array with order by clause
return mixed - associate representing the fetched table row, false on failure

update() public method

- updates a row to the specified table
public update ( string $table, array $params, array $wheres = [], boolean $timestamp_this = null ) : integer | boolean
$table string - the name of the db table we are adding row to
$params array - associative array representing the columns and their respective values to update
$wheres array (Optional) - the where clause of the query
$timestamp_this boolean (Optional) - if true we set date_created and date_modified values to now
return integer | boolean - the amount of rows updated, false on failure

Property Details

$LOG_ERRORS public static property

Write all errors to error log
public static bool $LOG_ERRORS
return boolean

$TIMESTAMP_WRITES public static property

Automatically add/update created/updated fields
public static bool $TIMESTAMP_WRITES
return boolean

$config_master protected property

Dynamic master config creds
protected $config_master

$config_slaves protected property

Dynamic slave config creds
protected $config_slaves

$instance protected static property

A reference to the singleton instance
protected static PDOWrapper $instance
return PDOWrapper

$pdo_exception protected property

We will cache any PDO errors in case we want to get out them externally
protected $pdo_exception

$pdo_master protected property

The PDO objects for the master connection
protected $pdo_master

$pdo_slave protected property

The PDO objects for the slave connection
protected $pdo_slave