mirror of
https://github.com/silverstripe/silverstripe-postgresql
synced 2024-10-22 17:05:45 +02:00
commit
0d9fcabc80
@ -18,6 +18,23 @@ class PostgreSQLQuery extends Query
|
|||||||
*/
|
*/
|
||||||
private $handle;
|
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.
|
* 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.
|
* @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)
|
public function __construct($handle)
|
||||||
{
|
{
|
||||||
$this->handle = $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()
|
public function __destruct()
|
||||||
@ -37,7 +59,7 @@ class PostgreSQLQuery extends Query
|
|||||||
public function seek($row)
|
public function seek($row)
|
||||||
{
|
{
|
||||||
pg_result_seek($this->handle, $row);
|
pg_result_seek($this->handle, $row);
|
||||||
return pg_fetch_assoc($this->handle);
|
return $this->nextRecord();
|
||||||
}
|
}
|
||||||
|
|
||||||
public function numRecords()
|
public function numRecords()
|
||||||
@ -47,6 +69,24 @@ class PostgreSQLQuery extends Query
|
|||||||
|
|
||||||
public function nextRecord()
|
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;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -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');
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
Loading…
Reference in New Issue
Block a user