Commit b35b6824 authored by Davi Koscianski Vidal's avatar Davi Koscianski Vidal Committed by lucasvanlierop

Working on PostgreSQL incorrect boolean handling when emulating prepared statements

parent 86e58794
......@@ -2245,7 +2245,7 @@ abstract class AbstractPlatform
} elseif ((string)$field['type'] == 'Date' && $field['default'] == $this->getCurrentDateSQL()) {
$default = " DEFAULT ".$this->getCurrentDateSQL();
} elseif ((string) $field['type'] == 'Boolean') {
$default = " DEFAULT '" . $this->convertBooleans($field['default']) . "'";
$default = " DEFAULT '" . $this->convertBoolToSqlLiteral($field['default']) . "'";
}
}
}
......@@ -2562,11 +2562,13 @@ abstract class AbstractPlatform
*
* The default conversion in this implementation converts to integers (false => 0, true => 1).
*
* @param mixed $item
* There are two contexts when converting booleans: Literals and Prepared Statements.
* This method should handle the literal case
*
* @param mixed $item
* @return mixed
*/
public function convertBooleans($item)
public function convertBoolToSqlLiteral($item)
{
if (is_array($item)) {
foreach ($item as $k => $value) {
......@@ -2595,6 +2597,18 @@ abstract class AbstractPlatform
return null === $item ? null: (bool) $item ;
}
/**
* This method should handle the prepared statements case. When there is no
* distinction, it's OK to use the same method.
*
* @param mixed $item
* @return mixed
*/
public function convertBoolToDbValue($item)
{
return self::convertBoolToSqlLiteral($item);
}
/**
* Returns the SQL specific for the platform to get the current date.
*
......
......@@ -690,10 +690,10 @@ class PostgreSqlPlatform extends AbstractPlatform
*
* Postgres wants boolean values converted to the strings 'true'/'false'.
*/
public function convertBooleans($item)
public function convertBoolToSqlLiteral($item)
{
if ( ! $this->useBooleanTrueFalseStrings) {
return parent::convertBooleans($item);
if (! $this->useBooleanTrueFalseStrings) {
return parent::convertBoolToSqlLiteral($item);
}
if (is_array($item)) {
......@@ -703,9 +703,41 @@ class PostgreSqlPlatform extends AbstractPlatform
}
}
} else {
if (is_bool($item) || is_numeric($item)) {
$item = ($item) ? 'true' : 'false';
}
if (is_bool($item) || is_numeric($item)) {
$item = ($item) ? 'true' : 'false';
}
}
return $item;
}
/**
* {@inheritDoc}
*/
public function convertBoolToDbValue($item)
{
if (! $this->useBooleanTrueFalseStrings) {
return parent::convertBoolToDbValue($item);
}
if (is_array($item)) {
foreach ($item as $key => $value) {
if (is_bool($value) || is_numeric($value)) {
$item[$key] = $value ? 1 : 0;
} elseif (is_string($value)) {
$item[$key] = (trim(strtolower($value)) === 'false');
}
}
} else {
if (is_bool($item) || is_numeric($item)) {
$item = $item ? 1 : 0;
} elseif (is_string($item)) {
if (trim(strtolower($item)) === 'false') {
$item = 0;
} else {
$item = 1;
}
}
}
return $item;
......
......@@ -41,7 +41,7 @@ class BooleanType extends Type
*/
public function convertToDatabaseValue($value, AbstractPlatform $platform)
{
return $platform->convertBooleans($value);
return $platform->convertBoolToDbValue($value);
}
/**
......
......@@ -33,6 +33,7 @@ class DBAL630Test extends \Doctrine\Tests\DbalFunctionalTestCase
{
if ($this->running) {
$this->_conn->getWrappedConnection()->setAttribute(PDO::ATTR_EMULATE_PREPARES, false);
$this->_conn->getWrappedConnection()->setAttribute(PDO::PGSQL_ATTR_DISABLE_NATIVE_PREPARED_STATEMENT, false);
}
}
......@@ -60,12 +61,15 @@ class DBAL630Test extends \Doctrine\Tests\DbalFunctionalTestCase
public function testBooleanConversionBoolParamEmulatedPrepares()
{
$this->markTestIncomplete('There is something missing here, on some machines it fails on some it passes.');
// $this->markTestIncomplete('There is something missing here, on some machines it fails on some it passes.');
$this->_conn->getWrappedConnection()->setAttribute(PDO::ATTR_EMULATE_PREPARES, true);
$this->_conn->getWrappedConnection()->setAttribute(PDO::PGSQL_ATTR_DISABLE_NATIVE_PREPARED_STATEMENT, true);
$platform = $this->_conn->getDatabasePlatform();
$stmt = $this->_conn->prepare('INSERT INTO dbal630 (bool_col) VALUES(?)');
$stmt->bindValue(1, 'false', PDO::PARAM_BOOL);
$stmt->bindValue(1, $platform->convertBoolToDbValue('false'), PDO::PARAM_BOOL);
$stmt->execute();
$id = $this->_conn->lastInsertId('dbal630_id_seq');
......@@ -74,6 +78,6 @@ class DBAL630Test extends \Doctrine\Tests\DbalFunctionalTestCase
$row = $this->_conn->fetchAssoc('SELECT bool_col FROM dbal630 WHERE id = ?', array($id));
$this->assertTrue($row['bool_col']);
$this->assertFalse($row['bool_col']);
}
}
......@@ -291,24 +291,47 @@ abstract class AbstractPostgreSqlPlatformTestCase extends AbstractPlatformTestCa
/**
* @group DBAL-457
*/
public function testConvertBooleanAsStrings()
public function testConvertBooleanAsLiteralStrings()
{
$platform = $this->createPlatform();
$this->assertEquals('true', $platform->convertBooleans(true));
$this->assertEquals('false', $platform->convertBooleans(false));
$this->assertEquals('true', $platform->convertBoolToSqlLiteral(true));
$this->assertEquals('false', $platform->convertBoolToSqlLiteral(false));
}
/**
* @group DBAL-457
*/
public function testConvertBooleanAsIntegers()
public function testConvertBooleanAsLiteralIntegers()
{
$platform = $this->createPlatform();
$platform->setUseBooleanTrueFalseStrings(false);
$this->assertEquals('1', $platform->convertBooleans(true));
$this->assertEquals('0', $platform->convertBooleans(false));
$this->assertEquals('1', $platform->convertBoolToSqlLiteral(true));
$this->assertEquals('0', $platform->convertBoolToSqlLiteral(false));
}
/**
* @group DBAL-630
*/
public function testConvertBooleanAsDbValueStrings()
{
$platform = $this->createPlatform();
$this->assertEquals(1, $platform->convertBoolToDbValue(true));
$this->assertEquals(0, $platform->convertBoolToDbValue(false));
}
/**
* @group DBAL-630
*/
public function testConvertBooleanAsDbValueIntegers()
{
$platform = $this->createPlatform();
$platform->setUseBooleanTrueFalseStrings(false);
$this->assertEquals(1, $platform->convertBoolToDbValue(true));
$this->assertEquals(0, $platform->convertBoolToDbValue(false));
}
public function testConvertFromBoolean()
......
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