From 321b9fe89092bd7aa26a418829a5f2f06fca67ce Mon Sep 17 00:00:00 2001 From: Loz Calver Date: Fri, 16 Mar 2018 16:52:47 +0000 Subject: [PATCH] Update SQLServerQuery to use generators --- code/SQLServerQuery.php | 42 +++++++++++++---------------------------- 1 file changed, 13 insertions(+), 29 deletions(-) diff --git a/code/SQLServerQuery.php b/code/SQLServerQuery.php index 046b61e..d0cf3b5 100644 --- a/code/SQLServerQuery.php +++ b/code/SQLServerQuery.php @@ -43,13 +43,21 @@ class SQLServerQuery extends Query } } - public function seek($row) + public function getIterator() { - if (!is_resource($this->handle)) { - return false; - } + if (is_resource($this->handle)) { + while ($data = sqlsrv_fetch_array($this->handle, SQLSRV_FETCH_ASSOC)) { + // special case for sqlsrv - date values are DateTime coming out of the sqlsrv drivers, + // so we convert to the usual Y-m-d H:i:s value! + foreach ($data as $name => $value) { + if ($value instanceof DateTime) { + $data[$name] = $value->format('Y-m-d H:i:s'); + } + } - user_error('MSSQLQuery::seek() not supported in sqlsrv', E_USER_WARNING); + yield $data; + } + } } public function numRecords() @@ -65,28 +73,4 @@ class SQLServerQuery extends Query user_error('MSSQLQuery::numRecords() not supported in this version of sqlsrv', E_USER_WARNING); } } - - public function nextRecord() - { - if (!is_resource($this->handle)) { - return false; - } - - if ($data = sqlsrv_fetch_array($this->handle, SQLSRV_FETCH_ASSOC)) { - // special case for sqlsrv - date values are DateTime coming out of the sqlsrv drivers, - // so we convert to the usual Y-m-d H:i:s value! - foreach ($data as $name => $value) { - if ($value instanceof DateTime) { - $data[$name] = $value->format('Y-m-d H:i:s'); - } - } - return $data; - } else { - // Free the handle if there are no more results - sqlsrv crashes if there are too many handles - sqlsrv_free_stmt($this->handle); - $this->handle = null; - } - - return false; - } }