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 OIDplusQueryResultPDO extends OIDplusQueryResult {
  21.         protected $no_resultset;
  22.         protected $res;
  23.  
  24.         public function __construct($res) {
  25.                 $this->no_resultset = is_bool($res);
  26.  
  27.                 if (!$this->no_resultset) {
  28.                         $this->res = $res;
  29.                 }
  30.         }
  31.  
  32.         public function __destruct() {
  33.                 if ($this->res) $this->res->closeCursor();
  34.         }
  35.  
  36.         public function containsResultSet(): bool {
  37.                 return !$this->no_resultset;
  38.         }
  39.  
  40.         public function num_rows(): int {
  41.                 if ($this->no_resultset) throw new OIDplusException(_L('The query has returned no result set (i.e. it was not a SELECT query)'));
  42.                 return $this->res->rowCount();
  43.         }
  44.  
  45.         public function fetch_array()/*: ?array*/ {
  46.                 if ($this->no_resultset) throw new OIDplusException(_L('The query has returned no result set (i.e. it was not a SELECT query)'));
  47.                 $ret = $this->res->fetch(PDO::FETCH_ASSOC);
  48.                 if ($ret === false) $ret = null;
  49.                 return $ret;
  50.         }
  51.  
  52.         public function fetch_object()/*: ?object*/ {
  53.                 if ($this->no_resultset) throw new OIDplusException(_L('The query has returned no result set (i.e. it was not a SELECT query)'));
  54.                 $ret = $this->res->fetch(PDO::FETCH_OBJ);
  55.                 if ($ret === false) $ret = null;
  56.                 return $ret;
  57.         }
  58. }