Merge pull request #91 from sminnee/strict-types

Strict types
This commit is contained in:
Loz Calver 2018-11-05 10:14:23 +01:00 committed by GitHub
commit 0d9fcabc80
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 42 additions and 59 deletions

View File

@ -18,6 +18,23 @@ class PostgreSQLQuery extends Query
*/
private $handle;
private $columnNames = [];
/**
* Mapping of postgresql types to PHP types
* Note that the bool => int mapping is by design, designed to mimic MySQL's behaviour
* @var array
*/
protected static $typeMapping = [
'bool' => 'int',
'int2' => 'int',
'int4' => 'int',
'int8' => 'int',
'float4' => 'float',
'float8' => 'float',
'numeric' => 'float',
];
/**
* Hook the result-set given into a Query class, suitable for use by sapphire.
* @param resource $handle the internal Postgres handle that is points to the resultset.
@ -25,6 +42,11 @@ class PostgreSQLQuery extends Query
public function __construct($handle)
{
$this->handle = $handle;
$numColumns = pg_num_fields($handle);
for ($i = 0; $i<$numColumns; $i++) {
$this->columnNames[$i] = pg_field_name($handle, $i);
}
}
public function __destruct()
@ -37,7 +59,7 @@ class PostgreSQLQuery extends Query
public function seek($row)
{
pg_result_seek($this->handle, $row);
return pg_fetch_assoc($this->handle);
return $this->nextRecord();
}
public function numRecords()
@ -47,6 +69,24 @@ class PostgreSQLQuery extends Query
public function nextRecord()
{
return pg_fetch_assoc($this->handle);
$row = pg_fetch_array($this->handle, null, PGSQL_NUM);
// Correct non-string types
if ($row) {
$record = [];
foreach ($row as $i => $v) {
$k = $this->columnNames[$i];
$record[$k] = $v;
$type = pg_field_type($this->handle, $i);
if (isset(self::$typeMapping[$type])) {
settype($record[$k], self::$typeMapping[$type]);
}
}
return $record;
}
return false;
}
}

View File

@ -1,57 +0,0 @@
<?php
namespace SilverStripe\PostgreSQL\Tests;
use Exception;
use Page;
use SilverStripe\Dev\SapphireTest;
use SilverStripe\ORM\DataObject;
use SilverStripe\ORM\DB;
use SilverStripe\PostgreSQL\PostgreSQLDatabase;
/**
* @package postgresql
* @subpackage tests
*/
class PostgreSQLDatabaseTest extends SapphireTest
{
protected $usesDatabase = true;
public function testReadOnlyTransaction()
{
if (DB::get_conn()->supportsTransactions() == true
&& DB::get_conn() instanceof PostgreSQLDatabase
) {
$page = new Page();
$page->Title = 'Read only success';
$page->write();
DB::get_conn()->transactionStart('READ ONLY');
try {
$page = new Page();
$page->Title = 'Read only page failed';
$page->write();
} catch (Exception $e) {
//could not write this record
//We need to do a rollback or a commit otherwise we'll get error messages
DB::get_conn()->transactionRollback();
}
DB::get_conn()->transactionEnd();
DataObject::flush_and_destroy_cache();
$success = DataObject::get('Page', "\"Title\"='Read only success'");
$fail = DataObject::get('Page', "\"Title\"='Read only page failed'");
//This page should be in the system
$this->assertTrue(is_object($success) && $success->exists());
//This page should NOT exist, we had 'read only' permissions
$this->assertFalse(is_object($fail) && $fail->exists());
} else {
$this->markTestSkipped('Current database is not PostgreSQL');
}
}
}