mirror of
https://github.com/silverstripe/silverstripe-framework
synced 2024-10-22 14:05:37 +02:00
FIX: Perform type coercion on PDO-based MySQL and SQLite connections
It turns out that this is needed for decimal values on MySQL and all values on SQLite
This commit is contained in:
parent
4f4153c834
commit
adb6e9eb8d
@ -427,7 +427,7 @@ class PDOConnector extends DBConnector implements TransactionManager
|
||||
} elseif ($statement) {
|
||||
// Count and return results
|
||||
$this->rowCount = $statement->rowCount();
|
||||
return new PDOQuery($statement, $this);
|
||||
return new PDOQuery($statement);
|
||||
}
|
||||
|
||||
// Ensure statement is closed
|
||||
|
@ -18,33 +18,42 @@ class PDOQuery extends Query
|
||||
|
||||
protected $results = null;
|
||||
|
||||
protected static $type_mapping = [
|
||||
// PGSQL
|
||||
'float8' => 'float',
|
||||
'float16' => 'float',
|
||||
'numeric' => 'float',
|
||||
|
||||
// MySQL
|
||||
'NEWDECIMAL' => 'float',
|
||||
|
||||
// SQlite
|
||||
'integer' => 'int',
|
||||
'double' => 'float',
|
||||
];
|
||||
|
||||
/**
|
||||
* Hook the result-set given into a Query class, suitable for use by SilverStripe.
|
||||
* @param PDOStatement $statement The internal PDOStatement containing the results
|
||||
*/
|
||||
public function __construct(PDOStatement $statement, PDOConnector $conn)
|
||||
public function __construct(PDOStatement $statement)
|
||||
{
|
||||
$this->statement = $statement;
|
||||
// Since no more than one PDOStatement for any one connection can be safely
|
||||
// traversed, each statement simply requests all rows at once for safety.
|
||||
// This could be re-engineered to call fetchAll on an as-needed basis
|
||||
|
||||
// Special case for Postgres
|
||||
if ($conn->getDriver() == 'pgsql') {
|
||||
$this->results = $this->fetchAllPgsql($statement);
|
||||
} else {
|
||||
$this->results = $statement->fetchAll(PDO::FETCH_ASSOC);
|
||||
}
|
||||
$this->results = $this->typeCorrectedFetchAll($statement);
|
||||
|
||||
$statement->closeCursor();
|
||||
}
|
||||
|
||||
/**
|
||||
* Fetch a record form the statement with its type data corrected
|
||||
* Necessary to fix float data retrieved from PGSQL
|
||||
* Returns data as an array of maps
|
||||
* @return array
|
||||
*/
|
||||
protected function fetchAllPgsql($statement)
|
||||
protected function typeCorrectedFetchAll($statement)
|
||||
{
|
||||
$columnCount = $statement->columnCount();
|
||||
$columnMeta = [];
|
||||
@ -57,10 +66,9 @@ class PDOQuery extends Query
|
||||
function ($rowArray) use ($columnMeta) {
|
||||
$row = [];
|
||||
foreach ($columnMeta as $i => $meta) {
|
||||
// Coerce floats from string to float
|
||||
// PDO PostgreSQL fails to do this
|
||||
if (isset($meta['native_type']) && strpos($meta['native_type'], 'float') === 0) {
|
||||
$rowArray[$i] = (float)$rowArray[$i];
|
||||
// Coerce any column types that aren't correctly retrieved from the database
|
||||
if (isset($meta['native_type']) && isset(self::$type_mapping[$meta['native_type']])) {
|
||||
settype($rowArray[$i], self::$type_mapping[$meta['native_type']]);
|
||||
}
|
||||
$row[$meta['name']] = $rowArray[$i];
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user