Subversion Repositories oidplus

Rev

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

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