| 68 |
lars |
1 |
<?php
|
|
|
2 |
/**
|
|
|
3 |
* CodeIgniter
|
|
|
4 |
*
|
|
|
5 |
* An open source application development framework for PHP
|
|
|
6 |
*
|
|
|
7 |
* This content is released under the MIT License (MIT)
|
|
|
8 |
*
|
| 2414 |
lars |
9 |
* Copyright (c) 2014 - 2019, British Columbia Institute of Technology
|
| 68 |
lars |
10 |
*
|
|
|
11 |
* Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
|
12 |
* of this software and associated documentation files (the "Software"), to deal
|
|
|
13 |
* in the Software without restriction, including without limitation the rights
|
|
|
14 |
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
|
15 |
* copies of the Software, and to permit persons to whom the Software is
|
|
|
16 |
* furnished to do so, subject to the following conditions:
|
|
|
17 |
*
|
|
|
18 |
* The above copyright notice and this permission notice shall be included in
|
|
|
19 |
* all copies or substantial portions of the Software.
|
|
|
20 |
*
|
|
|
21 |
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
|
22 |
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
|
23 |
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
|
24 |
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
|
25 |
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
|
26 |
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
|
|
27 |
* THE SOFTWARE.
|
|
|
28 |
*
|
|
|
29 |
* @package CodeIgniter
|
|
|
30 |
* @author EllisLab Dev Team
|
|
|
31 |
* @copyright Copyright (c) 2008 - 2014, EllisLab, Inc. (https://ellislab.com/)
|
| 2414 |
lars |
32 |
* @copyright Copyright (c) 2014 - 2019, British Columbia Institute of Technology (https://bcit.ca/)
|
|
|
33 |
* @license https://opensource.org/licenses/MIT MIT License
|
| 68 |
lars |
34 |
* @link https://codeigniter.com
|
|
|
35 |
* @since Version 2.1.0
|
|
|
36 |
* @filesource
|
|
|
37 |
*/
|
|
|
38 |
defined('BASEPATH') OR exit('No direct script access allowed');
|
|
|
39 |
|
|
|
40 |
/**
|
|
|
41 |
* CUBRID Database Adapter Class
|
|
|
42 |
*
|
|
|
43 |
* Note: _DB is an extender class that the app controller
|
|
|
44 |
* creates dynamically based on whether the query builder
|
|
|
45 |
* class is being used or not.
|
|
|
46 |
*
|
|
|
47 |
* @package CodeIgniter
|
|
|
48 |
* @subpackage Drivers
|
|
|
49 |
* @category Database
|
|
|
50 |
* @author Esen Sagynov
|
|
|
51 |
* @link https://codeigniter.com/user_guide/database/
|
|
|
52 |
*/
|
|
|
53 |
class CI_DB_cubrid_driver extends CI_DB {
|
|
|
54 |
|
|
|
55 |
/**
|
|
|
56 |
* Database driver
|
|
|
57 |
*
|
|
|
58 |
* @var string
|
|
|
59 |
*/
|
|
|
60 |
public $dbdriver = 'cubrid';
|
|
|
61 |
|
|
|
62 |
/**
|
|
|
63 |
* Auto-commit flag
|
|
|
64 |
*
|
|
|
65 |
* @var bool
|
|
|
66 |
*/
|
|
|
67 |
public $auto_commit = TRUE;
|
|
|
68 |
|
|
|
69 |
// --------------------------------------------------------------------
|
|
|
70 |
|
|
|
71 |
/**
|
|
|
72 |
* Identifier escape character
|
|
|
73 |
*
|
|
|
74 |
* @var string
|
|
|
75 |
*/
|
|
|
76 |
protected $_escape_char = '`';
|
|
|
77 |
|
|
|
78 |
/**
|
|
|
79 |
* ORDER BY random keyword
|
|
|
80 |
*
|
|
|
81 |
* @var array
|
|
|
82 |
*/
|
|
|
83 |
protected $_random_keyword = array('RANDOM()', 'RANDOM(%d)');
|
|
|
84 |
|
|
|
85 |
// --------------------------------------------------------------------
|
|
|
86 |
|
|
|
87 |
/**
|
|
|
88 |
* Class constructor
|
|
|
89 |
*
|
|
|
90 |
* @param array $params
|
|
|
91 |
* @return void
|
|
|
92 |
*/
|
|
|
93 |
public function __construct($params)
|
|
|
94 |
{
|
|
|
95 |
parent::__construct($params);
|
|
|
96 |
|
|
|
97 |
if (preg_match('/^CUBRID:[^:]+(:[0-9][1-9]{0,4})?:[^:]+:[^:]*:[^:]*:(\?.+)?$/', $this->dsn, $matches))
|
|
|
98 |
{
|
|
|
99 |
if (stripos($matches[2], 'autocommit=off') !== FALSE)
|
|
|
100 |
{
|
|
|
101 |
$this->auto_commit = FALSE;
|
|
|
102 |
}
|
|
|
103 |
}
|
|
|
104 |
else
|
|
|
105 |
{
|
|
|
106 |
// If no port is defined by the user, use the default value
|
|
|
107 |
empty($this->port) OR $this->port = 33000;
|
|
|
108 |
}
|
|
|
109 |
}
|
|
|
110 |
|
|
|
111 |
// --------------------------------------------------------------------
|
|
|
112 |
|
|
|
113 |
/**
|
|
|
114 |
* Non-persistent database connection
|
|
|
115 |
*
|
|
|
116 |
* @param bool $persistent
|
|
|
117 |
* @return resource
|
|
|
118 |
*/
|
|
|
119 |
public function db_connect($persistent = FALSE)
|
|
|
120 |
{
|
|
|
121 |
if (preg_match('/^CUBRID:[^:]+(:[0-9][1-9]{0,4})?:[^:]+:([^:]*):([^:]*):(\?.+)?$/', $this->dsn, $matches))
|
|
|
122 |
{
|
|
|
123 |
$func = ($persistent !== TRUE) ? 'cubrid_connect_with_url' : 'cubrid_pconnect_with_url';
|
|
|
124 |
return ($matches[2] === '' && $matches[3] === '' && $this->username !== '' && $this->password !== '')
|
|
|
125 |
? $func($this->dsn, $this->username, $this->password)
|
|
|
126 |
: $func($this->dsn);
|
|
|
127 |
}
|
|
|
128 |
|
|
|
129 |
$func = ($persistent !== TRUE) ? 'cubrid_connect' : 'cubrid_pconnect';
|
|
|
130 |
return ($this->username !== '')
|
|
|
131 |
? $func($this->hostname, $this->port, $this->database, $this->username, $this->password)
|
|
|
132 |
: $func($this->hostname, $this->port, $this->database);
|
|
|
133 |
}
|
|
|
134 |
|
|
|
135 |
// --------------------------------------------------------------------
|
|
|
136 |
|
|
|
137 |
/**
|
|
|
138 |
* Reconnect
|
|
|
139 |
*
|
|
|
140 |
* Keep / reestablish the db connection if no queries have been
|
|
|
141 |
* sent for a length of time exceeding the server's idle timeout
|
|
|
142 |
*
|
|
|
143 |
* @return void
|
|
|
144 |
*/
|
|
|
145 |
public function reconnect()
|
|
|
146 |
{
|
|
|
147 |
if (cubrid_ping($this->conn_id) === FALSE)
|
|
|
148 |
{
|
|
|
149 |
$this->conn_id = FALSE;
|
|
|
150 |
}
|
|
|
151 |
}
|
|
|
152 |
|
|
|
153 |
// --------------------------------------------------------------------
|
|
|
154 |
|
|
|
155 |
/**
|
|
|
156 |
* Database version number
|
|
|
157 |
*
|
|
|
158 |
* @return string
|
|
|
159 |
*/
|
|
|
160 |
public function version()
|
|
|
161 |
{
|
|
|
162 |
if (isset($this->data_cache['version']))
|
|
|
163 |
{
|
|
|
164 |
return $this->data_cache['version'];
|
|
|
165 |
}
|
|
|
166 |
|
|
|
167 |
return ( ! $this->conn_id OR ($version = cubrid_get_server_info($this->conn_id)) === FALSE)
|
|
|
168 |
? FALSE
|
|
|
169 |
: $this->data_cache['version'] = $version;
|
|
|
170 |
}
|
|
|
171 |
|
|
|
172 |
// --------------------------------------------------------------------
|
|
|
173 |
|
|
|
174 |
/**
|
|
|
175 |
* Execute the query
|
|
|
176 |
*
|
|
|
177 |
* @param string $sql an SQL query
|
|
|
178 |
* @return resource
|
|
|
179 |
*/
|
|
|
180 |
protected function _execute($sql)
|
|
|
181 |
{
|
|
|
182 |
return cubrid_query($sql, $this->conn_id);
|
|
|
183 |
}
|
|
|
184 |
|
|
|
185 |
// --------------------------------------------------------------------
|
|
|
186 |
|
|
|
187 |
/**
|
|
|
188 |
* Begin Transaction
|
|
|
189 |
*
|
|
|
190 |
* @return bool
|
|
|
191 |
*/
|
|
|
192 |
protected function _trans_begin()
|
|
|
193 |
{
|
|
|
194 |
if (($autocommit = cubrid_get_autocommit($this->conn_id)) === NULL)
|
|
|
195 |
{
|
|
|
196 |
return FALSE;
|
|
|
197 |
}
|
|
|
198 |
elseif ($autocommit === TRUE)
|
|
|
199 |
{
|
|
|
200 |
return cubrid_set_autocommit($this->conn_id, CUBRID_AUTOCOMMIT_FALSE);
|
|
|
201 |
}
|
|
|
202 |
|
|
|
203 |
return TRUE;
|
|
|
204 |
}
|
|
|
205 |
|
|
|
206 |
// --------------------------------------------------------------------
|
|
|
207 |
|
|
|
208 |
/**
|
|
|
209 |
* Commit Transaction
|
|
|
210 |
*
|
|
|
211 |
* @return bool
|
|
|
212 |
*/
|
|
|
213 |
protected function _trans_commit()
|
|
|
214 |
{
|
|
|
215 |
if ( ! cubrid_commit($this->conn_id))
|
|
|
216 |
{
|
|
|
217 |
return FALSE;
|
|
|
218 |
}
|
|
|
219 |
|
|
|
220 |
if ($this->auto_commit && ! cubrid_get_autocommit($this->conn_id))
|
|
|
221 |
{
|
|
|
222 |
return cubrid_set_autocommit($this->conn_id, CUBRID_AUTOCOMMIT_TRUE);
|
|
|
223 |
}
|
|
|
224 |
|
|
|
225 |
return TRUE;
|
|
|
226 |
}
|
|
|
227 |
|
|
|
228 |
// --------------------------------------------------------------------
|
|
|
229 |
|
|
|
230 |
/**
|
|
|
231 |
* Rollback Transaction
|
|
|
232 |
*
|
|
|
233 |
* @return bool
|
|
|
234 |
*/
|
|
|
235 |
protected function _trans_rollback()
|
|
|
236 |
{
|
|
|
237 |
if ( ! cubrid_rollback($this->conn_id))
|
|
|
238 |
{
|
|
|
239 |
return FALSE;
|
|
|
240 |
}
|
|
|
241 |
|
|
|
242 |
if ($this->auto_commit && ! cubrid_get_autocommit($this->conn_id))
|
|
|
243 |
{
|
|
|
244 |
cubrid_set_autocommit($this->conn_id, CUBRID_AUTOCOMMIT_TRUE);
|
|
|
245 |
}
|
|
|
246 |
|
|
|
247 |
return TRUE;
|
|
|
248 |
}
|
|
|
249 |
|
|
|
250 |
// --------------------------------------------------------------------
|
|
|
251 |
|
|
|
252 |
/**
|
| 2107 |
lars |
253 |
* Platform-dependent string escape
|
| 68 |
lars |
254 |
*
|
|
|
255 |
* @param string
|
|
|
256 |
* @return string
|
|
|
257 |
*/
|
|
|
258 |
protected function _escape_str($str)
|
|
|
259 |
{
|
|
|
260 |
return cubrid_real_escape_string($str, $this->conn_id);
|
|
|
261 |
}
|
|
|
262 |
|
|
|
263 |
// --------------------------------------------------------------------
|
|
|
264 |
|
|
|
265 |
/**
|
|
|
266 |
* Affected Rows
|
|
|
267 |
*
|
|
|
268 |
* @return int
|
|
|
269 |
*/
|
|
|
270 |
public function affected_rows()
|
|
|
271 |
{
|
|
|
272 |
return cubrid_affected_rows();
|
|
|
273 |
}
|
|
|
274 |
|
|
|
275 |
// --------------------------------------------------------------------
|
|
|
276 |
|
|
|
277 |
/**
|
|
|
278 |
* Insert ID
|
|
|
279 |
*
|
|
|
280 |
* @return int
|
|
|
281 |
*/
|
|
|
282 |
public function insert_id()
|
|
|
283 |
{
|
|
|
284 |
return cubrid_insert_id($this->conn_id);
|
|
|
285 |
}
|
|
|
286 |
|
|
|
287 |
// --------------------------------------------------------------------
|
|
|
288 |
|
|
|
289 |
/**
|
|
|
290 |
* List table query
|
|
|
291 |
*
|
|
|
292 |
* Generates a platform-specific query string so that the table names can be fetched
|
|
|
293 |
*
|
|
|
294 |
* @param bool $prefix_limit
|
|
|
295 |
* @return string
|
|
|
296 |
*/
|
|
|
297 |
protected function _list_tables($prefix_limit = FALSE)
|
|
|
298 |
{
|
|
|
299 |
$sql = 'SHOW TABLES';
|
|
|
300 |
|
|
|
301 |
if ($prefix_limit !== FALSE && $this->dbprefix !== '')
|
|
|
302 |
{
|
|
|
303 |
return $sql." LIKE '".$this->escape_like_str($this->dbprefix)."%'";
|
|
|
304 |
}
|
|
|
305 |
|
|
|
306 |
return $sql;
|
|
|
307 |
}
|
|
|
308 |
|
|
|
309 |
// --------------------------------------------------------------------
|
|
|
310 |
|
|
|
311 |
/**
|
|
|
312 |
* Show column query
|
|
|
313 |
*
|
|
|
314 |
* Generates a platform-specific query string so that the column names can be fetched
|
|
|
315 |
*
|
|
|
316 |
* @param string $table
|
|
|
317 |
* @return string
|
|
|
318 |
*/
|
|
|
319 |
protected function _list_columns($table = '')
|
|
|
320 |
{
|
|
|
321 |
return 'SHOW COLUMNS FROM '.$this->protect_identifiers($table, TRUE, NULL, FALSE);
|
|
|
322 |
}
|
|
|
323 |
|
|
|
324 |
// --------------------------------------------------------------------
|
|
|
325 |
|
|
|
326 |
/**
|
|
|
327 |
* Returns an object with field data
|
|
|
328 |
*
|
|
|
329 |
* @param string $table
|
|
|
330 |
* @return array
|
|
|
331 |
*/
|
|
|
332 |
public function field_data($table)
|
|
|
333 |
{
|
|
|
334 |
if (($query = $this->query('SHOW COLUMNS FROM '.$this->protect_identifiers($table, TRUE, NULL, FALSE))) === FALSE)
|
|
|
335 |
{
|
|
|
336 |
return FALSE;
|
|
|
337 |
}
|
|
|
338 |
$query = $query->result_object();
|
|
|
339 |
|
|
|
340 |
$retval = array();
|
|
|
341 |
for ($i = 0, $c = count($query); $i < $c; $i++)
|
|
|
342 |
{
|
|
|
343 |
$retval[$i] = new stdClass();
|
|
|
344 |
$retval[$i]->name = $query[$i]->Field;
|
|
|
345 |
|
|
|
346 |
sscanf($query[$i]->Type, '%[a-z](%d)',
|
|
|
347 |
$retval[$i]->type,
|
|
|
348 |
$retval[$i]->max_length
|
|
|
349 |
);
|
|
|
350 |
|
|
|
351 |
$retval[$i]->default = $query[$i]->Default;
|
|
|
352 |
$retval[$i]->primary_key = (int) ($query[$i]->Key === 'PRI');
|
|
|
353 |
}
|
|
|
354 |
|
|
|
355 |
return $retval;
|
|
|
356 |
}
|
|
|
357 |
|
|
|
358 |
// --------------------------------------------------------------------
|
|
|
359 |
|
|
|
360 |
/**
|
|
|
361 |
* Error
|
|
|
362 |
*
|
|
|
363 |
* Returns an array containing code and message of the last
|
| 2107 |
lars |
364 |
* database error that has occurred.
|
| 68 |
lars |
365 |
*
|
|
|
366 |
* @return array
|
|
|
367 |
*/
|
|
|
368 |
public function error()
|
|
|
369 |
{
|
|
|
370 |
return array('code' => cubrid_errno($this->conn_id), 'message' => cubrid_error($this->conn_id));
|
|
|
371 |
}
|
|
|
372 |
|
|
|
373 |
// --------------------------------------------------------------------
|
|
|
374 |
|
|
|
375 |
/**
|
|
|
376 |
* FROM tables
|
|
|
377 |
*
|
|
|
378 |
* Groups tables in FROM clauses if needed, so there is no confusion
|
|
|
379 |
* about operator precedence.
|
|
|
380 |
*
|
|
|
381 |
* @return string
|
|
|
382 |
*/
|
|
|
383 |
protected function _from_tables()
|
|
|
384 |
{
|
|
|
385 |
if ( ! empty($this->qb_join) && count($this->qb_from) > 1)
|
|
|
386 |
{
|
|
|
387 |
return '('.implode(', ', $this->qb_from).')';
|
|
|
388 |
}
|
|
|
389 |
|
|
|
390 |
return implode(', ', $this->qb_from);
|
|
|
391 |
}
|
|
|
392 |
|
|
|
393 |
// --------------------------------------------------------------------
|
|
|
394 |
|
|
|
395 |
/**
|
|
|
396 |
* Close DB Connection
|
|
|
397 |
*
|
|
|
398 |
* @return void
|
|
|
399 |
*/
|
|
|
400 |
protected function _close()
|
|
|
401 |
{
|
|
|
402 |
cubrid_close($this->conn_id);
|
|
|
403 |
}
|
|
|
404 |
|
|
|
405 |
}
|