Subversion Repositories oidplus

Rev

Rev 1116 | Go to most recent revision | Blame | Compare with Previous | Last modification | View Log | RSS feed

  1. <?php
  2.  
  3. /*
  4.  * OIDplus 2.0
  5.  * Copyright 2019 - 2023 Daniel Marschall, ViaThinkSoft
  6.  *
  7.  * Licensed under the Apache License, Version 2.0 (the "License");
  8.  * you may not use this file except in compliance with the License.
  9.  * You may obtain a copy of the License at
  10.  *
  11.  *     http://www.apache.org/licenses/LICENSE-2.0
  12.  *
  13.  * Unless required by applicable law or agreed to in writing, software
  14.  * distributed under the License is distributed on an "AS IS" BASIS,
  15.  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  16.  * See the License for the specific language governing permissions and
  17.  * limitations under the License.
  18.  */
  19.  
  20. namespace ViaThinkSoft\OIDplus;
  21.  
  22. // phpcs:disable PSR1.Files.SideEffects
  23. \defined('INSIDE_OIDPLUS') or die;
  24. // phpcs:enable PSR1.Files.SideEffects
  25.  
  26. class OIDplusQueryResultSQLite3 extends OIDplusQueryResult {
  27.         /**
  28.          * @var bool
  29.          */
  30.         protected $no_resultset;
  31.  
  32.         /**
  33.          * @var mixed
  34.          */
  35.         protected $res;
  36.  
  37.         /**
  38.          * @var array
  39.          */
  40.         protected $all_results = array();
  41.  
  42.         /**
  43.          * @var int
  44.          */
  45.         protected $cursor = 0;
  46.  
  47.         /**
  48.          * @param mixed $res
  49.          */
  50.         public function __construct($res) {
  51.                 if (is_bool($res) || ($res->numColumns() == 0)) {
  52.                         // Why do qe need to check numColumns() ?
  53.                         // We need to do this because SQLite3::query() will always
  54.                         // return a result, even for Non-SELECT queries.
  55.                         // If you call fetchArray(), the query (e.g. INSERT)
  56.                         // will be executed again.
  57.                         $this->no_resultset = true;
  58.                         return;
  59.                 }
  60.  
  61.                 if (!$this->no_resultset) {
  62.                         $this->res = $res;
  63.                         while ($row = $this->res->fetchArray(SQLITE3_ASSOC)) {
  64.                                 // we need that because there is no numRows() function!
  65.                                 $this->all_results[] = $row;
  66.                         }
  67.                 }
  68.         }
  69.  
  70.         /**
  71.          *
  72.          */
  73.         public function __destruct() {
  74.                 $this->all_results = array();
  75.                 if (!is_null($this->res)) {
  76.                         $this->res->finalize();
  77.                         $this->res = null;
  78.                 }
  79.         }
  80.  
  81.         /**
  82.          * @return bool
  83.          */
  84.         public function containsResultSet(): bool {
  85.                 return !$this->no_resultset;
  86.         }
  87.  
  88.         /**
  89.          * @return int
  90.          * @throws OIDplusException
  91.          */
  92.         public function num_rows(): int {
  93.                 if ($this->no_resultset) throw new OIDplusException(_L('The query has returned no result set (i.e. it was not a SELECT query)'));
  94.                 return count($this->all_results);
  95.         }
  96.  
  97.         /**
  98.          * @return array|mixed|null
  99.          * @throws OIDplusException
  100.          */
  101.         public function fetch_array()/*: ?array*/ {
  102.                 if ($this->no_resultset) throw new OIDplusException(_L('The query has returned no result set (i.e. it was not a SELECT query)'));
  103.  
  104.                 //$ret = $this->res->fetchArray(SQLITE3_ASSOC);
  105.                 $cursor = $this->cursor;
  106.                 if (!isset($this->all_results[$cursor])) return null;
  107.                 $ret = $this->all_results[$cursor];
  108.                 $cursor++;
  109.                 $this->cursor = $cursor;
  110.  
  111.                 if ($ret === false) $ret = null;
  112.                 return $ret;
  113.         }
  114.  
  115.         /**
  116.          * @return \stdClass|null
  117.          * @throws OIDplusException
  118.          */
  119.         public function fetch_object()/*: ?object*/ {
  120.                 if ($this->no_resultset) throw new OIDplusException(_L('The query has returned no result set (i.e. it was not a SELECT query)'));
  121.  
  122.                 $ary = $this->fetch_array();
  123.                 if (!$ary) return null;
  124.  
  125.                 $obj = new \stdClass;
  126.                 foreach ($ary as $name => $val) {
  127.                         $obj->$name = $val;
  128.                 }
  129.                 return $obj;
  130.         }
  131. }
  132.