Source for file pdo.php
Documentation is available at pdo.php
* @package Joomla.Platform
* @copyright Copyright (C) 2005 - 2013 Open Source Matters, Inc. All rights reserved.
* @license GNU General Public License version 2 or later; see LICENSE
* Joomla Platform PDO Database Driver Class
* @package Joomla.Platform
* @see http://php.net/pdo
* The name of the database driver.
* The character(s) used to quote SQL statement names such as table names or field names,
* etc. The child classes should define this as necessary. If a single character string the
* same character is used for both sides of the quoted name, else the first character will be
* used for the opening quote and the second for the closing quote.
* The null or zero representation of a timestamp for the database driver. This should be
* defined in child classes to hold the appropriate value for the engine.
* @var resource The prepared statement.
* Contains the current query execution status
* @param array $options List of options used to configure the connection
// Get some basic values from the options.
$options['driver'] =
(isset
($options['driver'])) ?
$options['driver'] :
'odbc';
$options['dsn'] =
(isset
($options['dsn'])) ?
$options['dsn'] :
'';
$options['host'] =
(isset
($options['host'])) ?
$options['host'] :
'localhost';
$options['database'] =
(isset
($options['database'])) ?
$options['database'] :
'';
$options['user'] =
(isset
($options['user'])) ?
$options['user'] :
'';
$options['password'] =
(isset
($options['password'])) ?
$options['password'] :
'';
$options['driverOptions'] =
(isset
($options['driverOptions'])) ?
$options['driverOptions'] :
array();
// Finalize initialisation
* Connects to the database if needed.
* @return void Returns void if the database connected successfully.
* @throws RuntimeException
// Make sure the PDO extension for PHP is installed and enabled.
if (!self::isSupported())
throw
new RuntimeException('PDO Extension is not available.', 1);
// Find the correct PDO DSN Format to use:
$format =
'cubrid:host=#HOST#;port=#PORT#;dbname=#DBNAME#';
$replace =
array('#HOST#', '#PORT#', '#DBNAME#');
$format =
'dblib:host=#HOST#;port=#PORT#;dbname=#DBNAME#';
$replace =
array('#HOST#', '#PORT#', '#DBNAME#');
$format =
'firebird:dbname=#DBNAME#';
$replace =
array('#DBNAME#');
$with =
array($this->options['database']);
$format =
'ibm:DSN=#DSN#';
$replace =
array('#DSN#');
$with =
array($this->options['dsn']);
$format =
'ibm:hostname=#HOST#;port=#PORT#;database=#DBNAME#';
$replace =
array('#HOST#', '#PORT#', '#DBNAME#');
$this->options['protocol'] =
(isset
($this->options['protocol'])) ?
$this->options['protocol'] :
'onsoctcp';
$format =
'informix:DSN=#DSN#';
$replace =
array('#DSN#');
$with =
array($this->options['dsn']);
$format =
'informix:host=#HOST#;service=#PORT#;database=#DBNAME#;server=#SERVER#;protocol=#PROTOCOL#';
$replace =
array('#HOST#', '#PORT#', '#DBNAME#', '#SERVER#', '#PROTOCOL#');
$format =
'mssql:host=#HOST#;port=#PORT#;dbname=#DBNAME#';
$replace =
array('#HOST#', '#PORT#', '#DBNAME#');
$format =
'mysql:host=#HOST#;port=#PORT#;dbname=#DBNAME#';
$replace =
array('#HOST#', '#PORT#', '#DBNAME#');
$format =
'oci:dbname=#DSN#';
$replace =
array('#DSN#');
$with =
array($this->options['dsn']);
$format =
'oci:dbname=//#HOST#:#PORT#/#DBNAME#';
$replace =
array('#HOST#', '#PORT#', '#DBNAME#');
$format .=
';charset=' .
$this->options['charset'];
$format =
'odbc:DSN=#DSN#;UID:#USER#;PWD=#PASSWORD#';
$replace =
array('#DSN#', '#USER#', '#PASSWORD#');
$format =
'pgsql:host=#HOST#;port=#PORT#;dbname=#DBNAME#';
$replace =
array('#HOST#', '#PORT#', '#DBNAME#');
if (isset
($this->options['version']) &&
$this->options['version'] ==
2)
$format =
'sqlite2:#DBNAME#';
$format =
'sqlite:#DBNAME#';
$replace =
array('#DBNAME#');
$with =
array($this->options['database']);
$format =
'mssql:host=#HOST#;port=#PORT#;dbname=#DBNAME#';
$replace =
array('#HOST#', '#PORT#', '#DBNAME#');
// Create the connection string:
$connectionString =
str_replace($replace, $with, $format);
throw
new RuntimeException('Could not connect to PDO: ' .
$e->getMessage(), 2, $e);
* Disconnects the database.
* Method to escape a string for usage in an SQL statement.
* Oracle escaping reference:
* http://www.orafaq.com/wiki/SQL_FAQ#How_does_one_escape_special_characters_when_writing_SQL_queries.3F
* http://www.sqlite.org/faq.html#q14
* Method body is as implemented by the Zend Framework
* Note: Using query objects with bound variables is
* preferable to the below.
* @param string $text The string to be escaped.
* @param boolean $extra Unused optional parameter to provide extra escaping.
* @return string The escaped string.
public function escape($text, $extra =
false)
* Execute the SQL statement.
* @return mixed A database cursor resource on success, boolean false on failure.
* @throws RuntimeException
// Take a local copy so that we don't modify the original query and cause issues later
$query .=
' LIMIT ' .
$this->offset .
', ' .
$this->limit;
// Increment the query counter.
// Reset the error values.
// If debugging is enabled then let's log the query.
// Add the query to the object queue.
if ($this->prepared instanceof
PDOStatement)
$bounded =
& $this->sql->getBounded();
foreach ($bounded as $key =>
$obj)
$this->prepared->bindParam($key, $obj->value, $obj->dataType, $obj->length, $obj->driverOptions);
if (defined('DEBUG_BACKTRACE_IGNORE_ARGS'))
// If an error occurred handle it.
// Get the error number and message before we execute any more queries.
// Check if the server was disconnected.
// If connect fails, ignore that exception and throw the normal exception.
catch
(RuntimeException $e)
// Get the error number and message.
// Throw the normal query exception.
// Since we were able to reconnect, run the query again.
// The server was not disconnected.
// Get the error number and message from before we tried to reconnect.
// Throw the normal query exception.
* Retrieve a PDO database connection attribute
* http://www.php.net/manual/en/pdo.getattribute.php
* Usage: $db->getOption(PDO::ATTR_CASE);
* @param mixed $key One of the PDO::ATTR_* Constants
* Get a query to run and verify the database is operational.
* @return string The query to check the health of the DB.
* Sets an attribute on the PDO database handle.
* http://www.php.net/manual/en/pdo.setattribute.php
* Usage: $db->setOption(PDO::ATTR_CASE, PDO::CASE_UPPER);
* @param integer $key One of the PDO::ATTR_* Constants
* @param mixed $value One of the associated PDO Constants
* related to the particular attribute
return $this->connection->setAttribute($key, $value);
* Test to see if the PDO extension is available.
* Override as needed to check for specific PDO Drivers.
* @return boolean True on success, false otherwise.
return defined('PDO::ATTR_DRIVER_NAME');
* Determines if the connection to the server is active.
* @return boolean True if connected to the database engine.
// Flag to prevent recursion into this function.
static $checkingConnected =
false;
// Reset this flag and throw an exception.
$checkingConnected =
true;
die('Recursion trying to check if connected.');
// Backup the query state.
// Set the checking connection flag.
$checkingConnected =
true;
// Run a simple query to check the connection.
// If we catch an exception here, we must not be connected.
// Restore the query state.
$checkingConnected =
false;
* Get the number of affected rows for the previous executed SQL statement.
* Only applicable for DELETE, INSERT, or UPDATE statements.
* @return integer The number of affected rows.
if ($this->prepared instanceof
PDOStatement)
* Get the number of returned rows for the previous executed SQL statement.
* @param resource $cursor An optional database cursor resource to extract the row count from.
* @return integer The number of returned rows.
if ($cursor instanceof
PDOStatement)
return $cursor->rowCount();
elseif ($this->prepared instanceof
PDOStatement)
* Method to get the auto-incremented value from the last INSERT statement.
* @return string The value of the auto-increment field from the last inserted row.
// Error suppress this to prevent PDO warning us that the driver doesn't support this operation.
* Select a database for use.
* @param string $database The name of the database to select for use.
* @return boolean True if the database was successfully selected.
* @throws RuntimeException
public function select($database)
* Sets the SQL statement string for later execution.
* @param mixed $query The SQL statement to set either as a JDatabaseQuery object or a string.
* @param integer $offset The affected row offset to set.
* @param integer $limit The maximum affected rows to set.
* @param array $driverOptions The optional PDO driver options
* @return JDatabaseDriver This object to support method chaining.
public function setQuery($query, $offset =
null, $limit =
null, $driverOptions =
array())
// Allows taking advantage of bound variables in a direct query:
$query->setLimit($limit, $offset);
// Store reference to the JDatabaseQuery instance:
parent::setQuery($query, $offset, $limit);
* Set the connection to use UTF-8 character encoding.
* @return boolean True on success.
* Method to commit a transaction.
* @param boolean $toSavepoint If true, commit to the last savepoint.
* @throws RuntimeException
* Method to roll back a transaction.
* @param boolean $toSavepoint If true, rollback to the last savepoint.
* @throws RuntimeException
* Method to initialize a transaction.
* @param boolean $asSavepoint If true and a transaction is already active, a savepoint will be created.
* @throws RuntimeException
* Method to fetch a row from the result set cursor as an array.
* @param mixed $cursor The optional result set cursor from which to fetch the row.
* @return mixed Either the next row from the result set or false if there are no more rows.
if (!empty($cursor) &&
$cursor instanceof
PDOStatement)
return $cursor->fetch(PDO::FETCH_NUM);
if ($this->prepared instanceof
PDOStatement)
return $this->prepared->fetch(PDO::FETCH_NUM);
* Method to fetch a row from the result set cursor as an associative array.
* @param mixed $cursor The optional result set cursor from which to fetch the row.
* @return mixed Either the next row from the result set or false if there are no more rows.
if (!empty($cursor) &&
$cursor instanceof
PDOStatement)
return $cursor->fetch(PDO::FETCH_ASSOC);
if ($this->prepared instanceof
PDOStatement)
return $this->prepared->fetch(PDO::FETCH_ASSOC);
* Method to fetch a row from the result set cursor as an object.
* @param mixed $cursor The optional result set cursor from which to fetch the row.
* @param string $class Unused, only necessary so method signature will be the same as parent.
* @return mixed Either the next row from the result set or false if there are no more rows.
protected function fetchObject($cursor =
null, $class =
'stdClass')
if (!empty($cursor) &&
$cursor instanceof
PDOStatement)
if ($this->prepared instanceof
PDOStatement)
* Method to free up the memory used for the result set.
* @param mixed $cursor The optional result set cursor from which to fetch the row.
if ($cursor instanceof
PDOStatement)
if ($this->prepared instanceof
PDOStatement)
* Method to get the next row in the result set from the database query as an object.
* @param string $class The class name to use for the returned row object.
* @return mixed The result of the query as an array, false if there are no more rows.
* @throws RuntimeException
// Execute the query and get the result set cursor.
// Get the next row from the result set as an object of type $class.
// Free up system resources and return.
* Method to get the next row in the result set from the database query as an array.
* @return mixed The result of the query as an array, false if there are no more rows.
* @throws RuntimeException
// Execute the query and get the result set cursor.
// Get the next row from the result set as an object of type $class.
// Free up system resources and return.
* Method to get the next row in the result set from the database query as an array.
* @return mixed The result of the query as an array, false if there are no more rows.
* @throws RuntimeException
// Execute the query and get the result set cursor.
// Get the next row from the result set as an object of type $class.
// Free up system resources and return.
* PDO does not support serialize
$serializedProperties =
array();
$reflect =
new ReflectionClass($this);
// Get properties of the current class
$properties =
$reflect->getProperties();
foreach ($properties as $property)
// Do not serialize properties that are PDO
if ($property->isStatic() ==
false &&
!($this->{$property->name} instanceof
PDO))
return $serializedProperties;
* Wake up after serialization
Documentation generated on Tue, 19 Nov 2013 15:10:26 +0100 by phpDocumentor 1.4.3