New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
[DBAL-630] Incorrect PostgreSQL boolean handling #564
Closed
Closed
Changes from all commits
Commits
Show all changes
13 commits
Select commit
Hold shift + click to select a range
4736075
Working on PostgreSQL incorrect boolean handling when emulating prepa…
davividal 0b4a087
Complying with Doctrine PSR exceptions
davividal 63d3e88
'Unbreaking' public API
davividal a73268d
Applying requested fixes
davividal 17e9deb
Forgot to update references
davividal c3df91d
Renaming DbValue to DatabaseValue
davividal cf44e4b
Checking for strings when converting to booleans
davividal 038c524
Handling all Postgres boolean literals
davividal c8ffb84
DRYing up tests and abstraction layer
davividal a328758
Improving conversion using @deeky666 idea
davividal f68c998
Checking if the boolean string is valid
davividal 30589a5
Fixing indentation
davividal 956d5a7
Simplifying code
davividal File filter
Filter by extension
Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -42,6 +42,28 @@ class PostgreSqlPlatform extends AbstractPlatform | |
*/ | ||
private $useBooleanTrueFalseStrings = true; | ||
|
||
/** | ||
* @var array PostgreSQL booleans literals | ||
*/ | ||
private $booleanLiterals = array( | ||
'true' => array( | ||
't', | ||
'true', | ||
'y', | ||
'yes', | ||
'on', | ||
'1' | ||
), | ||
'false' => array( | ||
'f', | ||
'false', | ||
'n', | ||
'no', | ||
'off', | ||
'0' | ||
) | ||
); | ||
|
||
/** | ||
* PostgreSQL has different behavior with some drivers | ||
* with regard to how booleans have to be handled. | ||
|
@@ -703,6 +725,69 @@ protected function _getCreateTableSQL($tableName, array $columns, array $options | |
return $sql; | ||
} | ||
|
||
/** | ||
* Converts a single boolean value. | ||
* | ||
* First converts the value to its native PHP boolean type | ||
* and passes it to the given callback function to be reconverted | ||
* into any custom representation. | ||
* | ||
* @param mixed $value The value to convert. | ||
* @param callable $callback The callback function to use for converting the real boolean value. | ||
* | ||
* @return mixed | ||
*/ | ||
private function convertSingleBooleanValue($value, $callback) | ||
{ | ||
if (null === $value) { | ||
return $callback(false); | ||
} | ||
|
||
if (is_bool($value) || is_numeric($value)) { | ||
return $callback($value ? true : false); | ||
} | ||
|
||
if ( ! is_string($value)) { | ||
return $callback(true); | ||
} | ||
|
||
/** | ||
* Better safe than sorry: http://php.net/in_array#106319 | ||
*/ | ||
if (in_array(trim(strtolower($value)), $this->booleanLiterals['false'], true)) { | ||
return $callback(false); | ||
} elseif (in_array(trim(strtolower($value)), $this->booleanLiterals['true'], true)) { | ||
return $callback(true); | ||
} else { | ||
throw new \UnexpectedValueException("Unrecognized boolean literal '${value}'"); | ||
} | ||
} | ||
|
||
/** | ||
* Converts one or multiple boolean values. | ||
* | ||
* First converts the value(s) to their native PHP boolean type | ||
* and passes them to the given callback function to be reconverted | ||
* into any custom representation. | ||
* | ||
* @param $item The value(s) to convert. | ||
* @param $callback The callback function to use for converting the real boolean value(s). | ||
* | ||
* @return mixed | ||
*/ | ||
private function doConvertBooleans($item, $callback) | ||
{ | ||
if (is_array($item)) { | ||
foreach ($item as $key => $value) { | ||
$item[$key] = $this->convertSingleBooleanValue($value, $callback); | ||
} | ||
|
||
return $item; | ||
} | ||
|
||
return $this->convertSingleBooleanValue($item, $callback); | ||
} | ||
|
||
/** | ||
* {@inheritDoc} | ||
* | ||
|
@@ -714,19 +799,29 @@ public function convertBooleans($item) | |
return parent::convertBooleans($item); | ||
} | ||
|
||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. If we're not using TrueFalseStrings, convertBooleans('t') won't convert it to neither 1 nor 0. |
||
if (is_array($item)) { | ||
foreach ($item as $key => $value) { | ||
if (is_bool($value) || is_numeric($item)) { | ||
$item[$key] = ($value) ? 'true' : 'false'; | ||
} | ||
return $this->doConvertBooleans( | ||
$item, | ||
function ($boolean) { | ||
return true === $boolean ? 'true' : 'false'; | ||
} | ||
} else { | ||
if (is_bool($item) || is_numeric($item)) { | ||
$item = ($item) ? 'true' : 'false'; | ||
} | ||
); | ||
} | ||
|
||
/** | ||
* {@inheritDoc} | ||
*/ | ||
public function convertBooleansToDatabaseValue($item) | ||
{ | ||
if ( ! $this->useBooleanTrueFalseStrings) { | ||
return parent::convertBooleansToDatabaseValue($item); | ||
} | ||
|
||
return $item; | ||
return $this->doConvertBooleans( | ||
$item, | ||
function ($boolean) { | ||
return (int) $boolean; | ||
} | ||
); | ||
} | ||
|
||
/** | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
To reduce nested complexity this can be rewritten as: