diff --git a/UPGRADE.md b/UPGRADE.md index 6336d81e8ac..cc238ee34ac 100644 --- a/UPGRADE.md +++ b/UPGRADE.md @@ -12,6 +12,15 @@ ``Doctrine\DBAL\Connection::TRANSACTION_*`` were moved into ``Doctrine\DBAL\TransactionIsolationLevel`` class without the ``TRANSACTION_`` prefix. +## DEPRECATION: direct usage of the PDO APIs in the DBAL API + +1. When calling `Doctrine\DBAL\Driver\Statement` methods, instead of `PDO::PARAM_*` constants, `Doctrine\DBAL\ParameterType` constants should be used. +2. When calling `Doctrine\DBAL\Driver\ResultStatement` methods, instead of `PDO::FETCH_*` constants, `Doctrine\DBAL\FetchMode` constants should be used. +3. When configuring `Doctrine\DBAL\Portability\Connection`, instead of `PDO::CASE_*` constants, `Doctrine\DBAL\ColumnCase` constants should be used. +4. Usage of `PDO::PARAM_INPUT_OUTPUT` in `Doctrine\DBAL\Driver\Statement::bindValue()` is deprecated. +5. Usage of `PDO::FETCH_FUNC` in `Doctrine\DBAL\Driver\ResultStatement::fetch()` is deprecated. +6. Calls to `\PDOStatement` methods on a `\Doctrine\DBAL\Driver\PDOStatement` instance (e.g. `fetchObject()`) are deprecated. + # Upgrade to 2.6 ## MINOR BC BREAK: `fetch()` and `fetchAll()` method signatures in `Doctrine\DBAL\Driver\ResultStatement` diff --git a/docs/en/reference/data-retrieval-and-manipulation.rst b/docs/en/reference/data-retrieval-and-manipulation.rst index 404e8c47282..31397a532b9 100644 --- a/docs/en/reference/data-retrieval-and-manipulation.rst +++ b/docs/en/reference/data-retrieval-and-manipulation.rst @@ -180,13 +180,13 @@ Binding Types ------------- Doctrine DBAL extends PDOs handling of binding types in prepared statements -considerably. Besides the well known ``\PDO::PARAM_*`` constants you +considerably. Besides ``Doctrine\DBAL\ParameterType`` constants, you can make use of two very powerful additional features. Doctrine\DBAL\Types Conversion ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ -If you don't specify an integer (through a ``PDO::PARAM*`` constant) to +If you don't specify an integer (through one of ``Doctrine\DBAL\ParameterType`` constants) to any of the parameter binding methods but a string, Doctrine DBAL will ask the type abstraction layer to convert the passed value from its PHP to a database representation. This way you can pass ``\DateTime`` @@ -271,7 +271,14 @@ be specified as well: // Same SQL WITHOUT usage of Doctrine\DBAL\Connection::PARAM_INT_ARRAY $stmt = $conn->executeQuery('SELECT * FROM articles WHERE id IN (?, ?, ?, ?, ?, ?)', array(1, 2, 3, 4, 5, 6), - array(\PDO::PARAM_INT, \PDO::PARAM_INT, \PDO::PARAM_INT, \PDO::PARAM_INT, \PDO::PARAM_INT, \PDO::PARAM_INT) + array( + ParameterType::INTEGER, + ParameterType::INTEGER, + ParameterType::INTEGER, + ParameterType::INTEGER, + ParameterType::INTEGER, + ParameterType::INTEGER, + ) ); This is much more complicated and is ugly to write generically. @@ -469,8 +476,11 @@ Quote a value: .. code-block:: php quote('value'); - $quoted = $conn->quote('1234', \PDO::PARAM_INT); + $quoted = $conn->quote('1234', ParameterType::INTEGER); quoteIdentifier() ~~~~~~~~~~~~~~~~~ diff --git a/docs/en/reference/known-vendor-issues.rst b/docs/en/reference/known-vendor-issues.rst index d27fe30789f..09c44aeb8f2 100644 --- a/docs/en/reference/known-vendor-issues.rst +++ b/docs/en/reference/known-vendor-issues.rst @@ -187,6 +187,6 @@ The ``PDO_SQLSRV`` driver currently has a bug when binding values to VARBINARY/BLOB columns with ``bindValue`` in prepared statements. This raises an implicit conversion from data type error as it tries to convert a character type value to a binary type value even if -you explicitly define the value as ``\PDO::PARAM_LOB`` type. +you explicitly define the value as ``ParameterType::LARGE_OBJECT`` type. Therefore it is highly encouraged to use the native ``sqlsrv`` driver instead which does not have this limitation. diff --git a/docs/en/reference/portability.rst b/docs/en/reference/portability.rst index a69b59f5bca..6cd6388fbda 100644 --- a/docs/en/reference/portability.rst +++ b/docs/en/reference/portability.rst @@ -51,12 +51,16 @@ Using the following code block in your initialization will: .. code-block:: php 'Doctrine\DBAL\Portability\Connection', - 'portability' => \Doctrine\DBAL\Portability\Connection::PORTABILITY_ALL, - 'fetch_case' => \PDO::CASE_LOWER, + 'wrapperClass' => PortableConnection::class, + 'portability' => PortableConnection::PORTABILITY_ALL, + 'fetch_case' => ColumnCase::LOWER, ); This sort of portability handling is pretty expensive because all the result @@ -80,4 +84,4 @@ This functionality is only implemented with Doctrine 2.1 upwards. Doctrine ships with lists of keywords for every supported vendor. You can access a keyword list through the schema manager of the vendor you are currently using or just instantiating it from the ``Doctrine\DBAL\Platforms\Keywords`` -namespace. \ No newline at end of file +namespace. diff --git a/docs/en/reference/security.rst b/docs/en/reference/security.rst index e8a01f0b7ec..82f159c87fb 100644 --- a/docs/en/reference/security.rst +++ b/docs/en/reference/security.rst @@ -151,7 +151,7 @@ the ``Connection#quote`` method: quote($_GET['username'], \PDO::PARAM_STR); + $sql = "SELECT * FROM users WHERE name = " . $connection->quote($_GET['username']); This method is only available for SQL, not for DQL. For DQL you are always encouraged to use prepared -statements not only for security, but also for caching reasons. \ No newline at end of file +statements not only for security, but also for caching reasons. diff --git a/lib/Doctrine/DBAL/Cache/ArrayStatement.php b/lib/Doctrine/DBAL/Cache/ArrayStatement.php index 2e1cba9fe68..b77f85cacfe 100644 --- a/lib/Doctrine/DBAL/Cache/ArrayStatement.php +++ b/lib/Doctrine/DBAL/Cache/ArrayStatement.php @@ -20,7 +20,7 @@ namespace Doctrine\DBAL\Cache; use Doctrine\DBAL\Driver\ResultStatement; -use PDO; +use Doctrine\DBAL\FetchMode; class ArrayStatement implements \IteratorAggregate, ResultStatement { @@ -42,7 +42,7 @@ class ArrayStatement implements \IteratorAggregate, ResultStatement /** * @var int */ - private $defaultFetchMode = PDO::FETCH_BOTH; + private $defaultFetchMode = FetchMode::MIXED; /** * @param array $data @@ -100,23 +100,30 @@ public function getIterator() */ public function fetch($fetchMode = null, $cursorOrientation = \PDO::FETCH_ORI_NEXT, $cursorOffset = 0) { - if (isset($this->data[$this->num])) { - $row = $this->data[$this->num++]; - $fetchMode = $fetchMode ?: $this->defaultFetchMode; - if ($fetchMode === PDO::FETCH_ASSOC) { - return $row; - } elseif ($fetchMode === PDO::FETCH_NUM) { - return array_values($row); - } elseif ($fetchMode === PDO::FETCH_BOTH) { - return array_merge($row, array_values($row)); - } elseif ($fetchMode === PDO::FETCH_COLUMN) { - return reset($row); - } else { - throw new \InvalidArgumentException("Invalid fetch-style given for fetching result."); - } + if (! isset($this->data[$this->num])) { + return false; } - return false; + $row = $this->data[$this->num++]; + $fetchMode = $fetchMode ?: $this->defaultFetchMode; + + if ($fetchMode === FetchMode::ASSOCIATIVE) { + return $row; + } + + if ($fetchMode === FetchMode::NUMERIC) { + return array_values($row); + } + + if ($fetchMode === FetchMode::MIXED) { + return array_merge($row, array_values($row)); + } + + if ($fetchMode === FetchMode::COLUMN) { + return reset($row); + } + + throw new \InvalidArgumentException('Invalid fetch-style given for fetching result.'); } /** @@ -137,7 +144,7 @@ public function fetchAll($fetchMode = null, $fetchArgument = null, $ctorArgs = n */ public function fetchColumn($columnIndex = 0) { - $row = $this->fetch(PDO::FETCH_NUM); + $row = $this->fetch(FetchMode::NUMERIC); // TODO: verify that return false is the correct behavior return $row[$columnIndex] ?? false; diff --git a/lib/Doctrine/DBAL/Cache/ResultCacheStatement.php b/lib/Doctrine/DBAL/Cache/ResultCacheStatement.php index c36c60e6c49..cc5b4e3e941 100644 --- a/lib/Doctrine/DBAL/Cache/ResultCacheStatement.php +++ b/lib/Doctrine/DBAL/Cache/ResultCacheStatement.php @@ -22,7 +22,7 @@ use Doctrine\DBAL\Driver\Statement; use Doctrine\DBAL\Driver\ResultStatement; use Doctrine\Common\Cache\Cache; -use PDO; +use Doctrine\DBAL\FetchMode; /** * Cache statement for SQL results. @@ -80,7 +80,7 @@ class ResultCacheStatement implements \IteratorAggregate, ResultStatement /** * @var int */ - private $defaultFetchMode = PDO::FETCH_BOTH; + private $defaultFetchMode = FetchMode::MIXED; /** * @param \Doctrine\DBAL\Driver\Statement $stmt @@ -153,24 +153,32 @@ public function fetch($fetchMode = null, $cursorOrientation = \PDO::FETCH_ORI_NE $this->data = []; } - $row = $this->statement->fetch(PDO::FETCH_ASSOC); + $row = $this->statement->fetch(FetchMode::ASSOCIATIVE); + if ($row) { $this->data[] = $row; $fetchMode = $fetchMode ?: $this->defaultFetchMode; - if ($fetchMode == PDO::FETCH_ASSOC) { + if ($fetchMode === FetchMode::ASSOCIATIVE) { return $row; - } elseif ($fetchMode == PDO::FETCH_NUM) { + } + + if ($fetchMode === FetchMode::NUMERIC) { return array_values($row); - } elseif ($fetchMode == PDO::FETCH_BOTH) { + } + + if ($fetchMode === FetchMode::MIXED) { return array_merge($row, array_values($row)); - } elseif ($fetchMode == PDO::FETCH_COLUMN) { + } + + if ($fetchMode === FetchMode::COLUMN) { return reset($row); - } else { - throw new \InvalidArgumentException("Invalid fetch-style given for caching result."); } + + throw new \InvalidArgumentException('Invalid fetch-style given for caching result.'); } + $this->emptied = true; return false; @@ -194,7 +202,7 @@ public function fetchAll($fetchMode = null, $fetchArgument = null, $ctorArgs = n */ public function fetchColumn($columnIndex = 0) { - $row = $this->fetch(PDO::FETCH_NUM); + $row = $this->fetch(FetchMode::NUMERIC); // TODO: verify that return false is the correct behavior return $row[$columnIndex] ?? false; diff --git a/lib/Doctrine/DBAL/ColumnCase.php b/lib/Doctrine/DBAL/ColumnCase.php new file mode 100644 index 00000000000..420d868f582 --- /dev/null +++ b/lib/Doctrine/DBAL/ColumnCase.php @@ -0,0 +1,30 @@ +executeQuery($statement, $params, $types)->fetch(PDO::FETCH_ASSOC); + return $this->executeQuery($statement, $params, $types)->fetch(FetchMode::ASSOCIATIVE); } /** @@ -578,7 +577,7 @@ public function fetchAssoc($statement, array $params = [], array $types = []) */ public function fetchArray($statement, array $params = [], array $types = []) { - return $this->executeQuery($statement, $params, $types)->fetch(PDO::FETCH_NUM); + return $this->executeQuery($statement, $params, $types)->fetch(FetchMode::NUMERIC); } /** @@ -808,7 +807,7 @@ private function extractTypeValues(array $columnList, array $types) $typeValues = []; foreach ($columnList as $columnIndex => $columnName) { - $typeValues[] = $types[$columnName] ?? \PDO::PARAM_STR; + $typeValues[] = $types[$columnName] ?? ParameterType::STRING; } return $typeValues; @@ -1592,7 +1591,7 @@ private function getBindingInfo($value, $type) $value = $type->convertToDatabaseValue($value, $this->getDatabasePlatform()); $bindingType = $type->getBindingType(); } else { - $bindingType = $type; // PDO::PARAM_* constants + $bindingType = $type; } return [$value, $bindingType]; diff --git a/lib/Doctrine/DBAL/Driver/Connection.php b/lib/Doctrine/DBAL/Driver/Connection.php index 833b2430c47..773407510a6 100644 --- a/lib/Doctrine/DBAL/Driver/Connection.php +++ b/lib/Doctrine/DBAL/Driver/Connection.php @@ -19,6 +19,8 @@ namespace Doctrine\DBAL\Driver; +use Doctrine\DBAL\ParameterType; + /** * Connection interface. * Driver connections must implement this interface. @@ -36,14 +38,14 @@ interface Connection * * @return \Doctrine\DBAL\Driver\Statement */ - function prepare($prepareString); + public function prepare($prepareString); /** * Executes an SQL statement, returning a result set as a Statement object. * * @return \Doctrine\DBAL\Driver\Statement */ - function query(); + public function query(); /** * Quotes a string for use in a query. @@ -53,7 +55,7 @@ function query(); * * @return mixed */ - function quote($input, $type=\PDO::PARAM_STR); + public function quote($input, $type = ParameterType::STRING); /** * Executes an SQL statement and return the number of affected rows. @@ -62,7 +64,7 @@ function quote($input, $type=\PDO::PARAM_STR); * * @return int */ - function exec($statement); + public function exec($statement); /** * Returns the ID of the last inserted row or sequence value. @@ -71,40 +73,40 @@ function exec($statement); * * @return string */ - function lastInsertId($name = null); + public function lastInsertId($name = null); /** * Initiates a transaction. * * @return bool TRUE on success or FALSE on failure. */ - function beginTransaction(); + public function beginTransaction(); /** * Commits a transaction. * * @return bool TRUE on success or FALSE on failure. */ - function commit(); + public function commit(); /** * Rolls back the current transaction, as initiated by beginTransaction(). * * @return bool TRUE on success or FALSE on failure. */ - function rollBack(); + public function rollBack(); /** * Returns the error code associated with the last operation on the database handle. * * @return string|null The error code, or null if no operation has been run on the database handle. */ - function errorCode(); + public function errorCode(); /** * Returns extended error information associated with the last operation on the database handle. * * @return array */ - function errorInfo(); + public function errorInfo(); } diff --git a/lib/Doctrine/DBAL/Driver/DrizzlePDOMySql/Connection.php b/lib/Doctrine/DBAL/Driver/DrizzlePDOMySql/Connection.php index 61a23e772dd..bfc61acf9b8 100644 --- a/lib/Doctrine/DBAL/Driver/DrizzlePDOMySql/Connection.php +++ b/lib/Doctrine/DBAL/Driver/DrizzlePDOMySql/Connection.php @@ -19,6 +19,8 @@ namespace Doctrine\DBAL\Driver\DrizzlePDOMySql; +use Doctrine\DBAL\ParameterType; + /** * @author Kim Hemsø Rasmussen */ @@ -27,9 +29,9 @@ class Connection extends \Doctrine\DBAL\Driver\PDOConnection /** * {@inheritdoc} */ - public function quote($value, $type = \PDO::PARAM_STR) + public function quote($value, $type = ParameterType::STRING) { - if (\PDO::PARAM_BOOL === $type) { + if ($type === ParameterType::BOOLEAN) { return $value ? 'true' : 'false'; } diff --git a/lib/Doctrine/DBAL/Driver/IBMDB2/DB2Connection.php b/lib/Doctrine/DBAL/Driver/IBMDB2/DB2Connection.php index b538a2f240f..be43fc5f88b 100644 --- a/lib/Doctrine/DBAL/Driver/IBMDB2/DB2Connection.php +++ b/lib/Doctrine/DBAL/Driver/IBMDB2/DB2Connection.php @@ -21,6 +21,7 @@ use Doctrine\DBAL\Driver\Connection; use Doctrine\DBAL\Driver\ServerInfoAwareConnection; +use Doctrine\DBAL\ParameterType; class DB2Connection implements Connection, ServerInfoAwareConnection { @@ -98,10 +99,11 @@ public function query() /** * {@inheritdoc} */ - public function quote($input, $type=\PDO::PARAM_STR) + public function quote($input, $type = ParameterType::STRING) { $input = db2_escape_string($input); - if ($type == \PDO::PARAM_INT) { + + if ($type === ParameterType::INTEGER) { return $input; } diff --git a/lib/Doctrine/DBAL/Driver/IBMDB2/DB2Statement.php b/lib/Doctrine/DBAL/Driver/IBMDB2/DB2Statement.php index 21f341a8e8f..9a8ba334db6 100644 --- a/lib/Doctrine/DBAL/Driver/IBMDB2/DB2Statement.php +++ b/lib/Doctrine/DBAL/Driver/IBMDB2/DB2Statement.php @@ -21,6 +21,8 @@ use Doctrine\DBAL\Driver\Statement; use Doctrine\DBAL\Driver\StatementIterator; +use Doctrine\DBAL\FetchMode; +use Doctrine\DBAL\ParameterType; class DB2Statement implements \IteratorAggregate, Statement { @@ -35,19 +37,19 @@ class DB2Statement implements \IteratorAggregate, Statement private $_bindParam = []; /** - * @var string Name of the default class to instantiate when fetch mode is \PDO::FETCH_CLASS. + * @var string Name of the default class to instantiate when fetching class instances. */ private $defaultFetchClass = '\stdClass'; /** - * @var string Constructor arguments for the default class to instantiate when fetch mode is \PDO::FETCH_CLASS. + * @var string Constructor arguments for the default class to instantiate when fetching class instances. */ private $defaultFetchClassCtorArgs = []; /** * @var int */ - private $_defaultFetchMode = \PDO::FETCH_BOTH; + private $_defaultFetchMode = FetchMode::MIXED; /** * Indicates whether the statement is in the state when fetching results is possible @@ -62,8 +64,8 @@ class DB2Statement implements \IteratorAggregate, Statement * @var array */ static private $_typeMap = [ - \PDO::PARAM_INT => DB2_LONG, - \PDO::PARAM_STR => DB2_CHAR, + ParameterType::INTEGER => DB2_LONG, + ParameterType::STRING => DB2_CHAR, ]; /** @@ -77,7 +79,7 @@ public function __construct($stmt) /** * {@inheritdoc} */ - public function bindValue($param, $value, $type = null) + public function bindValue($param, $value, $type = ParameterType::STRING) { return $this->bindParam($param, $value, $type); } @@ -85,7 +87,7 @@ public function bindValue($param, $value, $type = null) /** * {@inheritdoc} */ - public function bindParam($column, &$variable, $type = null, $length = null) + public function bindParam($column, &$variable, $type = ParameterType::STRING, $length = null) { $this->_bindParam[$column] =& $variable; @@ -216,14 +218,16 @@ public function fetch($fetchMode = null, $cursorOrientation = \PDO::FETCH_ORI_NE $fetchMode = $fetchMode ?: $this->_defaultFetchMode; switch ($fetchMode) { - case \PDO::FETCH_COLUMN: + case FetchMode::COLUMN: return $this->fetchColumn(); - case \PDO::FETCH_BOTH: + case FetchMode::MIXED: return db2_fetch_both($this->_stmt); - case \PDO::FETCH_ASSOC: + + case FetchMode::ASSOCIATIVE: return db2_fetch_assoc($this->_stmt); - case \PDO::FETCH_CLASS: + + case FetchMode::CUSTOM_OBJECT: $className = $this->defaultFetchClass; $ctorArgs = $this->defaultFetchClassCtorArgs; @@ -240,10 +244,13 @@ public function fetch($fetchMode = null, $cursorOrientation = \PDO::FETCH_ORI_NE } return $result; - case \PDO::FETCH_NUM: + + case FetchMode::NUMERIC: return db2_fetch_array($this->_stmt); - case \PDO::FETCH_OBJ: + + case FetchMode::STANDARD_OBJECT: return db2_fetch_object($this->_stmt); + default: throw new DB2Exception('Given Fetch-Style ' . $fetchMode . ' is not supported.'); } @@ -257,12 +264,12 @@ public function fetchAll($fetchMode = null, $fetchArgument = null, $ctorArgs = n $rows = []; switch ($fetchMode) { - case \PDO::FETCH_CLASS: + case FetchMode::CUSTOM_OBJECT: while ($row = call_user_func_array([$this, 'fetch'], func_get_args())) { $rows[] = $row; } break; - case \PDO::FETCH_COLUMN: + case FetchMode::COLUMN: while ($row = $this->fetchColumn()) { $rows[] = $row; } @@ -281,7 +288,7 @@ public function fetchAll($fetchMode = null, $fetchArgument = null, $ctorArgs = n */ public function fetchColumn($columnIndex = 0) { - $row = $this->fetch(\PDO::FETCH_NUM); + $row = $this->fetch(FetchMode::NUMERIC); if (false === $row) { return false; diff --git a/lib/Doctrine/DBAL/Driver/Mysqli/MysqliConnection.php b/lib/Doctrine/DBAL/Driver/Mysqli/MysqliConnection.php index 182fd5817fb..b3314b46dfb 100644 --- a/lib/Doctrine/DBAL/Driver/Mysqli/MysqliConnection.php +++ b/lib/Doctrine/DBAL/Driver/Mysqli/MysqliConnection.php @@ -22,6 +22,7 @@ use Doctrine\DBAL\Driver\Connection as Connection; use Doctrine\DBAL\Driver\PingableConnection; use Doctrine\DBAL\Driver\ServerInfoAwareConnection; +use Doctrine\DBAL\ParameterType; /** * @author Kim Hemsø Rasmussen @@ -145,7 +146,7 @@ public function query() /** * {@inheritdoc} */ - public function quote($input, $type=\PDO::PARAM_STR) + public function quote($input, $type = ParameterType::STRING) { return "'". $this->_conn->escape_string($input) ."'"; } diff --git a/lib/Doctrine/DBAL/Driver/Mysqli/MysqliStatement.php b/lib/Doctrine/DBAL/Driver/Mysqli/MysqliStatement.php index e27951cccaa..38b05e318b2 100644 --- a/lib/Doctrine/DBAL/Driver/Mysqli/MysqliStatement.php +++ b/lib/Doctrine/DBAL/Driver/Mysqli/MysqliStatement.php @@ -21,7 +21,8 @@ use Doctrine\DBAL\Driver\Statement; use Doctrine\DBAL\Driver\StatementIterator; -use PDO; +use Doctrine\DBAL\FetchMode; +use Doctrine\DBAL\ParameterType; /** * @author Kim Hemsø Rasmussen @@ -32,11 +33,13 @@ class MysqliStatement implements \IteratorAggregate, Statement * @var array */ protected static $_paramTypeMap = [ - PDO::PARAM_STR => 's', - PDO::PARAM_BOOL => 'i', - PDO::PARAM_NULL => 's', - PDO::PARAM_INT => 'i', - PDO::PARAM_LOB => 's' // TODO Support LOB bigger then max package size. + ParameterType::STRING => 's', + ParameterType::BOOLEAN => 'i', + ParameterType::NULL => 's', + ParameterType::INTEGER => 'i', + + // TODO Support LOB bigger then max package size + ParameterType::LARGE_OBJECT => 's', ]; /** @@ -79,7 +82,7 @@ class MysqliStatement implements \IteratorAggregate, Statement /** * @var int */ - protected $_defaultFetchMode = PDO::FETCH_BOTH; + protected $_defaultFetchMode = FetchMode::MIXED; /** * Indicates whether the statement is in the state when fetching results is possible @@ -112,7 +115,7 @@ public function __construct(\mysqli $conn, $prepareString) /** * {@inheritdoc} */ - public function bindParam($column, &$variable, $type = null, $length = null) + public function bindParam($column, &$variable, $type = ParameterType::STRING, $length = null) { if (null === $type) { $type = 's'; @@ -133,7 +136,7 @@ public function bindParam($column, &$variable, $type = null, $length = null) /** * {@inheritdoc} */ - public function bindValue($param, $value, $type = null) + public function bindValue($param, $value, $type = ParameterType::STRING) { if (null === $type) { $type = 's'; @@ -274,7 +277,7 @@ public function fetch($fetchMode = null, $cursorOrientation = \PDO::FETCH_ORI_NE $fetchMode = $fetchMode ?: $this->_defaultFetchMode; - if ($fetchMode === PDO::FETCH_COLUMN) { + if ($fetchMode === FetchMode::COLUMN) { return $this->fetchColumn(); } @@ -288,19 +291,19 @@ public function fetch($fetchMode = null, $cursorOrientation = \PDO::FETCH_ORI_NE } switch ($fetchMode) { - case PDO::FETCH_NUM: + case FetchMode::NUMERIC: return $values; - case PDO::FETCH_ASSOC: + case FetchMode::ASSOCIATIVE: return array_combine($this->_columnNames, $values); - case PDO::FETCH_BOTH: + case FetchMode::MIXED: $ret = array_combine($this->_columnNames, $values); $ret += $values; return $ret; - case PDO::FETCH_OBJ: + case FetchMode::STANDARD_OBJECT: $assoc = array_combine($this->_columnNames, $values); $ret = new \stdClass(); @@ -323,7 +326,8 @@ public function fetchAll($fetchMode = null, $fetchArgument = null, $ctorArgs = n $fetchMode = $fetchMode ?: $this->_defaultFetchMode; $rows = []; - if (PDO::FETCH_COLUMN == $fetchMode) { + + if ($fetchMode === FetchMode::COLUMN) { while (($row = $this->fetchColumn()) !== false) { $rows[] = $row; } @@ -341,7 +345,8 @@ public function fetchAll($fetchMode = null, $fetchArgument = null, $ctorArgs = n */ public function fetchColumn($columnIndex = 0) { - $row = $this->fetch(PDO::FETCH_NUM); + $row = $this->fetch(FetchMode::NUMERIC); + if (false === $row) { return false; } diff --git a/lib/Doctrine/DBAL/Driver/OCI8/OCI8Connection.php b/lib/Doctrine/DBAL/Driver/OCI8/OCI8Connection.php index 95898aa8b4c..9d68bea7de1 100644 --- a/lib/Doctrine/DBAL/Driver/OCI8/OCI8Connection.php +++ b/lib/Doctrine/DBAL/Driver/OCI8/OCI8Connection.php @@ -21,6 +21,7 @@ use Doctrine\DBAL\Driver\Connection; use Doctrine\DBAL\Driver\ServerInfoAwareConnection; +use Doctrine\DBAL\ParameterType; /** * OCI8 implementation of the Connection interface. @@ -120,7 +121,7 @@ public function query() /** * {@inheritdoc} */ - public function quote($value, $type=\PDO::PARAM_STR) + public function quote($value, $type = ParameterType::STRING) { if (is_int($value) || is_float($value)) { return $value; @@ -152,13 +153,13 @@ public function lastInsertId($name = null) $sql = 'SELECT ' . $name . '.CURRVAL FROM DUAL'; $stmt = $this->query($sql); - $result = $stmt->fetch(\PDO::FETCH_ASSOC); + $result = $stmt->fetchColumn(); - if ($result === false || !isset($result['CURRVAL'])) { + if ($result === false) { throw new OCI8Exception("lastInsertId failed: Query was executed but no result was returned."); } - return (int) $result['CURRVAL']; + return (int) $result; } /** diff --git a/lib/Doctrine/DBAL/Driver/OCI8/OCI8Statement.php b/lib/Doctrine/DBAL/Driver/OCI8/OCI8Statement.php index 09770e05b0d..66bedeab5a3 100644 --- a/lib/Doctrine/DBAL/Driver/OCI8/OCI8Statement.php +++ b/lib/Doctrine/DBAL/Driver/OCI8/OCI8Statement.php @@ -21,8 +21,9 @@ use Doctrine\DBAL\Driver\Statement; use Doctrine\DBAL\Driver\StatementIterator; +use Doctrine\DBAL\FetchMode; +use Doctrine\DBAL\ParameterType; use IteratorAggregate; -use PDO; /** * The OCI8 implementation of the Statement interface. @@ -56,16 +57,16 @@ class OCI8Statement implements IteratorAggregate, Statement * @var array */ protected static $fetchModeMap = [ - PDO::FETCH_BOTH => OCI_BOTH, - PDO::FETCH_ASSOC => OCI_ASSOC, - PDO::FETCH_NUM => OCI_NUM, - PDO::FETCH_COLUMN => OCI_NUM, + FetchMode::MIXED => OCI_BOTH, + FetchMode::ASSOCIATIVE => OCI_ASSOC, + FetchMode::NUMERIC => OCI_NUM, + FetchMode::COLUMN => OCI_NUM, ]; /** * @var int */ - protected $_defaultFetchMode = PDO::FETCH_BOTH; + protected $_defaultFetchMode = FetchMode::MIXED; /** * @var array @@ -254,7 +255,7 @@ private static function findToken($statement, &$offset, $regex) /** * {@inheritdoc} */ - public function bindValue($param, $value, $type = null) + public function bindValue($param, $value, $type = ParameterType::STRING) { return $this->bindParam($param, $value, $type, null); } @@ -262,11 +263,11 @@ public function bindValue($param, $value, $type = null) /** * {@inheritdoc} */ - public function bindParam($column, &$variable, $type = null, $length = null) + public function bindParam($column, &$variable, $type = ParameterType::STRING, $length = null) { $column = $this->_paramMap[$column] ?? $column; - if ($type == \PDO::PARAM_LOB) { + if ($type === ParameterType::LARGE_OBJECT) { $lob = oci_new_descriptor($this->_dbh, OCI_D_LOB); $lob->writeTemporary($variable, OCI_TEMP_BLOB); @@ -387,11 +388,11 @@ public function fetch($fetchMode = null, $cursorOrientation = \PDO::FETCH_ORI_NE $fetchMode = $fetchMode ?: $this->_defaultFetchMode; - if ($fetchMode === PDO::FETCH_COLUMN) { + if ($fetchMode === FetchMode::COLUMN) { return $this->fetchColumn(); } - if (PDO::FETCH_OBJ == $fetchMode) { + if ($fetchMode === FetchMode::STANDARD_OBJECT) { return oci_fetch_object($this->_sth); } @@ -414,7 +415,7 @@ public function fetchAll($fetchMode = null, $fetchArgument = null, $ctorArgs = n $result = []; - if (PDO::FETCH_OBJ == $fetchMode) { + if ($fetchMode === FetchMode::STANDARD_OBJECT) { while ($row = $this->fetch($fetchMode)) { $result[] = $row; } @@ -432,7 +433,8 @@ public function fetchAll($fetchMode = null, $fetchArgument = null, $ctorArgs = n } } else { $fetchStructure = OCI_FETCHSTATEMENT_BY_ROW; - if ($fetchMode == PDO::FETCH_COLUMN) { + + if ($fetchMode === FetchMode::COLUMN) { $fetchStructure = OCI_FETCHSTATEMENT_BY_COLUMN; } @@ -445,7 +447,7 @@ public function fetchAll($fetchMode = null, $fetchArgument = null, $ctorArgs = n oci_fetch_all($this->_sth, $result, 0, -1, self::$fetchModeMap[$fetchMode] | OCI_RETURN_NULLS | $fetchStructure | OCI_RETURN_LOBS); - if ($fetchMode == PDO::FETCH_COLUMN) { + if ($fetchMode === FetchMode::COLUMN) { $result = $result[0]; } } diff --git a/lib/Doctrine/DBAL/Driver/PDOConnection.php b/lib/Doctrine/DBAL/Driver/PDOConnection.php index cb47c3e9a86..91629f65bdb 100644 --- a/lib/Doctrine/DBAL/Driver/PDOConnection.php +++ b/lib/Doctrine/DBAL/Driver/PDOConnection.php @@ -19,6 +19,7 @@ namespace Doctrine\DBAL\Driver; +use Doctrine\DBAL\ParameterType; use PDO; /** @@ -110,7 +111,7 @@ public function query() /** * {@inheritdoc} */ - public function quote($input, $type = \PDO::PARAM_STR) + public function quote($input, $type = ParameterType::STRING) { return parent::quote($input, $type); } diff --git a/lib/Doctrine/DBAL/Driver/PDOSqlsrv/Connection.php b/lib/Doctrine/DBAL/Driver/PDOSqlsrv/Connection.php index 8c561555cff..36747110109 100644 --- a/lib/Doctrine/DBAL/Driver/PDOSqlsrv/Connection.php +++ b/lib/Doctrine/DBAL/Driver/PDOSqlsrv/Connection.php @@ -20,6 +20,7 @@ namespace Doctrine\DBAL\Driver\PDOSqlsrv; use Doctrine\DBAL\Driver\PDOConnection; +use Doctrine\DBAL\ParameterType; /** * Sqlsrv Connection implementation. @@ -55,7 +56,7 @@ public function lastInsertId($name = null) /** * {@inheritDoc} */ - public function quote($value, $type=\PDO::PARAM_STR) + public function quote($value, $type = ParameterType::STRING) { $val = parent::quote($value, $type); diff --git a/lib/Doctrine/DBAL/Driver/PDOSqlsrv/Statement.php b/lib/Doctrine/DBAL/Driver/PDOSqlsrv/Statement.php index 3d1b22a656e..82e2c3e3565 100644 --- a/lib/Doctrine/DBAL/Driver/PDOSqlsrv/Statement.php +++ b/lib/Doctrine/DBAL/Driver/PDOSqlsrv/Statement.php @@ -20,6 +20,7 @@ namespace Doctrine\DBAL\Driver\PDOSqlsrv; use Doctrine\DBAL\Driver\PDOStatement; +use Doctrine\DBAL\ParameterType; use PDO; /** @@ -30,9 +31,9 @@ class Statement extends PDOStatement /** * {@inheritdoc} */ - public function bindParam($column, &$variable, $type = PDO::PARAM_STR, $length = null, $driverOptions = null) + public function bindParam($column, &$variable, $type = ParameterType::STRING, $length = null, $driverOptions = null) { - if ($type === PDO::PARAM_LOB && $driverOptions === null) { + if ($type === ParameterType::LARGE_OBJECT && $driverOptions === null) { $driverOptions = PDO::SQLSRV_ENCODING_BINARY; } @@ -42,7 +43,7 @@ public function bindParam($column, &$variable, $type = PDO::PARAM_STR, $length = /** * {@inheritdoc} */ - public function bindValue($param, $value, $type = PDO::PARAM_STR) + public function bindValue($param, $value, $type = ParameterType::STRING) { return $this->bindParam($param, $value, $type); } diff --git a/lib/Doctrine/DBAL/Driver/PDOStatement.php b/lib/Doctrine/DBAL/Driver/PDOStatement.php index deb35de0162..9e45eb9ba29 100644 --- a/lib/Doctrine/DBAL/Driver/PDOStatement.php +++ b/lib/Doctrine/DBAL/Driver/PDOStatement.php @@ -19,6 +19,10 @@ namespace Doctrine\DBAL\Driver; +use Doctrine\DBAL\FetchMode; +use Doctrine\DBAL\ParameterType; +use PDO; + /** * The PDO implementation of the Statement interface. * Used by all PDO-based drivers. @@ -27,6 +31,29 @@ */ class PDOStatement extends \PDOStatement implements Statement { + /** + * @var int[] + */ + private const PARAM_TYPE_MAP = [ + ParameterType::NULL => PDO::PARAM_NULL, + ParameterType::INTEGER => PDO::PARAM_INT, + ParameterType::STRING => PDO::PARAM_STR, + ParameterType::LARGE_OBJECT => PDO::PARAM_LOB, + ParameterType::BOOLEAN => PDO::PARAM_BOOL, + ]; + + /** + * @var int[] + */ + private const FETCH_MODE_MAP = [ + FetchMode::ASSOCIATIVE => PDO::FETCH_ASSOC, + FetchMode::NUMERIC => PDO::FETCH_NUM, + FetchMode::MIXED => PDO::FETCH_BOTH, + FetchMode::STANDARD_OBJECT => PDO::FETCH_OBJ, + FetchMode::COLUMN => PDO::FETCH_COLUMN, + FetchMode::CUSTOM_OBJECT => PDO::FETCH_CLASS, + ]; + /** * Protected constructor. */ @@ -39,6 +66,8 @@ protected function __construct() */ public function setFetchMode($fetchMode, $arg2 = null, $arg3 = null) { + $fetchMode = $this->convertFetchMode($fetchMode); + // This thin wrapper is necessary to shield against the weird signature // of PDOStatement::setFetchMode(): even if the second and third // parameters are optional, PHP will not let us remove it from this @@ -61,8 +90,10 @@ public function setFetchMode($fetchMode, $arg2 = null, $arg3 = null) /** * {@inheritdoc} */ - public function bindValue($param, $value, $type = \PDO::PARAM_STR) + public function bindValue($param, $value, $type = ParameterType::STRING) { + $type = $this->convertParamType($type); + try { return parent::bindValue($param, $value, $type); } catch (\PDOException $exception) { @@ -73,8 +104,10 @@ public function bindValue($param, $value, $type = \PDO::PARAM_STR) /** * {@inheritdoc} */ - public function bindParam($column, &$variable, $type = \PDO::PARAM_STR, $length = null, $driverOptions = null) + public function bindParam($column, &$variable, $type = ParameterType::STRING, $length = null, $driverOptions = null) { + $type = $this->convertParamType($type); + try { return parent::bindParam($column, $variable, $type, $length, $driverOptions); } catch (\PDOException $exception) { @@ -113,6 +146,8 @@ public function execute($params = null) */ public function fetch($fetchMode = null, $cursorOrientation = \PDO::FETCH_ORI_NEXT, $cursorOffset = 0) { + $fetchMode = $this->convertFetchMode($fetchMode); + try { if ($fetchMode === null && \PDO::FETCH_ORI_NEXT === $cursorOrientation && 0 === $cursorOffset) { return parent::fetch(); @@ -137,6 +172,8 @@ public function fetch($fetchMode = null, $cursorOrientation = \PDO::FETCH_ORI_NE */ public function fetchAll($fetchMode = null, $fetchArgument = null, $ctorArgs = null) { + $fetchMode = $this->convertFetchMode($fetchMode); + try { if ($fetchMode === null && null === $fetchArgument && null === $ctorArgs) { return parent::fetchAll(); @@ -167,4 +204,36 @@ public function fetchColumn($columnIndex = 0) throw new PDOException($exception); } } + + /** + * Converts DBAL parameter type to PDO parameter type + * + * @param int $type Parameter type + */ + private function convertParamType(int $type) : int + { + if (! isset(self::PARAM_TYPE_MAP[$type])) { + throw new \InvalidArgumentException('Invalid parameter type: ' . $type); + } + + return self::PARAM_TYPE_MAP[$type]; + } + + /** + * Converts DBAL fetch mode to PDO fetch mode + * + * @param int|null $fetchMode Fetch mode + */ + private function convertFetchMode(?int $fetchMode) : ?int + { + if ($fetchMode === null) { + return null; + } + + if (! isset(self::FETCH_MODE_MAP[$fetchMode])) { + throw new \InvalidArgumentException('Invalid fetch mode: ' . $fetchMode); + } + + return self::FETCH_MODE_MAP[$fetchMode]; + } } diff --git a/lib/Doctrine/DBAL/Driver/ResultStatement.php b/lib/Doctrine/DBAL/Driver/ResultStatement.php index 92b9de7d0b3..b03862ff6e3 100644 --- a/lib/Doctrine/DBAL/Driver/ResultStatement.php +++ b/lib/Doctrine/DBAL/Driver/ResultStatement.php @@ -45,13 +45,11 @@ public function columnCount(); /** * Sets the fetch mode to use while iterating this statement. * - * @param int $fetchMode The fetch mode must be one of the PDO::FETCH_* constants. + * @param int $fetchMode The fetch mode must be one of the {@link \Doctrine\DBAL\FetchMode} constants. * @param mixed $arg2 * @param mixed $arg3 * * @return bool - * - * @see PDO::FETCH_* constants. */ public function setFetchMode($fetchMode, $arg2 = null, $arg3 = null); @@ -59,8 +57,8 @@ public function setFetchMode($fetchMode, $arg2 = null, $arg3 = null); * Returns the next row of a result set. * * @param int|null $fetchMode Controls how the next row will be returned to the caller. - * The value must be one of the \PDO::FETCH_* constants, - * defaulting to \PDO::FETCH_BOTH. + * The value must be one of the {@link \Doctrine\DBAL\FetchMode} constants, + * defaulting to {@link \Doctrine\DBAL\FetchMode::MIXED}. * @param int $cursorOrientation For a ResultStatement object representing a scrollable cursor, * this value determines which row will be returned to the caller. * This value must be one of the \PDO::FETCH_ORI_* constants, @@ -79,8 +77,6 @@ public function setFetchMode($fetchMode, $arg2 = null, $arg3 = null); * * @return mixed The return value of this method on success depends on the fetch mode. In all cases, FALSE is * returned on failure. - * - * @see PDO::FETCH_* constants. */ public function fetch($fetchMode = null, $cursorOrientation = \PDO::FETCH_ORI_NEXT, $cursorOffset = 0); @@ -88,21 +84,21 @@ public function fetch($fetchMode = null, $cursorOrientation = \PDO::FETCH_ORI_NE * Returns an array containing all of the result set rows. * * @param int|null $fetchMode Controls how the next row will be returned to the caller. - * The value must be one of the \PDO::FETCH_* constants, - * defaulting to \PDO::FETCH_BOTH. + * The value must be one of the {@link \Doctrine\DBAL\FetchMode} constants, + * defaulting to {@link \Doctrine\DBAL\FetchMode::MIXED}. * @param int|null $fetchArgument This argument has a different meaning depending on the value of the $fetchMode parameter: - * * \PDO::FETCH_COLUMN: Returns the indicated 0-indexed column. - * * \PDO::FETCH_CLASS: Returns instances of the specified class, mapping the columns of each - * row to named properties in the class. + * * {@link \Doctrine\DBAL\FetchMode::COLUMN}: + * Returns the indicated 0-indexed column. + * * {@link \Doctrine\DBAL\FetchMode::CUSTOM_OBJECT}: + * Returns instances of the specified class, mapping the columns of each row + * to named properties in the class. * * \PDO::FETCH_FUNC: Returns the results of calling the specified function, using each row's - * columns as parameters in the call. + * columns as parameters in the call. * @param array|null $ctorArgs Controls how the next row will be returned to the caller. - * The value must be one of the \PDO::FETCH_* constants, - * defaulting to \PDO::FETCH_BOTH. + * The value must be one of the {@link \Doctrine\DBAL\FetchMode} constants, + * defaulting to {@link \Doctrine\DBAL\FetchMode::MIXED}. * * @return array - * - * @see \PDO::FETCH_* constants. */ public function fetchAll($fetchMode = null, $fetchArgument = null, $ctorArgs = null); diff --git a/lib/Doctrine/DBAL/Driver/SQLAnywhere/SQLAnywhereConnection.php b/lib/Doctrine/DBAL/Driver/SQLAnywhere/SQLAnywhereConnection.php index 378f4e4144a..03be0576a7b 100644 --- a/lib/Doctrine/DBAL/Driver/SQLAnywhere/SQLAnywhereConnection.php +++ b/lib/Doctrine/DBAL/Driver/SQLAnywhere/SQLAnywhereConnection.php @@ -21,6 +21,7 @@ use Doctrine\DBAL\Driver\Connection; use Doctrine\DBAL\Driver\ServerInfoAwareConnection; +use Doctrine\DBAL\ParameterType; /** * SAP Sybase SQL Anywhere implementation of the Connection interface. @@ -172,7 +173,7 @@ public function query() /** * {@inheritdoc} */ - public function quote($input, $type = \PDO::PARAM_STR) + public function quote($input, $type = ParameterType::STRING) { if (is_int($input) || is_float($input)) { return $input; diff --git a/lib/Doctrine/DBAL/Driver/SQLAnywhere/SQLAnywhereStatement.php b/lib/Doctrine/DBAL/Driver/SQLAnywhere/SQLAnywhereStatement.php index 7d022b9afab..8a6b4a1176a 100644 --- a/lib/Doctrine/DBAL/Driver/SQLAnywhere/SQLAnywhereStatement.php +++ b/lib/Doctrine/DBAL/Driver/SQLAnywhere/SQLAnywhereStatement.php @@ -19,10 +19,11 @@ namespace Doctrine\DBAL\Driver\SQLAnywhere; +use Doctrine\DBAL\Driver\Statement; use Doctrine\DBAL\Driver\StatementIterator; +use Doctrine\DBAL\FetchMode; +use Doctrine\DBAL\ParameterType; use IteratorAggregate; -use PDO; -use Doctrine\DBAL\Driver\Statement; /** * SAP SQL Anywhere implementation of the Statement interface. @@ -39,19 +40,19 @@ class SQLAnywhereStatement implements IteratorAggregate, Statement private $conn; /** - * @var string Name of the default class to instantiate when fetch mode is \PDO::FETCH_CLASS. + * @var string Name of the default class to instantiate when fetching class instances. */ private $defaultFetchClass = '\stdClass'; /** - * @var string Constructor arguments for the default class to instantiate when fetch mode is \PDO::FETCH_CLASS. + * @var string Constructor arguments for the default class to instantiate when fetching class instances. */ private $defaultFetchClassCtorArgs = []; /** * @var int Default fetch mode to use. */ - private $defaultFetchMode = PDO::FETCH_BOTH; + private $defaultFetchMode = FetchMode::MIXED; /** * @var resource The result set resource to fetch. @@ -92,20 +93,23 @@ public function __construct($conn, $sql) * * @throws SQLAnywhereException */ - public function bindParam($column, &$variable, $type = null, $length = null) + public function bindParam($column, &$variable, $type = ParameterType::STRING, $length = null) { switch ($type) { - case PDO::PARAM_INT: - case PDO::PARAM_BOOL: + case ParameterType::INTEGER: + case ParameterType::BOOLEAN: $type = 'i'; break; - case PDO::PARAM_LOB: + + case ParameterType::LARGE_OBJECT: $type = 'b'; break; - case PDO::PARAM_NULL: - case PDO::PARAM_STR: + + case ParameterType::NULL: + case ParameterType::STRING: $type = 's'; break; + default: throw new SQLAnywhereException('Unknown type: ' . $type); } @@ -120,7 +124,7 @@ public function bindParam($column, &$variable, $type = null, $length = null) /** * {@inheritdoc} */ - public function bindValue($param, $value, $type = null) + public function bindValue($param, $value, $type = ParameterType::STRING) { return $this->bindParam($param, $value, $type); } @@ -203,14 +207,16 @@ public function fetch($fetchMode = null, $cursorOrientation = \PDO::FETCH_ORI_NE $fetchMode = $fetchMode ?: $this->defaultFetchMode; switch ($fetchMode) { - case PDO::FETCH_COLUMN: + case FetchMode::COLUMN: return $this->fetchColumn(); - case PDO::FETCH_ASSOC: + case FetchMode::ASSOCIATIVE: return sasql_fetch_assoc($this->result); - case PDO::FETCH_BOTH: + + case FetchMode::MIXED: return sasql_fetch_array($this->result, SASQL_BOTH); - case PDO::FETCH_CLASS: + + case FetchMode::CUSTOM_OBJECT: $className = $this->defaultFetchClass; $ctorArgs = $this->defaultFetchClassCtorArgs; @@ -227,10 +233,13 @@ public function fetch($fetchMode = null, $cursorOrientation = \PDO::FETCH_ORI_NE } return $result; - case PDO::FETCH_NUM: + + case FetchMode::NUMERIC: return sasql_fetch_row($this->result); - case PDO::FETCH_OBJ: + + case FetchMode::STANDARD_OBJECT: return sasql_fetch_object($this->result); + default: throw new SQLAnywhereException('Fetch mode is not supported: ' . $fetchMode); } @@ -244,16 +253,18 @@ public function fetchAll($fetchMode = null, $fetchArgument = null, $ctorArgs = n $rows = []; switch ($fetchMode) { - case PDO::FETCH_CLASS: + case FetchMode::CUSTOM_OBJECT: while ($row = call_user_func_array([$this, 'fetch'], func_get_args())) { $rows[] = $row; } break; - case PDO::FETCH_COLUMN: + + case FetchMode::COLUMN: while ($row = $this->fetchColumn()) { $rows[] = $row; } break; + default: while ($row = $this->fetch($fetchMode)) { $rows[] = $row; @@ -268,7 +279,7 @@ public function fetchAll($fetchMode = null, $fetchArgument = null, $ctorArgs = n */ public function fetchColumn($columnIndex = 0) { - $row = $this->fetch(PDO::FETCH_NUM); + $row = $this->fetch(FetchMode::NUMERIC); if (false === $row) { return false; diff --git a/lib/Doctrine/DBAL/Driver/SQLSrv/SQLSrvConnection.php b/lib/Doctrine/DBAL/Driver/SQLSrv/SQLSrvConnection.php index 6de138a4af5..52ad200cf7f 100644 --- a/lib/Doctrine/DBAL/Driver/SQLSrv/SQLSrvConnection.php +++ b/lib/Doctrine/DBAL/Driver/SQLSrv/SQLSrvConnection.php @@ -21,6 +21,7 @@ use Doctrine\DBAL\Driver\Connection; use Doctrine\DBAL\Driver\ServerInfoAwareConnection; +use Doctrine\DBAL\ParameterType; /** * SQL Server implementation for the Connection interface. @@ -102,7 +103,7 @@ public function query() * {@inheritDoc} * @license New BSD, code from Zend Framework */ - public function quote($value, $type=\PDO::PARAM_STR) + public function quote($value, $type = ParameterType::STRING) { if (is_int($value)) { return $value; diff --git a/lib/Doctrine/DBAL/Driver/SQLSrv/SQLSrvStatement.php b/lib/Doctrine/DBAL/Driver/SQLSrv/SQLSrvStatement.php index 9c03f26ca7e..ea601ed0e9b 100644 --- a/lib/Doctrine/DBAL/Driver/SQLSrv/SQLSrvStatement.php +++ b/lib/Doctrine/DBAL/Driver/SQLSrv/SQLSrvStatement.php @@ -20,7 +20,8 @@ namespace Doctrine\DBAL\Driver\SQLSrv; use Doctrine\DBAL\Driver\StatementIterator; -use PDO; +use Doctrine\DBAL\FetchMode; +use Doctrine\DBAL\ParameterType; use IteratorAggregate; use Doctrine\DBAL\Driver\Statement; @@ -73,20 +74,20 @@ class SQLSrvStatement implements IteratorAggregate, Statement * @var array */ private static $fetchMap = [ - PDO::FETCH_BOTH => SQLSRV_FETCH_BOTH, - PDO::FETCH_ASSOC => SQLSRV_FETCH_ASSOC, - PDO::FETCH_NUM => SQLSRV_FETCH_NUMERIC, + FetchMode::MIXED => SQLSRV_FETCH_BOTH, + FetchMode::ASSOCIATIVE => SQLSRV_FETCH_ASSOC, + FetchMode::NUMERIC => SQLSRV_FETCH_NUMERIC, ]; /** - * The name of the default class to instantiate when fetch mode is \PDO::FETCH_CLASS. + * The name of the default class to instantiate when fetching class instances. * * @var string */ private $defaultFetchClass = '\stdClass'; /** - * The constructor arguments for the default class to instantiate when fetch mode is \PDO::FETCH_CLASS. + * The constructor arguments for the default class to instantiate when fetching class instances. * * @var string */ @@ -95,9 +96,9 @@ class SQLSrvStatement implements IteratorAggregate, Statement /** * The fetch style. * - * @param int + * @var int */ - private $defaultFetchMode = PDO::FETCH_BOTH; + private $defaultFetchMode = FetchMode::MIXED; /** * The last insert ID. @@ -139,7 +140,7 @@ public function __construct($conn, $sql, LastInsertId $lastInsertId = null) /** * {@inheritdoc} */ - public function bindValue($param, $value, $type = null) + public function bindValue($param, $value, $type = ParameterType::STRING) { if (!is_numeric($param)) { throw new SQLSrvException( @@ -154,7 +155,7 @@ public function bindValue($param, $value, $type = null) /** * {@inheritdoc} */ - public function bindParam($column, &$variable, $type = null, $length = null) + public function bindParam($column, &$variable, $type = ParameterType::STRING, $length = null) { if (!is_numeric($column)) { throw new SQLSrvException("sqlsrv does not support named parameters to queries, use question mark (?) placeholders instead."); @@ -258,7 +259,7 @@ private function prepare() $params = []; foreach ($this->variables as $column => &$variable) { - if (PDO::PARAM_LOB === $this->types[$column]) { + if ($this->types[$column] === ParameterType::LARGE_OBJECT) { $params[$column - 1] = [ &$variable, SQLSRV_PARAM_IN, @@ -315,7 +316,7 @@ public function fetch($fetchMode = null, $cursorOrientation = PDO::FETCH_ORI_NEX $args = func_get_args(); $fetchMode = $fetchMode ?: $this->defaultFetchMode; - if ($fetchMode === PDO::FETCH_COLUMN) { + if ($fetchMode === FetchMode::COLUMN) { return $this->fetchColumn(); } @@ -323,7 +324,7 @@ public function fetch($fetchMode = null, $cursorOrientation = PDO::FETCH_ORI_NEX return sqlsrv_fetch_array($this->stmt, self::$fetchMap[$fetchMode]) ?: false; } - if (in_array($fetchMode, [PDO::FETCH_OBJ, PDO::FETCH_CLASS], true)) { + if (in_array($fetchMode, [FetchMode::STANDARD_OBJECT, FetchMode::CUSTOM_OBJECT], true)) { $className = $this->defaultFetchClass; $ctorArgs = $this->defaultFetchClassCtorArgs; @@ -346,16 +347,18 @@ public function fetchAll($fetchMode = null, $fetchArgument = null, $ctorArgs = n $rows = []; switch ($fetchMode) { - case PDO::FETCH_CLASS: + case FetchMode::CUSTOM_OBJECT: while ($row = call_user_func_array([$this, 'fetch'], func_get_args())) { $rows[] = $row; } break; - case PDO::FETCH_COLUMN: + + case FetchMode::COLUMN: while ($row = $this->fetchColumn()) { $rows[] = $row; } break; + default: while ($row = $this->fetch($fetchMode)) { $rows[] = $row; @@ -370,7 +373,7 @@ public function fetchAll($fetchMode = null, $fetchArgument = null, $ctorArgs = n */ public function fetchColumn($columnIndex = 0) { - $row = $this->fetch(PDO::FETCH_NUM); + $row = $this->fetch(FetchMode::NUMERIC); if (false === $row) { return false; diff --git a/lib/Doctrine/DBAL/Driver/Statement.php b/lib/Doctrine/DBAL/Driver/Statement.php index 1ad827aea81..15b6bee1bae 100644 --- a/lib/Doctrine/DBAL/Driver/Statement.php +++ b/lib/Doctrine/DBAL/Driver/Statement.php @@ -19,6 +19,8 @@ namespace Doctrine\DBAL\Driver; +use Doctrine\DBAL\ParameterType; + /** * Statement interface. * Drivers must implement this interface. @@ -43,12 +45,12 @@ interface Statement extends ResultStatement * this will be a parameter name of the form :name. For a prepared statement * using question mark placeholders, this will be the 1-indexed position of the parameter. * @param mixed $value The value to bind to the parameter. - * @param int $type Explicit data type for the parameter using the PDO::PARAM_* constants. + * @param int $type Explicit data type for the parameter using the {@link \Doctrine\DBAL\ParameterType} + * constants. * * @return bool TRUE on success or FALSE on failure. */ - function bindValue($param, $value, $type = null); - + public function bindValue($param, $value, $type = ParameterType::STRING); /** * Binds a PHP variable to a corresponding named (not supported by mysqli driver, see comment below) or question @@ -68,15 +70,15 @@ function bindValue($param, $value, $type = null); * this will be a parameter name of the form :name. For a prepared statement using * question mark placeholders, this will be the 1-indexed position of the parameter. * @param mixed $variable Name of the PHP variable to bind to the SQL statement parameter. - * @param int|null $type Explicit data type for the parameter using the PDO::PARAM_* constants. To return - * an INOUT parameter from a stored procedure, use the bitwise OR operator to set the - * PDO::PARAM_INPUT_OUTPUT bits for the data_type parameter. + * @param int|null $type Explicit data type for the parameter using the {@link \Doctrine\DBAL\ParameterType} + * constants. To return an INOUT parameter from a stored procedure, use the bitwise + * OR operator to set the PDO::PARAM_INPUT_OUTPUT bits for the data_type parameter. * @param int|null $length You must specify maxlength when using an OUT bind * so that PHP allocates enough memory to hold the returned value. * * @return bool TRUE on success or FALSE on failure. */ - function bindParam($column, &$variable, $type = null, $length = null); + public function bindParam($column, &$variable, $type = ParameterType::STRING, $length = null); /** * Fetches the SQLSTATE associated with the last operation on the statement handle. @@ -85,7 +87,7 @@ function bindParam($column, &$variable, $type = null, $length = null); * * @return string The error code string. */ - function errorCode(); + public function errorCode(); /** * Fetches extended error information associated with the last operation on the statement handle. @@ -94,7 +96,7 @@ function errorCode(); * * @return array The error info array. */ - function errorInfo(); + public function errorInfo(); /** * Executes a prepared statement @@ -111,7 +113,7 @@ function errorInfo(); * * @return bool TRUE on success or FALSE on failure. */ - function execute($params = null); + public function execute($params = null); /** * Returns the number of rows affected by the last DELETE, INSERT, or UPDATE statement @@ -124,5 +126,5 @@ function execute($params = null); * * @return int The number of rows. */ - function rowCount(); + public function rowCount(); } diff --git a/lib/Doctrine/DBAL/FetchMode.php b/lib/Doctrine/DBAL/FetchMode.php new file mode 100644 index 00000000000..a3e7f3ba601 --- /dev/null +++ b/lib/Doctrine/DBAL/FetchMode.php @@ -0,0 +1,69 @@ +conn->getDatabasePlatform(); - $sql = "SELECT sequence_value, sequence_increment_by " . - "FROM " . $platform->appendLockHint($this->generatorTableName, \Doctrine\DBAL\LockMode::PESSIMISTIC_WRITE) . " " . - "WHERE sequence_name = ? " . $platform->getWriteLockSQL(); - $stmt = $this->conn->executeQuery($sql, [$sequenceName]); + $sql = 'SELECT sequence_value, sequence_increment_by' + . ' FROM ' . $platform->appendLockHint($this->generatorTableName, LockMode::PESSIMISTIC_WRITE) + . ' WHERE sequence_name = ? ' . $platform->getWriteLockSQL(); + $stmt = $this->conn->executeQuery($sql, [$sequenceName]); + $row = $stmt->fetch(FetchMode::ASSOCIATIVE); - if ($row = $stmt->fetch(\PDO::FETCH_ASSOC)) { + if ($row !== false) { $row = array_change_key_case($row, CASE_LOWER); $value = $row['sequence_value']; diff --git a/lib/Doctrine/DBAL/ParameterType.php b/lib/Doctrine/DBAL/ParameterType.php new file mode 100644 index 00000000000..362a0514d23 --- /dev/null +++ b/lib/Doctrine/DBAL/ParameterType.php @@ -0,0 +1,51 @@ +portability = $params['portability']; } + if (isset($params['fetch_case']) && $this->portability & self::PORTABILITY_FIX_CASE) { if ($this->_conn instanceof \Doctrine\DBAL\Driver\PDOConnection) { // make use of c-level support for case handling $this->_conn->setAttribute(\PDO::ATTR_CASE, $params['fetch_case']); } else { - $this->case = ($params['fetch_case'] == \PDO::CASE_LOWER) ? CASE_LOWER : CASE_UPPER; + $this->case = ($params['fetch_case'] === ColumnCase::LOWER) ? CASE_LOWER : CASE_UPPER; } } } diff --git a/lib/Doctrine/DBAL/Portability/Statement.php b/lib/Doctrine/DBAL/Portability/Statement.php index 39d8d04b2d2..f36c365b4ea 100644 --- a/lib/Doctrine/DBAL/Portability/Statement.php +++ b/lib/Doctrine/DBAL/Portability/Statement.php @@ -19,7 +19,8 @@ namespace Doctrine\DBAL\Portability; -use PDO; +use Doctrine\DBAL\FetchMode; +use Doctrine\DBAL\ParameterType; /** * Portability wrapper for a Statement. @@ -48,7 +49,7 @@ class Statement implements \IteratorAggregate, \Doctrine\DBAL\Driver\Statement /** * @var int */ - private $defaultFetchMode = PDO::FETCH_BOTH; + private $defaultFetchMode = FetchMode::MIXED; /** * Wraps Statement and applies portability measures. @@ -66,15 +67,15 @@ public function __construct($stmt, Connection $conn) /** * {@inheritdoc} */ - public function bindParam($column, &$variable, $type = null, $length = null) + public function bindParam($column, &$variable, $type = ParameterType::STRING, $length = null) { return $this->stmt->bindParam($column, $variable, $type, $length); } + /** * {@inheritdoc} */ - - public function bindValue($param, $value, $type = null) + public function bindValue($param, $value, $type = ParameterType::STRING) { return $this->stmt->bindValue($param, $value, $type); } @@ -148,10 +149,12 @@ public function fetch($fetchMode = null, $cursorOrientation = \PDO::FETCH_ORI_NE $row = $this->stmt->fetch($fetchMode); - $row = $this->fixRow($row, - $this->portability & (Connection::PORTABILITY_EMPTY_TO_NULL|Connection::PORTABILITY_RTRIM), - !is_null($this->case) && ($fetchMode == PDO::FETCH_ASSOC || $fetchMode == PDO::FETCH_BOTH) && ($this->portability & Connection::PORTABILITY_FIX_CASE) - ); + $iterateRow = $this->portability & (Connection::PORTABILITY_EMPTY_TO_NULL|Connection::PORTABILITY_RTRIM); + $fixCase = ! is_null($this->case) + && ($fetchMode === FetchMode::ASSOCIATIVE || $fetchMode === FetchMode::MIXED) + && ($this->portability & Connection::PORTABILITY_FIX_CASE); + + $row = $this->fixRow($row, $iterateRow, $fixCase); return $row; } @@ -170,12 +173,15 @@ public function fetchAll($fetchMode = null, $fetchArgument = null, $ctorArgs = n } $iterateRow = $this->portability & (Connection::PORTABILITY_EMPTY_TO_NULL|Connection::PORTABILITY_RTRIM); - $fixCase = !is_null($this->case) && ($fetchMode == PDO::FETCH_ASSOC || $fetchMode == PDO::FETCH_BOTH) && ($this->portability & Connection::PORTABILITY_FIX_CASE); + $fixCase = ! is_null($this->case) + && ($fetchMode === FetchMode::ASSOCIATIVE || $fetchMode === FetchMode::MIXED) + && ($this->portability & Connection::PORTABILITY_FIX_CASE); + if ( ! $iterateRow && !$fixCase) { return $rows; } - if ($fetchMode === PDO::FETCH_COLUMN) { + if ($fetchMode === FetchMode::COLUMN) { foreach ($rows as $num => $row) { $rows[$num] = [$row]; } @@ -185,7 +191,7 @@ public function fetchAll($fetchMode = null, $fetchArgument = null, $ctorArgs = n $rows[$num] = $this->fixRow($row, $iterateRow, $fixCase); } - if ($fetchMode === PDO::FETCH_COLUMN) { + if ($fetchMode === FetchMode::COLUMN) { foreach ($rows as $num => $row) { $rows[$num] = $row[0]; } diff --git a/lib/Doctrine/DBAL/Query/QueryBuilder.php b/lib/Doctrine/DBAL/Query/QueryBuilder.php index 5e0ce9118a1..43f2d82c6b4 100644 --- a/lib/Doctrine/DBAL/Query/QueryBuilder.php +++ b/lib/Doctrine/DBAL/Query/QueryBuilder.php @@ -19,6 +19,7 @@ namespace Doctrine\DBAL\Query; +use Doctrine\DBAL\ParameterType; use Doctrine\DBAL\Query\Expression\CompositeExpression; use Doctrine\DBAL\Connection; @@ -262,9 +263,9 @@ public function getSQL() * ->setParameter(':user_id', 1); * * - * @param string|integer $key The parameter position or name. - * @param mixed $value The parameter value. - * @param string|integer|null $type One of the PDO::PARAM_* constants. + * @param string|int $key The parameter position or name. + * @param mixed $value The parameter value. + * @param string|int|null $type One of the {@link \Doctrine\DBAL\ParameterType} constants. * * @return $this This QueryBuilder instance. */ @@ -1256,7 +1257,7 @@ public function __toString() * * @return string the placeholder name used. */ - public function createNamedParameter($value, $type = \PDO::PARAM_STR, $placeHolder = null) + public function createNamedParameter($value, $type = ParameterType::STRING, $placeHolder = null) { if ($placeHolder === null) { $this->boundCounter++; @@ -1280,8 +1281,8 @@ public function createNamedParameter($value, $type = \PDO::PARAM_STR, $placeHold * $qb = $conn->createQueryBuilder(); * $qb->select('u.*') * ->from('users', 'u') - * ->where('u.username = ' . $qb->createPositionalParameter('Foo', PDO::PARAM_STR)) - * ->orWhere('u.username = ' . $qb->createPositionalParameter('Bar', PDO::PARAM_STR)) + * ->where('u.username = ' . $qb->createPositionalParameter('Foo', ParameterType::STRING)) + * ->orWhere('u.username = ' . $qb->createPositionalParameter('Bar', ParameterType::STRING)) * * * @param mixed $value @@ -1289,7 +1290,7 @@ public function createNamedParameter($value, $type = \PDO::PARAM_STR, $placeHold * * @return string */ - public function createPositionalParameter($value, $type = \PDO::PARAM_STR) + public function createPositionalParameter($value, $type = ParameterType::STRING) { $this->boundCounter++; $this->setParameter($this->boundCounter, $value, $type); diff --git a/lib/Doctrine/DBAL/SQLParserUtils.php b/lib/Doctrine/DBAL/SQLParserUtils.php index afbb6aa4f10..d3b600c917a 100644 --- a/lib/Doctrine/DBAL/SQLParserUtils.php +++ b/lib/Doctrine/DBAL/SQLParserUtils.php @@ -139,7 +139,9 @@ public static function expandListParameters($query, $params, $types) $types = array_merge( array_slice($types, 0, $needle), $count ? - array_fill(0, $count, $types[$needle] - Connection::ARRAY_PARAM_OFFSET) : // array needles are at PDO::PARAM_* + 100 + // array needles are at {@link \Doctrine\DBAL\ParameterType} constants + // + {@link Doctrine\DBAL\Connection::ARRAY_PARAM_OFFSET} + array_fill(0, $count, $types[$needle] - Connection::ARRAY_PARAM_OFFSET) : [], array_slice($types, $needle + 1) ); @@ -166,7 +168,7 @@ public static function expandListParameters($query, $params, $types) $pos += $queryOffset; $queryOffset -= ($paramLen - 1); $paramsOrd[] = $value; - $typesOrd[] = static::extractParam($paramName, $types, false, \PDO::PARAM_STR); + $typesOrd[] = static::extractParam($paramName, $types, false, ParameterType::STRING); $query = substr($query, 0, $pos) . '?' . substr($query, ($pos + $paramLen)); continue; diff --git a/lib/Doctrine/DBAL/Schema/SqliteSchemaManager.php b/lib/Doctrine/DBAL/Schema/SqliteSchemaManager.php index 9420bad888f..9f1adbd0f4e 100644 --- a/lib/Doctrine/DBAL/Schema/SqliteSchemaManager.php +++ b/lib/Doctrine/DBAL/Schema/SqliteSchemaManager.php @@ -20,6 +20,7 @@ namespace Doctrine\DBAL\Schema; use Doctrine\DBAL\DBALException; +use Doctrine\DBAL\FetchMode; use Doctrine\DBAL\Types\StringType; use Doctrine\DBAL\Types\TextType; use Doctrine\DBAL\Types\Type; @@ -169,7 +170,7 @@ protected function _getPortableTableIndexesList($tableIndexes, $tableName=null) // fetch primary $stmt = $this->_conn->executeQuery("PRAGMA TABLE_INFO ('$tableName')"); - $indexArray = $stmt->fetchAll(\PDO::FETCH_ASSOC); + $indexArray = $stmt->fetchAll(FetchMode::ASSOCIATIVE); usort($indexArray, function($a, $b) { if ($a['pk'] == $b['pk']) { @@ -200,7 +201,7 @@ protected function _getPortableTableIndexesList($tableIndexes, $tableName=null) $idx['non_unique'] = $tableIndex['unique']?false:true; $stmt = $this->_conn->executeQuery("PRAGMA INDEX_INFO ('{$keyName}')"); - $indexArray = $stmt->fetchAll(\PDO::FETCH_ASSOC); + $indexArray = $stmt->fetchAll(FetchMode::ASSOCIATIVE); foreach ($indexArray as $indexColumnRow) { $idx['column_name'] = $indexColumnRow['name']; diff --git a/lib/Doctrine/DBAL/Statement.php b/lib/Doctrine/DBAL/Statement.php index 409d83fb79e..6469e501f0a 100644 --- a/lib/Doctrine/DBAL/Statement.php +++ b/lib/Doctrine/DBAL/Statement.php @@ -19,7 +19,6 @@ namespace Doctrine\DBAL; -use PDO; use Doctrine\DBAL\Types\Type; use Doctrine\DBAL\Driver\Statement as DriverStatement; @@ -102,7 +101,7 @@ public function __construct($sql, Connection $conn) * * @return bool TRUE on success, FALSE on failure. */ - public function bindValue($name, $value, $type = null) + public function bindValue($name, $value, $type = ParameterType::STRING) { $this->params[$name] = $value; $this->types[$name] = $type; @@ -114,7 +113,7 @@ public function bindValue($name, $value, $type = null) $value = $type->convertToDatabaseValue($value, $this->platform); $bindingType = $type->getBindingType(); } else { - $bindingType = $type; // PDO::PARAM_* constants + $bindingType = $type; } return $this->stmt->bindValue($name, $value, $bindingType); @@ -136,7 +135,7 @@ public function bindValue($name, $value, $type = null) * * @return bool TRUE on success, FALSE on failure. */ - public function bindParam($name, &$var, $type = PDO::PARAM_STR, $length = null) + public function bindParam($name, &$var, $type = ParameterType::STRING, $length = null) { $this->params[$name] = $var; $this->types[$name] = $type; diff --git a/lib/Doctrine/DBAL/Types/BigIntType.php b/lib/Doctrine/DBAL/Types/BigIntType.php index c0ede8f5b12..9a2b42953ad 100644 --- a/lib/Doctrine/DBAL/Types/BigIntType.php +++ b/lib/Doctrine/DBAL/Types/BigIntType.php @@ -19,6 +19,7 @@ namespace Doctrine\DBAL\Types; +use Doctrine\DBAL\ParameterType; use Doctrine\DBAL\Platforms\AbstractPlatform; /** @@ -50,7 +51,7 @@ public function getSQLDeclaration(array $fieldDeclaration, AbstractPlatform $pla */ public function getBindingType() { - return \PDO::PARAM_STR; + return ParameterType::STRING; } /** diff --git a/lib/Doctrine/DBAL/Types/BinaryType.php b/lib/Doctrine/DBAL/Types/BinaryType.php index a22a4405425..a112e776a15 100644 --- a/lib/Doctrine/DBAL/Types/BinaryType.php +++ b/lib/Doctrine/DBAL/Types/BinaryType.php @@ -19,6 +19,7 @@ namespace Doctrine\DBAL\Types; +use Doctrine\DBAL\ParameterType; use Doctrine\DBAL\Platforms\AbstractPlatform; /** @@ -73,6 +74,6 @@ public function getName() */ public function getBindingType() { - return \PDO::PARAM_LOB; + return ParameterType::LARGE_OBJECT; } } diff --git a/lib/Doctrine/DBAL/Types/BlobType.php b/lib/Doctrine/DBAL/Types/BlobType.php index bd89301e21b..b0173e1a6d7 100644 --- a/lib/Doctrine/DBAL/Types/BlobType.php +++ b/lib/Doctrine/DBAL/Types/BlobType.php @@ -19,6 +19,7 @@ namespace Doctrine\DBAL\Types; +use Doctrine\DBAL\ParameterType; use Doctrine\DBAL\Platforms\AbstractPlatform; /** @@ -72,6 +73,6 @@ public function getName() */ public function getBindingType() { - return \PDO::PARAM_LOB; + return ParameterType::LARGE_OBJECT; } } diff --git a/lib/Doctrine/DBAL/Types/BooleanType.php b/lib/Doctrine/DBAL/Types/BooleanType.php index 9fea80f10a2..748f7f52942 100644 --- a/lib/Doctrine/DBAL/Types/BooleanType.php +++ b/lib/Doctrine/DBAL/Types/BooleanType.php @@ -19,6 +19,7 @@ namespace Doctrine\DBAL\Types; +use Doctrine\DBAL\ParameterType; use Doctrine\DBAL\Platforms\AbstractPlatform; /** @@ -65,6 +66,6 @@ public function getName() */ public function getBindingType() { - return \PDO::PARAM_BOOL; + return ParameterType::BOOLEAN; } } diff --git a/lib/Doctrine/DBAL/Types/IntegerType.php b/lib/Doctrine/DBAL/Types/IntegerType.php index 9e8d0da3799..3c83341ded0 100644 --- a/lib/Doctrine/DBAL/Types/IntegerType.php +++ b/lib/Doctrine/DBAL/Types/IntegerType.php @@ -19,6 +19,7 @@ namespace Doctrine\DBAL\Types; +use Doctrine\DBAL\ParameterType; use Doctrine\DBAL\Platforms\AbstractPlatform; /** @@ -58,6 +59,6 @@ public function convertToPHPValue($value, AbstractPlatform $platform) */ public function getBindingType() { - return \PDO::PARAM_INT; + return ParameterType::INTEGER; } } diff --git a/lib/Doctrine/DBAL/Types/SmallIntType.php b/lib/Doctrine/DBAL/Types/SmallIntType.php index 4d7229c9be0..1a2abdbe062 100644 --- a/lib/Doctrine/DBAL/Types/SmallIntType.php +++ b/lib/Doctrine/DBAL/Types/SmallIntType.php @@ -19,6 +19,7 @@ namespace Doctrine\DBAL\Types; +use Doctrine\DBAL\ParameterType; use Doctrine\DBAL\Platforms\AbstractPlatform; /** @@ -57,6 +58,6 @@ public function convertToPHPValue($value, AbstractPlatform $platform) */ public function getBindingType() { - return \PDO::PARAM_INT; + return ParameterType::INTEGER; } } diff --git a/lib/Doctrine/DBAL/Types/Type.php b/lib/Doctrine/DBAL/Types/Type.php index 7aa04f41360..eed08fc5a76 100644 --- a/lib/Doctrine/DBAL/Types/Type.php +++ b/lib/Doctrine/DBAL/Types/Type.php @@ -19,6 +19,7 @@ namespace Doctrine\DBAL\Types; +use Doctrine\DBAL\ParameterType; use Doctrine\DBAL\Platforms\AbstractPlatform; use Doctrine\DBAL\DBALException; @@ -247,19 +248,13 @@ public static function overrideType($name, $className) * Gets the (preferred) binding type for values of this type that * can be used when binding parameters to prepared statements. * - * This method should return one of the PDO::PARAM_* constants, that is, one of: - * - * PDO::PARAM_BOOL - * PDO::PARAM_NULL - * PDO::PARAM_INT - * PDO::PARAM_STR - * PDO::PARAM_LOB + * This method should return one of the {@link \Doctrine\DBAL\ParameterType} constants. * * @return int */ public function getBindingType() { - return \PDO::PARAM_STR; + return ParameterType::STRING; } /** diff --git a/tests/Doctrine/Tests/DBAL/Cache/QueryCacheProfileTest.php b/tests/Doctrine/Tests/DBAL/Cache/QueryCacheProfileTest.php index fcc46382d18..553af666784 100644 --- a/tests/Doctrine/Tests/DBAL/Cache/QueryCacheProfileTest.php +++ b/tests/Doctrine/Tests/DBAL/Cache/QueryCacheProfileTest.php @@ -3,8 +3,8 @@ namespace Doctrine\Tests\DBAL\Cache; use Doctrine\DBAL\Cache\QueryCacheProfile; +use Doctrine\DBAL\ParameterType; use Doctrine\Tests\DbalTestCase; -use PDO; class QueryCacheProfileTest extends DbalTestCase { @@ -23,7 +23,7 @@ public function testShouldUseTheGivenCacheKeyIfPresent() { $query = 'SELECT * FROM foo WHERE bar = ?'; $params = [666]; - $types = [PDO::PARAM_INT]; + $types = [ParameterType::INTEGER]; $connectionParams = array( 'dbname' => 'database_name', @@ -47,7 +47,7 @@ public function testShouldGenerateAnAutomaticKeyIfNoKeyHasBeenGiven() { $query = 'SELECT * FROM foo WHERE bar = ?'; $params = [666]; - $types = [PDO::PARAM_INT]; + $types = [ParameterType::INTEGER]; $connectionParams = array( 'dbname' => 'database_name', @@ -79,7 +79,7 @@ public function testShouldGenerateDifferentKeysForSameQueryAndParamsAndDifferent { $query = 'SELECT * FROM foo WHERE bar = ?'; $params = [666]; - $types = [PDO::PARAM_INT]; + $types = [ParameterType::INTEGER]; $connectionParams = array( 'dbname' => 'database_name', @@ -114,7 +114,7 @@ public function testShouldGenerateSameKeysIfNoneOfTheParamsChanges() { $query = 'SELECT * FROM foo WHERE bar = ?'; $params = [666]; - $types = [PDO::PARAM_INT]; + $types = [ParameterType::INTEGER]; $connectionParams = array( 'dbname' => 'database_name', diff --git a/tests/Doctrine/Tests/DBAL/ConnectionTest.php b/tests/Doctrine/Tests/DBAL/ConnectionTest.php index 43ed2b214cb..46a3c49df02 100644 --- a/tests/Doctrine/Tests/DBAL/ConnectionTest.php +++ b/tests/Doctrine/Tests/DBAL/ConnectionTest.php @@ -11,13 +11,18 @@ use Doctrine\DBAL\ConnectionException; use Doctrine\DBAL\DBALException; use Doctrine\DBAL\Driver; +use Doctrine\DBAL\Driver\Connection as DriverConnection; use Doctrine\DBAL\Events; use Doctrine\DBAL\Exception\InvalidArgumentException; +use Doctrine\DBAL\FetchMode; +use Doctrine\DBAL\ParameterType; use Doctrine\DBAL\Platforms\AbstractPlatform; -use Doctrine\Tests\Mocks\DriverConnectionMock; use Doctrine\Tests\Mocks\DriverMock; use Doctrine\Tests\Mocks\VersionAwarePlatformDriverMock; +/** + * @requires extension pdo_mysql + */ class ConnectionTest extends \Doctrine\Tests\DbalTestCase { /** @@ -44,7 +49,9 @@ public function getExecuteUpdateMockConnection() $driverMock->expects($this->any()) ->method('connect') - ->will($this->returnValue(new DriverConnectionMock())); + ->will($this->returnValue( + $this->createMock(DriverConnection::class) + )); $conn = $this->getMockBuilder(Connection::class) ->setMethods(['executeUpdate']) @@ -153,6 +160,7 @@ public function testEventManagerPassedToPlatform() } /** + * @requires extension pdo_sqlite * @expectedException \Doctrine\DBAL\DBALException * @dataProvider getQueryMethods */ @@ -231,7 +239,9 @@ public function testConnectStartsTransactionInNoAutoCommitMode() $driverMock = $this->createMock('Doctrine\DBAL\Driver'); $driverMock->expects($this->any()) ->method('connect') - ->will($this->returnValue(new DriverConnectionMock())); + ->will($this->returnValue( + $this->createMock(DriverConnection::class) + )); $conn = new Connection(array('platform' => new Mocks\MockPlatform()), $driverMock); $conn->setAutoCommit(false); @@ -251,7 +261,9 @@ public function testCommitStartsTransactionInNoAutoCommitMode() $driverMock = $this->createMock('Doctrine\DBAL\Driver'); $driverMock->expects($this->any()) ->method('connect') - ->will($this->returnValue(new DriverConnectionMock())); + ->will($this->returnValue( + $this->createMock(DriverConnection::class) + )); $conn = new Connection(array('platform' => new Mocks\MockPlatform()), $driverMock); $conn->setAutoCommit(false); @@ -269,7 +281,9 @@ public function testRollBackStartsTransactionInNoAutoCommitMode() $driverMock = $this->createMock('Doctrine\DBAL\Driver'); $driverMock->expects($this->any()) ->method('connect') - ->will($this->returnValue(new DriverConnectionMock())); + ->will($this->returnValue( + $this->createMock(DriverConnection::class) + )); $conn = new Connection(array('platform' => new Mocks\MockPlatform()), $driverMock); $conn->setAutoCommit(false); @@ -287,7 +301,9 @@ public function testSwitchingAutoCommitModeCommitsAllCurrentTransactions() $driverMock = $this->createMock('Doctrine\DBAL\Driver'); $driverMock->expects($this->any()) ->method('connect') - ->will($this->returnValue(new DriverConnectionMock())); + ->will($this->returnValue( + $this->createMock(DriverConnection::class) + )); $conn = new Connection(array('platform' => new Mocks\MockPlatform()), $driverMock); $conn->connect(); @@ -479,21 +495,23 @@ public function testDeleteWithIsNull() public function testFetchAssoc() { $statement = 'SELECT * FROM foo WHERE bar = ?'; - $params = array(666); - $types = array(\PDO::PARAM_INT); - $result = array(); + $params = [666]; + $types = [ParameterType::INTEGER]; + $result = []; $driverMock = $this->createMock('Doctrine\DBAL\Driver'); $driverMock->expects($this->any()) ->method('connect') - ->will($this->returnValue(new DriverConnectionMock())); + ->will($this->returnValue( + $this->createMock(DriverConnection::class) + )); $driverStatementMock = $this->createMock('Doctrine\Tests\Mocks\DriverStatementMock'); $driverStatementMock->expects($this->once()) ->method('fetch') - ->with(\PDO::FETCH_ASSOC) + ->with(FetchMode::ASSOCIATIVE) ->will($this->returnValue($result)); /** @var \PHPUnit_Framework_MockObject_MockObject|\Doctrine\DBAL\Connection $conn */ @@ -513,21 +531,23 @@ public function testFetchAssoc() public function testFetchArray() { $statement = 'SELECT * FROM foo WHERE bar = ?'; - $params = array(666); - $types = array(\PDO::PARAM_INT); - $result = array(); + $params = [666]; + $types = [ParameterType::INTEGER]; + $result = []; $driverMock = $this->createMock('Doctrine\DBAL\Driver'); $driverMock->expects($this->any()) ->method('connect') - ->will($this->returnValue(new DriverConnectionMock())); + ->will($this->returnValue( + $this->createMock(DriverConnection::class) + )); $driverStatementMock = $this->createMock('Doctrine\Tests\Mocks\DriverStatementMock'); $driverStatementMock->expects($this->once()) ->method('fetch') - ->with(\PDO::FETCH_NUM) + ->with(FetchMode::NUMERIC) ->will($this->returnValue($result)); /** @var \PHPUnit_Framework_MockObject_MockObject|\Doctrine\DBAL\Connection $conn */ @@ -547,16 +567,18 @@ public function testFetchArray() public function testFetchColumn() { $statement = 'SELECT * FROM foo WHERE bar = ?'; - $params = array(666); - $types = array(\PDO::PARAM_INT); + $params = [666]; + $types = [ParameterType::INTEGER]; $column = 0; - $result = array(); + $result = []; $driverMock = $this->createMock('Doctrine\DBAL\Driver'); $driverMock->expects($this->any()) ->method('connect') - ->will($this->returnValue(new DriverConnectionMock())); + ->will($this->returnValue( + $this->createMock(DriverConnection::class) + )); $driverStatementMock = $this->createMock('Doctrine\Tests\Mocks\DriverStatementMock'); @@ -605,15 +627,17 @@ public function testConnectionIsClosedButNotUnset() public function testFetchAll() { $statement = 'SELECT * FROM foo WHERE bar = ?'; - $params = array(666); - $types = array(\PDO::PARAM_INT); - $result = array(); + $params = [666]; + $types = [ParameterType::INTEGER]; + $result = []; $driverMock = $this->createMock('Doctrine\DBAL\Driver'); $driverMock->expects($this->any()) ->method('connect') - ->will($this->returnValue(new DriverConnectionMock())); + ->will($this->returnValue( + $this->createMock(DriverConnection::class) + )); $driverStatementMock = $this->createMock('Doctrine\Tests\Mocks\DriverStatementMock'); @@ -756,7 +780,7 @@ public function testConnectionParamsArePassedToTheQueryCacheProfileInExecuteCach $query = 'SELECT * FROM foo WHERE bar = ?'; $params = [666]; - $types = [\PDO::PARAM_INT]; + $types = [ParameterType::INTEGER]; /* @var $queryCacheProfileMock QueryCacheProfile|\PHPUnit_Framework_MockObject_MockObject */ $queryCacheProfileMock = $this->createMock(QueryCacheProfile::class); diff --git a/tests/Doctrine/Tests/DBAL/DriverManagerTest.php b/tests/Doctrine/Tests/DBAL/DriverManagerTest.php index d5155de2f73..473c0ac0206 100644 --- a/tests/Doctrine/Tests/DBAL/DriverManagerTest.php +++ b/tests/Doctrine/Tests/DBAL/DriverManagerTest.php @@ -3,42 +3,51 @@ namespace Doctrine\Tests\DBAL; use Doctrine\DBAL\DBALException; -use Doctrine\Tests\Mocks\PDOMock; +use Doctrine\DBAL\Driver\DrizzlePDOMySql\Driver as DrizzlePDOMySqlDriver; +use Doctrine\DBAL\Driver\PDOMySQL\Driver as PDOMySQLDriver; +use Doctrine\DBAL\Driver\PDOSqlite\Driver as PDOSqliteDriver; +use Doctrine\DBAL\Driver\SQLSrv\Driver as SQLSrvDriver; +use Doctrine\DBAL\DriverManager; +use Doctrine\Tests\DBAL\Mocks\MockPlatform; +use Doctrine\Tests\DbalTestCase; +use Doctrine\Tests\Mocks\ConnectionMock; +use Doctrine\Tests\Mocks\DriverMock; +use stdClass; -class DriverManagerTest extends \Doctrine\Tests\DbalTestCase +class DriverManagerTest extends DbalTestCase { /** + * @requires extension pdo_sqlite * @expectedException \Doctrine\DBAL\DBALException */ public function testInvalidPdoInstance() { - $options = array( - 'pdo' => 'test' - ); - $test = \Doctrine\DBAL\DriverManager::getConnection($options); + DriverManager::getConnection(['pdo' => 'test']); } + /** + * @requires extension pdo_sqlite + */ public function testValidPdoInstance() { - $options = array( - 'pdo' => new \PDO('sqlite::memory:') - ); - $conn = \Doctrine\DBAL\DriverManager::getConnection($options); + $conn = DriverManager::getConnection([ + 'pdo' => new \PDO('sqlite::memory:'), + ]); + self::assertEquals('sqlite', $conn->getDatabasePlatform()->getName()); } /** * @group DBAL-32 + * @requires extension pdo_sqlite */ public function testPdoInstanceSetErrorMode() { $pdo = new \PDO('sqlite::memory:'); $pdo->setAttribute(\PDO::ATTR_ERRMODE, \PDO::ERRMODE_SILENT); - $options = array( - 'pdo' => $pdo - ); + $options = ['pdo' => $pdo]; - $conn = \Doctrine\DBAL\DriverManager::getConnection($options); + DriverManager::getConnection($options); self::assertEquals(\PDO::ERRMODE_EXCEPTION, $pdo->getAttribute(\PDO::ATTR_ERRMODE)); } @@ -47,7 +56,7 @@ public function testPdoInstanceSetErrorMode() */ public function testCheckParams() { - $conn = \Doctrine\DBAL\DriverManager::getConnection(array()); + DriverManager::getConnection([]); } /** @@ -55,65 +64,70 @@ public function testCheckParams() */ public function testInvalidDriver() { - $conn = \Doctrine\DBAL\DriverManager::getConnection(array('driver' => 'invalid_driver')); + DriverManager::getConnection(['driver' => 'invalid_driver']); } + /** + * @requires extension pdo_sqlite + */ public function testCustomPlatform() { - $mockPlatform = new \Doctrine\Tests\DBAL\Mocks\MockPlatform(); - $options = array( - 'pdo' => new \PDO('sqlite::memory:'), - 'platform' => $mockPlatform - ); + $mockPlatform = new MockPlatform(); + $options = [ + 'pdo' => new \PDO('sqlite::memory:'), + 'platform' => $mockPlatform, + ]; - $conn = \Doctrine\DBAL\DriverManager::getConnection($options); + $conn = DriverManager::getConnection($options); self::assertSame($mockPlatform, $conn->getDatabasePlatform()); } + /** + * @requires extension pdo_sqlite + */ public function testCustomWrapper() { - $wrapperClass = 'Doctrine\Tests\Mocks\ConnectionMock'; + $wrapperClass = ConnectionMock::class; - $options = array( + $options = [ 'pdo' => new \PDO('sqlite::memory:'), 'wrapperClass' => $wrapperClass, - ); + ]; - $conn = \Doctrine\DBAL\DriverManager::getConnection($options); + $conn = DriverManager::getConnection($options); self::assertInstanceOf($wrapperClass, $conn); } + /** + * @requires extension pdo_sqlite + */ public function testInvalidWrapperClass() { $this->expectException(DBALException::class); - $options = array( + $options = [ 'pdo' => new \PDO('sqlite::memory:'), - 'wrapperClass' => 'stdClass', - ); + 'wrapperClass' => stdClass::class, + ]; - $conn = \Doctrine\DBAL\DriverManager::getConnection($options); + DriverManager::getConnection($options); } public function testInvalidDriverClass() { $this->expectException(DBALException::class); - $options = array( - 'driverClass' => 'stdClass' - ); + $options = ['driverClass' => stdClass::class]; - $conn = \Doctrine\DBAL\DriverManager::getConnection($options); + DriverManager::getConnection($options); } public function testValidDriverClass() { - $options = array( - 'driverClass' => 'Doctrine\DBAL\Driver\PDOMySql\Driver', - ); + $options = ['driverClass' => PDOMySQLDriver::class]; - $conn = \Doctrine\DBAL\DriverManager::getConnection($options); - self::assertInstanceOf('Doctrine\DBAL\Driver\PDOMySql\Driver', $conn->getDriver()); + $conn = DriverManager::getConnection($options); + self::assertInstanceOf(PDOMySQLDriver::class, $conn->getDriver()); } /** @@ -121,19 +135,27 @@ public function testValidDriverClass() */ public function testDatabaseUrl($url, $expected) { - $options = is_array($url) ? $url : array( - 'url' => $url, - ); + $options = is_array($url) ? $url : ['url' => $url]; + + if (isset($options['pdo'])) { + if (! extension_loaded('pdo')) { + $this->markTestSkipped('PDO is not installed'); + } + + $options['pdo'] = $this->createMock(\PDO::class); + } + + $options = is_array($url) ? $url : ['url' => $url]; if ($expected === false) { $this->expectException(DBALException::class); } - $conn = \Doctrine\DBAL\DriverManager::getConnection($options); + $conn = DriverManager::getConnection($options); $params = $conn->getParams(); foreach ($expected as $key => $value) { - if (in_array($key, array('pdo', 'driver', 'driverClass'), true)) { + if (in_array($key, ['pdo', 'driver', 'driverClass'], true)) { self::assertInstanceOf($value, $conn->getDriver()); } else { self::assertEquals($value, $params[$key]); @@ -143,123 +165,294 @@ public function testDatabaseUrl($url, $expected) public function databaseUrls() { - $pdoMock = $this->createMock(PDOMock::class); - - return array( - 'simple URL' => array( + return [ + 'simple URL' => [ 'mysql://foo:bar@localhost/baz', - array('user' => 'foo', 'password' => 'bar', 'host' => 'localhost', 'dbname' => 'baz', 'driver' => 'Doctrine\DBAL\Driver\PDOMySQL\Driver'), - ), - 'simple URL with port' => array( + [ + 'user' => 'foo', + 'password' => 'bar', + 'host' => 'localhost', + 'dbname' => 'baz', + 'driver' => PDOMySQLDriver::class, + ], + ], + 'simple URL with port' => [ 'mysql://foo:bar@localhost:11211/baz', - array('user' => 'foo', 'password' => 'bar', 'host' => 'localhost', 'port' => 11211, 'dbname' => 'baz', 'driver' => 'Doctrine\DBAL\Driver\PDOMySQL\Driver'), - ), - 'sqlite relative URL with host' => array( + [ + 'user' => 'foo', + 'password' => 'bar', + 'host' => 'localhost', + 'port' => 11211, + 'dbname' => 'baz', + 'driver' => PDOMySQLDriver::class, + ], + ], + 'sqlite relative URL with host' => [ 'sqlite://localhost/foo/dbname.sqlite', - array('path' => 'foo/dbname.sqlite', 'driver' => 'Doctrine\DBAL\Driver\PDOSqlite\Driver'), - ), - 'sqlite absolute URL with host' => array( + [ + 'path' => 'foo/dbname.sqlite', + 'driver' => PDOSqliteDriver::class, + ], + ], + 'sqlite absolute URL with host' => [ 'sqlite://localhost//tmp/dbname.sqlite', - array('path' => '/tmp/dbname.sqlite', 'driver' => 'Doctrine\DBAL\Driver\PDOSqlite\Driver'), - ), - 'sqlite relative URL without host' => array( + [ + 'path' => '/tmp/dbname.sqlite', + 'driver' => PDOSqliteDriver::class, + ], + ], + 'sqlite relative URL without host' => [ 'sqlite:///foo/dbname.sqlite', - array('path' => 'foo/dbname.sqlite', 'driver' => 'Doctrine\DBAL\Driver\PDOSqlite\Driver'), - ), - 'sqlite absolute URL without host' => array( + [ + 'path' => 'foo/dbname.sqlite', + 'driver' => PDOSqliteDriver::class, + ], + ], + 'sqlite absolute URL without host' => [ 'sqlite:////tmp/dbname.sqlite', - array('path' => '/tmp/dbname.sqlite', 'driver' => 'Doctrine\DBAL\Driver\PDOSqlite\Driver'), - ), - 'sqlite memory' => array( + [ + 'path' => '/tmp/dbname.sqlite', + 'driver' => PDOSqliteDriver::class, + ], + ], + 'sqlite memory' => [ 'sqlite:///:memory:', - array('memory' => true, 'driver' => 'Doctrine\DBAL\Driver\PDOSqlite\Driver'), - ), - 'sqlite memory with host' => array( + [ + 'memory' => true, + 'driver' => PDOSqliteDriver::class, + ], + ], + 'sqlite memory with host' => [ 'sqlite://localhost/:memory:', - array('memory' => true, 'driver' => 'Doctrine\DBAL\Driver\PDOSqlite\Driver'), - ), - 'params parsed from URL override individual params' => array( - array('url' => 'mysql://foo:bar@localhost/baz', 'password' => 'lulz'), - array('user' => 'foo', 'password' => 'bar', 'host' => 'localhost', 'dbname' => 'baz', 'driver' => 'Doctrine\DBAL\Driver\PDOMySQL\Driver'), - ), - 'params not parsed from URL but individual params are preserved' => array( - array('url' => 'mysql://foo:bar@localhost/baz', 'port' => 1234), - array('user' => 'foo', 'password' => 'bar', 'host' => 'localhost', 'port' => 1234, 'dbname' => 'baz', 'driver' => 'Doctrine\DBAL\Driver\PDOMySQL\Driver'), - ), - 'query params from URL are used as extra params' => array( + [ + 'memory' => true, + 'driver' => PDOSqliteDriver::class, + ], + ], + 'params parsed from URL override individual params' => [ + [ + 'url' => 'mysql://foo:bar@localhost/baz', + 'password' => 'lulz', + ], + [ + 'user' => 'foo', + 'password' => 'bar', + 'host' => 'localhost', + 'dbname' => 'baz', + 'driver' => PDOMySQLDriver::class, + ], + ], + 'params not parsed from URL but individual params are preserved' => [ + [ + 'url' => 'mysql://foo:bar@localhost/baz', + 'port' => 1234, + ], + [ + 'user' => 'foo', + 'password' => 'bar', + 'host' => 'localhost', + 'port' => 1234, + 'dbname' => 'baz', + 'driver' => PDOMySQLDriver::class, + ], + ], + 'query params from URL are used as extra params' => [ 'url' => 'mysql://foo:bar@localhost/dbname?charset=UTF-8', - array('charset' => 'UTF-8'), - ), - 'simple URL with fallthrough scheme not defined in map' => array( + ['charset' => 'UTF-8'], + ], + 'simple URL with fallthrough scheme not defined in map' => [ 'sqlsrv://foo:bar@localhost/baz', - array('user' => 'foo', 'password' => 'bar', 'host' => 'localhost', 'dbname' => 'baz', 'driver' => 'Doctrine\DBAL\Driver\SQLSrv\Driver'), - ), - 'simple URL with fallthrough scheme containing underscores fails' => array( + [ + 'user' => 'foo', + 'password' => 'bar', + 'host' => 'localhost', + 'dbname' => 'baz', + 'driver' => SQLSrvDriver::class, + ], + ], + 'simple URL with fallthrough scheme containing underscores fails' => [ 'drizzle_pdo_mysql://foo:bar@localhost/baz', false, - ), - 'simple URL with fallthrough scheme containing dashes works' => array( + ], + 'simple URL with fallthrough scheme containing dashes works' => [ 'drizzle-pdo-mysql://foo:bar@localhost/baz', - array('user' => 'foo', 'password' => 'bar', 'host' => 'localhost', 'dbname' => 'baz', 'driver' => 'Doctrine\DBAL\Driver\DrizzlePDOMySql\Driver'), - ), - 'simple URL with percent encoding' => array( + [ + 'user' => 'foo', + 'password' => 'bar', + 'host' => 'localhost', + 'dbname' => 'baz', + 'driver' => DrizzlePDOMySqlDriver::class, + ], + ], + 'simple URL with percent encoding' => [ 'mysql://foo%3A:bar%2F@localhost/baz+baz%40', - array('user' => 'foo:', 'password' => 'bar/', 'host' => 'localhost', 'dbname' => 'baz+baz@', 'driver' => 'Doctrine\DBAL\Driver\PDOMySQL\Driver'), - ), - 'simple URL with percent sign in password' => array( + [ + 'user' => 'foo:', + 'password' => 'bar/', + 'host' => 'localhost', + 'dbname' => 'baz+baz@', + 'driver' => PDOMySQLDriver::class, + ], + ], + 'simple URL with percent sign in password' => [ 'mysql://foo:bar%25bar@localhost/baz', - array('user' => 'foo', 'password' => 'bar%bar', 'host' => 'localhost', 'dbname' => 'baz', 'driver' => 'Doctrine\DBAL\Driver\PDOMySQL\Driver'), - ), + [ + 'user' => 'foo', + 'password' => 'bar%bar', + 'host' => 'localhost', + 'dbname' => 'baz', + 'driver' => PDOMySQLDriver::class, + ], + ], // DBAL-1234 - 'URL without scheme and without any driver information' => array( - array('url' => '//foo:bar@localhost/baz'), + 'URL without scheme and without any driver information' => [ + ['url' => '//foo:bar@localhost/baz'], false, - ), - 'URL without scheme but default PDO driver' => array( - array('url' => '//foo:bar@localhost/baz', 'pdo' => $pdoMock), + ], + 'URL without scheme but default PDO driver' => [ + [ + 'url' => '//foo:bar@localhost/baz', + 'pdo' => true, + ], false, - ), - 'URL without scheme but default driver' => array( - array('url' => '//foo:bar@localhost/baz', 'driver' => 'pdo_mysql'), - array('user' => 'foo', 'password' => 'bar', 'host' => 'localhost', 'dbname' => 'baz', 'driver' => 'Doctrine\DBAL\Driver\PDOMySQL\Driver'), - ), - 'URL without scheme but custom driver' => array( - array('url' => '//foo:bar@localhost/baz', 'driverClass' => 'Doctrine\Tests\Mocks\DriverMock'), - array('user' => 'foo', 'password' => 'bar', 'host' => 'localhost', 'dbname' => 'baz', 'driverClass' => 'Doctrine\Tests\Mocks\DriverMock'), - ), - 'URL without scheme but default PDO driver and default driver' => array( - array('url' => '//foo:bar@localhost/baz', 'pdo' => $pdoMock, 'driver' => 'pdo_mysql'), - array('user' => 'foo', 'password' => 'bar', 'host' => 'localhost', 'dbname' => 'baz', 'driver' => 'Doctrine\DBAL\Driver\PDOMySQL\Driver'), - ), - 'URL without scheme but driver and custom driver' => array( - array('url' => '//foo:bar@localhost/baz', 'driver' => 'pdo_mysql', 'driverClass' => 'Doctrine\Tests\Mocks\DriverMock'), - array('user' => 'foo', 'password' => 'bar', 'host' => 'localhost', 'dbname' => 'baz', 'driverClass' => 'Doctrine\Tests\Mocks\DriverMock'), - ), - 'URL with default PDO driver' => array( - array('url' => 'mysql://foo:bar@localhost/baz', 'pdo' => $pdoMock), - array('user' => 'foo', 'password' => 'bar', 'host' => 'localhost', 'dbname' => 'baz', 'driver' => 'Doctrine\DBAL\Driver\PDOMySQL\Driver'), - ), - 'URL with default driver' => array( - array('url' => 'mysql://foo:bar@localhost/baz', 'driver' => 'sqlite'), - array('user' => 'foo', 'password' => 'bar', 'host' => 'localhost', 'dbname' => 'baz', 'driver' => 'Doctrine\DBAL\Driver\PDOMySQL\Driver'), - ), - 'URL with default custom driver' => array( - array('url' => 'mysql://foo:bar@localhost/baz', 'driverClass' => 'Doctrine\Tests\Mocks\DriverMock'), - array('user' => 'foo', 'password' => 'bar', 'host' => 'localhost', 'dbname' => 'baz', 'driver' => 'Doctrine\DBAL\Driver\PDOMySQL\Driver'), - ), - 'URL with default PDO driver and default driver' => array( - array('url' => 'mysql://foo:bar@localhost/baz', 'pdo' => $pdoMock, 'driver' => 'sqlite'), - array('user' => 'foo', 'password' => 'bar', 'host' => 'localhost', 'dbname' => 'baz', 'driver' => 'Doctrine\DBAL\Driver\PDOMySQL\Driver'), - ), - 'URL with default driver and default custom driver' => array( - array('url' => 'mysql://foo:bar@localhost/baz', 'driver' => 'sqlite', 'driverClass' => 'Doctrine\Tests\Mocks\DriverMock'), - array('user' => 'foo', 'password' => 'bar', 'host' => 'localhost', 'dbname' => 'baz', 'driver' => 'Doctrine\DBAL\Driver\PDOMySQL\Driver'), - ), - 'URL with default PDO driver and default driver and default custom driver' => array( - array('url' => 'mysql://foo:bar@localhost/baz', 'pdo' => $pdoMock, 'driver' => 'sqlite', 'driverClass' => 'Doctrine\Tests\Mocks\DriverMock'), - array('user' => 'foo', 'password' => 'bar', 'host' => 'localhost', 'dbname' => 'baz', 'driver' => 'Doctrine\DBAL\Driver\PDOMySQL\Driver'), - ), - ); + ], + 'URL without scheme but default driver' => [ + [ + 'url' => '//foo:bar@localhost/baz', + 'driver' => 'pdo_mysql', + ], + [ + 'user' => 'foo', + 'password' => 'bar', + 'host' => 'localhost', + 'dbname' => 'baz', + 'driver' => PDOMySQLDriver::class, + ], + ], + 'URL without scheme but custom driver' => [ + [ + 'url' => '//foo:bar@localhost/baz', + 'driverClass' => DriverMock::class, + ], + [ + 'user' => 'foo', + 'password' => 'bar', + 'host' => 'localhost', + 'dbname' => 'baz', + 'driverClass' => DriverMock::class, + ], + ], + 'URL without scheme but default PDO driver and default driver' => [ + [ + 'url' => '//foo:bar@localhost/baz', + 'pdo' => true, + 'driver' => 'pdo_mysql', + ], + [ + 'user' => 'foo', + 'password' => 'bar', + 'host' => 'localhost', + 'dbname' => 'baz', + 'driver' => PDOMySQLDriver::class, + ], + ], + 'URL without scheme but driver and custom driver' => [ + [ + 'url' => '//foo:bar@localhost/baz', + 'driver' => 'pdo_mysql', + 'driverClass' => DriverMock::class, + ], + [ + 'user' => 'foo', + 'password' => 'bar', + 'host' => 'localhost', + 'dbname' => 'baz', + 'driverClass' => DriverMock::class, + ], + ], + 'URL with default PDO driver' => [ + [ + 'url' => 'mysql://foo:bar@localhost/baz', + 'pdo' => true, + ], + [ + 'user' => 'foo', + 'password' => 'bar', + 'host' => 'localhost', + 'dbname' => 'baz', + 'driver' => PDOMySQLDriver::class, + ], + ], + 'URL with default driver' => [ + [ + 'url' => 'mysql://foo:bar@localhost/baz', + 'driver' => 'sqlite', + ], + [ + 'user' => 'foo', + 'password' => 'bar', + 'host' => 'localhost', + 'dbname' => 'baz', + 'driver' => PDOMySQLDriver::class, + ], + ], + 'URL with default custom driver' => [ + [ + 'url' => 'mysql://foo:bar@localhost/baz', + 'driverClass' => DriverMock::class, + ], + [ + 'user' => 'foo', + 'password' => 'bar', + 'host' => 'localhost', + 'dbname' => 'baz', + 'driver' => PDOMySQLDriver::class, + ], + ], + 'URL with default PDO driver and default driver' => [ + [ + 'url' => 'mysql://foo:bar@localhost/baz', + 'pdo' => true, + 'driver' => 'sqlite', + ], + [ + 'user' => 'foo', + 'password' => 'bar', + 'host' => 'localhost', + 'dbname' => 'baz', + 'driver' => PDOMySQLDriver::class, + ], + ], + 'URL with default driver and default custom driver' => [ + [ + 'url' => 'mysql://foo:bar@localhost/baz', + 'driver' => 'sqlite', + 'driverClass' => DriverMock::class, + ], + [ + 'user' => 'foo', + 'password' => 'bar', + 'host' => 'localhost', + 'dbname' => 'baz', + 'driver' => PDOMySQLDriver::class, + ], + ], + 'URL with default PDO driver and default driver and default custom driver' => [ + [ + 'url' => 'mysql://foo:bar@localhost/baz', + 'pdo' => true, + 'driver' => 'sqlite', + 'driverClass' => DriverMock::class, + ], + [ + 'user' => 'foo', + 'password' => 'bar', + 'host' => 'localhost', + 'dbname' => 'baz', + 'driver' => PDOMySQLDriver::class, + ], + ], + ]; } } diff --git a/tests/Doctrine/Tests/DBAL/Functional/BlobTest.php b/tests/Doctrine/Tests/DBAL/Functional/BlobTest.php index 557974e4cdc..ebbff67bcef 100644 --- a/tests/Doctrine/Tests/DBAL/Functional/BlobTest.php +++ b/tests/Doctrine/Tests/DBAL/Functional/BlobTest.php @@ -2,8 +2,8 @@ namespace Doctrine\Tests\DBAL\Functional; +use Doctrine\DBAL\ParameterType; use Doctrine\DBAL\Types\Type; -use PDO; /** * @group DBAL-6 @@ -37,35 +37,60 @@ protected function setUp() public function testInsert() { - $ret = $this->_conn->insert('blob_table', - array('id' => 1, 'clobfield' => 'test', 'blobfield' => 'test', 'binaryfield' => 'test'), - array(\PDO::PARAM_INT, \PDO::PARAM_STR, \PDO::PARAM_LOB, \PDO::PARAM_LOB) - ); + $ret = $this->_conn->insert('blob_table', [ + 'id' => 1, + 'clobfield' => 'test', + 'blobfield' => 'test', + 'binaryfield' => 'test', + ], [ + ParameterType::INTEGER, + ParameterType::STRING, + ParameterType::LARGE_OBJECT, + ParameterType::LARGE_OBJECT, + ]); + self::assertEquals(1, $ret); } public function testSelect() { - $this->_conn->insert('blob_table', - array('id' => 1, 'clobfield' => 'test', 'blobfield' => 'test', 'binaryfield' => 'test'), - array(\PDO::PARAM_INT, \PDO::PARAM_STR, \PDO::PARAM_LOB, \PDO::PARAM_LOB) - ); + $this->_conn->insert('blob_table', [ + 'id' => 1, + 'clobfield' => 'test', + 'blobfield' => 'test', + 'binaryfield' => 'test', + ], [ + ParameterType::INTEGER, + ParameterType::STRING, + ParameterType::LARGE_OBJECT, + ParameterType::LARGE_OBJECT, + ]); $this->assertBlobContains('test'); } public function testUpdate() { - $this->_conn->insert('blob_table', - array('id' => 1, 'clobfield' => 'test', 'blobfield' => 'test', 'binaryfield' => 'test'), - array(\PDO::PARAM_INT, \PDO::PARAM_STR, \PDO::PARAM_LOB, \PDO::PARAM_LOB) - ); - - $this->_conn->update('blob_table', - array('blobfield' => 'test2', 'binaryfield' => 'test2'), - array('id' => 1), - array(\PDO::PARAM_LOB, \PDO::PARAM_LOB, \PDO::PARAM_INT) - ); + $this->_conn->insert('blob_table', [ + 'id' => 1, + 'clobfield' => 'test', + 'blobfield' => 'test', + 'binaryfield' => 'test', + ], [ + ParameterType::INTEGER, + ParameterType::STRING, + ParameterType::LARGE_OBJECT, + ParameterType::LARGE_OBJECT, + ]); + + $this->_conn->update('blob_table', [ + 'blobfield' => 'test2', + 'binaryfield' => 'test2', + ], ['id' => 1], [ + ParameterType::LARGE_OBJECT, + ParameterType::LARGE_OBJECT, + ParameterType::INTEGER, + ]); $this->assertBlobContains('test2'); $this->assertBinaryContains('test2'); diff --git a/tests/Doctrine/Tests/DBAL/Functional/ConnectionTest.php b/tests/Doctrine/Tests/DBAL/Functional/ConnectionTest.php index a595d2d2a2d..e1b81198727 100644 --- a/tests/Doctrine/Tests/DBAL/Functional/ConnectionTest.php +++ b/tests/Doctrine/Tests/DBAL/Functional/ConnectionTest.php @@ -4,6 +4,7 @@ use Doctrine\DBAL\ConnectionException; use Doctrine\DBAL\DriverManager; +use Doctrine\DBAL\ParameterType; use Doctrine\DBAL\Platforms\AbstractPlatform; use Doctrine\DBAL\Types\Type; @@ -242,7 +243,10 @@ public function testTransactionalReturnValue() */ public function testQuote() { - self::assertEquals($this->_conn->quote("foo", Type::STRING), $this->_conn->quote("foo", \PDO::PARAM_STR)); + self::assertEquals( + $this->_conn->quote('foo', Type::STRING), + $this->_conn->quote('foo', ParameterType::STRING) + ); } public function testPingDoesTriggersConnect() diff --git a/tests/Doctrine/Tests/DBAL/Functional/DataAccessTest.php b/tests/Doctrine/Tests/DBAL/Functional/DataAccessTest.php index d35e9baf5c6..503594d8ebf 100644 --- a/tests/Doctrine/Tests/DBAL/Functional/DataAccessTest.php +++ b/tests/Doctrine/Tests/DBAL/Functional/DataAccessTest.php @@ -3,10 +3,11 @@ namespace Doctrine\Tests\DBAL\Functional; use Doctrine\DBAL\Connection; +use Doctrine\DBAL\FetchMode; +use Doctrine\DBAL\ParameterType; use Doctrine\DBAL\Platforms\AbstractPlatform; use Doctrine\DBAL\Platforms\TrimMode; use Doctrine\DBAL\Types\Type; -use PDO; class DataAccessTest extends \Doctrine\Tests\DbalFunctionalTestCase { @@ -42,7 +43,7 @@ public function testPrepareWithBindValue() $stmt->bindValue(2, 'foo'); $stmt->execute(); - $row = $stmt->fetch(\PDO::FETCH_ASSOC); + $row = $stmt->fetch(FetchMode::ASSOCIATIVE); $row = array_change_key_case($row, \CASE_LOWER); self::assertEquals(array('test_int' => 1, 'test_string' => 'foo'), $row); } @@ -60,7 +61,7 @@ public function testPrepareWithBindParam() $stmt->bindParam(2, $paramStr); $stmt->execute(); - $row = $stmt->fetch(\PDO::FETCH_ASSOC); + $row = $stmt->fetch(FetchMode::ASSOCIATIVE); $row = array_change_key_case($row, \CASE_LOWER); self::assertEquals(array('test_int' => 1, 'test_string' => 'foo'), $row); } @@ -78,7 +79,7 @@ public function testPrepareWithFetchAll() $stmt->bindParam(2, $paramStr); $stmt->execute(); - $rows = $stmt->fetchAll(\PDO::FETCH_ASSOC); + $rows = $stmt->fetchAll(FetchMode::ASSOCIATIVE); $rows[0] = array_change_key_case($rows[0], \CASE_LOWER); self::assertEquals(array('test_int' => 1, 'test_string' => 'foo'), $rows[0]); } @@ -99,7 +100,7 @@ public function testPrepareWithFetchAllBoth() $stmt->bindParam(2, $paramStr); $stmt->execute(); - $rows = $stmt->fetchAll(\PDO::FETCH_BOTH); + $rows = $stmt->fetchAll(FetchMode::MIXED); $rows[0] = array_change_key_case($rows[0], \CASE_LOWER); self::assertEquals(array('test_int' => 1, 'test_string' => 'foo', 0 => 1, 1 => 'foo'), $rows[0]); } @@ -135,7 +136,7 @@ public function testPrepareWithIterator() $stmt->execute(); $rows = array(); - $stmt->setFetchMode(\PDO::FETCH_ASSOC); + $stmt->setFetchMode(FetchMode::ASSOCIATIVE); foreach ($stmt as $row) { $rows[] = array_change_key_case($row, \CASE_LOWER); } @@ -165,7 +166,7 @@ public function testPrepareWithExecuteParams() self::assertInstanceOf('Doctrine\DBAL\Statement', $stmt); $stmt->execute(array($paramInt, $paramStr)); - $row = $stmt->fetch(\PDO::FETCH_ASSOC); + $row = $stmt->fetch(FetchMode::ASSOCIATIVE); self::assertNotFalse($row); $row = array_change_key_case($row, \CASE_LOWER); self::assertEquals(array('test_int' => 1, 'test_string' => 'foo'), $row); @@ -193,8 +194,9 @@ public function testFetchAllWithTypes() { $datetimeString = '2010-01-01 10:10:10'; $datetime = new \DateTime($datetimeString); - $sql = "SELECT test_int, test_datetime FROM fetch_table WHERE test_int = ? AND test_datetime = ?"; - $data = $this->_conn->fetchAll($sql, array(1, $datetime), array(PDO::PARAM_STR, Type::DATETIME)); + + $sql = 'SELECT test_int, test_datetime FROM fetch_table WHERE test_int = ? AND test_datetime = ?'; + $data = $this->_conn->fetchAll($sql, [1, $datetime], [ParameterType::STRING, Type::DATETIME]); self::assertCount(1, $data); @@ -225,8 +227,8 @@ public function testFetchAllWithMissingTypes() public function testFetchBoth() { - $sql = "SELECT test_int, test_string FROM fetch_table WHERE test_int = ? AND test_string = ?"; - $row = $this->_conn->executeQuery($sql, array(1, 'foo'))->fetch(\PDO::FETCH_BOTH); + $sql = 'SELECT test_int, test_string FROM fetch_table WHERE test_int = ? AND test_string = ?'; + $row = $this->_conn->executeQuery($sql, [1, 'foo'])->fetch(FetchMode::MIXED); self::assertNotFalse($row); @@ -262,8 +264,9 @@ public function testFetchAssocWithTypes() { $datetimeString = '2010-01-01 10:10:10'; $datetime = new \DateTime($datetimeString); - $sql = "SELECT test_int, test_datetime FROM fetch_table WHERE test_int = ? AND test_datetime = ?"; - $row = $this->_conn->fetchAssoc($sql, array(1, $datetime), array(PDO::PARAM_STR, Type::DATETIME)); + + $sql = 'SELECT test_int, test_datetime FROM fetch_table WHERE test_int = ? AND test_datetime = ?'; + $row = $this->_conn->fetchAssoc($sql, [1, $datetime], [ParameterType::STRING, Type::DATETIME]); self::assertNotFalse($row); @@ -302,8 +305,9 @@ public function testFetchArrayWithTypes() { $datetimeString = '2010-01-01 10:10:10'; $datetime = new \DateTime($datetimeString); - $sql = "SELECT test_int, test_datetime FROM fetch_table WHERE test_int = ? AND test_datetime = ?"; - $row = $this->_conn->fetchArray($sql, array(1, $datetime), array(PDO::PARAM_STR, Type::DATETIME)); + + $sql = 'SELECT test_int, test_datetime FROM fetch_table WHERE test_int = ? AND test_datetime = ?'; + $row = $this->_conn->fetchArray($sql, [1, $datetime], [ParameterType::STRING, Type::DATETIME]); self::assertNotFalse($row); @@ -346,8 +350,9 @@ public function testFetchColumnWithTypes() { $datetimeString = '2010-01-01 10:10:10'; $datetime = new \DateTime($datetimeString); - $sql = "SELECT test_int, test_datetime FROM fetch_table WHERE test_int = ? AND test_datetime = ?"; - $column = $this->_conn->fetchColumn($sql, array(1, $datetime), 1, array(PDO::PARAM_STR, Type::DATETIME)); + + $sql = 'SELECT test_int, test_datetime FROM fetch_table WHERE test_int = ? AND test_datetime = ?'; + $column = $this->_conn->fetchColumn($sql, [1, $datetime], 1, [ParameterType::STRING, Type::DATETIME]); self::assertNotFalse($column); @@ -392,10 +397,15 @@ public function testExecuteUpdateBindDateTimeType() $datetime = new \DateTime('2010-02-02 20:20:20'); $sql = 'INSERT INTO fetch_table (test_int, test_string, test_datetime) VALUES (?, ?, ?)'; - $affectedRows = $this->_conn->executeUpdate($sql, - array(1 => 50, 2 => 'foo', 3 => $datetime), - array(1 => PDO::PARAM_INT, 2 => PDO::PARAM_STR, 3 => Type::DATETIME) - ); + $affectedRows = $this->_conn->executeUpdate($sql, [ + 1 => 50, + 2 => 'foo', + 3 => $datetime, + ], [ + 1 => ParameterType::INTEGER, + 2 => ParameterType::STRING, + 3 => Type::DATETIME, + ]); self::assertEquals(1, $affectedRows); self::assertEquals(1, $this->_conn->executeQuery( @@ -430,14 +440,14 @@ public function testNativeArrayListSupport() $stmt = $this->_conn->executeQuery('SELECT test_int FROM fetch_table WHERE test_int IN (?)', array(array(100, 101, 102, 103, 104)), array(Connection::PARAM_INT_ARRAY)); - $data = $stmt->fetchAll(PDO::FETCH_NUM); + $data = $stmt->fetchAll(FetchMode::NUMERIC); self::assertCount(5, $data); self::assertEquals(array(array(100), array(101), array(102), array(103), array(104)), $data); $stmt = $this->_conn->executeQuery('SELECT test_int FROM fetch_table WHERE test_string IN (?)', array(array('foo100', 'foo101', 'foo102', 'foo103', 'foo104')), array(Connection::PARAM_STR_ARRAY)); - $data = $stmt->fetchAll(PDO::FETCH_NUM); + $data = $stmt->fetchAll(FetchMode::NUMERIC); self::assertCount(5, $data); self::assertEquals(array(array(100), array(101), array(102), array(103), array(104)), $data); } @@ -614,8 +624,8 @@ public function testBitComparisonExpressionSupport() $sql[] = $platform->getBitAndComparisonExpression('test_int', 2) . ' AS bit_and '; $sql[] = 'FROM fetch_table'; - $stmt = $this->_conn->executeQuery(implode(PHP_EOL, $sql)); - $data = $stmt->fetchAll(PDO::FETCH_ASSOC); + $stmt = $this->_conn->executeQuery(implode(PHP_EOL, $sql)); + $data = $stmt->fetchAll(FetchMode::ASSOCIATIVE); self::assertCount(4, $data); @@ -641,7 +651,7 @@ public function testBitComparisonExpressionSupport() public function testSetDefaultFetchMode() { $stmt = $this->_conn->query("SELECT * FROM fetch_table"); - $stmt->setFetchMode(\PDO::FETCH_NUM); + $stmt->setFetchMode(FetchMode::NUMERIC); $row = array_keys($stmt->fetch()); self::assertCount(0, array_filter($row, function($v) { return ! is_numeric($v); }), "should be no non-numerical elements in the result."); @@ -659,7 +669,7 @@ public function testFetchAllStyleObject() $stmt->execute(); - $results = $stmt->fetchAll(\PDO::FETCH_OBJ); + $results = $stmt->fetchAll(FetchMode::STANDARD_OBJECT); self::assertCount(1, $results); self::assertInstanceOf('stdClass', $results[0]); @@ -691,7 +701,7 @@ public function testFetchAllSupportFetchClass() $stmt->execute(); $results = $stmt->fetchAll( - \PDO::FETCH_CLASS, + FetchMode::CUSTOM_OBJECT, __NAMESPACE__.'\\MyFetchClass' ); @@ -715,7 +725,7 @@ public function testFetchAllStyleColumn() $this->_conn->insert('fetch_table', array('test_int' => 10, 'test_string' => 'foo')); $sql = "SELECT test_int FROM fetch_table"; - $rows = $this->_conn->query($sql)->fetchAll(\PDO::FETCH_COLUMN); + $rows = $this->_conn->query($sql)->fetchAll(FetchMode::COLUMN); self::assertEquals(array(1, 10), $rows); } @@ -730,7 +740,7 @@ public function testSetFetchModeClassFetchAll() $sql = "SELECT * FROM fetch_table"; $stmt = $this->_conn->query($sql); - $stmt->setFetchMode(\PDO::FETCH_CLASS, __NAMESPACE__ . '\\MyFetchClass'); + $stmt->setFetchMode(FetchMode::CUSTOM_OBJECT, __NAMESPACE__ . '\\MyFetchClass'); $results = $stmt->fetchAll(); @@ -752,7 +762,7 @@ public function testSetFetchModeClassFetch() $sql = "SELECT * FROM fetch_table"; $stmt = $this->_conn->query($sql); - $stmt->setFetchMode(\PDO::FETCH_CLASS, __NAMESPACE__ . '\\MyFetchClass'); + $stmt->setFetchMode(FetchMode::CUSTOM_OBJECT, __NAMESPACE__ . '\\MyFetchClass'); $results = array(); while ($row = $stmt->fetch()) { @@ -786,7 +796,7 @@ public function testSetFetchModeOnDbalStatement() { $sql = "SELECT test_int, test_string FROM fetch_table WHERE test_int = ? AND test_string = ?"; $stmt = $this->_conn->executeQuery($sql, array(1, "foo")); - $stmt->setFetchMode(\PDO::FETCH_NUM); + $stmt->setFetchMode(FetchMode::NUMERIC); $row = $stmt->fetch(); diff --git a/tests/Doctrine/Tests/DBAL/Functional/Driver/PDOPgsqlConnectionTest.php b/tests/Doctrine/Tests/DBAL/Functional/Driver/PDOPgsqlConnectionTest.php index c4d7382d525..1b7c9b587ce 100644 --- a/tests/Doctrine/Tests/DBAL/Functional/Driver/PDOPgsqlConnectionTest.php +++ b/tests/Doctrine/Tests/DBAL/Functional/Driver/PDOPgsqlConnectionTest.php @@ -3,6 +3,7 @@ namespace Doctrine\Tests\DBAL\Functional\Driver; use Doctrine\DBAL\DriverManager; +use Doctrine\DBAL\FetchMode; use Doctrine\DBAL\Platforms\PostgreSqlPlatform; use Doctrine\Tests\DbalFunctionalTestCase; @@ -40,7 +41,11 @@ public function testConnectsWithValidCharsetOption($charset) $this->_conn->getEventManager() ); - self::assertEquals($charset, $connection->query("SHOW client_encoding")->fetch(\PDO::FETCH_COLUMN)); + self::assertEquals( + $charset, + $connection->query('SHOW client_encoding') + ->fetch(FetchMode::COLUMN) + ); } /** diff --git a/tests/Doctrine/Tests/DBAL/Functional/NamedParametersTest.php b/tests/Doctrine/Tests/DBAL/Functional/NamedParametersTest.php index 539f0503b30..6f99a44d17a 100644 --- a/tests/Doctrine/Tests/DBAL/Functional/NamedParametersTest.php +++ b/tests/Doctrine/Tests/DBAL/Functional/NamedParametersTest.php @@ -3,138 +3,191 @@ namespace Doctrine\Tests\DBAL\Functional\Ticket; use Doctrine\DBAL\Connection; -use PDO; +use Doctrine\DBAL\FetchMode; +use Doctrine\DBAL\ParameterType; +use Doctrine\DBAL\Schema\Table; /** * @group DDC-1372 */ class NamedParametersTest extends \Doctrine\Tests\DbalFunctionalTestCase { - public function ticketProvider() { - return array( - array( + return [ + [ 'SELECT * FROM ddc1372_foobar f WHERE f.foo = :foo AND f.bar IN (:bar)', - array('foo'=>1,'bar'=> array(1, 2, 3)), - array('foo'=>PDO::PARAM_INT,'bar'=> Connection::PARAM_INT_ARRAY,), - array( - array('id'=>1,'foo'=>1,'bar'=>1), - array('id'=>2,'foo'=>1,'bar'=>2), - array('id'=>3,'foo'=>1,'bar'=>3), - ) - ), - - array( + [ + 'foo' => 1, + 'bar' => [1, 2, 3], + ], + [ + 'foo' => ParameterType::INTEGER, + 'bar' => Connection::PARAM_INT_ARRAY, + ], + [ + ['id' => 1, 'foo' => 1, 'bar' => 1], + ['id' => 2, 'foo' => 1, 'bar' => 2], + ['id' => 3, 'foo' => 1, 'bar' => 3], + ], + ], + + [ 'SELECT * FROM ddc1372_foobar f WHERE f.foo = :foo AND f.bar IN (:bar)', - array('foo'=>1,'bar'=> array(1, 2, 3)), - array('bar'=> Connection::PARAM_INT_ARRAY,'foo'=>PDO::PARAM_INT), - array( - array('id'=>1,'foo'=>1,'bar'=>1), - array('id'=>2,'foo'=>1,'bar'=>2), - array('id'=>3,'foo'=>1,'bar'=>3), - ) - ), - - array( + [ + 'foo' => 1, + 'bar' => [1, 2, 3], + ], + [ + 'bar' => Connection::PARAM_INT_ARRAY, + 'foo' => ParameterType::INTEGER, + ], + [ + ['id' => 1, 'foo' => 1, 'bar' => 1], + ['id' => 2, 'foo' => 1, 'bar' => 2], + ['id' => 3, 'foo' => 1, 'bar' => 3], + ], + ], + + [ 'SELECT * FROM ddc1372_foobar f WHERE f.bar IN (:bar) AND f.foo = :foo', - array('foo'=>1,'bar'=> array(1, 2, 3)), - array('bar'=> Connection::PARAM_INT_ARRAY,'foo'=>PDO::PARAM_INT), - array( - array('id'=>1,'foo'=>1,'bar'=>1), - array('id'=>2,'foo'=>1,'bar'=>2), - array('id'=>3,'foo'=>1,'bar'=>3), - ) - ), - - array( + [ + 'foo' => 1, + 'bar' => [1, 2, 3], + ], + [ + 'bar' => Connection::PARAM_INT_ARRAY, + 'foo' => ParameterType::INTEGER, + ], + [ + ['id' => 1, 'foo' => 1, 'bar' => 1], + ['id' => 2, 'foo' => 1, 'bar' => 2], + ['id' => 3, 'foo' => 1, 'bar' => 3], + ], + ], + + [ 'SELECT * FROM ddc1372_foobar f WHERE f.bar IN (:bar) AND f.foo = :foo', - array('foo'=>1,'bar'=> array('1', '2', '3')), - array('bar'=> Connection::PARAM_STR_ARRAY,'foo'=>PDO::PARAM_INT), - array( - array('id'=>1,'foo'=>1,'bar'=>1), - array('id'=>2,'foo'=>1,'bar'=>2), - array('id'=>3,'foo'=>1,'bar'=>3), - ) - ), - - array( + [ + 'foo' => 1, + 'bar' => ['1', '2', '3'], + ], + [ + 'bar' => Connection::PARAM_STR_ARRAY, + 'foo' => ParameterType::INTEGER, + ], + [ + ['id' => 1, 'foo' => 1, 'bar' => 1], + ['id' => 2, 'foo' => 1, 'bar' => 2], + ['id' => 3, 'foo' => 1, 'bar' => 3], + ], + ], + + [ 'SELECT * FROM ddc1372_foobar f WHERE f.bar IN (:bar) AND f.foo IN (:foo)', - array('foo'=>array('1'),'bar'=> array(1, 2, 3,4)), - array('bar'=> Connection::PARAM_STR_ARRAY,'foo'=>Connection::PARAM_INT_ARRAY), - array( - array('id'=>1,'foo'=>1,'bar'=>1), - array('id'=>2,'foo'=>1,'bar'=>2), - array('id'=>3,'foo'=>1,'bar'=>3), - array('id'=>4,'foo'=>1,'bar'=>4), - ) - ), - - array( + [ + 'foo' => ['1'], + 'bar' => [1, 2, 3, 4], + ], + [ + 'bar' => Connection::PARAM_STR_ARRAY, + 'foo' => Connection::PARAM_INT_ARRAY, + ], + [ + ['id' => 1, 'foo' => 1, 'bar' => 1], + ['id' => 2, 'foo' => 1, 'bar' => 2], + ['id' => 3, 'foo' => 1, 'bar' => 3], + ['id' => 4, 'foo' => 1, 'bar' => 4], + ], + ], + + [ 'SELECT * FROM ddc1372_foobar f WHERE f.bar IN (:bar) AND f.foo IN (:foo)', - array('foo'=>1,'bar'=> 2), - array('bar'=>PDO::PARAM_INT,'foo'=>PDO::PARAM_INT), - array( - array('id'=>2,'foo'=>1,'bar'=>2), - ) - ), - - array( + [ + 'foo' => 1, + 'bar' => 2, + ], + [ + 'bar' => ParameterType::INTEGER, + 'foo' => ParameterType::INTEGER, + ], + [ + ['id' => 2, 'foo' => 1, 'bar' => 2], + ], + ], + + [ 'SELECT * FROM ddc1372_foobar f WHERE f.bar = :arg AND f.foo <> :arg', - array('arg'=>'1'), - array('arg'=>PDO::PARAM_STR), - array( - array('id'=>5,'foo'=>2,'bar'=>1), - ) - ), - - array( + ['arg' => '1'], + [ + 'arg' => ParameterType::STRING, + ], + [ + ['id' => 5, 'foo' => 2, 'bar' => 1], + ], + ], + + [ 'SELECT * FROM ddc1372_foobar f WHERE f.bar NOT IN (:arg) AND f.foo IN (:arg)', - array('arg'=>array(1, 2)), - array('arg'=>Connection::PARAM_INT_ARRAY), - array( - array('id'=>3,'foo'=>1,'bar'=>3), - array('id'=>4,'foo'=>1,'bar'=>4), - ) - ), - - ); + [ + 'arg' => [1, 2], + ], + [ + 'arg' => Connection::PARAM_INT_ARRAY, + ], + [ + ['id' => 3, 'foo' => 1, 'bar' => 3], + ['id' => 4, 'foo' => 1, 'bar' => 4], + ], + ], + ]; } protected function setUp() { parent::setUp(); - if (!$this->_conn->getSchemaManager()->tablesExist("ddc1372_foobar")) { + if (! $this->_conn->getSchemaManager()->tablesExist('ddc1372_foobar')) { try { - $table = new \Doctrine\DBAL\Schema\Table("ddc1372_foobar"); + $table = new Table('ddc1372_foobar'); $table->addColumn('id', 'integer'); - $table->addColumn('foo','string'); - $table->addColumn('bar','string'); - $table->setPrimaryKey(array('id')); - + $table->addColumn('foo', 'string'); + $table->addColumn('bar', 'string'); + $table->setPrimaryKey(['id']); $sm = $this->_conn->getSchemaManager(); $sm->createTable($table); - $this->_conn->insert('ddc1372_foobar', array( - 'id' => 1, 'foo' => 1, 'bar' => 1 - )); - $this->_conn->insert('ddc1372_foobar', array( - 'id' => 2, 'foo' => 1, 'bar' => 2 - )); - $this->_conn->insert('ddc1372_foobar', array( - 'id' => 3, 'foo' => 1, 'bar' => 3 - )); - $this->_conn->insert('ddc1372_foobar', array( - 'id' => 4, 'foo' => 1, 'bar' => 4 - )); - $this->_conn->insert('ddc1372_foobar', array( - 'id' => 5, 'foo' => 2, 'bar' => 1 - )); - $this->_conn->insert('ddc1372_foobar', array( - 'id' => 6, 'foo' => 2, 'bar' => 2 - )); + $this->_conn->insert('ddc1372_foobar', [ + 'id' => 1, + 'foo' => 1, + 'bar' => 1, + ]); + $this->_conn->insert('ddc1372_foobar', [ + 'id' => 2, + 'foo' => 1, + 'bar' => 2, + ]); + $this->_conn->insert('ddc1372_foobar', [ + 'id' => 3, + 'foo' => 1, + 'bar' => 3, + ]); + $this->_conn->insert('ddc1372_foobar', [ + 'id' => 4, + 'foo' => 1, + 'bar' => 4, + ]); + $this->_conn->insert('ddc1372_foobar', [ + 'id' => 5, + 'foo' => 2, + 'bar' => 1, + ]); + $this->_conn->insert('ddc1372_foobar', [ + 'id' => 6, + 'foo' => 2, + 'bar' => 2, + ]); } catch(\Exception $e) { $this->fail($e->getMessage()); } @@ -151,7 +204,7 @@ protected function setUp() public function testTicket($query,$params,$types,$expected) { $stmt = $this->_conn->executeQuery($query, $params, $types); - $result = $stmt->fetchAll(\PDO::FETCH_ASSOC); + $result = $stmt->fetchAll(FetchMode::ASSOCIATIVE); foreach ($result as $k => $v) { $result[$k] = array_change_key_case($v, CASE_LOWER); @@ -159,5 +212,4 @@ public function testTicket($query,$params,$types,$expected) self::assertEquals($result, $expected); } - } diff --git a/tests/Doctrine/Tests/DBAL/Functional/PortabilityTest.php b/tests/Doctrine/Tests/DBAL/Functional/PortabilityTest.php index ac4b7ef18b9..d76abdaca29 100644 --- a/tests/Doctrine/Tests/DBAL/Functional/PortabilityTest.php +++ b/tests/Doctrine/Tests/DBAL/Functional/PortabilityTest.php @@ -2,10 +2,11 @@ namespace Doctrine\Tests\DBAL\Functional; +use Doctrine\DBAL\ColumnCase; use Doctrine\DBAL\Connection; use Doctrine\DBAL\DriverManager; +use Doctrine\DBAL\FetchMode; use Doctrine\DBAL\Portability\Connection as ConnectionPortability; -use PDO; /** * @group DBAL-56 @@ -28,13 +29,17 @@ protected function tearDown() * @param int $case * @return Connection */ - private function getPortableConnection($portabilityMode = \Doctrine\DBAL\Portability\Connection::PORTABILITY_ALL, $case = \PDO::CASE_LOWER) - { + private function getPortableConnection( + $portabilityMode = ConnectionPortability::PORTABILITY_ALL, + $case = ColumnCase::LOWER + ) { if (!$this->portableConnection) { $params = $this->_conn->getParams(); - $params['wrapperClass'] = 'Doctrine\DBAL\Portability\Connection'; - $params['portability'] = $portabilityMode; - $params['fetch_case'] = $case; + + $params['wrapperClass'] = ConnectionPortability::class; + $params['portability'] = $portabilityMode; + $params['fetch_case'] = $case; + $this->portableConnection = DriverManager::getConnection($params, $this->_conn->getConfiguration(), $this->_conn->getEventManager()); try { @@ -64,19 +69,22 @@ public function testFullFetchMode() $this->assertFetchResultRows($rows); $stmt = $this->getPortableConnection()->query('SELECT * FROM portability_table'); - $stmt->setFetchMode(\PDO::FETCH_ASSOC); + $stmt->setFetchMode(FetchMode::ASSOCIATIVE); + foreach ($stmt as $row) { $this->assertFetchResultRow($row); } $stmt = $this->getPortableConnection()->query('SELECT * FROM portability_table'); - while (($row = $stmt->fetch(\PDO::FETCH_ASSOC))) { + + while (($row = $stmt->fetch(FetchMode::ASSOCIATIVE))) { $this->assertFetchResultRow($row); } $stmt = $this->getPortableConnection()->prepare('SELECT * FROM portability_table'); $stmt->execute(); - while (($row = $stmt->fetch(\PDO::FETCH_ASSOC))) { + + while (($row = $stmt->fetch(FetchMode::ASSOCIATIVE))) { $this->assertFetchResultRow($row); } } @@ -84,7 +92,7 @@ public function testFullFetchMode() public function testConnFetchMode() { $conn = $this->getPortableConnection(); - $conn->setFetchMode(\PDO::FETCH_ASSOC); + $conn->setFetchMode(FetchMode::ASSOCIATIVE); $rows = $conn->fetchAll('SELECT * FROM portability_table'); $this->assertFetchResultRows($rows); @@ -120,10 +128,13 @@ public function assertFetchResultRow($row) self::assertArrayHasKey('test_string', $row, "Case should be lowered."); self::assertEquals(3, strlen($row['test_string']), "test_string should be rtrimed to length of three for CHAR(32) column."); self::assertNull($row['test_null']); - self::assertArrayNotHasKey(0, $row, "PDO::FETCH_ASSOC should not return numerical keys."); + self::assertArrayNotHasKey(0, $row, 'The row should not contain numerical keys.'); } - public function testPortabilitySqlServer() + /** + * @requires extension pdo + */ + public function testPortabilityPdoSqlServer() { $portability = ConnectionPortability::PORTABILITY_SQLSRV; $params = array( @@ -153,7 +164,7 @@ public function testFetchAllColumn($field, array $expected) $conn = $this->getPortableConnection(); $stmt = $conn->query('SELECT ' . $field . ' FROM portability_table'); - $column = $stmt->fetchAll(PDO::FETCH_COLUMN); + $column = $stmt->fetchAll(FetchMode::COLUMN); self::assertEquals($expected, $column); } @@ -176,7 +187,7 @@ public function testFetchAllNullColumn() $conn = $this->getPortableConnection(); $stmt = $conn->query('SELECT Test_Null FROM portability_table'); - $column = $stmt->fetchAll(PDO::FETCH_COLUMN); + $column = $stmt->fetchAll(FetchMode::COLUMN); self::assertSame(array(null, null), $column); } } diff --git a/tests/Doctrine/Tests/DBAL/Functional/ResultCacheTest.php b/tests/Doctrine/Tests/DBAL/Functional/ResultCacheTest.php index 76c2c8424e4..367a09f9e35 100644 --- a/tests/Doctrine/Tests/DBAL/Functional/ResultCacheTest.php +++ b/tests/Doctrine/Tests/DBAL/Functional/ResultCacheTest.php @@ -1,8 +1,9 @@ expectedResult, \PDO::FETCH_ASSOC); + self::assertCacheNonCacheSelectSameFetchModeAreEqual( + $this->expectedResult, + FetchMode::ASSOCIATIVE + ); } public function testFetchNum() @@ -53,7 +57,8 @@ public function testFetchNum() foreach ($this->expectedResult as $v) { $expectedResult[] = array_values($v); } - self::assertCacheNonCacheSelectSameFetchModeAreEqual($expectedResult, \PDO::FETCH_NUM); + + self::assertCacheNonCacheSelectSameFetchModeAreEqual($expectedResult, FetchMode::NUMERIC); } public function testFetchBoth() @@ -62,7 +67,8 @@ public function testFetchBoth() foreach ($this->expectedResult as $v) { $expectedResult[] = array_merge($v, array_values($v)); } - self::assertCacheNonCacheSelectSameFetchModeAreEqual($expectedResult, \PDO::FETCH_BOTH); + + self::assertCacheNonCacheSelectSameFetchModeAreEqual($expectedResult, FetchMode::MIXED); } public function testFetchColumn() @@ -71,7 +77,8 @@ public function testFetchColumn() foreach ($this->expectedResult as $v) { $expectedResult[] = array_shift($v); } - self::assertCacheNonCacheSelectSameFetchModeAreEqual($expectedResult, \PDO::FETCH_COLUMN); + + self::assertCacheNonCacheSelectSameFetchModeAreEqual($expectedResult, FetchMode::COLUMN); } public function testMixingFetch() @@ -82,22 +89,22 @@ public function testMixingFetch() } $stmt = $this->_conn->executeQuery("SELECT * FROM caching ORDER BY test_int ASC", array(), array(), new QueryCacheProfile(10, "testcachekey")); - $data = $this->hydrateStmt($stmt, \PDO::FETCH_ASSOC); + $data = $this->hydrateStmt($stmt, FetchMode::ASSOCIATIVE); self::assertEquals($this->expectedResult, $data); $stmt = $this->_conn->executeQuery("SELECT * FROM caching ORDER BY test_int ASC", array(), array(), new QueryCacheProfile(10, "testcachekey")); - $data = $this->hydrateStmt($stmt, \PDO::FETCH_NUM); + $data = $this->hydrateStmt($stmt, FetchMode::NUMERIC); self::assertEquals($numExpectedResult, $data); } public function testIteratorFetch() { - self::assertStandardAndIteratorFetchAreEqual(\PDO::FETCH_BOTH); - self::assertStandardAndIteratorFetchAreEqual(\PDO::FETCH_ASSOC); - self::assertStandardAndIteratorFetchAreEqual(\PDO::FETCH_NUM); + self::assertStandardAndIteratorFetchAreEqual(FetchMode::MIXED); + self::assertStandardAndIteratorFetchAreEqual(FetchMode::ASSOCIATIVE); + self::assertStandardAndIteratorFetchAreEqual(FetchMode::NUMERIC); } public function assertStandardAndIteratorFetchAreEqual($fetchMode) @@ -116,14 +123,16 @@ public function testDontCloseNoCache() $stmt = $this->_conn->executeQuery("SELECT * FROM caching ORDER BY test_int ASC", array(), array(), new QueryCacheProfile(10, "testcachekey")); $data = array(); - while ($row = $stmt->fetch(\PDO::FETCH_ASSOC)) { + + while ($row = $stmt->fetch(FetchMode::ASSOCIATIVE)) { $data[] = $row; } $stmt = $this->_conn->executeQuery("SELECT * FROM caching ORDER BY test_int ASC", array(), array(), new QueryCacheProfile(10, "testcachekey")); $data = array(); - while ($row = $stmt->fetch(\PDO::FETCH_NUM)) { + + while ($row = $stmt->fetch(FetchMode::NUMERIC)) { $data[] = $row; } @@ -134,12 +143,12 @@ public function testDontFinishNoCache() { $stmt = $this->_conn->executeQuery("SELECT * FROM caching ORDER BY test_int ASC", array(), array(), new QueryCacheProfile(10, "testcachekey")); - $row = $stmt->fetch(\PDO::FETCH_ASSOC); + $stmt->fetch(FetchMode::ASSOCIATIVE); $stmt->closeCursor(); $stmt = $this->_conn->executeQuery("SELECT * FROM caching ORDER BY test_int ASC", array(), array(), new QueryCacheProfile(10, "testcachekey")); - $data = $this->hydrateStmt($stmt, \PDO::FETCH_NUM); + $this->hydrateStmt($stmt, FetchMode::NUMERIC); self::assertCount(2, $this->sqlLogger->queries); } @@ -184,7 +193,7 @@ public function testChangeCacheImpl() self::assertCount(1, $secondCache->fetch("emptycachekey")); } - private function hydrateStmt($stmt, $fetchMode = \PDO::FETCH_ASSOC) + private function hydrateStmt($stmt, $fetchMode = FetchMode::ASSOCIATIVE) { $data = array(); while ($row = $stmt->fetch($fetchMode)) { @@ -194,7 +203,7 @@ private function hydrateStmt($stmt, $fetchMode = \PDO::FETCH_ASSOC) return $data; } - private function hydrateStmtIterator($stmt, $fetchMode = \PDO::FETCH_ASSOC) + private function hydrateStmtIterator($stmt, $fetchMode = FetchMode::ASSOCIATIVE) { $data = array(); $stmt->setFetchMode($fetchMode); diff --git a/tests/Doctrine/Tests/DBAL/Functional/StatementTest.php b/tests/Doctrine/Tests/DBAL/Functional/StatementTest.php index be100f47a49..824ba65ad74 100644 --- a/tests/Doctrine/Tests/DBAL/Functional/StatementTest.php +++ b/tests/Doctrine/Tests/DBAL/Functional/StatementTest.php @@ -3,6 +3,8 @@ namespace Doctrine\Tests\DBAL\Functional; use Doctrine\DBAL\Driver\Statement; +use Doctrine\DBAL\FetchMode; +use Doctrine\DBAL\ParameterType; use Doctrine\DBAL\Schema\Table; use Doctrine\DBAL\Types\Type; @@ -57,7 +59,7 @@ public function testReuseStatementWithLongerResults() $stmt->execute(); self::assertArraySubset(array( array('param1', 'X'), - ), $stmt->fetchAll(\PDO::FETCH_NUM)); + ), $stmt->fetchAll(FetchMode::NUMERIC)); $row2 = array( 'param' => 'param2', @@ -69,7 +71,7 @@ public function testReuseStatementWithLongerResults() self::assertArraySubset(array( array('param1', 'X'), array('param2', 'A bit longer value'), - ), $stmt->fetchAll(\PDO::FETCH_NUM)); + ), $stmt->fetchAll(FetchMode::NUMERIC)); } public function testFetchLongBlob() @@ -102,9 +104,7 @@ public function testFetchLongBlob() EOF ); - $this->_conn->insert('stmt_long_blob', array( - 'contents' => $contents, - ), array(\PDO::PARAM_LOB)); + $this->_conn->insert('stmt_long_blob', ['contents' => $contents], [ParameterType::LARGE_OBJECT]); $stmt = $this->_conn->prepare('SELECT contents FROM stmt_long_blob'); $stmt->execute(); @@ -287,7 +287,7 @@ public function testFetchInColumnMode() : void { $platform = $this->_conn->getDatabasePlatform(); $query = $platform->getDummySelectSQL(); - $result = $this->_conn->executeQuery($query)->fetch(\PDO::FETCH_COLUMN); + $result = $this->_conn->executeQuery($query)->fetch(FetchMode::COLUMN); self::assertEquals(1, $result); } diff --git a/tests/Doctrine/Tests/DBAL/Functional/Ticket/DBAL630Test.php b/tests/Doctrine/Tests/DBAL/Functional/Ticket/DBAL630Test.php index 0d9eb3ea5d9..a0816b35303 100644 --- a/tests/Doctrine/Tests/DBAL/Functional/Ticket/DBAL630Test.php +++ b/tests/Doctrine/Tests/DBAL/Functional/Ticket/DBAL630Test.php @@ -3,6 +3,7 @@ namespace Doctrine\Tests\DBAL\Functional\Ticket; use Doctrine\DBAL\DBALException; +use Doctrine\DBAL\ParameterType; use PDO; /** @@ -52,7 +53,11 @@ public function testBooleanConversionSqlLiteral() public function testBooleanConversionBoolParamRealPrepares() { - $this->_conn->executeUpdate('INSERT INTO dbal630 (bool_col) VALUES(?)', array('false'), array(PDO::PARAM_BOOL)); + $this->_conn->executeUpdate( + 'INSERT INTO dbal630 (bool_col) VALUES(?)', + ['false'], + [ParameterType::BOOLEAN] + ); $id = $this->_conn->lastInsertId('dbal630_id_seq'); self::assertNotEmpty($id); @@ -68,7 +73,7 @@ public function testBooleanConversionBoolParamEmulatedPrepares() $platform = $this->_conn->getDatabasePlatform(); $stmt = $this->_conn->prepare('INSERT INTO dbal630 (bool_col) VALUES(?)'); - $stmt->bindValue(1, $platform->convertBooleansToDatabaseValue('false'), PDO::PARAM_BOOL); + $stmt->bindValue(1, $platform->convertBooleansToDatabaseValue('false'), ParameterType::BOOLEAN); $stmt->execute(); $id = $this->_conn->lastInsertId('dbal630_id_seq'); @@ -116,7 +121,11 @@ public function testBooleanConversionNullParamEmulatedPreparesWithBooleanTypeInB $platform = $this->_conn->getDatabasePlatform(); $stmt = $this->_conn->prepare('INSERT INTO dbal630_allow_nulls (bool_col) VALUES(?)'); - $stmt->bindValue(1, $platform->convertBooleansToDatabaseValue($statementValue), PDO::PARAM_BOOL); + $stmt->bindValue( + 1, + $platform->convertBooleansToDatabaseValue($statementValue), + ParameterType::BOOLEAN + ); $stmt->execute(); $id = $this->_conn->lastInsertId('dbal630_allow_nulls_id_seq'); diff --git a/tests/Doctrine/Tests/DBAL/Functional/WriteTest.php b/tests/Doctrine/Tests/DBAL/Functional/WriteTest.php index 7f072d26807..c7241592169 100644 --- a/tests/Doctrine/Tests/DBAL/Functional/WriteTest.php +++ b/tests/Doctrine/Tests/DBAL/Functional/WriteTest.php @@ -1,8 +1,9 @@ _conn->executeUpdate($sql, array("text", 1111), array(null, PDO::PARAM_INT)); + $this->_conn->executeUpdate($sql, ['text', 1111], [null, ParameterType::INTEGER]); $sql = "SELECT * FROM write_table WHERE test_string = ? AND test_int = ?"; - self::assertTrue((bool)$this->_conn->fetchColumn($sql, array("text", 1111))); + self::assertTrue((bool) $this->_conn->fetchColumn($sql, ['text', 1111])); } public function testExecuteUpdate() @@ -48,7 +49,11 @@ public function testExecuteUpdate() public function testExecuteUpdateWithTypes() { $sql = "INSERT INTO write_table (test_int, test_string) VALUES (?, ?)"; - $affected = $this->_conn->executeUpdate($sql, array(1, 'foo'), array(\PDO::PARAM_INT, \PDO::PARAM_STR)); + $affected = $this->_conn->executeUpdate( + $sql, + [1, 'foo'], + [ParameterType::INTEGER, ParameterType::STRING] + ); self::assertEquals(1, $affected, "executeUpdate() should return the number of affected rows!"); } @@ -70,8 +75,8 @@ public function testPrepareWithPdoTypes() $sql = "INSERT INTO write_table (test_int, test_string) VALUES (?, ?)"; $stmt = $this->_conn->prepare($sql); - $stmt->bindValue(1, 1, \PDO::PARAM_INT); - $stmt->bindValue(2, "foo", \PDO::PARAM_STR); + $stmt->bindValue(1, 1, ParameterType::INTEGER); + $stmt->bindValue(2, 'foo', ParameterType::STRING); $stmt->execute(); self::assertEquals(1, $stmt->rowCount()); diff --git a/tests/Doctrine/Tests/DBAL/Portability/StatementTest.php b/tests/Doctrine/Tests/DBAL/Portability/StatementTest.php index e369aab9286..2f35f0b64be 100644 --- a/tests/Doctrine/Tests/DBAL/Portability/StatementTest.php +++ b/tests/Doctrine/Tests/DBAL/Portability/StatementTest.php @@ -2,6 +2,8 @@ namespace Doctrine\Tests\DBAL\Portability; +use Doctrine\DBAL\FetchMode; +use Doctrine\DBAL\ParameterType; use Doctrine\DBAL\Portability\Connection; use Doctrine\DBAL\Portability\Statement; @@ -39,7 +41,7 @@ public function testBindParam() { $column = 'mycolumn'; $variable = 'myvalue'; - $type = \PDO::PARAM_STR; + $type = ParameterType::STRING; $length = 666; $this->wrappedStmt->expects($this->once()) @@ -54,7 +56,7 @@ public function testBindValue() { $param = 'myparam'; $value = 'myvalue'; - $type = \PDO::PARAM_STR; + $type = ParameterType::STRING; $this->wrappedStmt->expects($this->once()) ->method('bindValue') @@ -123,7 +125,7 @@ public function testExecute() public function testSetFetchMode() { - $fetchMode = \PDO::FETCH_CLASS; + $fetchMode = FetchMode::CUSTOM_OBJECT; $arg1 = 'MyClass'; $arg2 = array(1, 2); @@ -132,7 +134,7 @@ public function testSetFetchMode() ->with($fetchMode, $arg1, $arg2) ->will($this->returnValue(true)); - self::assertAttributeSame(\PDO::FETCH_BOTH, 'defaultFetchMode', $this->stmt); + self::assertAttributeSame(FetchMode::MIXED, 'defaultFetchMode', $this->stmt); self::assertTrue($this->stmt->setFetchMode($fetchMode, $arg1, $arg2)); self::assertAttributeSame($fetchMode, 'defaultFetchMode', $this->stmt); } diff --git a/tests/Doctrine/Tests/DBAL/Query/QueryBuilderTest.php b/tests/Doctrine/Tests/DBAL/Query/QueryBuilderTest.php index 35c1b7e4f39..26058b25942 100644 --- a/tests/Doctrine/Tests/DBAL/Query/QueryBuilderTest.php +++ b/tests/Doctrine/Tests/DBAL/Query/QueryBuilderTest.php @@ -2,6 +2,7 @@ namespace Doctrine\Tests\DBAL\Query; +use Doctrine\DBAL\ParameterType; use Doctrine\DBAL\Query\Expression\ExpressionBuilder; use Doctrine\DBAL\Query\QueryBuilder; @@ -600,12 +601,12 @@ public function testCreateNamedParameter() $qb = new QueryBuilder($this->conn); $qb->select('u.*')->from('users', 'u')->where( - $qb->expr()->eq('u.name', $qb->createNamedParameter(10, \PDO::PARAM_INT)) + $qb->expr()->eq('u.name', $qb->createNamedParameter(10, ParameterType::INTEGER)) ); self::assertEquals('SELECT u.* FROM users u WHERE u.name = :dcValue1', (string)$qb); self::assertEquals(10, $qb->getParameter('dcValue1')); - self::assertEquals(\PDO::PARAM_INT, $qb->getParameterType('dcValue1')); + self::assertEquals(ParameterType::INTEGER, $qb->getParameterType('dcValue1')); } public function testCreateNamedParameterCustomPlaceholder() @@ -613,12 +614,12 @@ public function testCreateNamedParameterCustomPlaceholder() $qb = new QueryBuilder($this->conn); $qb->select('u.*')->from('users', 'u')->where( - $qb->expr()->eq('u.name', $qb->createNamedParameter(10, \PDO::PARAM_INT, ':test')) + $qb->expr()->eq('u.name', $qb->createNamedParameter(10, ParameterType::INTEGER, ':test')) ); self::assertEquals('SELECT u.* FROM users u WHERE u.name = :test', (string)$qb); self::assertEquals(10, $qb->getParameter('test')); - self::assertEquals(\PDO::PARAM_INT, $qb->getParameterType('test')); + self::assertEquals(ParameterType::INTEGER, $qb->getParameterType('test')); } public function testCreatePositionalParameter() @@ -626,12 +627,12 @@ public function testCreatePositionalParameter() $qb = new QueryBuilder($this->conn); $qb->select('u.*')->from('users', 'u')->where( - $qb->expr()->eq('u.name', $qb->createPositionalParameter(10, \PDO::PARAM_INT)) + $qb->expr()->eq('u.name', $qb->createPositionalParameter(10, ParameterType::INTEGER)) ); self::assertEquals('SELECT u.* FROM users u WHERE u.name = ?', (string)$qb); self::assertEquals(10, $qb->getParameter(1)); - self::assertEquals(\PDO::PARAM_INT, $qb->getParameterType(1)); + self::assertEquals(ParameterType::INTEGER, $qb->getParameterType(1)); } /** @@ -847,9 +848,9 @@ public function testGetParameterType() self::assertNull($qb->getParameterType('name')); - $qb->setParameter('name', 'foo', \PDO::PARAM_STR); + $qb->setParameter('name', 'foo', ParameterType::STRING); - self::assertSame(\PDO::PARAM_STR, $qb->getParameterType('name')); + self::assertSame(ParameterType::STRING, $qb->getParameterType('name')); } /** @@ -868,12 +869,15 @@ public function testGetParameterTypes() self::assertSame(array(), $qb->getParameterTypes()); - $qb->setParameter('name', 'foo', \PDO::PARAM_STR); + $qb->setParameter('name', 'foo', ParameterType::STRING); $qb->where('is_active = :isActive'); - $qb->setParameter('isActive', true, \PDO::PARAM_BOOL); + $qb->setParameter('isActive', true, ParameterType::BOOLEAN); - self::assertSame(array('name' => \PDO::PARAM_STR, 'isActive' => \PDO::PARAM_BOOL), $qb->getParameterTypes()); + self::assertSame([ + 'name' => ParameterType::STRING, + 'isActive' => ParameterType::BOOLEAN, + ], $qb->getParameterTypes()); } /** diff --git a/tests/Doctrine/Tests/DBAL/SQLParserUtilsTest.php b/tests/Doctrine/Tests/DBAL/SQLParserUtilsTest.php index d284b107eb1..c357988bf6a 100644 --- a/tests/Doctrine/Tests/DBAL/SQLParserUtilsTest.php +++ b/tests/Doctrine/Tests/DBAL/SQLParserUtilsTest.php @@ -3,6 +3,7 @@ namespace Doctrine\Tests\DBAL; use Doctrine\DBAL\Connection; +use Doctrine\DBAL\ParameterType; use Doctrine\DBAL\SQLParserUtils; /** @@ -95,34 +96,40 @@ public function dataExpandListParameters() array(Connection::PARAM_INT_ARRAY), 'SELECT * FROM Foo WHERE foo IN (?, ?, ?)', array(1, 2, 3), - array(\PDO::PARAM_INT, \PDO::PARAM_INT, \PDO::PARAM_INT) + array(ParameterType::INTEGER, ParameterType::INTEGER, ParameterType::INTEGER) ), // Positional: One non-list before d one after list-needle array( "SELECT * FROM Foo WHERE foo = ? AND bar IN (?)", array("string", array(1, 2, 3)), - array(\PDO::PARAM_STR, Connection::PARAM_INT_ARRAY), + array(ParameterType::STRING, Connection::PARAM_INT_ARRAY), 'SELECT * FROM Foo WHERE foo = ? AND bar IN (?, ?, ?)', array("string", 1, 2, 3), - array(\PDO::PARAM_STR, \PDO::PARAM_INT, \PDO::PARAM_INT, \PDO::PARAM_INT) + array(ParameterType::STRING, ParameterType::INTEGER, ParameterType::INTEGER, ParameterType::INTEGER) ), // Positional: One non-list after list-needle array( "SELECT * FROM Foo WHERE bar IN (?) AND baz = ?", array(array(1, 2, 3), "foo"), - array(Connection::PARAM_INT_ARRAY, \PDO::PARAM_STR), + array(Connection::PARAM_INT_ARRAY, ParameterType::STRING), 'SELECT * FROM Foo WHERE bar IN (?, ?, ?) AND baz = ?', array(1, 2, 3, "foo"), - array(\PDO::PARAM_INT, \PDO::PARAM_INT, \PDO::PARAM_INT, \PDO::PARAM_STR) + array(ParameterType::INTEGER, ParameterType::INTEGER, ParameterType::INTEGER, ParameterType::STRING) ), // Positional: One non-list before and one after list-needle array( "SELECT * FROM Foo WHERE foo = ? AND bar IN (?) AND baz = ?", array(1, array(1, 2, 3), 4), - array(\PDO::PARAM_INT, Connection::PARAM_INT_ARRAY, \PDO::PARAM_INT), + array(ParameterType::INTEGER, Connection::PARAM_INT_ARRAY, ParameterType::INTEGER), 'SELECT * FROM Foo WHERE foo = ? AND bar IN (?, ?, ?) AND baz = ?', array(1, 1, 2, 3, 4), - array(\PDO::PARAM_INT, \PDO::PARAM_INT, \PDO::PARAM_INT, \PDO::PARAM_INT, \PDO::PARAM_INT) + array( + ParameterType::INTEGER, + ParameterType::INTEGER, + ParameterType::INTEGER, + ParameterType::INTEGER, + ParameterType::INTEGER, + ) ), // Positional: Two lists array( @@ -131,7 +138,13 @@ public function dataExpandListParameters() array(Connection::PARAM_INT_ARRAY, Connection::PARAM_INT_ARRAY), 'SELECT * FROM Foo WHERE foo IN (?, ?, ?, ?, ?)', array(1, 2, 3, 4, 5), - array(\PDO::PARAM_INT, \PDO::PARAM_INT, \PDO::PARAM_INT, \PDO::PARAM_INT, \PDO::PARAM_INT) + array( + ParameterType::INTEGER, + ParameterType::INTEGER, + ParameterType::INTEGER, + ParameterType::INTEGER, + ParameterType::INTEGER, + ) ), // Positional: Empty "integer" array DDC-1978 array( @@ -155,47 +168,67 @@ public function dataExpandListParameters() array( "SELECT * FROM Foo WHERE foo = ? AND bar = ? AND baz = ?", array(1 => 'bar', 2 => 'baz', 0 => 1), - array(2 => \PDO::PARAM_STR, 1 => \PDO::PARAM_STR), + array(2 => ParameterType::STRING, 1 => ParameterType::STRING), 'SELECT * FROM Foo WHERE foo = ? AND bar = ? AND baz = ?', array(1 => 'bar', 0 => 1, 2 => 'baz'), - array(1 => \PDO::PARAM_STR, 2 => \PDO::PARAM_STR) + array(1 => ParameterType::STRING, 2 => ParameterType::STRING), ), // Positional: explicit keys for array params and array types array( "SELECT * FROM Foo WHERE foo IN (?) AND bar IN (?) AND baz = ?", array(1 => array('bar1', 'bar2'), 2 => true, 0 => array(1, 2, 3)), - array(2 => \PDO::PARAM_BOOL, 1 => Connection::PARAM_STR_ARRAY, 0 => Connection::PARAM_INT_ARRAY), + array(2 => ParameterType::BOOLEAN, 1 => Connection::PARAM_STR_ARRAY, 0 => Connection::PARAM_INT_ARRAY), 'SELECT * FROM Foo WHERE foo IN (?, ?, ?) AND bar IN (?, ?) AND baz = ?', array(1, 2, 3, 'bar1', 'bar2', true), - array(\PDO::PARAM_INT, \PDO::PARAM_INT, \PDO::PARAM_INT, \PDO::PARAM_STR, \PDO::PARAM_STR, \PDO::PARAM_BOOL) + array( + ParameterType::INTEGER, + ParameterType::INTEGER, + ParameterType::INTEGER, + ParameterType::STRING, + ParameterType::STRING, + ParameterType::BOOLEAN, + ) ), // Positional starts from 1: One non-list before and one after list-needle array( "SELECT * FROM Foo WHERE foo = ? AND bar IN (?) AND baz = ? AND foo IN (?)", array(1 => 1, 2 => array(1, 2, 3), 3 => 4, 4 => array(5, 6)), - array(1 => \PDO::PARAM_INT, 2 => Connection::PARAM_INT_ARRAY, 3 => \PDO::PARAM_INT, 4 => Connection::PARAM_INT_ARRAY), + array( + 1 => ParameterType::INTEGER, + 2 => Connection::PARAM_INT_ARRAY, + 3 => ParameterType::INTEGER, + 4 => Connection::PARAM_INT_ARRAY, + ), 'SELECT * FROM Foo WHERE foo = ? AND bar IN (?, ?, ?) AND baz = ? AND foo IN (?, ?)', array(1, 1, 2, 3, 4, 5, 6), - array(\PDO::PARAM_INT, \PDO::PARAM_INT, \PDO::PARAM_INT, \PDO::PARAM_INT, \PDO::PARAM_INT, \PDO::PARAM_INT, \PDO::PARAM_INT) + array( + ParameterType::INTEGER, + ParameterType::INTEGER, + ParameterType::INTEGER, + ParameterType::INTEGER, + ParameterType::INTEGER, + ParameterType::INTEGER, + ParameterType::INTEGER, + ) ), // Named parameters : Very simple with param int array( "SELECT * FROM Foo WHERE foo = :foo", array('foo'=>1), - array('foo'=>\PDO::PARAM_INT), + array('foo' => ParameterType::INTEGER), 'SELECT * FROM Foo WHERE foo = ?', array(1), - array(\PDO::PARAM_INT) + array(ParameterType::INTEGER), ), // Named parameters : Very simple with param int and string array( "SELECT * FROM Foo WHERE foo = :foo AND bar = :bar", array('bar'=>'Some String','foo'=>1), - array('foo'=>\PDO::PARAM_INT,'bar'=>\PDO::PARAM_STR), + array('foo' => ParameterType::INTEGER, 'bar' => ParameterType::STRING), 'SELECT * FROM Foo WHERE foo = ? AND bar = ?', array(1,'Some String'), - array(\PDO::PARAM_INT, \PDO::PARAM_STR) + array(ParameterType::INTEGER, ParameterType::STRING) ), // Named parameters : Very simple with one needle array( @@ -204,34 +237,34 @@ public function dataExpandListParameters() array('foo'=>Connection::PARAM_INT_ARRAY), 'SELECT * FROM Foo WHERE foo IN (?, ?, ?)', array(1, 2, 3), - array(\PDO::PARAM_INT, \PDO::PARAM_INT, \PDO::PARAM_INT) + array(ParameterType::INTEGER, ParameterType::INTEGER, ParameterType::INTEGER), ), // Named parameters: One non-list before d one after list-needle array( "SELECT * FROM Foo WHERE foo = :foo AND bar IN (:bar)", array('foo'=>"string", 'bar'=>array(1, 2, 3)), - array('foo'=>\PDO::PARAM_STR, 'bar'=>Connection::PARAM_INT_ARRAY), + array('foo' => ParameterType::STRING, 'bar' => Connection::PARAM_INT_ARRAY), 'SELECT * FROM Foo WHERE foo = ? AND bar IN (?, ?, ?)', array("string", 1, 2, 3), - array(\PDO::PARAM_STR, \PDO::PARAM_INT, \PDO::PARAM_INT, \PDO::PARAM_INT) + array(ParameterType::STRING, ParameterType::INTEGER, ParameterType::INTEGER, ParameterType::INTEGER) ), // Named parameters: One non-list after list-needle array( "SELECT * FROM Foo WHERE bar IN (:bar) AND baz = :baz", array('bar'=>array(1, 2, 3), 'baz'=>"foo"), - array('bar'=>Connection::PARAM_INT_ARRAY, 'baz'=>\PDO::PARAM_STR), + array('bar'=>Connection::PARAM_INT_ARRAY, 'baz'=>ParameterType::STRING), 'SELECT * FROM Foo WHERE bar IN (?, ?, ?) AND baz = ?', array(1, 2, 3, "foo"), - array(\PDO::PARAM_INT, \PDO::PARAM_INT, \PDO::PARAM_INT, \PDO::PARAM_STR) + array(ParameterType::INTEGER, ParameterType::INTEGER, ParameterType::INTEGER, ParameterType::STRING) ), // Named parameters: One non-list before and one after list-needle array( "SELECT * FROM Foo WHERE foo = :foo AND bar IN (:bar) AND baz = :baz", array('bar'=>array(1, 2, 3),'foo'=>1, 'baz'=>4), - array('bar'=>Connection::PARAM_INT_ARRAY, 'foo'=>\PDO::PARAM_INT, 'baz'=>\PDO::PARAM_INT), + array('bar'=>Connection::PARAM_INT_ARRAY, 'foo'=>ParameterType::INTEGER, 'baz'=>ParameterType::INTEGER), 'SELECT * FROM Foo WHERE foo = ? AND bar IN (?, ?, ?) AND baz = ?', array(1, 1, 2, 3, 4), - array(\PDO::PARAM_INT, \PDO::PARAM_INT, \PDO::PARAM_INT, \PDO::PARAM_INT, \PDO::PARAM_INT) + array(ParameterType::INTEGER, ParameterType::INTEGER, ParameterType::INTEGER, ParameterType::INTEGER, ParameterType::INTEGER) ), // Named parameters: Two lists array( @@ -240,16 +273,16 @@ public function dataExpandListParameters() array('a'=>Connection::PARAM_INT_ARRAY, 'b'=>Connection::PARAM_INT_ARRAY), 'SELECT * FROM Foo WHERE foo IN (?, ?, ?, ?, ?)', array(1, 2, 3, 4, 5), - array(\PDO::PARAM_INT, \PDO::PARAM_INT, \PDO::PARAM_INT, \PDO::PARAM_INT, \PDO::PARAM_INT) + array(ParameterType::INTEGER, ParameterType::INTEGER, ParameterType::INTEGER, ParameterType::INTEGER, ParameterType::INTEGER) ), // Named parameters : With the same name arg type string array( "SELECT * FROM Foo WHERE foo <> :arg AND bar = :arg", array('arg'=>"Some String"), - array('arg'=>\PDO::PARAM_STR), + array('arg'=>ParameterType::STRING), 'SELECT * FROM Foo WHERE foo <> ? AND bar = ?', array("Some String","Some String"), - array(\PDO::PARAM_STR,\PDO::PARAM_STR,) + array(ParameterType::STRING,ParameterType::STRING,) ), // Named parameters : With the same name arg array( @@ -258,17 +291,17 @@ public function dataExpandListParameters() array('arg'=>Connection::PARAM_INT_ARRAY), 'SELECT * FROM Foo WHERE foo IN (?, ?, ?) AND NOT bar IN (?, ?, ?)', array(1, 2, 3, 1, 2, 3), - array(\PDO::PARAM_INT,\PDO::PARAM_INT, \PDO::PARAM_INT,\PDO::PARAM_INT,\PDO::PARAM_INT, \PDO::PARAM_INT) + array(ParameterType::INTEGER,ParameterType::INTEGER, ParameterType::INTEGER,ParameterType::INTEGER,ParameterType::INTEGER, ParameterType::INTEGER) ), // Named parameters : Same name, other name in between DBAL-299 array( "SELECT * FROM Foo WHERE (:foo = 2) AND (:bar = 3) AND (:foo = 2)", array('foo'=>2,'bar'=>3), - array('foo'=>\PDO::PARAM_INT,'bar'=>\PDO::PARAM_INT), + array('foo'=>ParameterType::INTEGER,'bar'=>ParameterType::INTEGER), 'SELECT * FROM Foo WHERE (? = 2) AND (? = 3) AND (? = 2)', array(2, 3, 2), - array(\PDO::PARAM_INT, \PDO::PARAM_INT, \PDO::PARAM_INT) + array(ParameterType::INTEGER, ParameterType::INTEGER, ParameterType::INTEGER) ), // Named parameters : Empty "integer" array DDC-1978 array( @@ -294,7 +327,7 @@ public function dataExpandListParameters() array('foo' => Connection::PARAM_INT_ARRAY, 'baz' => 'string'), 'SELECT * FROM Foo WHERE foo IN (?, ?) OR bar = ? OR baz = ?', array(1, 2, 'bar', 'baz'), - array(\PDO::PARAM_INT, \PDO::PARAM_INT, \PDO::PARAM_STR, 'string') + array(ParameterType::INTEGER, ParameterType::INTEGER, ParameterType::STRING, 'string') ), array( "SELECT * FROM Foo WHERE foo IN (:foo) OR bar = :bar", @@ -302,24 +335,24 @@ public function dataExpandListParameters() array('foo' => Connection::PARAM_INT_ARRAY), 'SELECT * FROM Foo WHERE foo IN (?, ?) OR bar = ?', array(1, 2, 'bar'), - array(\PDO::PARAM_INT, \PDO::PARAM_INT, \PDO::PARAM_STR) + array(ParameterType::INTEGER, ParameterType::INTEGER, ParameterType::STRING) ), // Params/types with colons array( "SELECT * FROM Foo WHERE foo = :foo OR bar = :bar", array(':foo' => 'foo', ':bar' => 'bar'), - array(':foo' => \PDO::PARAM_INT), + array(':foo' => ParameterType::INTEGER), 'SELECT * FROM Foo WHERE foo = ? OR bar = ?', array('foo', 'bar'), - array(\PDO::PARAM_INT, \PDO::PARAM_STR) + array(ParameterType::INTEGER, ParameterType::STRING) ), array( "SELECT * FROM Foo WHERE foo = :foo OR bar = :bar", array(':foo' => 'foo', ':bar' => 'bar'), - array(':foo' => \PDO::PARAM_INT, 'bar' => \PDO::PARAM_INT), + array(':foo' => ParameterType::INTEGER, 'bar' => ParameterType::INTEGER), 'SELECT * FROM Foo WHERE foo = ? OR bar = ?', array('foo', 'bar'), - array(\PDO::PARAM_INT, \PDO::PARAM_INT) + array(ParameterType::INTEGER, ParameterType::INTEGER) ), array( "SELECT * FROM Foo WHERE foo IN (:foo) OR bar = :bar", @@ -327,7 +360,7 @@ public function dataExpandListParameters() array('foo' => Connection::PARAM_INT_ARRAY), 'SELECT * FROM Foo WHERE foo IN (?, ?) OR bar = ?', array(1, 2, 'bar'), - array(\PDO::PARAM_INT, \PDO::PARAM_INT, \PDO::PARAM_STR) + array(ParameterType::INTEGER, ParameterType::INTEGER, ParameterType::STRING) ), array( "SELECT * FROM Foo WHERE foo IN (:foo) OR bar = :bar", @@ -335,33 +368,33 @@ public function dataExpandListParameters() array(':foo' => Connection::PARAM_INT_ARRAY), 'SELECT * FROM Foo WHERE foo IN (?, ?) OR bar = ?', array(1, 2, 'bar'), - array(\PDO::PARAM_INT, \PDO::PARAM_INT, \PDO::PARAM_STR) + array(ParameterType::INTEGER, ParameterType::INTEGER, ParameterType::STRING) ), // DBAL-522 - null valued parameters are not considered array( 'INSERT INTO Foo (foo, bar) values (:foo, :bar)', array('foo' => 1, 'bar' => null), - array(':foo' => \PDO::PARAM_INT, ':bar' => \PDO::PARAM_NULL), + array(':foo' => ParameterType::INTEGER, ':bar' => ParameterType::NULL), 'INSERT INTO Foo (foo, bar) values (?, ?)', array(1, null), - array(\PDO::PARAM_INT, \PDO::PARAM_NULL) + array(ParameterType::INTEGER, ParameterType::NULL) ), array( 'INSERT INTO Foo (foo, bar) values (?, ?)', array(1, null), - array(\PDO::PARAM_INT, \PDO::PARAM_NULL), + array(ParameterType::INTEGER, ParameterType::NULL), 'INSERT INTO Foo (foo, bar) values (?, ?)', array(1, null), - array(\PDO::PARAM_INT, \PDO::PARAM_NULL) + array(ParameterType::INTEGER, ParameterType::NULL) ), // DBAL-1205 - Escaped single quotes SQL- and C-Style array( "SELECT * FROM Foo WHERE foo = :foo||''':not_a_param''\\'' OR bar = ''':not_a_param''\\'':bar", array(':foo' => 1, ':bar' => 2), - array(':foo' => \PDO::PARAM_INT, 'bar' => \PDO::PARAM_INT), + array(':foo' => ParameterType::INTEGER, 'bar' => ParameterType::INTEGER), 'SELECT * FROM Foo WHERE foo = ?||\'\'\':not_a_param\'\'\\\'\' OR bar = \'\'\':not_a_param\'\'\\\'\'?', array(1, 2), - array(\PDO::PARAM_INT, \PDO::PARAM_INT) + array(ParameterType::INTEGER, ParameterType::INTEGER) ), ); } diff --git a/tests/Doctrine/Tests/DBAL/Schema/Synchronizer/SingleDatabaseSynchronizerTest.php b/tests/Doctrine/Tests/DBAL/Schema/Synchronizer/SingleDatabaseSynchronizerTest.php index 06b30e08d28..0071d217280 100644 --- a/tests/Doctrine/Tests/DBAL/Schema/Synchronizer/SingleDatabaseSynchronizerTest.php +++ b/tests/Doctrine/Tests/DBAL/Schema/Synchronizer/SingleDatabaseSynchronizerTest.php @@ -23,6 +23,9 @@ use Doctrine\DBAL\Schema\Schema; use Doctrine\DBAL\Schema\Synchronizer\SingleDatabaseSynchronizer; +/** + * @requires extension pdo_sqlite + */ class SingleDatabaseSynchronizerTest extends \PHPUnit\Framework\TestCase { private $conn; diff --git a/tests/Doctrine/Tests/DBAL/Sharding/PoolingShardConnectionTest.php b/tests/Doctrine/Tests/DBAL/Sharding/PoolingShardConnectionTest.php index ae3e05b0e14..22cbcd6c066 100644 --- a/tests/Doctrine/Tests/DBAL/Sharding/PoolingShardConnectionTest.php +++ b/tests/Doctrine/Tests/DBAL/Sharding/PoolingShardConnectionTest.php @@ -22,6 +22,9 @@ use Doctrine\DBAL\DriverManager; use Doctrine\DBAL\Sharding\ShardChoser\MultiTenantShardChoser; +/** + * @requires extension pdo_sqlite + */ class PoolingShardConnectionTest extends \PHPUnit\Framework\TestCase { public function testConnect() diff --git a/tests/Doctrine/Tests/DBAL/StatementTest.php b/tests/Doctrine/Tests/DBAL/StatementTest.php index 3e05c5fb9c5..fa22536f2d4 100644 --- a/tests/Doctrine/Tests/DBAL/StatementTest.php +++ b/tests/Doctrine/Tests/DBAL/StatementTest.php @@ -2,6 +2,7 @@ namespace Doctrine\Tests\DBAL; +use Doctrine\DBAL\ParameterType; use Doctrine\DBAL\Statement; use Doctrine\DBAL\Logging\SQLLogger; @@ -62,12 +63,12 @@ protected function setUp() public function testExecuteCallsLoggerStartQueryWithParametersWhenValuesBound() { - $name = 'foo'; - $var = 'bar'; - $type = \PDO::PARAM_STR; - $values = array($name => $var); - $types = array($name => $type); - $sql = ''; + $name = 'foo'; + $var = 'bar'; + $type = ParameterType::STRING; + $values = [$name => $var]; + $types = [$name => $type]; + $sql = ''; $logger = $this->createMock('\Doctrine\DBAL\Logging\SQLLogger'); $logger->expects($this->once()) @@ -106,11 +107,11 @@ public function testExecuteCallsLoggerStartQueryWithParametersWhenParamsPassedTo public function testExecuteCallsStartQueryWithTheParametersBoundViaBindParam() { - $name = 'foo'; - $var = 'bar'; + $name = 'foo'; + $var = 'bar'; $values = [$name => $var]; - $types = [$name => \PDO::PARAM_STR]; - $sql = ''; + $types = [$name => ParameterType::STRING]; + $sql = ''; $logger = $this->createMock(SQLLogger::class); $logger->expects(self::once()) diff --git a/tests/Doctrine/Tests/DBAL/Types/BinaryTest.php b/tests/Doctrine/Tests/DBAL/Types/BinaryTest.php index a68bf692381..f260d57c9f5 100644 --- a/tests/Doctrine/Tests/DBAL/Types/BinaryTest.php +++ b/tests/Doctrine/Tests/DBAL/Types/BinaryTest.php @@ -2,6 +2,7 @@ namespace Doctrine\Tests\DBAL\Types; +use Doctrine\DBAL\ParameterType; use Doctrine\DBAL\Types\Type; use Doctrine\Tests\DBAL\Mocks\MockPlatform; @@ -28,7 +29,7 @@ protected function setUp() public function testReturnsBindingType() { - self::assertSame(\PDO::PARAM_LOB, $this->type->getBindingType()); + self::assertSame(ParameterType::LARGE_OBJECT, $this->type->getBindingType()); } public function testReturnsName() diff --git a/tests/Doctrine/Tests/DBAL/Types/DateImmutableTypeTest.php b/tests/Doctrine/Tests/DBAL/Types/DateImmutableTypeTest.php index 9d32448be51..5051853eb2a 100644 --- a/tests/Doctrine/Tests/DBAL/Types/DateImmutableTypeTest.php +++ b/tests/Doctrine/Tests/DBAL/Types/DateImmutableTypeTest.php @@ -2,6 +2,7 @@ namespace Doctrine\Tests\DBAL\Types; +use Doctrine\DBAL\ParameterType; use Doctrine\DBAL\Platforms\AbstractPlatform; use Doctrine\DBAL\Types\ConversionException; use Doctrine\DBAL\Types\DateImmutableType; @@ -37,7 +38,7 @@ public function testReturnsName() public function testReturnsBindingType() { - self::assertSame(\PDO::PARAM_STR, $this->type->getBindingType()); + self::assertSame(ParameterType::STRING, $this->type->getBindingType()); } public function testConvertsDateTimeImmutableInstanceToDatabaseValue() diff --git a/tests/Doctrine/Tests/DBAL/Types/DateTimeImmutableTypeTest.php b/tests/Doctrine/Tests/DBAL/Types/DateTimeImmutableTypeTest.php index a134180830c..b7c99825e0a 100644 --- a/tests/Doctrine/Tests/DBAL/Types/DateTimeImmutableTypeTest.php +++ b/tests/Doctrine/Tests/DBAL/Types/DateTimeImmutableTypeTest.php @@ -2,6 +2,7 @@ namespace Doctrine\Tests\DBAL\Types; +use Doctrine\DBAL\ParameterType; use Doctrine\DBAL\Platforms\AbstractPlatform; use Doctrine\DBAL\Types\ConversionException; use Doctrine\DBAL\Types\DateTimeImmutableType; @@ -37,7 +38,7 @@ public function testReturnsName() public function testReturnsBindingType() { - self::assertSame(\PDO::PARAM_STR, $this->type->getBindingType()); + self::assertSame(ParameterType::STRING, $this->type->getBindingType()); } public function testConvertsDateTimeImmutableInstanceToDatabaseValue() diff --git a/tests/Doctrine/Tests/DBAL/Types/DateTimeTzImmutableTypeTest.php b/tests/Doctrine/Tests/DBAL/Types/DateTimeTzImmutableTypeTest.php index 87d448ceab0..302aea26b7f 100644 --- a/tests/Doctrine/Tests/DBAL/Types/DateTimeTzImmutableTypeTest.php +++ b/tests/Doctrine/Tests/DBAL/Types/DateTimeTzImmutableTypeTest.php @@ -2,6 +2,7 @@ namespace Doctrine\Tests\DBAL\Types; +use Doctrine\DBAL\ParameterType; use Doctrine\DBAL\Platforms\AbstractPlatform; use Doctrine\DBAL\Types\ConversionException; use Doctrine\DBAL\Types\DateTimeTzImmutableType; @@ -37,7 +38,7 @@ public function testReturnsName() public function testReturnsBindingType() { - self::assertSame(\PDO::PARAM_STR, $this->type->getBindingType()); + self::assertSame(ParameterType::STRING, $this->type->getBindingType()); } public function testConvertsDateTimeImmutableInstanceToDatabaseValue() diff --git a/tests/Doctrine/Tests/DBAL/Types/JsonArrayTest.php b/tests/Doctrine/Tests/DBAL/Types/JsonArrayTest.php index bbfc0365e30..211baeef101 100644 --- a/tests/Doctrine/Tests/DBAL/Types/JsonArrayTest.php +++ b/tests/Doctrine/Tests/DBAL/Types/JsonArrayTest.php @@ -2,6 +2,7 @@ namespace Doctrine\Tests\DBAL\Types; +use Doctrine\DBAL\ParameterType; use Doctrine\DBAL\Types\Type; use Doctrine\Tests\DBAL\Mocks\MockPlatform; @@ -28,7 +29,7 @@ protected function setUp() public function testReturnsBindingType() { - self::assertSame(\PDO::PARAM_STR, $this->type->getBindingType()); + self::assertSame(ParameterType::STRING, $this->type->getBindingType()); } public function testReturnsName() diff --git a/tests/Doctrine/Tests/DBAL/Types/JsonTest.php b/tests/Doctrine/Tests/DBAL/Types/JsonTest.php index 851b2e972b6..a92bf5f6c41 100644 --- a/tests/Doctrine/Tests/DBAL/Types/JsonTest.php +++ b/tests/Doctrine/Tests/DBAL/Types/JsonTest.php @@ -2,6 +2,7 @@ namespace Doctrine\Tests\DBAL\Types; +use Doctrine\DBAL\ParameterType; use Doctrine\DBAL\Types\Type; use Doctrine\Tests\DBAL\Mocks\MockPlatform; @@ -28,7 +29,7 @@ protected function setUp() public function testReturnsBindingType() { - self::assertSame(\PDO::PARAM_STR, $this->type->getBindingType()); + self::assertSame(ParameterType::STRING, $this->type->getBindingType()); } public function testReturnsName() diff --git a/tests/Doctrine/Tests/DBAL/Types/TimeImmutableTypeTest.php b/tests/Doctrine/Tests/DBAL/Types/TimeImmutableTypeTest.php index 9bca6fa0c34..c1de23ac641 100644 --- a/tests/Doctrine/Tests/DBAL/Types/TimeImmutableTypeTest.php +++ b/tests/Doctrine/Tests/DBAL/Types/TimeImmutableTypeTest.php @@ -2,6 +2,7 @@ namespace Doctrine\Tests\DBAL\Types; +use Doctrine\DBAL\ParameterType; use Doctrine\DBAL\Platforms\AbstractPlatform; use Doctrine\DBAL\Types\ConversionException; use Doctrine\DBAL\Types\TimeImmutableType; @@ -37,7 +38,7 @@ public function testReturnsName() public function testReturnsBindingType() { - self::assertSame(\PDO::PARAM_STR, $this->type->getBindingType()); + self::assertSame(ParameterType::STRING, $this->type->getBindingType()); } public function testConvertsDateTimeImmutableInstanceToDatabaseValue() diff --git a/tests/Doctrine/Tests/DBAL/Types/VarDateTimeImmutableTypeTest.php b/tests/Doctrine/Tests/DBAL/Types/VarDateTimeImmutableTypeTest.php index 1ce9adfc029..ebf21284651 100644 --- a/tests/Doctrine/Tests/DBAL/Types/VarDateTimeImmutableTypeTest.php +++ b/tests/Doctrine/Tests/DBAL/Types/VarDateTimeImmutableTypeTest.php @@ -2,6 +2,7 @@ namespace Doctrine\Tests\DBAL\Types; +use Doctrine\DBAL\ParameterType; use Doctrine\DBAL\Platforms\AbstractPlatform; use Doctrine\DBAL\Types\ConversionException; use Doctrine\DBAL\Types\VarDateTimeImmutableType; @@ -36,7 +37,7 @@ public function testReturnsName() public function testReturnsBindingType() { - self::assertSame(\PDO::PARAM_STR, $this->type->getBindingType()); + self::assertSame(ParameterType::STRING, $this->type->getBindingType()); } public function testConvertsDateTimeImmutableInstanceToDatabaseValue() diff --git a/tests/Doctrine/Tests/Mocks/DriverConnectionMock.php b/tests/Doctrine/Tests/Mocks/DriverConnectionMock.php index 03d44caae48..760dbff40cf 100644 --- a/tests/Doctrine/Tests/Mocks/DriverConnectionMock.php +++ b/tests/Doctrine/Tests/Mocks/DriverConnectionMock.php @@ -2,11 +2,17 @@ namespace Doctrine\Tests\Mocks; +use Doctrine\DBAL\ParameterType; + class DriverConnectionMock implements \Doctrine\DBAL\Driver\Connection { public function prepare($prepareString) {} public function query() {} - public function quote($input, $type=\PDO::PARAM_STR) {} + + public function quote($input, $type = ParameterType::STRING) + { + } + public function exec($statement) {} public function lastInsertId($name = null) {} public function beginTransaction() {} @@ -14,4 +20,4 @@ public function commit() {} public function rollBack() {} public function errorCode() {} public function errorInfo() {} -} \ No newline at end of file +} diff --git a/tests/Doctrine/Tests/Mocks/HydratorMockStatement.php b/tests/Doctrine/Tests/Mocks/HydratorMockStatement.php deleted file mode 100644 index 00967829bd9..00000000000 --- a/tests/Doctrine/Tests/Mocks/HydratorMockStatement.php +++ /dev/null @@ -1,101 +0,0 @@ - - */ -class HydratorMockStatement implements \Doctrine\DBAL\Driver\Statement -{ - private $_resultSet; - - /** - * Creates a new mock statement that will serve the provided fake result set to clients. - * - * @param array $resultSet The faked SQL result set. - */ - public function __construct(array $resultSet) - { - $this->_resultSet = $resultSet; - } - - /** - * Fetches all rows from the result set. - * - * @return array - */ - public function fetchAll($fetchMode = null, $columnIndex = null, array $ctorArgs = null) - { - return $this->_resultSet; - } - - public function fetchColumn($columnNumber = 0) - { - $row = current($this->_resultSet); - if ( ! is_array($row)) return false; - $val = array_shift($row); - return $val !== null ? $val : false; - } - - /** - * Fetches the next row in the result set. - * - */ - public function fetch($fetchMode = null) - { - $current = current($this->_resultSet); - next($this->_resultSet); - return $current; - } - - /** - * Closes the cursor, enabling the statement to be executed again. - * - * @return bool - */ - public function closeCursor() - { - return true; - } - - public function setResultSet(array $resultSet) - { - reset($resultSet); - $this->_resultSet = $resultSet; - } - - public function bindColumn($column, &$param, $type = null) - { - } - - public function bindValue($param, $value, $type = null) - { - } - - public function bindParam($column, &$variable, $type = null, $length = null, $driverOptions = array()) - { - } - - public function columnCount() - { - } - - public function errorCode() - { - } - - public function errorInfo() - { - } - - public function execute($params = array()) - { - } - - public function rowCount() - { - } -} \ No newline at end of file diff --git a/tests/Doctrine/Tests/Mocks/PDOMock.php b/tests/Doctrine/Tests/Mocks/PDOMock.php deleted file mode 100644 index 0967c17b517..00000000000 --- a/tests/Doctrine/Tests/Mocks/PDOMock.php +++ /dev/null @@ -1,10 +0,0 @@ - 'pdo_sqlite', 'memory' => true