Subversion Repositories oidplus

Rev

Rev 1220 | 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.                         // convert ? ? ? to :param1 :param2 :param3
  62.                         $count = 0;
  63.                         $sql = preg_replace_callback('@\\?@', function($found) {
  64.                                 static $i = 0;
  65.                                 $i++;
  66.                                 return ":param$i";
  67.                         }, $sql, count($prepared_args), $count);
  68.  
  69.                         $res = @oci_parse($this->conn, $sql);
  70.                         if ($res === false) {
  71.                                 $this->last_error = oci_error($this->conn);
  72.                                 throw new OIDplusSQLException($sql, _L('Cannot prepare statement').': '.$this->error());
  73.                         }
  74.  
  75.                         $i = 0;
  76.                         foreach ($prepared_args as $value) {
  77.                                 $i++;
  78.                                 if ($i > $count) break;
  79.                                 if (is_bool($value)) $value = $value ? 1 : 0;
  80.                                 $paramname = ":param$i";
  81.                                 $$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 !
  82.                                 if (@oci_bind_by_name($res, $paramname, $$paramname) === false) {
  83.                                         $this->last_error = oci_error($res);
  84.                                         throw new OIDplusSQLException($sql, _L('Cannot bind parameter %1 to value %2',$paramname,$$paramname).': '.$this->error());
  85.                                 }
  86.                         }
  87.  
  88.                         if (!@oci_execute($res, $mode)) {
  89.                                 $this->last_error = oci_error($res);
  90.                                 throw new OIDplusSQLException($sql, $this->error());
  91.                         } else {
  92.                                 return new OIDplusQueryResultOci($res);
  93.                         }
  94.                         //oci_free_statement($res); // will be done in OIDplusQueryResultOci::__destruct()
  95.  
  96.                 }
  97.         }
  98.  
  99.         /**
  100.          * @return string
  101.          */
  102.         public function error(): string {
  103.                 $err = $this->last_error;
  104.                 if (!$err) $err = '';
  105.                 /*
  106.                 array(4) {
  107.                   ["code"]=>
  108.                   int(1)
  109.                   ["message"]=>
  110.                   string(55) "ORA-00001: unique constraint (HR.SYS_C0012493) violated"
  111.                   ["offset"]=>
  112.                   int(0)
  113.                   ["sqltext"]=>
  114.                   string(118) "insert into config (name, description, value, protected, visible) values (:param1, :param2, :param3, :param4, :param5)"
  115.                 }
  116.                 */
  117.                 if (isset($err['message'])) return $err['message'];
  118.                 return $err;
  119.         }
  120.  
  121.         /**
  122.          * @return void
  123.          * @throws OIDplusConfigInitializationException
  124.          * @throws OIDplusException
  125.          */
  126.         protected function doConnect()/*: void*/ {
  127.                 if (!function_exists('oci_connect')) throw new OIDplusConfigInitializationException(_L('PHP extension "%1" not installed','OCI8'));
  128.  
  129.                 // Try connecting to the database
  130.                 ob_start();
  131.                 $err = '';
  132.                 try {
  133.                         $conn_str = OIDplus::baseConfig()->getValue('OCI_CONN_STR', 'localhost/XE');
  134.                         $username = OIDplus::baseConfig()->getValue('OCI_USERNAME', 'hr');
  135.                         $password = OIDplus::baseConfig()->getValue('OCI_PASSWORD', 'oracle');
  136.                         $this->conn = oci_connect($username, $password, $conn_str, "AL32UTF8" /*, $session_mode*/);
  137.                 } finally {
  138.                         $err = ob_get_contents();
  139.                         ob_end_clean();
  140.  
  141.                         $tmp = oci_error();
  142.                         if ($tmp !== false) {
  143.                                 $err .= $tmp['message'];
  144.                         }
  145.                 }
  146.  
  147.                 if (!$this->conn) {
  148.                         throw new OIDplusConfigInitializationException(trim(_L('Connection to the database failed!').' ' . strip_tags($err)));
  149.                 }
  150.  
  151.                 $this->last_error = null;
  152.         }
  153.  
  154.         /**
  155.          * @return void
  156.          */
  157.         protected function doDisconnect()/*: void*/ {
  158.                 if (!is_null($this->conn)) {
  159.                         oci_close($this->conn);
  160.                         $this->conn = null;
  161.                 }
  162.         }
  163.  
  164.         /**
  165.          * @var bool
  166.          */
  167.         private $intransaction = false;
  168.  
  169.         /**
  170.          * @return bool
  171.          */
  172.         public function transaction_supported(): bool {
  173.                 return true;
  174.         }
  175.  
  176.         /**
  177.          * @return int
  178.          */
  179.         public function transaction_level(): int {
  180.                 return $this->intransaction ? 1 : 0;
  181.         }
  182.  
  183.         /**
  184.          * @return void
  185.          * @throws OIDplusException
  186.          */
  187.         public function transaction_begin()/*: void*/ {
  188.                 if ($this->intransaction) throw new OIDplusException(_L('Nested transactions are not supported by this database plugin.'));
  189.                 // Later, in oci_execute() we will include OCI_NO_AUTO_COMMIT
  190.                 $this->intransaction = true;
  191.         }
  192.  
  193.         /**
  194.          * @return void
  195.          */
  196.         public function transaction_commit()/*: void*/ {
  197.                 oci_commit($this->conn);
  198.                 $this->intransaction = false;
  199.         }
  200.  
  201.         /**
  202.          * @return void
  203.          */
  204.         public function transaction_rollback()/*: void*/ {
  205.                 oci_rollback($this->conn);
  206.                 $this->intransaction = false;
  207.         }
  208.  
  209.         /**
  210.          * @param bool $mustExist
  211.          * @return OIDplusSqlSlangPlugin|null
  212.          * @throws OIDplusConfigInitializationException
  213.          */
  214.         protected function doGetSlang(bool $mustExist=true)/*: ?OIDplusSqlSlangPlugin*/ {
  215.                 $slang = OIDplus::getSqlSlangPlugin('oracle');
  216.                 if (is_null($slang)) {
  217.                         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 GIT/SVN snapshot or OIDplus archive file.','oracle'));
  218.                 }
  219.                 return $slang;
  220.         }
  221.  
  222.         /**
  223.          * @return array
  224.          */
  225.         public function getExtendedInfo(): array {
  226.                 $conn_str = OIDplus::baseConfig()->getValue('OCI_CONN_STR', 'localhost/XE');
  227.                 return array(
  228.                         _L('Connection String') => $conn_str
  229.                 );
  230.         }
  231. }
  232.