Subversion Repositories oidplus

Rev

Rev 360 | Go to most recent revision | Blame | Last modification | View Log | RSS feed

  1. <?php
  2.  
  3. /*
  4.  * OIDplus 2.0
  5.  * Copyright 2019 - 2021 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. if (!defined('INSIDE_OIDPLUS')) die();
  21.  
  22. class OIDplusQueryResultSQLite3 extends OIDplusQueryResult {
  23.         protected $no_resultset;
  24.         protected $res;
  25.         protected $all_results = array();
  26.         protected $cursor = 0;
  27.  
  28.         public function __construct($res) {
  29.                 if (is_bool($res) || ($res->numColumns() == 0)) {
  30.                         // Why do qe need to check numColumns() ?
  31.                         // We need to do this because SQLite3::query() will always
  32.                         // return a result, even for Non-SELECT queries.
  33.                         // If you call fetchArray(), the query (e.g. INSERT)
  34.                         // will be executed again.
  35.                         $this->no_resultset = true;
  36.                         return;
  37.                 }
  38.  
  39.                 if (!$this->no_resultset) {
  40.                         $this->res = $res;
  41.                         while ($row = $this->res->fetchArray(SQLITE3_ASSOC)) {
  42.                                 // we need that because there is no numRows() function!
  43.                                 $this->all_results[] = $row;
  44.                         }
  45.                 }
  46.         }
  47.  
  48.         public function __destruct() {
  49.                 $this->all_results = array();
  50.                 if (!is_null($this->res)) {
  51.                         $this->res->finalize();
  52.                         $this->res = null;
  53.                 }
  54.         }
  55.  
  56.         public function containsResultSet(): bool {
  57.                 return !$this->no_resultset;
  58.         }
  59.  
  60.         public function num_rows(): int {
  61.                 if ($this->no_resultset) throw new OIDplusException(_L('The query has returned no result set (i.e. it was not a SELECT query)'));
  62.                 return count($this->all_results);
  63.         }
  64.  
  65.         public function fetch_array()/*: ?array*/ {
  66.                 if ($this->no_resultset) throw new OIDplusException(_L('The query has returned no result set (i.e. it was not a SELECT query)'));
  67.  
  68.                 //$ret = $this->res->fetchArray(SQLITE3_ASSOC);
  69.                 $cursor = $this->cursor;
  70.                 if (!isset($this->all_results[$cursor])) return null;
  71.                 $ret = $this->all_results[$cursor];
  72.                 $cursor++;
  73.                 $this->cursor = $cursor;
  74.  
  75.                 if ($ret === false) $ret = null;
  76.                 return $ret;
  77.         }
  78.  
  79.         public function fetch_object()/*: ?object*/ {
  80.                 if ($this->no_resultset) throw new OIDplusException(_L('The query has returned no result set (i.e. it was not a SELECT query)'));
  81.  
  82.                 $ary = $this->fetch_array();
  83.                 if (!$ary) return null;
  84.  
  85.                 $obj = new stdClass;
  86.                 foreach ($ary as $name => $val) {
  87.                         $obj->$name = $val;
  88.                 }
  89.                 return $obj;
  90.         }
  91. }