2013-04-03 06:27:11 +02:00
|
|
|
<?php
|
|
|
|
|
2016-07-04 02:44:09 +02:00
|
|
|
namespace SilverStripe\PostgreSQL;
|
|
|
|
|
2016-09-09 05:47:01 +02:00
|
|
|
use SilverStripe\ORM\Connect\Query;
|
2016-07-04 02:44:09 +02:00
|
|
|
|
2013-04-03 06:27:11 +02:00
|
|
|
/**
|
|
|
|
* A result-set from a PostgreSQL database.
|
2016-07-04 02:44:09 +02:00
|
|
|
*
|
2013-04-03 06:27:11 +02:00
|
|
|
* @package sapphire
|
|
|
|
* @subpackage model
|
|
|
|
*/
|
2016-09-09 05:47:01 +02:00
|
|
|
class PostgreSQLQuery extends Query
|
2015-12-17 19:18:01 +01:00
|
|
|
{
|
|
|
|
/**
|
|
|
|
* The internal Postgres handle that points to the result set.
|
|
|
|
* @var resource
|
|
|
|
*/
|
|
|
|
private $handle;
|
2013-04-03 06:27:11 +02:00
|
|
|
|
2018-10-10 11:23:22 +02:00
|
|
|
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',
|
|
|
|
];
|
|
|
|
|
2015-12-17 19:18:01 +01:00
|
|
|
/**
|
|
|
|
* Hook the result-set given into a Query class, suitable for use by sapphire.
|
2016-07-04 02:44:09 +02:00
|
|
|
* @param resource $handle the internal Postgres handle that is points to the resultset.
|
2015-12-17 19:18:01 +01:00
|
|
|
*/
|
|
|
|
public function __construct($handle)
|
|
|
|
{
|
|
|
|
$this->handle = $handle;
|
2018-10-10 11:23:22 +02:00
|
|
|
|
|
|
|
$numColumns = pg_num_fields($handle);
|
2018-11-26 14:30:28 +01:00
|
|
|
for ($i = 0; $i < $numColumns; $i++) {
|
2018-10-10 11:23:22 +02:00
|
|
|
$this->columnNames[$i] = pg_field_name($handle, $i);
|
|
|
|
}
|
2015-12-17 19:18:01 +01:00
|
|
|
}
|
2013-04-03 06:27:11 +02:00
|
|
|
|
2015-12-17 19:18:01 +01:00
|
|
|
public function __destruct()
|
|
|
|
{
|
|
|
|
if (is_resource($this->handle)) {
|
|
|
|
pg_free_result($this->handle);
|
|
|
|
}
|
|
|
|
}
|
2013-04-03 06:27:11 +02:00
|
|
|
|
2018-03-16 17:43:07 +01:00
|
|
|
public function getIterator()
|
2015-12-17 19:18:01 +01:00
|
|
|
{
|
2020-03-29 16:17:34 +02:00
|
|
|
pg_result_seek($this->handle, 0);
|
2018-11-26 14:30:28 +01:00
|
|
|
while ($data = $this->nextRecord()) {
|
2018-03-16 17:43:07 +01:00
|
|
|
yield $data;
|
|
|
|
}
|
2015-12-17 19:18:01 +01:00
|
|
|
}
|
2013-04-03 06:27:11 +02:00
|
|
|
|
2015-12-17 19:18:01 +01:00
|
|
|
public function numRecords()
|
|
|
|
{
|
|
|
|
return pg_num_rows($this->handle);
|
|
|
|
}
|
2013-04-03 06:27:11 +02:00
|
|
|
|
2015-12-17 19:18:01 +01:00
|
|
|
public function nextRecord()
|
|
|
|
{
|
2018-10-10 11:23:22 +02:00
|
|
|
$row = pg_fetch_array($this->handle, null, PGSQL_NUM);
|
|
|
|
|
|
|
|
// Correct non-string types
|
|
|
|
if ($row) {
|
2019-06-28 04:41:59 +02:00
|
|
|
return $this->parseResult($row);
|
|
|
|
}
|
2018-10-10 11:23:22 +02:00
|
|
|
|
2019-06-28 04:41:59 +02:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @param array $row
|
|
|
|
* @return array
|
|
|
|
*/
|
|
|
|
protected function parseResult(array $row)
|
|
|
|
{
|
|
|
|
$record = [];
|
2018-10-10 11:23:22 +02:00
|
|
|
|
2019-06-28 04:41:59 +02:00
|
|
|
foreach ($row as $i => $v) {
|
|
|
|
$k = $this->columnNames[$i];
|
|
|
|
$record[$k] = $v;
|
|
|
|
$type = pg_field_type($this->handle, $i);
|
|
|
|
if (isset(self::$typeMapping[$type])) {
|
|
|
|
if ($type === 'bool' && $record[$k] === 't') {
|
|
|
|
$record[$k] = 1;
|
2019-01-23 10:19:26 +01:00
|
|
|
|
|
|
|
// Note that boolean 'f' will be converted to 0 by this
|
2019-06-28 04:41:59 +02:00
|
|
|
} else {
|
|
|
|
settype($record[$k], self::$typeMapping[$type]);
|
2018-10-10 11:23:22 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-06-28 04:41:59 +02:00
|
|
|
return $record;
|
2015-12-17 19:18:01 +01:00
|
|
|
}
|
2013-04-03 06:27:11 +02:00
|
|
|
}
|