Subversion Repositories oidplus

Rev

Rev 1116 | Rev 1219 | Go to most recent revision | Details | Compare with Previous | Last modification | View Log | RSS feed

Rev Author Line No. Line
786 daniel-mar 1
<?php
2
 
3
/*
4
 * OIDplus 2.0
1086 daniel-mar 5
 * Copyright 2019 - 2023 Daniel Marschall, ViaThinkSoft
786 daniel-mar 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
 
1050 daniel-mar 20
namespace ViaThinkSoft\OIDplus;
786 daniel-mar 21
 
1086 daniel-mar 22
// phpcs:disable PSR1.Files.SideEffects
23
\defined('INSIDE_OIDPLUS') or die;
24
// phpcs:enable PSR1.Files.SideEffects
25
 
786 daniel-mar 26
class OIDplusDatabaseConnectionOci extends OIDplusDatabaseConnection {
1130 daniel-mar 27
        /**
28
         * @var mixed|null
29
         */
786 daniel-mar 30
        private $conn = null;
1130 daniel-mar 31
 
32
        /**
33
         * @var array|null
34
         */
786 daniel-mar 35
        private $last_error = null; // do the same like MySQL+PDO, just to be equal in the behavior
36
 
1116 daniel-mar 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 {
786 daniel-mar 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)) {
787 daniel-mar 49
                        $res = @oci_parse($this->conn, $sql);
786 daniel-mar 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;
854 daniel-mar 80
                        foreach ($prepared_args as $value) {
786 daniel-mar 81
                                $i++;
82
                                if ($i > $count) break;
854 daniel-mar 83
                                if (is_bool($value)) $value = $value ? 1 : 0;
786 daniel-mar 84
                                $paramname = ":param$i";
854 daniel-mar 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 !
786 daniel-mar 86
                                if (@oci_bind_by_name($res, $paramname, $$paramname) === false) {
789 daniel-mar 87
                                        $this->last_error = oci_error($res);
88
                                        throw new OIDplusSQLException($sql, _L('Cannot bind parameter %1 to value %2',$paramname,$$paramname).': '.$this->error());
786 daniel-mar 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
 
1116 daniel-mar 103
        /**
104
         * @return string
105
         */
786 daniel-mar 106
        public function error(): string {
107
                $err = $this->last_error;
1130 daniel-mar 108
                if (!$err) $err = '';
786 daniel-mar 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
 
1116 daniel-mar 125
        /**
126
         * @return void
127
         * @throws OIDplusConfigInitializationException
128
         * @throws OIDplusException
129
         */
786 daniel-mar 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();
862 daniel-mar 144
 
145
                        $tmp = oci_error();
146
                        if ($tmp !== false) {
147
                                $err .= $tmp['message'];
148
                        }
786 daniel-mar 149
                }
1050 daniel-mar 150
 
786 daniel-mar 151
                if (!$this->conn) {
862 daniel-mar 152
                        throw new OIDplusConfigInitializationException(trim(_L('Connection to the database failed!').' ' . strip_tags($err)));
786 daniel-mar 153
                }
154
 
155
                $this->last_error = null;
156
        }
157
 
1116 daniel-mar 158
        /**
159
         * @return void
160
         */
786 daniel-mar 161
        protected function doDisconnect()/*: void*/ {
162
                if (!is_null($this->conn)) {
163
                        oci_close($this->conn);
164
                        $this->conn = null;
165
                }
166
        }
167
 
1116 daniel-mar 168
        /**
169
         * @var bool
170
         */
786 daniel-mar 171
        private $intransaction = false;
172
 
1116 daniel-mar 173
        /**
174
         * @return bool
175
         */
786 daniel-mar 176
        public function transaction_supported(): bool {
177
                return true;
178
        }
179
 
1116 daniel-mar 180
        /**
181
         * @return int
182
         */
786 daniel-mar 183
        public function transaction_level(): int {
184
                return $this->intransaction ? 1 : 0;
185
        }
186
 
1116 daniel-mar 187
        /**
188
         * @return void
189
         * @throws OIDplusException
190
         */
786 daniel-mar 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
 
1116 daniel-mar 197
        /**
198
         * @return void
199
         */
786 daniel-mar 200
        public function transaction_commit()/*: void*/ {
201
                oci_commit($this->conn);
202
                $this->intransaction = false;
203
        }
204
 
1116 daniel-mar 205
        /**
206
         * @return void
207
         */
786 daniel-mar 208
        public function transaction_rollback()/*: void*/ {
209
                oci_rollback($this->conn);
210
                $this->intransaction = false;
211
        }
212
 
1116 daniel-mar 213
        /**
214
         * @param bool $mustExist
215
         * @return OIDplusSqlSlangPlugin|null
216
         * @throws OIDplusConfigInitializationException
217
         */
786 daniel-mar 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
}