Subversion Repositories oidplus

Rev

Rev 1116 | Rev 1219 | 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 OIDplusDatabaseConnectionOci extends OIDplusDatabaseConnection {
  27.         /**
  28.          * @var mixed|null
  29.          */
  30.         private $conn = null;
  31.  
  32.         /**
  33.          * @var array|null
  34.          */
  35.         private $last_error = null; // do the same like MySQL+PDO, just to be equal in the behavior
  36.  
  37.         /**
  38.          * @param string $sql
  39.          * @param array|null $prepared_args
  40.          * @return OIDplusQueryResultOci
  41.          * @throws OIDplusException
  42.          */
  43.         public function doQuery(string $sql, array $prepared_args=null): OIDplusQueryResult {
  44.                 $this->last_error = null;
  45.  
  46.                 $mode = $this->intransaction ? OCI_NO_AUTO_COMMIT : OCI_COMMIT_ON_SUCCESS;
  47.  
  48.                 if (is_null($prepared_args)) {
  49.                         $res = @oci_parse($this->conn, $sql);
  50.                         if ($res === false) {
  51.                                 $this->last_error = oci_error($this->conn);
  52.                                 throw new OIDplusSQLException($sql, _L('Cannot prepare statement').': '.$this->error());
  53.                         } else if (!@oci_execute($res, $mode)) {
  54.                                 $this->last_error = oci_error($res);
  55.                                 throw new OIDplusSQLException($sql, $this->error());
  56.                         } else {
  57.                                 return new OIDplusQueryResultOci($res);
  58.                         }
  59.                         //oci_free_statement($res); // will be done in OIDplusQueryResultOci::__destruct()
  60.                 } else {
  61.                         if (!is_array($prepared_args)) {
  62.                                 throw new OIDplusException(_L('"prepared_args" must be either NULL or an ARRAY.'));
  63.                         }
  64.  
  65.                         // convert ? ? ? to :param1 :param2 :param3
  66.                         $count = 0;
  67.                         $sql = preg_replace_callback('@\\?@', function($found) {
  68.                                 static $i = 0;
  69.                                 $i++;
  70.                                 return ":param$i";
  71.                         }, $sql, count($prepared_args), $count);
  72.  
  73.                         $res = @oci_parse($this->conn, $sql);
  74.                         if ($res === false) {
  75.                                 $this->last_error = oci_error($this->conn);
  76.                                 throw new OIDplusSQLException($sql, _L('Cannot prepare statement').': '.$this->error());
  77.                         }
  78.  
  79.                         $i = 0;
  80.                         foreach ($prepared_args as $value) {
  81.                                 $i++;
  82.                                 if ($i > $count) break;
  83.                                 if (is_bool($value)) $value = $value ? 1 : 0;
  84.                                 $paramname = ":param$i";
  85.                                 $$paramname = $value; // It is VERY important to clone the variable in this stupid way, because the binding will be done to the memory address of $value !
  86.                                 if (@oci_bind_by_name($res, $paramname, $$paramname) === false) {
  87.                                         $this->last_error = oci_error($res);
  88.                                         throw new OIDplusSQLException($sql, _L('Cannot bind parameter %1 to value %2',$paramname,$$paramname).': '.$this->error());
  89.                                 }
  90.                         }
  91.  
  92.                         if (!@oci_execute($res, $mode)) {
  93.                                 $this->last_error = oci_error($res);
  94.                                 throw new OIDplusSQLException($sql, $this->error());
  95.                         } else {
  96.                                 return new OIDplusQueryResultOci($res);
  97.                         }
  98.                         //oci_free_statement($res); // will be done in OIDplusQueryResultOci::__destruct()
  99.  
  100.                 }
  101.         }
  102.  
  103.         /**
  104.          * @return string
  105.          */
  106.         public function error(): string {
  107.                 $err = $this->last_error;
  108.                 if (!$err) $err = '';
  109.                 /*
  110.                 array(4) {
  111.                   ["code"]=>
  112.                   int(1)
  113.                   ["message"]=>
  114.                   string(55) "ORA-00001: unique constraint (HR.SYS_C0012493) violated"
  115.                   ["offset"]=>
  116.                   int(0)
  117.                   ["sqltext"]=>
  118.                   string(118) "insert into config (name, description, value, protected, visible) values (:param1, :param2, :param3, :param4, :param5)"
  119.                 }
  120.                 */
  121.                 if (isset($err['message'])) return $err['message'];
  122.                 return $err;
  123.         }
  124.  
  125.         /**
  126.          * @return void
  127.          * @throws OIDplusConfigInitializationException
  128.          * @throws OIDplusException
  129.          */
  130.         protected function doConnect()/*: void*/ {
  131.                 if (!function_exists('oci_connect')) throw new OIDplusConfigInitializationException(_L('PHP extension "%1" not installed','OCI8'));
  132.  
  133.                 // Try connecting to the database
  134.                 ob_start();
  135.                 $err = '';
  136.                 try {
  137.                         $conn_str = OIDplus::baseConfig()->getValue('OCI_CONN_STR', 'localhost/XE');
  138.                         $username = OIDplus::baseConfig()->getValue('OCI_USERNAME', 'hr');
  139.                         $password = OIDplus::baseConfig()->getValue('OCI_PASSWORD', 'oracle');
  140.                         $this->conn = oci_connect($username, $password, $conn_str, "AL32UTF8" /*, $session_mode*/);
  141.                 } finally {
  142.                         $err = ob_get_contents();
  143.                         ob_end_clean();
  144.  
  145.                         $tmp = oci_error();
  146.                         if ($tmp !== false) {
  147.                                 $err .= $tmp['message'];
  148.                         }
  149.                 }
  150.  
  151.                 if (!$this->conn) {
  152.                         throw new OIDplusConfigInitializationException(trim(_L('Connection to the database failed!').' ' . strip_tags($err)));
  153.                 }
  154.  
  155.                 $this->last_error = null;
  156.         }
  157.  
  158.         /**
  159.          * @return void
  160.          */
  161.         protected function doDisconnect()/*: void*/ {
  162.                 if (!is_null($this->conn)) {
  163.                         oci_close($this->conn);
  164.                         $this->conn = null;
  165.                 }
  166.         }
  167.  
  168.         /**
  169.          * @var bool
  170.          */
  171.         private $intransaction = false;
  172.  
  173.         /**
  174.          * @return bool
  175.          */
  176.         public function transaction_supported(): bool {
  177.                 return true;
  178.         }
  179.  
  180.         /**
  181.          * @return int
  182.          */
  183.         public function transaction_level(): int {
  184.                 return $this->intransaction ? 1 : 0;
  185.         }
  186.  
  187.         /**
  188.          * @return void
  189.          * @throws OIDplusException
  190.          */
  191.         public function transaction_begin()/*: void*/ {
  192.                 if ($this->intransaction) throw new OIDplusException(_L('Nested transactions are not supported by this database plugin.'));
  193.                 // Later, in oci_execute() we will include OCI_NO_AUTO_COMMIT
  194.                 $this->intransaction = true;
  195.         }
  196.  
  197.         /**
  198.          * @return void
  199.          */
  200.         public function transaction_commit()/*: void*/ {
  201.                 oci_commit($this->conn);
  202.                 $this->intransaction = false;
  203.         }
  204.  
  205.         /**
  206.          * @return void
  207.          */
  208.         public function transaction_rollback()/*: void*/ {
  209.                 oci_rollback($this->conn);
  210.                 $this->intransaction = false;
  211.         }
  212.  
  213.         /**
  214.          * @param bool $mustExist
  215.          * @return OIDplusSqlSlangPlugin|null
  216.          * @throws OIDplusConfigInitializationException
  217.          */
  218.         protected function doGetSlang(bool $mustExist=true)/*: ?OIDplusSqlSlangPlugin*/ {
  219.                 $slang = OIDplus::getSqlSlangPlugin('oracle');
  220.                 if (is_null($slang)) {
  221.                         throw new OIDplusConfigInitializationException(_L('SQL-Slang plugin "%1" is missing. Please check if it exists in the directory "plugin/sqlSlang". If it is not existing, please recover it from an SVN snapshot or OIDplus TAR.GZ file.','oracle'));
  222.                 }
  223.                 return $slang;
  224.         }
  225. }
  226.