1
0
mirror of synced 2025-01-30 03:51:43 +03:00

[2.0] Formatting issues.

This commit is contained in:
romanb 2009-05-28 11:30:27 +00:00
parent d80b95964e
commit 6e760bacc0

View File

@ -55,14 +55,14 @@ use Doctrine\Common\DoctrineException;
*/
class Connection
{
/**
* Constant for transaction isolation level READ UNCOMMITTED.
*/
const TRANSACTION_READ_UNCOMMITTED = 1;
/**
* Constant for transaction isolation level READ COMMITTED.
*/
const TRANSACTION_READ_COMMITTED = 2;
/**
* Constant for transaction isolation level READ UNCOMMITTED.
*/
const TRANSACTION_READ_UNCOMMITTED = 1;
/**
* Constant for transaction isolation level READ COMMITTED.
*/
const TRANSACTION_READ_COMMITTED = 2;
/**
* Constant for transaction isolation level REPEATABLE READ.
*/
@ -143,7 +143,7 @@ class Connection
*/
protected $_schemaManager;
/**
/**
* The used DBAL driver.
*
* @var Doctrine\DBAL\Driver
@ -179,18 +179,18 @@ class Connection
// Create default config and event manager if none given
if ( ! $config) {
$config = new Configuration();
}
if ( ! $eventManager) {
}
if ( ! $eventManager) {
$eventManager = new EventManager();
}
}
$this->_config = $config;
$this->_config = $config;
$this->_eventManager = $eventManager;
$this->_platform = $driver->getDatabasePlatform();
$this->_transactionIsolationLevel = $this->_platform->getDefaultTransactionIsolationLevel();
$this->_quoteIdentifiers = $config->getQuoteIdentifiers();
$this->_platform->setQuoteIdentifiers($this->_quoteIdentifiers);
}
}
/**
* Get the array of parameters used to instantiated this connection instance
@ -202,465 +202,466 @@ class Connection
return $this->_params;
}
/**
* Get the name of the database connected to for this Connection instance
*
* @return string $database
*/
public function getDatabase()
{
return $this->_driver->getDatabase($this);
}
/**
* Get the name of the database connected to for this Connection instance
*
* @return string $database
*/
public function getDatabase()
{
return $this->_driver->getDatabase($this);
}
/**
* Gets the DBAL driver instance.
*
* @return Doctrine\DBAL\Driver
*/
public function getDriver()
{
return $this->_driver;
}
/**
* Gets the DBAL driver instance.
*
* @return Doctrine\DBAL\Driver
*/
public function getDriver()
{
return $this->_driver;
}
/**
* Gets the Configuration used by the Connection.
*
* @return Doctrine\DBAL\Configuration
*/
public function getConfiguration()
{
return $this->_config;
}
/**
* Gets the Configuration used by the Connection.
*
* @return Doctrine\DBAL\Configuration
*/
public function getConfiguration()
{
return $this->_config;
}
/**
* Gets the EventManager used by the Connection.
*
* @return Doctrine\Common\EventManager
*/
public function getEventManager()
{
return $this->_eventManager;
}
/**
* Gets the EventManager used by the Connection.
*
* @return Doctrine\Common\EventManager
*/
public function getEventManager()
{
return $this->_eventManager;
}
/**
* Gets the DatabasePlatform for the connection.
*
* @return Doctrine\DBAL\Platforms\AbstractPlatform
*/
public function getDatabasePlatform()
{
return $this->_platform;
}
/**
* Gets the DatabasePlatform for the connection.
*
* @return Doctrine\DBAL\Platforms\AbstractPlatform
*/
public function getDatabasePlatform()
{
return $this->_platform;
}
/**
* Establishes the connection with the database.
*
* @return boolean
*/
public function connect()
{
if ($this->_isConnected) return false;
/**
* Establishes the connection with the database.
*
* @return boolean
*/
public function connect()
{
if ($this->_isConnected) return false;
$driverOptions = isset($this->_params['driverOptions']) ?
$this->_params['driverOptions'] : array();
$user = isset($this->_params['user']) ?
$this->_params['user'] : null;
$password = isset($this->_params['password']) ?
$this->_params['password'] : null;
$driverOptions = isset($this->_params['driverOptions']) ?
$this->_params['driverOptions'] : array();
$user = isset($this->_params['user']) ?
$this->_params['user'] : null;
$password = isset($this->_params['password']) ?
$this->_params['password'] : null;
$this->_conn = $this->_driver->connect(
$this->_params,
$user,
$password,
$driverOptions
);
$this->_conn = $this->_driver->connect(
$this->_params,
$user,
$password,
$driverOptions
);
$this->_isConnected = true;
$this->_isConnected = true;
return true;
}
return true;
}
/**
* Convenience method for PDO::query("...") followed by $stmt->fetch(PDO::FETCH_ASSOC).
*
* @param string $statement The SQL query.
* @param array $params The query parameters.
* @return array
*/
public function fetchRow($statement, array $params = array())
{
return $this->execute($statement, $params)->fetch(\PDO::FETCH_ASSOC);
}
/**
* Convenience method for PDO::query("...") followed by $stmt->fetch(PDO::FETCH_ASSOC).
*
* @param string $statement The SQL query.
* @param array $params The query parameters.
* @return array
*/
public function fetchRow($statement, array $params = array())
{
return $this->execute($statement, $params)->fetch(\PDO::FETCH_ASSOC);
}
/**
* Convenience method for PDO::query("...") followed by $stmt->fetch(PDO::FETCH_NUM).
*
* @param string $statement sql query to be executed
* @param array $params prepared statement params
* @return array
*/
public function fetchArray($statement, array $params = array())
{
return $this->execute($statement, $params)->fetch(\PDO::FETCH_NUM);
}
/**
* Convenience method for PDO::query("...") followed by $stmt->fetch(PDO::FETCH_NUM).
*
* @param string $statement sql query to be executed
* @param array $params prepared statement params
* @return array
*/
public function fetchArray($statement, array $params = array())
{
return $this->execute($statement, $params)->fetch(\PDO::FETCH_NUM);
}
/**
* Convenience method for PDO::query("...") followed by $stmt->fetchAll(PDO::FETCH_COLUMN, ...).
*
* @param string $statement sql query to be executed
* @param array $params prepared statement params
* @param int $colnum 0-indexed column number to retrieve
* @return array
*/
public function fetchColumn($statement, array $params = array(), $colnum = 0)
{
return $this->execute($statement, $params)->fetchAll(\PDO::FETCH_COLUMN, $colnum);
}
/**
* Convenience method for PDO::query("...") followed by $stmt->fetchAll(PDO::FETCH_COLUMN, ...).
*
* @param string $statement sql query to be executed
* @param array $params prepared statement params
* @param int $colnum 0-indexed column number to retrieve
* @return array
*/
public function fetchColumn($statement, array $params = array(), $colnum = 0)
{
return $this->execute($statement, $params)->fetchAll(\PDO::FETCH_COLUMN, $colnum);
}
/**
* Whether an actual connection to the database is established.
*
* @return boolean
*/
public function isConnected()
{
return $this->_isConnected;
}
/**
* Whether an actual connection to the database is established.
*
* @return boolean
*/
public function isConnected()
{
return $this->_isConnected;
}
/**
* Convenience method for PDO::query("...") followed by $stmt->fetchAll(PDO::FETCH_BOTH).
*
* @param string $statement sql query to be executed
* @param array $params prepared statement params
* @return array
*/
public function fetchBoth($statement, array $params = array())
{
return $this->execute($statement, $params)->fetchAll(\PDO::FETCH_BOTH);
}
/**
* Convenience method for PDO::query("...") followed by $stmt->fetchAll(PDO::FETCH_BOTH).
*
* @param string $statement sql query to be executed
* @param array $params prepared statement params
* @return array
*/
public function fetchBoth($statement, array $params = array())
{
return $this->execute($statement, $params)->fetchAll(\PDO::FETCH_BOTH);
}
/**
* Deletes table row(s) matching the specified identifier.
*
* @param string $table The table to delete data from
* @param array $identifier An associateve array containing identifier fieldname-value pairs.
* @return integer The number of affected rows
*/
public function delete($tableName, array $identifier)
{
$this->connect();
$criteria = array();
foreach (array_keys($identifier) as $id) {
$criteria[] = $this->quoteIdentifier($id) . ' = ?';
}
/**
* Deletes table row(s) matching the specified identifier.
*
* @param string $table The table to delete data from
* @param array $identifier An associateve array containing identifier fieldname-value pairs.
* @return integer The number of affected rows
*/
public function delete($tableName, array $identifier)
{
$this->connect();
$criteria = array();
foreach (array_keys($identifier) as $id) {
$criteria[] = $this->quoteIdentifier($id) . ' = ?';
}
$query = 'DELETE FROM '
. $this->quoteIdentifier($tableName)
. ' WHERE ' . implode(' AND ', $criteria);
$query = 'DELETE FROM '
. $this->quoteIdentifier($tableName)
. ' WHERE ' . implode(' AND ', $criteria);
return $this->exec($query, array_values($identifier));
}
return $this->exec($query, array_values($identifier));
}
/**
* Closes the connection.
*
* @return void
*/
public function close()
{
unset($this->_conn);
$this->_isConnected = false;
}
/**
* Closes the connection.
*
* @return void
*/
public function close()
{
unset($this->_conn);
$this->_isConnected = false;
}
/**
* Sets the transaction isolation level.
*
* @param integer $level The level to set.
*/
public function setTransactionIsolation($level)
{
$this->_transactionIsolationLevel = $level;
return $this->exec($this->_platform->getSetTransactionIsolationSql($level));
}
/**
* Sets the transaction isolation level.
*
* @param integer $level The level to set.
*/
public function setTransactionIsolation($level)
{
$this->_transactionIsolationLevel = $level;
return $this->exec($this->_platform->getSetTransactionIsolationSql($level));
}
/**
* Gets the currently active transaction isolation level.
*
* @return integer The current transaction isolation level.
*/
public function getTransactionIsolation()
{
return $this->_transactionIsolationLevel;
}
/**
* Gets the currently active transaction isolation level.
*
* @return integer The current transaction isolation level.
*/
public function getTransactionIsolation()
{
return $this->_transactionIsolationLevel;
}
/**
* Updates table row(s) with specified data
*
* @throws Doctrine\DBAL\ConnectionException if something went wrong at the database level
* @param string $table The table to insert data into
* @param array $values An associateve array containing column-value pairs.
* @return mixed boolean false if empty value array was given,
* otherwise returns the number of affected rows
*/
public function update($tableName, array $data, array $identifier)
{
$this->connect();
if (empty($data)) {
return false;
}
/**
* Updates table row(s) with specified data
*
* @throws Doctrine\DBAL\ConnectionException if something went wrong at the database level
* @param string $table The table to insert data into
* @param array $values An associateve array containing column-value pairs.
* @return mixed boolean false if empty value array was given,
* otherwise returns the number of affected rows
*/
public function update($tableName, array $data, array $identifier)
{
$this->connect();
if (empty($data)) {
return false;
}
$set = array();
foreach ($data as $columnName => $value) {
$set[] = $this->quoteIdentifier($columnName) . ' = ?';
}
$set = array();
foreach ($data as $columnName => $value) {
$set[] = $this->quoteIdentifier($columnName) . ' = ?';
}
$params = array_merge(array_values($data), array_values($identifier));
$params = array_merge(array_values($data), array_values($identifier));
$sql = 'UPDATE ' . $this->quoteIdentifier($tableName)
. ' SET ' . implode(', ', $set)
. ' WHERE ' . implode(' = ? AND ', array_keys($identifier))
. ' = ?';
$sql = 'UPDATE ' . $this->quoteIdentifier($tableName)
. ' SET ' . implode(', ', $set)
. ' WHERE ' . implode(' = ? AND ', array_keys($identifier))
. ' = ?';
return $this->exec($sql, $params);
}
return $this->exec($sql, $params);
}
/**
* Inserts a table row with specified data.
*
* @param string $table The table to insert data into.
* @param array $fields An associateve array containing fieldname-value pairs.
* @return mixed boolean false if empty value array was given,
* otherwise returns the number of affected rows
*/
public function insert($tableName, array $data)
{
$this->connect();
if (empty($data)) {
return false;
}
/**
* Inserts a table row with specified data.
*
* @param string $table The table to insert data into.
* @param array $fields An associateve array containing fieldname-value pairs.
* @return mixed boolean false if empty value array was given,
* otherwise returns the number of affected rows
*/
public function insert($tableName, array $data)
{
$this->connect();
if (empty($data)) {
return false;
}
// column names are specified as array keys
$cols = array();
$a = array();
foreach ($data as $columnName => $value) {
$cols[] = $this->quoteIdentifier($columnName);
$a[] = '?';
}
// column names are specified as array keys
$cols = array();
$a = array();
foreach ($data as $columnName => $value) {
$cols[] = $this->quoteIdentifier($columnName);
$a[] = '?';
}
$query = 'INSERT INTO ' . $this->quoteIdentifier($tableName)
. ' (' . implode(', ', $cols) . ') '
. 'VALUES (';
$query .= implode(', ', $a) . ')';
$query = 'INSERT INTO ' . $this->quoteIdentifier($tableName)
. ' (' . implode(', ', $cols) . ') '
. 'VALUES (';
$query .= implode(', ', $a) . ')';
return $this->exec($query, array_values($data));
}
return $this->exec($query, array_values($data));
}
/**
* Set the charset on the current connection
*
* @param string charset
*/
public function setCharset($charset)
{
$this->exec($this->_platform->getSetCharsetSql($charset));
}
/**
* Set the charset on the current connection
*
* @param string charset
*/
public function setCharset($charset)
{
$this->exec($this->_platform->getSetCharsetSql($charset));
}
/**
* Quote a string so it can be safely used as a table or column name, even if
* it is a reserved name.
*
* Delimiting style depends on the underlying database platform that is being used.
*
* NOTE: Just because you CAN use delimited identifiers doesn't mean
* you SHOULD use them. In general, they end up causing way more
* problems than they solve.
*
* @param string $str identifier name to be quoted
* @param bool $checkOption check the 'quote_identifier' option
*
* @return string quoted identifier string
*/
public function quoteIdentifier($str)
{
if ($this->_quoteIdentifiers) {
return $this->_platform->quoteIdentifier($str);
}
return $str;
}
/**
* Quote a string so it can be safely used as a table or column name, even if
* it is a reserved name.
*
* Delimiting style depends on the underlying database platform that is being used.
*
* NOTE: Just because you CAN use delimited identifiers doesn't mean
* you SHOULD use them. In general, they end up causing way more
* problems than they solve.
*
* @param string $str identifier name to be quoted
* @param bool $checkOption check the 'quote_identifier' option
*
* @return string quoted identifier string
*/
public function quoteIdentifier($str)
{
if ($this->_quoteIdentifiers) {
return $this->_platform->quoteIdentifier($str);
}
return $str;
}
/**
* Quotes a given input parameter.
*
* @param mixed $input Parameter to be quoted.
* @param string $type Type of the parameter.
* @return string The quoted parameter.
*/
public function quote($input, $type = null)
{
$this->connect();
return $this->_conn->quote($input, $type);
}
/**
* Quotes a given input parameter.
*
* @param mixed $input Parameter to be quoted.
* @param string $type Type of the parameter.
* @return string The quoted parameter.
*/
public function quote($input, $type = null)
{
$this->connect();
return $this->_conn->quote($input, $type);
}
/**
* Convenience method for PDO::query("...") followed by $stmt->fetchAll(PDO::FETCH_ASSOC).
*
* @param string $sql The SQL query.
* @param array $params The query parameters.
* @return array
*/
public function fetchAll($sql, array $params = array())
{
return $this->execute($sql, $params)->fetchAll(\PDO::FETCH_ASSOC);
}
/**
* Convenience method for PDO::query("...") followed by $stmt->fetchAll(PDO::FETCH_ASSOC).
*
* @param string $sql The SQL query.
* @param array $params The query parameters.
* @return array
*/
public function fetchAll($sql, array $params = array())
{
return $this->execute($sql, $params)->fetchAll(\PDO::FETCH_ASSOC);
}
/**
* Convenience method for PDO::query("...") followed by $stmt->fetchColumn().
*
* @param string $statement The SQL query.
* @param array $params The query parameters.
* @param int $colnum 0-indexed column number to retrieve
* @return mixed
*/
public function fetchOne($statement, array $params = array(), $colnum = 0)
{
return $this->execute($statement, $params)->fetchColumn($colnum);
}
/**
* Convenience method for PDO::query("...") followed by $stmt->fetchColumn().
*
* @param string $statement The SQL query.
* @param array $params The query parameters.
* @param int $colnum 0-indexed column number to retrieve
* @return mixed
*/
public function fetchOne($statement, array $params = array(), $colnum = 0)
{
return $this->execute($statement, $params)->fetchColumn($colnum);
}
/**
* Prepares an SQL statement.
*
* @param string $statement
* @return Statement
*/
public function prepare($statement)
{
$this->connect();
return $this->_conn->prepare($statement);
}
/**
* Prepares an SQL statement.
*
* @param string $statement
* @return Statement
*/
public function prepare($statement)
{
$this->connect();
return $this->_conn->prepare($statement);
}
/**
* Queries the database with limit and offset added to the query and returns
* a Statement object.
*
* @param string $query
* @param integer $limit
* @param integer $offset
* @return Statement
*/
public function select($query, $limit = 0, $offset = 0)
{
if ($limit > 0 || $offset > 0) {
$query = $this->_platform->modifyLimitQuery($query, $limit, $offset);
}
return $this->execute($query);
}
/**
* Queries the database with limit and offset added to the query and returns
* a Statement object.
*
* @param string $query
* @param integer $limit
* @param integer $offset
* @return Statement
*/
public function select($query, $limit = 0, $offset = 0)
{
if ($limit > 0 || $offset > 0) {
$query = $this->_platform->modifyLimitQuery($query, $limit, $offset);
}
return $this->execute($query);
}
/**
* Executes an SQL SELECT query with the given parameters.
*
* @param string $query sql query
* @param array $params query parameters
*
* @return PDOStatement
*/
public function execute($query, array $params = array())
{
$this->connect();
/**
* Executes an SQL SELECT query with the given parameters.
*
* @param string $query sql query
* @param array $params query parameters
*
* @return PDOStatement
*/
public function execute($query, array $params = array())
{
$this->connect();
if ($this->_config->getSqlLogger()) {
$this->_config->getSqlLogger()->logSql($query, $params);
}
if ($this->_config->getSqlLogger()) {
$this->_config->getSqlLogger()->logSql($query, $params);
}
if ( ! empty($params)) {
$stmt = $this->prepare($query);
$stmt->execute($params);
return $stmt;
} else {
$stmt = $this->_conn->query($query);
$this->_queryCount++;
return $stmt;
}
}
if ( ! empty($params)) {
$stmt = $this->prepare($query);
$stmt->execute($params);
return $stmt;
} else {
$stmt = $this->_conn->query($query);
$this->_queryCount++;
return $stmt;
}
}
/**
* Executes an SQL INSERT/UPDATE/DELETE query with the given parameters.
*
* @param string $query sql query
* @param array $params query parameters
*
* @return PDOStatement
* @todo Rename to executeUpdate().
*/
public function exec($query, array $params = array()) {
$this->connect();
/**
* Executes an SQL INSERT/UPDATE/DELETE query with the given parameters.
*
* @param string $query sql query
* @param array $params query parameters
*
* @return PDOStatement
* @todo Rename to executeUpdate().
*/
public function exec($query, array $params = array())
{
$this->connect();
if ($this->_config->getSqlLogger()) {
$this->_config->getSqlLogger()->logSql($query, $params);
}
if ($this->_config->getSqlLogger()) {
$this->_config->getSqlLogger()->logSql($query, $params);
}
if ( ! empty($params)) {
$stmt = $this->prepare($query);
$stmt->execute($params);
return $stmt->rowCount();
} else {
$count = $this->_conn->exec($query);
$this->_queryCount++;
return $count;
}
}
if ( ! empty($params)) {
$stmt = $this->prepare($query);
$stmt->execute($params);
return $stmt->rowCount();
} else {
$count = $this->_conn->exec($query);
$this->_queryCount++;
return $count;
}
}
/**
* Returns the number of queries executed by the connection.
*
* @return integer
*/
public function getQueryCount()
{
return $this->_queryCount;
}
/**
* Returns the number of queries executed by the connection.
*
* @return integer
*/
public function getQueryCount()
{
return $this->_queryCount;
}
/**
* Returns the current transaction nesting level.
*
* @return integer The nesting level. A value of 0 means theres no active transaction.
*/
public function getTransactionNestingLevel()
{
return $this->_transactionNestingLevel;
}
/**
* Returns the current transaction nesting level.
*
* @return integer The nesting level. A value of 0 means theres no active transaction.
*/
public function getTransactionNestingLevel()
{
return $this->_transactionNestingLevel;
}
/**
* Fetch the SQLSTATE associated with the last operation on the database handle
*
* @return integer
*/
public function errorCode()
{
$this->connect();
return $this->_conn->errorCode();
}
/**
* Fetch the SQLSTATE associated with the last operation on the database handle
*
* @return integer
*/
public function errorCode()
{
$this->connect();
return $this->_conn->errorCode();
}
/**
* Fetch extended error information associated with the last operation on the database handle
*
* @return array
*/
public function errorInfo()
{
$this->connect();
return $this->_conn->errorInfo();
}
/**
* Fetch extended error information associated with the last operation on the database handle
*
* @return array
*/
public function errorInfo()
{
$this->connect();
return $this->_conn->errorInfo();
}
/**
* Returns the ID of the last inserted row, or the last value from a sequence object,
* depending on the underlying driver.
*
* Note: This method may not return a meaningful or consistent result across different drivers,
* because the underlying database may not even support the notion of auto-increment fields or sequences.
*
* @param string $table Name of the table into which a new row was inserted.
* @param string $field Name of the field into which a new row was inserted.
*/
/**
* Returns the ID of the last inserted row, or the last value from a sequence object,
* depending on the underlying driver.
*
* Note: This method may not return a meaningful or consistent result across different drivers,
* because the underlying database may not even support the notion of auto-increment fields or sequences.
*
* @param string $table Name of the table into which a new row was inserted.
* @param string $field Name of the field into which a new row was inserted.
*/
public function lastInsertId($seqName = null)
{
$this->connect();