blob: ef0b5b45194c5d140c3f193325414ee057583047 [file] [log] [blame]
Esen Sagynov2e087942011-08-09 23:35:01 -07001<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
2/**
3 * CodeIgniter
4 *
5 * An open source application development framework for PHP 5.1.6 or newer
6 *
7 * @package CodeIgniter
8 * @author Esen Sagynov
9 * @copyright Copyright (c) 2008 - 2011, EllisLab, Inc.
10 * @license http://codeigniter.com/user_guide/license.html
11 * @link http://codeigniter.com
12 * @since Version 2.0.2
13 * @filesource
14 */
15
16// ------------------------------------------------------------------------
17
18/**
19 * CUBRID Database Adapter Class
20 *
21 * Note: _DB is an extender class that the app controller
22 * creates dynamically based on whether the active record
23 * class is being used or not.
24 *
25 * @package CodeIgniter
26 * @subpackage Drivers
27 * @category Database
28 * @author ExpressionEngine Dev Team
29 * @link http://codeigniter.com/user_guide/database/
30 */
31class CI_DB_cubrid_driver extends CI_DB
32{
33 // Default CUBRID Broker port. Will be used unless user
34 // explicitly specifies another one.
35 const DEFAULT_PORT = 33000;
36
37 var $dbdriver = 'cubrid';
38
39 // The character used for escaping - no need in CUBRID
40 var $_escape_char = '';
41
42 // clause and character used for LIKE escape sequences - not used in CUBRID
43 var $_like_escape_str = '';
44 var $_like_escape_chr = '';
45
46 /**
47 * The syntax to count rows is slightly different across different
48 * database engines, so this string appears in each driver and is
49 * used for the count_all() and count_all_results() functions.
50 */
51 var $_count_string = 'SELECT COUNT(*) AS ';
52 var $_random_keyword = ' RAND()'; // database specific random keyword
53
54 /**
55 * Non-persistent database connection
56 *
57 * @access private called by the base class
58 * @return resource
59 */
60 function db_connect()
61 {
62 if ($this->port == '')
63 {
64 $this->port = self::DEFAULT_PORT;
65 }
66
67 $conn = cubrid_connect($this->hostname, $this->port, $this->database, $this->username, $this->password);
68
69 if ($conn){
70 if (isset($this->auto_commit) && !$this->auto_commit){
71 cubrid_set_autocommit($conn, CUBRID_AUTOCOMMIT_FALSE);
72 }
73 else{
74 cubrid_set_autocommit($conn, CUBRID_AUTOCOMMIT_TRUE);
75 $this->auto_commit = TRUE;
76 }
77 }
78
79 return $conn;
80 }
81
82 // --------------------------------------------------------------------
83
84 /**
85 * Persistent database connection
86 * In CUBRID persistent DB connection is supported natively in CUBRID
87 * engine which can be configured in the CUBRID Broker configuration
88 * file by setting the CCI_PCONNECT parameter to ON. In that case, all
89 * connections established between the client application and the
90 * server will become persistent. This is calling the same
91 * @cubrid_connect function will establish persisten connection
92 * considering that the CCI_PCONNECT is ON.
93 *
94 * @access private called by the base class
95 * @return resource
96 */
97 function db_pconnect()
98 {
99 return $this->db_connect();
100 }
101
102 // --------------------------------------------------------------------
103
104 /**
105 * Reconnect
106 *
107 * Keep / reestablish the db connection if no queries have been
108 * sent for a length of time exceeding the server's idle timeout
109 *
110 * @access public
111 * @return void
112 */
113 function reconnect()
114 {
115 if (cubrid_ping($this->conn_id) === FALSE)
116 {
117 $this->conn_id = FALSE;
118 }
119 }
120
121 // --------------------------------------------------------------------
122
123 /**
124 * Select the database
125 *
126 * @access private called by the base class
127 * @return resource
128 */
129 function db_select()
130 {
131 // In CUBRID there is no need to select a database as the database
132 // is chosen at the connection time.
133 // So, to determine if the database is "selected", all we have to
134 // do is return the connection identifier which can be later
135 // checked if it has been established or not.
136 return cubrid_ping($this->conn_id);
137 }
138
139 // --------------------------------------------------------------------
140
141 /**
142 * Set client character set
143 *
144 * @access public
145 * @param string
146 * @param string
147 * @return resource
148 */
149 function db_set_charset($charset, $collation)
150 {
151 // In CUBRID, there is no need to set charset or collation.
152 // This is why returning true will allow the application continue
153 // its normal process.
154 return TRUE;
155 }
156
157 // --------------------------------------------------------------------
158
159 /**
160 * Version number query string
161 *
162 * @access public
163 * @return string
164 */
165 function _version()
166 {
167 return cubrid_get_server_info($this->conn_id);
168 }
169
170 // --------------------------------------------------------------------
171
172 /**
173 * Execute the query
174 *
175 * @access private called by the base class
176 * @param string an SQL query
177 * @return resource
178 */
179 function _execute($sql)
180 {
181 $sql = $this->_prep_query($sql);
182 return @cubrid_query($sql, $this->conn_id);
183 }
184
185 // --------------------------------------------------------------------
186
187 /**
188 * Prep the query
189 *
190 * If needed, each database adapter can prep the query string
191 *
192 * @access private called by execute()
193 * @param string an SQL query
194 * @return string
195 */
196 function _prep_query($sql)
197 {
198 return $sql;
199 }
200
201 // --------------------------------------------------------------------
202
203 /**
204 * Begin Transaction
205 *
206 * @access public
207 * @return bool
208 */
209 function trans_begin($test_mode = FALSE)
210 {
211 if ( ! $this->trans_enabled)
212 {
213 return TRUE;
214 }
215
216 // When transactions are nested we only begin/commit/rollback the outermost ones
217 if ($this->_trans_depth > 0)
218 {
219 return TRUE;
220 }
221
222 // Reset the transaction failure flag.
223 // If the $test_mode flag is set to TRUE transactions will be rolled back
224 // even if the queries produce a successful result.
225 $this->_trans_failure = ($test_mode === TRUE) ? TRUE : FALSE;
226
227 if (cubrid_get_autocommit($this->conn_id))
228 cubrid_set_autocommit($this->conn_id, CUBRID_AUTOCOMMIT_FALSE);
229
230 return TRUE;
231 }
232
233 // --------------------------------------------------------------------
234
235 /**
236 * Commit Transaction
237 *
238 * @access public
239 * @return bool
240 */
241 function trans_commit()
242 {
243 if ( ! $this->trans_enabled)
244 {
245 return TRUE;
246 }
247
248 // When transactions are nested we only begin/commit/rollback the outermost ones
249 if ($this->_trans_depth > 0)
250 {
251 return TRUE;
252 }
253
254 cubrid_commit($this->conn_id);
255
256 if ($this->auto_commit && !cubrid_get_autocommit($this->conn_id))
257 cubrid_set_autocommit($this->conn_id, CUBRID_AUTOCOMMIT_TRUE);
258
259 return TRUE;
260 }
261
262 // --------------------------------------------------------------------
263
264 /**
265 * Rollback Transaction
266 *
267 * @access public
268 * @return bool
269 */
270 function trans_rollback()
271 {
272 if ( ! $this->trans_enabled)
273 {
274 return TRUE;
275 }
276
277 // When transactions are nested we only begin/commit/rollback the outermost ones
278 if ($this->_trans_depth > 0)
279 {
280 return TRUE;
281 }
282
283 cubrid_rollback($this->conn_id);
284
285 if ($this->auto_commit && !cubrid_get_autocommit($this->conn_id))
286 cubrid_set_autocommit($this->conn_id, CUBRID_AUTOCOMMIT_TRUE);
287
288 return TRUE;
289 }
290
291 // --------------------------------------------------------------------
292
293 /**
294 * Escape String
295 *
296 * @access public
297 * @param string
298 * @param bool whether or not the string will be used in a LIKE condition
299 * @return string
300 */
301 function escape_str($str, $like = FALSE)
302 {
303 if (is_array($str))
304 {
305 foreach ($str as $key => $val)
306 {
307 $str[$key] = $this->escape_str($val, $like);
308 }
309
310 return $str;
311 }
312
313 if (function_exists('cubrid_real_escape_string') AND is_resource($this->conn_id))
314 {
315 $str = cubrid_real_escape_string($str, $this->conn_id);
316 }
317 else
318 {
319 $str = addslashes($str);
320 }
321
322 // escape LIKE condition wildcards
323 if ($like === TRUE)
324 {
325 //TODO: check this
326 $str = str_replace(array('%', '_'), array('\\%', '\\_'), $str);
327 }
328
329 return $str;
330 }
331
332 // --------------------------------------------------------------------
333
334 /**
335 * Affected Rows
336 *
337 * @access public
338 * @return integer
339 */
340 function affected_rows()
341 {
342 return @cubrid_affected_rows($this->conn_id);
343 }
344
345 // --------------------------------------------------------------------
346
347 /**
348 * Insert ID
349 *
350 * @access public
351 * @return integer
352 */
353 function insert_id()
354 {
355 return @cubrid_insert_id($this->conn_id);
356 }
357
358 // --------------------------------------------------------------------
359
360 /**
361 * "Count All" query
362 *
363 * Generates a platform-specific query string that counts all records in
364 * the specified table
365 *
366 * @access public
367 * @param string
368 * @return string
369 */
370 function count_all($table = '')
371 {
372 if ($table == '')
373 {
374 return 0;
375 }
376
377 $query = $this->query($this->_count_string . $this->_protect_identifiers('numrows') . " FROM " . $this->_protect_identifiers($table, TRUE, NULL, FALSE));
378
379 if ($query->num_rows() == 0)
380 {
381 return 0;
382 }
383
384 $row = $query->row();
385 return (int) $row->numrows;
386 }
387
388 // --------------------------------------------------------------------
389
390 /**
391 * List table query
392 *
393 * Generates a platform-specific query string so that the table names can be fetched
394 *
395 * @access private
396 * @param boolean
397 * @return string
398 */
399 function _list_tables($prefix_limit = FALSE)
400 {
401 $sql = "SHOW TABLES";
402
403 if ($prefix_limit !== FALSE AND $this->dbprefix != '')
404 {
405 $sql .= " LIKE '".$this->escape_like_str($this->dbprefix)."%'";
406 }
407
408 return $sql;
409 }
410
411 // --------------------------------------------------------------------
412
413 /**
414 * Show column query
415 *
416 * Generates a platform-specific query string so that the column names can be fetched
417 *
418 * @access public
419 * @param string the table name
420 * @return string
421 */
422 function _list_columns($table = '')
423 {
424 return "SHOW COLUMNS FROM ".$this->_protect_identifiers($table, TRUE, NULL, FALSE);
425 }
426
427 // --------------------------------------------------------------------
428
429 /**
430 * Field data query
431 *
432 * Generates a platform-specific query so that the column data can be retrieved
433 *
434 * @access public
435 * @param string the table name
436 * @return object
437 */
438 function _field_data($table)
439 {
440 return "SELECT * FROM ".$table." LIMIT 1";
441 }
442
443 // --------------------------------------------------------------------
444
445 /**
446 * The error message string
447 *
448 * @access private
449 * @return string
450 */
451 function _error_message()
452 {
453 return cubrid_error($this->conn_id);
454 }
455
456 // --------------------------------------------------------------------
457
458 /**
459 * The error message number
460 *
461 * @access private
462 * @return integer
463 */
464 function _error_number()
465 {
466 return cubrid_errno($this->conn_id);
467 }
468
469 // --------------------------------------------------------------------
470
471 /**
472 * Escape the SQL Identifiers
473 *
474 * This function escapes column and table names
475 *
476 * @access private
477 * @param string
478 * @return string
479 */
480 function _escape_identifiers($item)
481 {
482 if ($this->_escape_char == '')
483 {
484 return $item;
485 }
486
487 foreach ($this->_reserved_identifiers as $id)
488 {
489 if (strpos($item, '.'.$id) !== FALSE)
490 {
491 $str = $this->_escape_char. str_replace('.', $this->_escape_char.'.', $item);
492
493 // remove duplicates if the user already included the escape
494 return preg_replace('/['.$this->_escape_char.']+/', $this->_escape_char, $str);
495 }
496 }
497
498 if (strpos($item, '.') !== FALSE)
499 {
500 $str = $this->_escape_char.str_replace('.', $this->_escape_char.'.'.$this->_escape_char, $item).$this->_escape_char;
501 }
502 else
503 {
504 $str = $this->_escape_char.$item.$this->_escape_char;
505 }
506
507 // remove duplicates if the user already included the escape
508 return preg_replace('/['.$this->_escape_char.']+/', $this->_escape_char, $str);
509 }
510
511 // --------------------------------------------------------------------
512
513 /**
514 * From Tables
515 *
516 * This function implicitly groups FROM tables so there is no confusion
517 * about operator precedence in harmony with SQL standards
518 *
519 * @access public
520 * @param type
521 * @return type
522 */
523 function _from_tables($tables)
524 {
525 if ( ! is_array($tables))
526 {
527 $tables = array($tables);
528 }
529
530 return '('.implode(', ', $tables).')';
531 }
532
533 // --------------------------------------------------------------------
534
535 /**
536 * Insert statement
537 *
538 * Generates a platform-specific insert string from the supplied data
539 *
540 * @access public
541 * @param string the table name
542 * @param array the insert keys
543 * @param array the insert values
544 * @return string
545 */
546 function _insert($table, $keys, $values)
547 {
548 return "INSERT INTO ".$table." (".implode(', ', $keys).") VALUES (".implode(', ', $values).")";
549 }
550
551 // --------------------------------------------------------------------
552
553
554 /**
555 * Replace statement
556 *
557 * Generates a platform-specific replace string from the supplied data
558 *
559 * @access public
560 * @param string the table name
561 * @param array the insert keys
562 * @param array the insert values
563 * @return string
564 */
565 function _replace($table, $keys, $values)
566 {
567 return "REPLACE INTO ".$table." (".implode(', ', $keys).") VALUES (".implode(', ', $values).")";
568 }
569
570 // --------------------------------------------------------------------
571
572 /**
573 * Insert_batch statement
574 *
575 * Generates a platform-specific insert string from the supplied data
576 *
577 * @access public
578 * @param string the table name
579 * @param array the insert keys
580 * @param array the insert values
581 * @return string
582 */
583 function _insert_batch($table, $keys, $values)
584 {
585 return "INSERT INTO ".$table." (".implode(', ', $keys).") VALUES ".implode(', ', $values);
586 }
587
588 // --------------------------------------------------------------------
589
590
591 /**
592 * Update statement
593 *
594 * Generates a platform-specific update string from the supplied data
595 *
596 * @access public
597 * @param string the table name
598 * @param array the update data
599 * @param array the where clause
600 * @param array the orderby clause
601 * @param array the limit clause
602 * @return string
603 */
604 function _update($table, $values, $where, $orderby = array(), $limit = FALSE)
605 {
606 foreach ($values as $key => $val)
607 {
608 $valstr[] = $key . ' = ' . $val;
609 }
610
611 $limit = ( ! $limit) ? '' : ' LIMIT '.$limit;
612
613 $orderby = (count($orderby) >= 1)?' ORDER BY '.implode(", ", $orderby):'';
614
615 $sql = "UPDATE ".$table." SET ".implode(', ', $valstr);
616
617 $sql .= ($where != '' AND count($where) >=1) ? " WHERE ".implode(" ", $where) : '';
618
619 $sql .= $orderby.$limit;
620
621 return $sql;
622 }
623
624 // --------------------------------------------------------------------
625
626
627 /**
628 * Update_Batch statement
629 *
630 * Generates a platform-specific batch update string from the supplied data
631 *
632 * @access public
633 * @param string the table name
634 * @param array the update data
635 * @param array the where clause
636 * @return string
637 */
638 function _update_batch($table, $values, $index, $where = NULL)
639 {
640 $ids = array();
641 $where = ($where != '' AND count($where) >=1) ? implode(" ", $where).' AND ' : '';
642
643 foreach ($values as $key => $val)
644 {
645 $ids[] = $val[$index];
646
647 foreach (array_keys($val) as $field)
648 {
649 if ($field != $index)
650 {
651 $final[$field][] = 'WHEN '.$index.' = '.$val[$index].' THEN '.$val[$field];
652 }
653 }
654 }
655
656 $sql = "UPDATE ".$table." SET ";
657 $cases = '';
658
659 foreach ($final as $k => $v)
660 {
661 $cases .= $k.' = CASE '."\n";
662 foreach ($v as $row)
663 {
664 $cases .= $row."\n";
665 }
666
667 $cases .= 'ELSE '.$k.' END, ';
668 }
669
670 $sql .= substr($cases, 0, -2);
671
672 $sql .= ' WHERE '.$where.$index.' IN ('.implode(',', $ids).')';
673
674 return $sql;
675 }
676
677 // --------------------------------------------------------------------
678
679
680 /**
681 * Truncate statement
682 *
683 * Generates a platform-specific truncate string from the supplied data
684 * If the database does not support the truncate() command
685 * This function maps to "DELETE FROM table"
686 *
687 * @access public
688 * @param string the table name
689 * @return string
690 */
691 function _truncate($table)
692 {
693 return "TRUNCATE ".$table;
694 }
695
696 // --------------------------------------------------------------------
697
698 /**
699 * Delete statement
700 *
701 * Generates a platform-specific delete string from the supplied data
702 *
703 * @access public
704 * @param string the table name
705 * @param array the where clause
706 * @param string the limit clause
707 * @return string
708 */
709 function _delete($table, $where = array(), $like = array(), $limit = FALSE)
710 {
711 $conditions = '';
712
713 if (count($where) > 0 OR count($like) > 0)
714 {
715 $conditions = "\nWHERE ";
716 $conditions .= implode("\n", $this->ar_where);
717
718 if (count($where) > 0 && count($like) > 0)
719 {
720 $conditions .= " AND ";
721 }
722 $conditions .= implode("\n", $like);
723 }
724
725 $limit = ( ! $limit) ? '' : ' LIMIT '.$limit;
726
727 return "DELETE FROM ".$table.$conditions.$limit;
728 }
729
730 // --------------------------------------------------------------------
731
732 /**
733 * Limit string
734 *
735 * Generates a platform-specific LIMIT clause
736 *
737 * @access public
738 * @param string the sql query string
739 * @param integer the number of rows to limit the query to
740 * @param integer the offset value
741 * @return string
742 */
743 function _limit($sql, $limit, $offset)
744 {
745 if ($offset == 0)
746 {
747 $offset = '';
748 }
749 else
750 {
751 $offset .= ", ";
752 }
753
754 return $sql."LIMIT ".$offset.$limit;
755 }
756
757 // --------------------------------------------------------------------
758
759 /**
760 * Close DB Connection
761 *
762 * @access public
763 * @param resource
764 * @return void
765 */
766 function _close($conn_id)
767 {
768 @cubrid_close($conn_id);
769 }
770
771}
772
773
774/* End of file cubrid_driver.php */
775/* Location: ./system/database/drivers/cubrid/cubrid_driver.php */