From 75f4a35f712144aaa23d796fbf2de7d3614bd486 Mon Sep 17 00:00:00 2001 From: Sam Minnee Date: Fri, 28 Jun 2019 14:41:59 +1200 Subject: [PATCH] =?UTF-8?q?FIX:=20Don=E2=80=99t=20drop=20first=20row=20on?= =?UTF-8?q?=20repeated=20iteration?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Fixes https://github.com/silverstripe/silverstripe-framework/issues/9097 Related: https://github.com/silverstripe/silverstripe-framework/issues/9098 Co-authored-by: Guy Marriott --- code/PostgreSQLQuery.php | 51 ++++++++++++++++++++++++---------------- 1 file changed, 31 insertions(+), 20 deletions(-) diff --git a/code/PostgreSQLQuery.php b/code/PostgreSQLQuery.php index 0b96c61..f010a11 100644 --- a/code/PostgreSQLQuery.php +++ b/code/PostgreSQLQuery.php @@ -58,8 +58,10 @@ class PostgreSQLQuery extends Query public function seek($row) { - pg_result_seek($this->handle, $row); - return $this->nextRecord(); + // Specifying the zero-th record here will reset the pointer + $result = pg_fetch_array($this->handle, $row, PGSQL_NUM); + + return $this->parseResult($result); } public function numRecords() @@ -73,26 +75,35 @@ class PostgreSQLQuery extends Query // 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])) { - if ($type === 'bool' && $record[$k] === 't') { - $record[$k] = 1; - - // Note that boolean 'f' will be converted to 0 by this - } else { - settype($record[$k], self::$typeMapping[$type]); - } - } - } - - return $record; + return $this->parseResult($row); } return false; } + + /** + * @param array $row + * @return array + */ + protected function parseResult(array $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])) { + if ($type === 'bool' && $record[$k] === 't') { + $record[$k] = 1; + + // Note that boolean 'f' will be converted to 0 by this + } else { + settype($record[$k], self::$typeMapping[$type]); + } + } + } + + return $record; + } }