Subversion Repositories oidplus

Rev

Rev 1050 | 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 OIDplusQueryResultODBC extends OIDplusQueryResult {
  27.         protected $no_resultset;
  28.         protected $res;
  29.  
  30.         public function __construct($res) {
  31.                 $this->no_resultset = is_bool($res);
  32.  
  33.                 if (!$this->no_resultset) {
  34.                         $this->res = $res;
  35.                 }
  36.         }
  37.  
  38.         public function __destruct() {
  39.                 // odbc_close_cursor($this->res);
  40.         }
  41.  
  42.         public function containsResultSet(): bool {
  43.                 return !$this->no_resultset;
  44.         }
  45.  
  46.         private function num_rows_workaround(): int {
  47.                 $dummy = 0;
  48.  
  49.                 // go to the end of the result set
  50.                 $till_eof = 0;
  51.                 while ($temp = odbc_fetch_into($this->res, $dummy)) $till_eof++;
  52.  
  53.                 // reset cursor
  54.                 @odbc_fetch_row($this->res, 0);
  55.  
  56.                 // count the rows in the result set
  57.                 $ret = 0;
  58.                 while ($temp = odbc_fetch_into($this->res, $dummy)) $ret++;
  59.  
  60.                 // this would indicate that the odbc_fetch_row() could not reset the cursor
  61.                 if ($ret < $till_eof) return -1;
  62.  
  63.                 // go back to the row were started with
  64.                 @odbc_fetch_row($this->res, $ret-$till_eof);
  65.  
  66.                 return $ret;
  67.         }
  68.  
  69.         public function num_rows(): int {
  70.                 if ($this->no_resultset) throw new OIDplusException(_L('The query has returned no result set (i.e. it was not a SELECT query)'));
  71.                 $ret = odbc_num_rows($this->res);
  72.  
  73.                 // Workaround for drivers that do not support odbc_num_rows (e.g. Microsoft Access)
  74.                 if ($ret === -1) $ret = $this->num_rows_workaround();
  75.  
  76.                 if ($ret === -1) throw new OIDplusException(_L('The database driver has problems with "%1"','num_rows'));
  77.  
  78.                 return $ret;
  79.         }
  80.  
  81.         public function fetch_array()/*: ?array*/ {
  82.                 if ($this->no_resultset) throw new OIDplusException(_L('The query has returned no result set (i.e. it was not a SELECT query)'));
  83.                 $ret = odbc_fetch_array($this->res);
  84.                 if ($ret === false) $ret = null;
  85.                 if (!is_null($ret)) {
  86.                         // ODBC gives bit(1) as binary, MySQL as integer and PDO as string.
  87.                         // We'll do it like MySQL does, even if ODBC is actually more correct.
  88.                         foreach ($ret as &$value) {
  89.                                 if ($value === chr(0)) $value = 0;
  90.                                 if ($value === chr(1)) $value = 1;
  91.                         }
  92.                 }
  93.  
  94.                 // Oracle returns $ret['VALUE'] because unquoted column-names are always upper-case
  95.                 // We can't quote every single column throughout the whole program, so we use this workaround...
  96.                 if ($ret) {
  97.                         $keys = array_keys($ret);
  98.                         foreach($keys as $key) {
  99.                                 $ret[strtolower($key)]=$ret[$key];
  100.                                 $ret[strtoupper($key)]=$ret[$key];
  101.                         }
  102.                 }
  103.  
  104.                 return $ret;
  105.         }
  106.  
  107.         public function fetch_object()/*: ?object*/ {
  108.                 if ($this->no_resultset) throw new OIDplusException(_L('The query has returned no result set (i.e. it was not a SELECT query)'));
  109.                 $ret = odbc_fetch_object($this->res);
  110.                 if ($ret === false) $ret = null;
  111.                 if (!is_null($ret)) {
  112.                         // ODBC gives bit(1) as binary, MySQL as integer and PDO as string.
  113.                         // We'll do it like MySQL does, even if ODBC is actually more correct.
  114.                         foreach ((array)$ret as &$value) {
  115.                                 if ($value === chr(0)) $value = 0;
  116.                                 if ($value === chr(1)) $value = 1;
  117.                         }
  118.                 }
  119.  
  120.                 // Oracle returns $ret['VALUE'] because unquoted column-names are always upper-case
  121.                 // We can't quote every single column throughout the whole program, so we use this workaround...
  122.                 if ($ret) {
  123.                         foreach ((array)$ret as $name => $val) {
  124.                                 $ret->{strtoupper($name)} = $val;
  125.                                 $ret->{strtolower($name)} = $val;
  126.                         }
  127.                 }
  128.  
  129.                 return $ret;
  130.         }
  131. }
  132.