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 OIDplusQueryResultODBC 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.                 // odbc_close_cursor($this->res);
  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 odbc_num_rows($this->res);
  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 = odbc_fetch_array($this->res);
  48.                 if ($ret === false) $ret = null;
  49.                 if (!is_null($ret)) {
  50.                         // ODBC gives bit(1) as binary, MySQL as integer and PDO as string.
  51.                         // We'll do it like MySQL does, even if ODBC is actually more correct.
  52.                         foreach ($ret as &$value) {
  53.                                 if ($value === chr(0)) $value = 0;
  54.                                 if ($value === chr(1)) $value = 1;
  55.                         }
  56.                 }
  57.                 return $ret;
  58.         }
  59.  
  60.         public function fetch_object()/*: ?object*/ {
  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.                 $ret = odbc_fetch_object($this->res);
  63.                 if ($ret === false) $ret = null;
  64.                 if (!is_null($ret)) {
  65.                         // ODBC gives bit(1) as binary, MySQL as integer and PDO as string.
  66.                         // We'll do it like MySQL does, even if ODBC is actually more correct.
  67.                         foreach ($ret as &$value) {
  68.                                 if ($value === chr(0)) $value = 0;
  69.                                 if ($value === chr(1)) $value = 1;
  70.                         }
  71.                 }
  72.                 return $ret;
  73.         }
  74. }