Subversion Repositories oidplus

Rev

Rev 501 | 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 OIDplusDatabaseConnectionODBC extends OIDplusDatabaseConnection {
  21.         private $conn;
  22.         private $last_error = null; // do the same like MySQL+PDO, just to be equal in the behavior
  23.         private $transactions_supported = false;
  24.  
  25.         public static function getPlugin(): OIDplusDatabasePlugin {
  26.                 return new OIDplusDatabasePluginODBC();
  27.         }
  28.  
  29.         protected function forcePrepareEmulation() {
  30.                 $mode = OIDplus::baseConfig()->getValue('PREPARED_STATEMENTS_EMULATION', 'auto');
  31.                 if ($mode === 'on') return true;
  32.                 if ($mode === 'off') return false;
  33.  
  34.                 static $res = null;
  35.                 if (is_null($res)) {
  36.                         $res = @odbc_prepare($this->conn, "select * from config where name = ?") === false;
  37.                 }
  38.                 return $res;
  39.         }
  40.  
  41.         public function doQuery(string $sql, /*?array*/ $prepared_args=null): OIDplusQueryResult {
  42.                 $this->last_error = null;
  43.                 if (is_null($prepared_args)) {
  44.                         $res = @odbc_exec($this->conn, $sql);
  45.  
  46.                         if ($res === false) {
  47.                                 $this->last_error = odbc_errormsg($this->conn);
  48.                                 throw new OIDplusSQLException($sql, $this->error());
  49.                         } else {
  50.                                 return new OIDplusQueryResultODBC($res);
  51.                         }
  52.                 } else {
  53.                         if (!is_array($prepared_args)) {
  54.                                 throw new OIDplusException(_L('"prepared_args" must be either NULL or an ARRAY.'));
  55.                         }
  56.  
  57.                         if ($this->forcePrepareEmulation()) {
  58.                                 // For some drivers (e.g. Microsoft Access), we need to do this kind of emulation, because odbc_prepare() does not work
  59.                                 $sql = str_replace('?', chr(1), $sql);
  60.                                 foreach ($prepared_args as $arg) {
  61.                                         $needle = chr(1);
  62.                                         if (is_bool($arg)) {
  63.                                                 if ($this->slangDetectionDone) {
  64.                                                         $replace = $this->getSlang()->getSQLBool($arg);
  65.                                                 } else {
  66.                                                         $replace = $arg ? '1' : '0';
  67.                                                 }
  68.                                         } else {
  69.                                                 $replace = "'$arg'"; // TODO: types
  70.                                         }
  71.                                         $pos = strpos($sql, $needle);
  72.                                         if ($pos !== false) {
  73.                                                 $sql = substr_replace($sql, $replace, $pos, strlen($needle));
  74.                                         }
  75.                                 }
  76.                                 $sql = str_replace(chr(1), '?', $sql);
  77.                                 $ps = @odbc_exec($this->conn, $sql);
  78.                                 if (!$ps) {
  79.                                         $this->last_error = odbc_errormsg($this->conn);
  80.                                         throw new OIDplusSQLException($sql, _L('Cannot prepare statement').': '.$this->error());
  81.                                 }
  82.                                 return new OIDplusQueryResultODBC($ps);
  83.                         } else {
  84.                                 foreach ($prepared_args as &$value) {
  85.                                         // ODBC/SQLServer has problems converting "true" to the data type "bit"
  86.                                         // Error "Invalid character value for cast specification"
  87.                                         if (is_bool($value)) {
  88.                                                 if ($this->slangDetectionDone) {
  89.                                                         $value = $this->getSlang()->getSQLBool($value);
  90.                                                 } else {
  91.                                                         $value = $value ? '1' : '0';
  92.                                                 }
  93.                                         }
  94.                                 }
  95.  
  96.                                 $ps = @odbc_prepare($this->conn, $sql);
  97.                                 if (!$ps) {
  98.                                         $this->last_error = odbc_errormsg($this->conn);
  99.                                         throw new OIDplusSQLException($sql, _L('Cannot prepare statement').': '.$this->error());
  100.                                 }
  101.  
  102.                                 if (!@odbc_execute($ps, $prepared_args)) {
  103.                                         $this->last_error = odbc_errormsg($this->conn);
  104.                                         throw new OIDplusSQLException($sql, $this->error());
  105.                                 }
  106.                                 return new OIDplusQueryResultODBC($ps);
  107.                         }
  108.                 }
  109.         }
  110.  
  111.         public function error(): string {
  112.                 $err = $this->last_error;
  113.                 if ($err == null) $err = '';
  114.                 $err = utf8_encode($err); // because ODBC might output weird stuff ...
  115.                 return $err;
  116.         }
  117.  
  118.         protected function doConnect()/*: void*/ {
  119.                 if (!function_exists('odbc_connect')) throw new OIDplusConfigInitializationException(_L('PHP extension "%1" not installed','ODBC'));
  120.  
  121.                 // Try connecting to the database
  122.                 $dsn      = OIDplus::baseConfig()->getValue('ODBC_DSN',      'DRIVER={SQL Server};SERVER=localhost;DATABASE=oidplus;CHARSET=UTF8');
  123.                 $username = OIDplus::baseConfig()->getValue('ODBC_USERNAME', '');
  124.                 $password = OIDplus::baseConfig()->getValue('ODBC_PASSWORD', '');
  125.                 $this->conn = @odbc_connect($dsn, $username, $password);
  126.  
  127.                 if (!$this->conn) {
  128.                         $message = odbc_errormsg();
  129.                         throw new OIDplusConfigInitializationException(_L('Connection to the database failed!').' '.$message);
  130.                 }
  131.  
  132.                 $this->last_error = null;
  133.  
  134.                 try {
  135.                         @odbc_exec($this->conn, "SET NAMES 'utf8'"); // Does most likely NOT work with ODBC. Try adding ";CHARSET=UTF8" (or similar) to the DSN
  136.                 } catch (Exception $e) {
  137.                 }
  138.  
  139.                 // We check if the DBMS supports autocommit.
  140.                 // Attention: Check it after you have sent a query already, because Microsoft Access doesn't seem to allow
  141.                 // changing auto commit once a query was executed ("Attribute cannot be set now SQLState: S1011")
  142.                 // Note: For some weird reason we *DO* need to redirect the output to "$dummy", otherwise it won't work!
  143.                 $dummy = @odbc_exec($this->conn, "select name from config where 1=0");
  144.                 $this->transactions_supported = @odbc_autocommit($this->conn, false);
  145.                 @odbc_autocommit($this->conn, true);
  146.         }
  147.  
  148.         protected function doDisconnect()/*: void*/ {
  149.                 if (!is_null($this->conn)) {
  150.                         @odbc_close($this->conn);
  151.                         $this->conn = null;
  152.                 }
  153.         }
  154.  
  155.         private $intransaction = false;
  156.  
  157.         public function transaction_supported(): bool {
  158.                 return $this->transactions_supported;
  159.         }
  160.  
  161.         public function transaction_level(): int {
  162.                 if (!$this->transaction_supported()) {
  163.                         // TODO?
  164.                         return 0;
  165.                 }
  166.                 return $this->intransaction ? 1 : 0;
  167.         }
  168.  
  169.         public function transaction_begin()/*: void*/ {
  170.                 if (!$this->transaction_supported()) {
  171.                         // TODO?
  172.                         return;
  173.                 }
  174.                 if ($this->intransaction) throw new OIDplusException(_L('Nested transactions are not supported by this database plugin.'));
  175.                 odbc_autocommit($this->conn, false); // begin transaction
  176.                 $this->intransaction = true;
  177.         }
  178.  
  179.         public function transaction_commit()/*: void*/ {
  180.                 if (!$this->transaction_supported()) {
  181.                         // TODO?
  182.                         return;
  183.                 }
  184.                 odbc_commit($this->conn);
  185.                 odbc_autocommit($this->conn, true);
  186.                 $this->intransaction = false;
  187.         }
  188.  
  189.         public function transaction_rollback()/*: void*/ {
  190.                 if (!$this->transaction_supported()) {
  191.                         // TODO?
  192.                         return;
  193.                 }
  194.                 odbc_rollback($this->conn);
  195.                 odbc_autocommit($this->conn, true);
  196.                 $this->intransaction = false;
  197.         }
  198. }
  199.