Commit 19776904 authored by Alexander's avatar Alexander

[DBAL-138] Connection::quote now supports both DBAL and PDO types

parent d096f0aa
......@@ -555,8 +555,9 @@ class Connection implements DriverConnection
public function quote($input, $type = null)
{
$this->connect();
return $this->_conn->quote($input, $type);
list($value, $bindingType) = $this->getBindingInfo($input, $type);
return $this->_conn->quote($input, $bindingType);
}
/**
......@@ -1069,15 +1070,7 @@ class Connection implements DriverConnection
$typeIndex = $bindIndex + $typeOffset;
if (isset($types[$typeIndex])) {
$type = $types[$typeIndex];
if (is_string($type)) {
$type = Type::getType($type);
}
if ($type instanceof Type) {
$value = $type->convertToDatabaseValue($value, $this->_platform);
$bindingType = $type->getBindingType();
} else {
$bindingType = $type; // PDO::PARAM_* constants
}
list($value, $bindingType) = $this->getBindingInfo($value, $type);
$stmt->bindValue($bindIndex, $value, $bindingType);
} else {
$stmt->bindValue($bindIndex, $value);
......@@ -1089,15 +1082,7 @@ class Connection implements DriverConnection
foreach ($params as $name => $value) {
if (isset($types[$name])) {
$type = $types[$name];
if (is_string($type)) {
$type = Type::getType($type);
}
if ($type instanceof Type) {
$value = $type->convertToDatabaseValue($value, $this->_platform);
$bindingType = $type->getBindingType();
} else {
$bindingType = $type; // PDO::PARAM_* constants
}
list($value, $bindingType) = $this->getBindingInfo($value, $type);
$stmt->bindValue($name, $value, $bindingType);
} else {
$stmt->bindValue($name, $value);
......@@ -1105,7 +1090,28 @@ class Connection implements DriverConnection
}
}
}
/**
* Gets the binding type of a given type. The given type can be a PDO or DBAL mapping type.
*
* @param mixed $value The value to bind
* @param mixed $type The type to bind (PDO or DBAL)
* @return array [0] => the (escaped) value, [1] => the binding type
*/
private function getBindingInfo($value, $type)
{
if (is_string($type)) {
$type = Type::getType($type);
}
if ($type instanceof Type) {
$value = $type->convertToDatabaseValue($value, $this->_platform);
$bindingType = $type->getBindingType();
} else {
$bindingType = $type; // PDO::PARAM_* constants
}
return array($value, $bindingType);
}
/**
* Create a new instance of a SQL query builder.
*
......@@ -1115,4 +1121,4 @@ class Connection implements DriverConnection
{
return new Query\QueryBuilder($this);
}
}
\ No newline at end of file
}
......@@ -3,6 +3,7 @@
namespace Doctrine\Tests\DBAL\Functional;
use Doctrine\DBAL\ConnectionException;
use Doctrine\DBAL\Types\Type;
require_once __DIR__ . '/../../TestInit.php';
......@@ -207,4 +208,12 @@ class ConnectionTest extends \Doctrine\Tests\DbalFunctionalTestCase
$conn->executeQuery($conn->getDatabasePlatform()->getDummySelectSQL());
});
}
}
\ No newline at end of file
/**
* Tests that the quote function accepts DBAL and PDO types.
*/
public function testQuote()
{
$this->assertEquals($this->_conn->quote("foo", Type::STRING), $this->_conn->quote("foo", \PDO::PARAM_STR));
}
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment