Commit 67429ab1 authored by Crell's avatar Crell

Initial move and rename of database classes to PSR-0 namespaces. So far only...

Initial move and rename of database classes to PSR-0 namespaces.  So far only MySQL has been converted.
parent f78b16e7
This diff is collapsed.
This diff is collapsed.
<?php
namespace Drupal\Database;
use Exception;
/**
* Exception thrown if an undefined database connection is requested.
*/
class DatabaseConnectionNotDefinedException extends Exception {}
<?php
namespace Drupal\Database;
use Exception;
/**
* Exception thrown if no driver is specified for a database connection.
*/
class DatabaseDriverNotSpecifiedException extends Exception {}
<?php
/**
* @file
* Logging classes for the database layer.
*/
namespace Drupal\Database;
/**
* Database query logger.
......
<?php
/**
* @file
* Generic Database schema code.
*/
namespace Drupal\Database;
require_once __DIR__ . '/query.inc';
use Drupal\Database\Query\PlaceholderInterface;
/**
* @defgroup schemaapi Schema API
......@@ -156,7 +153,7 @@
* @see drupal_install_schema()
*/
abstract class DatabaseSchema implements QueryPlaceholderInterface {
abstract class DatabaseSchema implements PlaceholderInterface {
protected $connection;
......@@ -194,14 +191,14 @@ public function __clone() {
}
/**
* Implements QueryPlaceHolderInterface::uniqueIdentifier().
* Implements PlaceHolderInterface::uniqueIdentifier().
*/
public function uniqueIdentifier() {
return $this->uniqueIdentifier;
}
/**
* Implements QueryPlaceHolderInterface::nextPlaceholder().
* Implements PlaceHolderInterface::nextPlaceholder().
*/
public function nextPlaceholder() {
return $this->placeholder++;
......@@ -698,26 +695,3 @@ public function prepareComment($comment, $length = NULL) {
return $this->connection->quote($comment);
}
}
/**
* Exception thrown if an object being created already exists.
*
* For example, this exception should be thrown whenever there is an attempt to
* create a new database table, field, or index that already exists in the
* database schema.
*/
class DatabaseSchemaObjectExistsException extends Exception {}
/**
* Exception thrown if an object being modified doesn't exist yet.
*
* For example, this exception should be thrown whenever there is an attempt to
* modify a database table, field, or index that does not currently exist in
* the database schema.
*/
class DatabaseSchemaObjectDoesNotExistException extends Exception {}
/**
* @} End of "defgroup schemaapi".
*/
<?php
namespace Drupal\Database;
use Exception;
/**
* Exception thrown if an object being modified doesn't exist yet.
*
* For example, this exception should be thrown whenever there is an attempt to
* modify a database table, field, or index that does not currently exist in
* the database schema.
*/
class DatabaseSchemaObjectDoesNotExistException extends Exception {}
<?php
namespace Drupal\Database;
use Exception;
/**
* Exception thrown if an object being created already exists.
*
* For example, this exception should be thrown whenever there is an attempt to
* create a new database table, field, or index that already exists in the
* database schema.
*/
class DatabaseSchemaObjectExistsException extends Exception {}
<?php
namespace Drupal\Database;
use PDO;
use PDOStatement;
/**
* Default implementation of DatabaseStatementInterface.
*
* PDO allows us to extend the PDOStatement class to provide additional
* functionality beyond that offered by default. We do need extra
* functionality. By default, this class is not driver-specific. If a given
* driver needs to set a custom statement class, it may do so in its
* constructor.
*
* @see http://us.php.net/pdostatement
*/
class DatabaseStatementBase extends PDOStatement implements DatabaseStatementInterface {
/**
* Reference to the database connection object for this statement.
*
* The name $dbh is inherited from PDOStatement.
*
* @var DatabaseConnection
*/
public $dbh;
protected function __construct($dbh) {
$this->dbh = $dbh;
$this->setFetchMode(PDO::FETCH_OBJ);
}
public function execute($args = array(), $options = array()) {
if (isset($options['fetch'])) {
if (is_string($options['fetch'])) {
// Default to an object. Note: db fields will be added to the object
// before the constructor is run. If you need to assign fields after
// the constructor is run, see http://drupal.org/node/315092.
$this->setFetchMode(PDO::FETCH_CLASS, $options['fetch']);
}
else {
$this->setFetchMode($options['fetch']);
}
}
$logger = $this->dbh->getLogger();
if (!empty($logger)) {
$query_start = microtime(TRUE);
}
$return = parent::execute($args);
if (!empty($logger)) {
$query_end = microtime(TRUE);
$logger->log($this, $args, $query_end - $query_start);
}
return $return;
}
public function getQueryString() {
return $this->queryString;
}
public function fetchCol($index = 0) {
return $this->fetchAll(PDO::FETCH_COLUMN, $index);
}
public function fetchAllAssoc($key, $fetch = NULL) {
$return = array();
if (isset($fetch)) {
if (is_string($fetch)) {
$this->setFetchMode(PDO::FETCH_CLASS, $fetch);
}
else {
$this->setFetchMode($fetch);
}
}
foreach ($this as $record) {
$record_key = is_object($record) ? $record->$key : $record[$key];
$return[$record_key] = $record;
}
return $return;
}
public function fetchAllKeyed($key_index = 0, $value_index = 1) {
$return = array();
$this->setFetchMode(PDO::FETCH_NUM);
foreach ($this as $record) {
$return[$record[$key_index]] = $record[$value_index];
}
return $return;
}
public function fetchField($index = 0) {
// Call PDOStatement::fetchColumn to fetch the field.
return $this->fetchColumn($index);
}
public function fetchAssoc() {
// Call PDOStatement::fetch to fetch the row.
return $this->fetch(PDO::FETCH_ASSOC);
}
}
<?php
namespace Drupal\Database;
use Iterator;
/**
* Empty implementation of a database statement.
*
* This class satisfies the requirements of being a database statement/result
* object, but does not actually contain data. It is useful when developers
* need to safely return an "empty" result set without connecting to an actual
* database. Calling code can then treat it the same as if it were an actual
* result set that happens to contain no records.
*
* @see SearchQuery
*/
class DatabaseStatementEmpty implements Iterator, DatabaseStatementInterface {
public function execute($args = array(), $options = array()) {
return FALSE;
}
public function getQueryString() {
return '';
}
public function rowCount() {
return 0;
}
public function setFetchMode($mode, $a1 = NULL, $a2 = array()) {
return;
}
public function fetch($mode = NULL, $cursor_orientation = NULL, $cursor_offset = NULL) {
return NULL;
}
public function fetchField($index = 0) {
return NULL;
}
public function fetchObject() {
return NULL;
}
public function fetchAssoc() {
return NULL;
}
function fetchAll($mode = NULL, $column_index = NULL, array $constructor_arguments = array()) {
return array();
}
public function fetchCol($index = 0) {
return array();
}
public function fetchAllKeyed($key_index = 0, $value_index = 1) {
return array();
}
public function fetchAllAssoc($key, $fetch = NULL) {
return array();
}
/* Implementations of Iterator. */
public function current() {
return NULL;
}
public function key() {
return NULL;
}
public function rewind() {
// Nothing to do: our DatabaseStatement can't be rewound.
}
public function next() {
// Do nothing, since this is an always-empty implementation.
}
public function valid() {
return FALSE;
}
}
<?php
namespace Drupal\Database;
use Traversable;
/**
* Represents a prepared statement.
*
* Some methods in that class are purposefully commented out. Due to a change in
* how PHP defines PDOStatement, we can't define a signature for those methods
* that will work the same way between versions older than 5.2.6 and later
* versions. See http://bugs.php.net/bug.php?id=42452 for more details.
*
* Child implementations should either extend PDOStatement:
* @code
* class DatabaseStatement_oracle extends PDOStatement implements DatabaseStatementInterface {}
* @endcode
* or define their own class. If defining their own class, they will also have
* to implement either the Iterator or IteratorAggregate interface before
* DatabaseStatementInterface:
* @code
* class DatabaseStatement_oracle implements Iterator, DatabaseStatementInterface {}
* @endcode
*/
interface DatabaseStatementInterface extends Traversable {
/**
* Executes a prepared statement
*
* @param $args
* An array of values with as many elements as there are bound parameters in
* the SQL statement being executed.
* @param $options
* An array of options for this query.
*
* @return
* TRUE on success, or FALSE on failure.
*/
public function execute($args = array(), $options = array());
/**
* Gets the query string of this statement.
*
* @return
* The query string, in its form with placeholders.
*/
public function getQueryString();
/**
* Returns the number of rows affected by the last SQL statement.
*
* @return
* The number of rows affected by the last DELETE, INSERT, or UPDATE
* statement executed.
*/
public function rowCount();
/**
* Sets the default fetch mode for this statement.
*
* See http://php.net/manual/en/pdo.constants.php for the definition of the
* constants used.
*
* @param $mode
* One of the PDO::FETCH_* constants.
* @param $a1
* An option depending of the fetch mode specified by $mode:
* - for PDO::FETCH_COLUMN, the index of the column to fetch
* - for PDO::FETCH_CLASS, the name of the class to create
* - for PDO::FETCH_INTO, the object to add the data to
* @param $a2
* If $mode is PDO::FETCH_CLASS, the optional arguments to pass to the
* constructor.
*/
// public function setFetchMode($mode, $a1 = NULL, $a2 = array());
/**
* Fetches the next row from a result set.
*
* See http://php.net/manual/en/pdo.constants.php for the definition of the
* constants used.
*
* @param $mode
* One of the PDO::FETCH_* constants.
* Default to what was specified by setFetchMode().
* @param $cursor_orientation
* Not implemented in all database drivers, don't use.
* @param $cursor_offset
* Not implemented in all database drivers, don't use.
*
* @return
* A result, formatted according to $mode.
*/
// public function fetch($mode = NULL, $cursor_orientation = NULL, $cursor_offset = NULL);
/**
* Returns a single field from the next record of a result set.
*
* @param $index
* The numeric index of the field to return. Defaults to the first field.
*
* @return
* A single field from the next record, or FALSE if there is no next record.
*/
public function fetchField($index = 0);
/**
* Fetches the next row and returns it as an object.
*
* The object will be of the class specified by DatabaseStatementInterface::setFetchMode()
* or stdClass if not specified.
*/
// public function fetchObject();
/**
* Fetches the next row and returns it as an associative array.
*
* This method corresponds to PDOStatement::fetchObject(), but for associative
* arrays. For some reason PDOStatement does not have a corresponding array
* helper method, so one is added.
*
* @return
* An associative array, or FALSE if there is no next row.
*/
public function fetchAssoc();
/**
* Returns an array containing all of the result set rows.
*
* @param $mode
* One of the PDO::FETCH_* constants.
* @param $column_index
* If $mode is PDO::FETCH_COLUMN, the index of the column to fetch.
* @param $constructor_arguments
* If $mode is PDO::FETCH_CLASS, the arguments to pass to the constructor.
*
* @return
* An array of results.
*/
// function fetchAll($mode = NULL, $column_index = NULL, array $constructor_arguments);
/**
* Returns an entire single column of a result set as an indexed array.
*
* Note that this method will run the result set to the end.
*
* @param $index
* The index of the column number to fetch.
*
* @return
* An indexed array, or an empty array if there is no result set.
*/
public function fetchCol($index = 0);
/**
* Returns the entire result set as a single associative array.
*
* This method is only useful for two-column result sets. It will return an
* associative array where the key is one column from the result set and the
* value is another field. In most cases, the default of the first two columns
* is appropriate.
*
* Note that this method will run the result set to the end.
*
* @param $key_index
* The numeric index of the field to use as the array key.
* @param $value_index
* The numeric index of the field to use as the array value.
*
* @return
* An associative array, or an empty array if there is no result set.
*/
public function fetchAllKeyed($key_index = 0, $value_index = 1);
/**
* Returns the result set as an associative array keyed by the given field.
*
* If the given key appears multiple times, later records will overwrite
* earlier ones.
*
* @param $key
* The name of the field on which to index the array.
* @param $fetch
* The fetchmode to use. If set to PDO::FETCH_ASSOC, PDO::FETCH_NUM, or
* PDO::FETCH_BOTH the returned value with be an array of arrays. For any
* other value it will be an array of objects. By default, the fetch mode
* set for the query will be used.
*
* @return
* An associative array, or an empty array if there is no result set.
*/
public function fetchAllAssoc($key, $fetch = NULL);
}
<?php
/**
* @file
* Database interface code for engines that need complete control over their
* result sets. For example, SQLite will prefix some column names by the name
* of the table. We post-process the data, by renaming the column names
* using the same convention as MySQL and PostgreSQL.
*/
namespace Drupal\Database;
/**
* @ingroup database
* @{
*/
use Drupal\Database\Connection;
use Iterator;
use PDO;
/**
* An implementation of DatabaseStatementInterface that prefetches all data.
......@@ -125,7 +118,7 @@ class DatabaseStatementPrefetch implements Iterator, DatabaseStatementInterface
'column' => 0,
);
public function __construct(DatabaseConnection $connection, $query, array $driver_options = array()) {
public function __construct(Connection $connection, $query, array $driver_options = array()) {
$this->dbh = $connection;
$this->queryString = $query;
$this->driverOptions = $driver_options;
......@@ -500,8 +493,3 @@ public function fetchAllAssoc($key, $fetch_style = NULL) {
}
}
/**
* @} End of "ingroup database".
*/
<?php
namespace Drupal\Database;
use Exception;
/**
* Exception thrown when a commit() function fails.
*/
class DatabaseTransactionCommitFailedException extends Exception { }
<?php
namespace Drupal\Database;
use Exception;
/**
* Exception to deny attempts to explicitly manage transactions.
*
* This exception will be thrown when the PDO connection commit() is called.
* Code should never call this method directly.
*/
class DatabaseTransactionExplicitCommitNotAllowedException extends Exception { }
<?php
namespace Drupal\Database;
use Exception;
/**
* Exception thrown when a savepoint or transaction name occurs twice.
*/
class DatabaseTransactionNameNonUniqueException extends Exception { }
<?php
namespace Drupal\Database;
use Exception;
/**
* Exception for when popTransaction() is called with no active transaction.