<?php
include_once('NestedSetDbTable/DbAdapter/Interface.php');
include_once('NestedSetDbTable/DbAdapter/Default/Exception.php');
/**
* Default database adapter.
*
* @author Nikola Posa <hide@address.com>
* @license http://opensource.org/licenses/gpl-3.0.html GNU General Public License
*/
class NestedSetDbTable_DbAdapter_Default implements NestedSetDbTable_DbAdapter_Interface
{
/**
* Database connection. Represents PDO instance.
*
* @var object|null
*/
protected $_connection;
/**
* Fetch mode
*
* @var integer
*/
protected $_fetchMode = PDO::FETCH_ASSOC;
/**
* Constructor.
*
* Supported params for $options are:
* dsn - the Data Source Name, or DSN, contains the information required to connect to the database,
* username - the username for the DSN string,
* password - The password for the DSN string,
* driver_options - additional driver-specific connection options.
*
* @param array Array with custom config options.
* @return NestedSetDbTable_DbAdapter_Interface
*/
public function __construct($config)
{
if(!$config['dsn']) {
throw new NestedSetDbTable_DbAdapter_Default_Exception('You must supply DSN string.');
}
if(!$config['username'] || null === $config['password']) {
throw new NestedSetDbTable_DbAdapter_Default_Exception('You must supply both connection username and password.');
}
$this->_connection = new PDO($config['dsn'], $config['username'], $config['password'], $config['driver_options']);
}
public function setFetchMode($mode)
{
$this->_fetchMode = (int)$mode;
}
/**
* NestedSetDbTable_DbAdapter_Interface implementation.
*/
/**
* Required by the NestedSetDbTable_DbAdapter_Interface
* implementation.
* Quotes a value for an SQL query.
*
* @param string The value that will be quoted.
* @return string
*/
public function quote($value)
{
return $this->_connection->quote($value);
}
/**
* Required by the NestedSetDbTable_DbAdapter_Interface
* implementation.
* Prepares and executes an SQL statement. This method should
* return PDOStatement object.
*
* @param string SQL statement that can contain placeholders.
* @param array An array of data to bind to the placeholders (optional).
* @return PDOStatement
*/
public function query($sql, $bind = array())
{
$stmt = $this->_connection->prepare($sql);
$stmt->execute($bind);
return $stmt;
}
/**
* Required by the NestedSetDbTable_DbAdapter_Interface
* implementation.
* Inserts a row with specified data.
*
* @param string Name of the table.
* @param array Array with data as column-value pairs.
* @return int The number of affected rows.
*/
public function insert($tableName, $data)
{
$cols = array_keys($data);
$vals = array_fill(0, count($data), '?');
$sql = "INSERT INTO
{$tableName}"
. ' (' . implode(', ', $cols) . ') '
. 'VALUES (' . implode(', ', $vals) . ')';
//Execute the statement and return the number of affected rows.
$stmt = $this->query($sql, array_values($data));
return $stmt->rowCount();
}
/**
* Required by the NestedSetDbTable_DbAdapter_Interface
* implementation.
* Updates table rows with specified data, optionally based on
* a SQL WHERE clause.
*
* @param string Name of the table.
* @param array Array with data as column-value pairs.
* @param string|null UPDATE WHERE clause. (optional)
* @return int The number of affected rows.
*/
public function update($tableName, $data, $where = null)
{
$set = array();
foreach (array_keys($data) as $col) {
$set[] = "{$col} = ?";
}
$sql = "UPDATE
{$tableName}"
. ' SET ' . implode(', ', $set)
. (($where != null) ? " WHERE $where" : '');
//Execute the statement and return the number of affected rows.
$stmt = $this->query($sql, array_values($data));
return $stmt->rowCount();
}
/**
* Required by the NestedSetDbTable_DbAdapter_Interface
* implementation.
* Deletes table rows based on a WHERE clause.
*
* @param string Name of the table.
* @param string|null DELETE WHERE clause. (optional)
* @return int The number of affected rows.
*/
public function delete($tableName, $where = null)
{
$sql = "DELETE FROM
{$tableName}"
. (($where != null) ? " WHERE $where" : '');
//Execute the statement and return the number of affected rows.
$stmt = $this->_connection->query($sql);
return $stmt->rowCount();
}
/**
* Required by the NestedSetDbTable_DbAdapter_Interface
* implementation.
* Fetches all SQL result rows as a sequential array. Uses
* current $_fetchMode for this adapter.
*
* @param string An SQL SELECT statement.
* @return array
*/
public function fetchAll($sql)
{
$stmt = $this->_connection->query($sql);
return $stmt->fetchAll($this->_fetchMode);
}
/**
* Required by the NestedSetDbTable_DbAdapter_Interface
* implementation.
* Fetches the first row of the SQL result. Uses current
* $_fetchMode for this adapter.
*
* @param string An SQL SELECT statement.
* @return array
*/
public function fetchRow($sql)
{
$stmt = $this->_connection->query($sql);
return $stmt->fetch($this->_fetchMode);
}
}
?>