Subversion Repositories oidplus

Rev

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