Subversion Repositories oidplus

Rev

Rev 1086 | Rev 1130 | Go to most recent revision | Show entire file | Regard whitespace | Details | Blame | Last modification | View Log | RSS feed

Rev 1086 Rev 1116
Line 26... Line 26...
26
class OIDplusDatabaseConnectionPDO extends OIDplusDatabaseConnection {
26
class OIDplusDatabaseConnectionPDO extends OIDplusDatabaseConnection {
27
        private $conn = null;
27
        private $conn = null;
28
        private $last_error = null; // we need that because PDO divides prepared statement errors and normal query errors, but we have only one "error()" method
28
        private $last_error = null; // we need that because PDO divides prepared statement errors and normal query errors, but we have only one "error()" method
29
        private $transactions_supported = false;
29
        private $transactions_supported = false;
30
 
30
 
-
 
31
        /**
-
 
32
         * @param string $sql
-
 
33
         * @param array|null $prepared_args
-
 
34
         * @return OIDplusQueryResultPDO
-
 
35
         * @throws OIDplusException
-
 
36
         */
31
        public function doQuery(string $sql, /*?array*/ $prepared_args=null): OIDplusQueryResult {
37
        public function doQuery(string $sql, array $prepared_args=null): OIDplusQueryResult {
32
                $this->last_error = null;
38
                $this->last_error = null;
33
                if (is_null($prepared_args)) {
39
                if (is_null($prepared_args)) {
34
                        $res = $this->conn->query($sql);
40
                        $res = $this->conn->query($sql);
35
 
41
 
36
                        if ($res === false) {
42
                        if ($res === false) {
Line 71... Line 77...
71
                        }
77
                        }
72
                        return new OIDplusQueryResultPDO($ps);
78
                        return new OIDplusQueryResultPDO($ps);
73
                }
79
                }
74
        }
80
        }
75
 
81
 
-
 
82
        /**
-
 
83
         * @return int
-
 
84
         * @throws OIDplusException
-
 
85
         */
76
        public function insert_id(): int {
86
        public function insert_id(): int {
77
                try {
87
                try {
78
                        $out = @($this->conn->lastInsertId());
88
                        $out = @($this->conn->lastInsertId());
79
                        if ($out === false) return parent::insert_id(); // fallback method that uses the SQL slang
89
                        if ($out === false) return parent::insert_id(); // fallback method that uses the SQL slang
80
                        return $out;
90
                        return $out;
81
                } catch (\Exception $e) {
91
                } catch (\Exception $e) {
82
                        return parent::insert_id(); // fallback method that uses the SQL slang
92
                        return parent::insert_id(); // fallback method that uses the SQL slang
83
                }
93
                }
84
        }
94
        }
85
 
95
 
-
 
96
        /**
-
 
97
         * @return string
-
 
98
         */
86
        public function error(): string {
99
        public function error(): string {
87
                $err = $this->last_error;
100
                $err = $this->last_error;
88
                if ($err == null) $err = '';
101
                if ($err == null) $err = '';
89
                return $err;
102
                return $err;
90
        }
103
        }
91
 
104
 
-
 
105
        /**
-
 
106
         * @return void
-
 
107
         * @throws OIDplusConfigInitializationException
-
 
108
         * @throws OIDplusException
-
 
109
         */
92
        protected function doConnect()/*: void*/ {
110
        protected function doConnect()/*: void*/ {
93
                if (!class_exists('PDO')) throw new OIDplusConfigInitializationException(_L('PHP extension "%1" not installed','PDO'));
111
                if (!class_exists('PDO')) throw new OIDplusConfigInitializationException(_L('PHP extension "%1" not installed','PDO'));
94
 
112
 
95
                try {
113
                try {
96
                        $options = [
114
                        $options = [
Line 133... Line 151...
133
                } catch (\Exception $e) {
151
                } catch (\Exception $e) {
134
                        $this->transactions_supported = false;
152
                        $this->transactions_supported = false;
135
                }
153
                }
136
        }
154
        }
137
 
155
 
-
 
156
        /**
-
 
157
         * @return void
-
 
158
         */
138
        protected function doDisconnect()/*: void*/ {
159
        protected function doDisconnect()/*: void*/ {
139
                $this->conn = null; // the connection will be closed by removing the reference
160
                $this->conn = null; // the connection will be closed by removing the reference
140
        }
161
        }
141
 
162
 
-
 
163
        /**
-
 
164
         * @var bool
-
 
165
         */
142
        private $intransaction = false;
166
        private $intransaction = false;
143
 
167
 
-
 
168
        /**
-
 
169
         * @return bool
-
 
170
         */
144
        public function transaction_supported(): bool {
171
        public function transaction_supported(): bool {
145
                return $this->transactions_supported;
172
                return $this->transactions_supported;
146
        }
173
        }
147
 
174
 
-
 
175
        /**
-
 
176
         * @return int
-
 
177
         */
148
        public function transaction_level(): int {
178
        public function transaction_level(): int {
149
                if (!$this->transaction_supported()) {
179
                if (!$this->transaction_supported()) {
150
                        // TODO?
180
                        // TODO?
151
                        return 0;
181
                        return 0;
152
                }
182
                }
153
                return $this->intransaction ? 1 : 0;
183
                return $this->intransaction ? 1 : 0;
154
        }
184
        }
155
 
185
 
-
 
186
        /**
-
 
187
         * @return void
-
 
188
         * @throws OIDplusException
-
 
189
         */
156
        public function transaction_begin()/*: void*/ {
190
        public function transaction_begin()/*: void*/ {
157
                if (!$this->transaction_supported()) {
191
                if (!$this->transaction_supported()) {
158
                        // TODO?
192
                        // TODO?
159
                        return;
193
                        return;
160
                }
194
                }
161
                if ($this->intransaction) throw new OIDplusException(_L('Nested transactions are not supported by this database plugin.'));
195
                if ($this->intransaction) throw new OIDplusException(_L('Nested transactions are not supported by this database plugin.'));
162
                $this->conn->beginTransaction();
196
                $this->conn->beginTransaction();
163
                $this->intransaction = true;
197
                $this->intransaction = true;
164
        }
198
        }
165
 
199
 
-
 
200
        /**
-
 
201
         * @return void
-
 
202
         */
166
        public function transaction_commit()/*: void*/ {
203
        public function transaction_commit()/*: void*/ {
167
                if (!$this->transaction_supported()) {
204
                if (!$this->transaction_supported()) {
168
                        // TODO?
205
                        // TODO?
169
                        return;
206
                        return;
170
                }
207
                }
171
                $this->conn->commit();
208
                $this->conn->commit();
172
                $this->intransaction = false;
209
                $this->intransaction = false;
173
        }
210
        }
174
 
211
 
-
 
212
        /**
-
 
213
         * @return void
-
 
214
         */
175
        public function transaction_rollback()/*: void*/ {
215
        public function transaction_rollback()/*: void*/ {
176
                if (!$this->transaction_supported()) {
216
                if (!$this->transaction_supported()) {
177
                        // TODO?
217
                        // TODO?
178
                        return;
218
                        return;
179
                }
219
                }