blob: 000ac083ba1d82e9767a7980981f0e1eca0cf57a [file] [log] [blame]
Timothy Warren80ab8162011-08-22 18:26:12 -04001<?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 ExpressionEngine Dev Team
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 1.0
13 * @filesource
14 */
15
16// ------------------------------------------------------------------------
17
18/**
19 * ODBC 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_pdo_driver extends CI_DB {
32
33 var $dbdriver = 'pdo';
34
35 // the character used to excape - not necessary for PDO
36 var $_escape_char = '';
37
38 // clause and character used for LIKE escape sequences
39 var $_like_escape_str = " {escape '%s'} ";
40 var $_like_escape_chr = '!';
41
42 /**
43 * The syntax to count rows is slightly different across different
44 * database engines, so this string appears in each driver and is
45 * used for the count_all() and count_all_results() functions.
46 */
47 var $_count_string = "SELECT COUNT(*) AS ";
48 var $_random_keyword;
49
50
51 function CI_DB_pdo_driver($params)
52 {
53 parent::CI_DB($params);
54
55 $this->_random_keyword = ' RND('.time().')'; // database specific random keyword
56 }
57
58 /**
59 * Non-persistent database connection
60 *
61 * @access private called by the base class
62 * @return resource
63 */
64 function db_connect()
65 {
66 return new PDO($this->hostname, $this->username, $this->password, array(
67 ));
68 }
69
70 // --------------------------------------------------------------------
71
72 /**
73 * Persistent database connection
74 *
75 * @access private called by the base class
76 * @return resource
77 */
78 function db_pconnect()
79 {
80 return new PDO($this->hostname, $this->username, $this->password, array(
81 ));
82 }
83
84 // --------------------------------------------------------------------
85
86 /**
87 * Reconnect
88 *
89 * Keep / reestablish the db connection if no queries have been
90 * sent for a length of time exceeding the server's idle timeout
91 *
92 * @access public
93 * @return void
94 */
95 function reconnect()
96 {
97 // not implemented in pdo
98 }
99
100 // --------------------------------------------------------------------
101
102 /**
103 * Select the database
104 *
105 * @access private called by the base class
106 * @return resource
107 */
108 function db_select()
109 {
110 // Not needed for PDO
111 return TRUE;
112 }
113
114 // --------------------------------------------------------------------
115
116 /**
117 * Set client character set
118 *
119 * @access public
120 * @param string
121 * @param string
122 * @return resource
123 */
124 function db_set_charset($charset, $collation)
125 {
126 // @todo - add support if needed
127 return TRUE;
128 }
129
130 // --------------------------------------------------------------------
131
132 /**
133 * Version number query string
134 *
135 * @access public
136 * @return string
137 */
138 function _version()
139 {
140 return "SELECT version() AS ver";
141 }
142
143 // --------------------------------------------------------------------
144
145 /**
146 * Execute the query
147 *
148 * @access private called by the base class
149 * @param string an SQL query
150 * @return resource
151 */
152 function _execute($sql)
153 {
154 $sql = $this->_prep_query($sql);
155 return @pdo_exec($this->conn_id, $sql);
156 }
157
158 // --------------------------------------------------------------------
159
160 /**
161 * Prep the query
162 *
163 * If needed, each database adapter can prep the query string
164 *
165 * @access private called by execute()
166 * @param string an SQL query
167 * @return string
168 */
169 function _prep_query($sql)
170 {
171 return $sql;
172 }
173
174 // --------------------------------------------------------------------
175
176 /**
177 * Begin Transaction
178 *
179 * @access public
180 * @return bool
181 */
182 function trans_begin($test_mode = FALSE)
183 {
184 if ( ! $this->trans_enabled)
185 {
186 return TRUE;
187 }
188
189 // When transactions are nested we only begin/commit/rollback the outermost ones
190 if ($this->_trans_depth > 0)
191 {
192 return TRUE;
193 }
194
195 // Reset the transaction failure flag.
196 // If the $test_mode flag is set to TRUE transactions will be rolled back
197 // even if the queries produce a successful result.
198 $this->_trans_failure = ($test_mode === TRUE) ? TRUE : FALSE;
199
200 return pdo_autocommit($this->conn_id, FALSE);
201 }
202
203 // --------------------------------------------------------------------
204
205 /**
206 * Commit Transaction
207 *
208 * @access public
209 * @return bool
210 */
211 function trans_commit()
212 {
213 if ( ! $this->trans_enabled)
214 {
215 return TRUE;
216 }
217
218 // When transactions are nested we only begin/commit/rollback the outermost ones
219 if ($this->_trans_depth > 0)
220 {
221 return TRUE;
222 }
223
224 $ret = pdo_commit($this->conn_id);
225 pdo_autocommit($this->conn_id, TRUE);
226 return $ret;
227 }
228
229 // --------------------------------------------------------------------
230
231 /**
232 * Rollback Transaction
233 *
234 * @access public
235 * @return bool
236 */
237 function trans_rollback()
238 {
239 if ( ! $this->trans_enabled)
240 {
241 return TRUE;
242 }
243
244 // When transactions are nested we only begin/commit/rollback the outermost ones
245 if ($this->_trans_depth > 0)
246 {
247 return TRUE;
248 }
249
250 $ret = pdo_rollback($this->conn_id);
251 pdo_autocommit($this->conn_id, TRUE);
252 return $ret;
253 }
254
255 // --------------------------------------------------------------------
256
257 /**
258 * Escape String
259 *
260 * @access public
261 * @param string
262 * @param bool whether or not the string will be used in a LIKE condition
263 * @return string
264 */
265 function escape_str($str, $like = FALSE)
266 {
267 if (is_array($str))
268 {
269 foreach ($str as $key => $val)
270 {
271 $str[$key] = $this->escape_str($val, $like);
272 }
273
274 return $str;
275 }
276
277 // PDO doesn't require escaping
278 $str = remove_invisible_characters($str);
279
280 // escape LIKE condition wildcards
281 if ($like === TRUE)
282 {
283 $str = str_replace( array('%', '_', $this->_like_escape_chr),
284 array($this->_like_escape_chr.'%', $this->_like_escape_chr.'_', $this->_like_escape_chr.$this->_like_escape_chr),
285 $str);
286 }
287
288 return $str;
289 }
290
291 // --------------------------------------------------------------------
292
293 /**
294 * Affected Rows
295 *
296 * @access public
297 * @return integer
298 */
299 function affected_rows()
300 {
301 return @pdo_num_rows($this->conn_id);
302 }
303
304 // --------------------------------------------------------------------
305
306 /**
307 * Insert ID
308 *
309 * @access public
310 * @return integer
311 */
312 function insert_id()
313 {
314 return @pdo_insert_id($this->conn_id);
315 }
316
317 // --------------------------------------------------------------------
318
319 /**
320 * "Count All" query
321 *
322 * Generates a platform-specific query string that counts all records in
323 * the specified database
324 *
325 * @access public
326 * @param string
327 * @return string
328 */
329 function count_all($table = '')
330 {
331 if ($table == '')
332 {
333 return 0;
334 }
335
336 $query = $this->query($this->_count_string . $this->_protect_identifiers('numrows') . " FROM " . $this->_protect_identifiers($table, TRUE, NULL, FALSE));
337
338 if ($query->num_rows() == 0)
339 {
340 return 0;
341 }
342
343 $row = $query->row();
344 $this->_reset_select();
345 return (int) $row->numrows;
346 }
347
348 // --------------------------------------------------------------------
349
350 /**
351 * Show table query
352 *
353 * Generates a platform-specific query string so that the table names can be fetched
354 *
355 * @access private
356 * @param boolean
357 * @return string
358 */
359 function _list_tables($prefix_limit = FALSE)
360 {
361 $sql = "SHOW TABLES FROM `".$this->database."`";
362
363 if ($prefix_limit !== FALSE AND $this->dbprefix != '')
364 {
365 //$sql .= " LIKE '".$this->escape_like_str($this->dbprefix)."%' ".sprintf($this->_like_escape_str, $this->_like_escape_chr);
366 return FALSE; // not currently supported
367 }
368
369 return $sql;
370 }
371
372 // --------------------------------------------------------------------
373
374 /**
375 * Show column query
376 *
377 * Generates a platform-specific query string so that the column names can be fetched
378 *
379 * @access public
380 * @param string the table name
381 * @return string
382 */
383 function _list_columns($table = '')
384 {
385 return "SHOW COLUMNS FROM ".$table;
386 }
387
388 // --------------------------------------------------------------------
389
390 /**
391 * Field data query
392 *
393 * Generates a platform-specific query so that the column data can be retrieved
394 *
395 * @access public
396 * @param string the table name
397 * @return object
398 */
399 function _field_data($table)
400 {
401 return "SELECT TOP 1 FROM ".$table;
402 }
403
404 // --------------------------------------------------------------------
405
406 /**
407 * The error message string
408 *
409 * @access private
410 * @return string
411 */
412 function _error_message()
413 {
414 return pdo_errormsg($this->conn_id);
415 }
416
417 // --------------------------------------------------------------------
418
419 /**
420 * The error message number
421 *
422 * @access private
423 * @return integer
424 */
425 function _error_number()
426 {
427 return pdo_error($this->conn_id);
428 }
429
430 // --------------------------------------------------------------------
431
432 /**
433 * Escape the SQL Identifiers
434 *
435 * This function escapes column and table names
436 *
437 * @access private
438 * @param string
439 * @return string
440 */
441 function _escape_identifiers($item)
442 {
443 if ($this->_escape_char == '')
444 {
445 return $item;
446 }
447
448 foreach ($this->_reserved_identifiers as $id)
449 {
450 if (strpos($item, '.'.$id) !== FALSE)
451 {
452 $str = $this->_escape_char. str_replace('.', $this->_escape_char.'.', $item);
453
454 // remove duplicates if the user already included the escape
455 return preg_replace('/['.$this->_escape_char.']+/', $this->_escape_char, $str);
456 }
457 }
458
459 if (strpos($item, '.') !== FALSE)
460 {
461 $str = $this->_escape_char.str_replace('.', $this->_escape_char.'.'.$this->_escape_char, $item).$this->_escape_char;
462 }
463 else
464 {
465 $str = $this->_escape_char.$item.$this->_escape_char;
466 }
467
468 // remove duplicates if the user already included the escape
469 return preg_replace('/['.$this->_escape_char.']+/', $this->_escape_char, $str);
470 }
471
472 // --------------------------------------------------------------------
473
474 /**
475 * From Tables
476 *
477 * This function implicitly groups FROM tables so there is no confusion
478 * about operator precedence in harmony with SQL standards
479 *
480 * @access public
481 * @param type
482 * @return type
483 */
484 function _from_tables($tables)
485 {
486 if ( ! is_array($tables))
487 {
488 $tables = array($tables);
489 }
490
491 return '('.implode(', ', $tables).')';
492 }
493
494 // --------------------------------------------------------------------
495
496 /**
497 * Insert statement
498 *
499 * Generates a platform-specific insert string from the supplied data
500 *
501 * @access public
502 * @param string the table name
503 * @param array the insert keys
504 * @param array the insert values
505 * @return string
506 */
507 function _insert($table, $keys, $values)
508 {
509 return "INSERT INTO ".$table." (".implode(', ', $keys).") VALUES (".implode(', ', $values).")";
510 }
511
512 // --------------------------------------------------------------------
513
514 /**
515 * Update statement
516 *
517 * Generates a platform-specific update string from the supplied data
518 *
519 * @access public
520 * @param string the table name
521 * @param array the update data
522 * @param array the where clause
523 * @param array the orderby clause
524 * @param array the limit clause
525 * @return string
526 */
527 function _update($table, $values, $where, $orderby = array(), $limit = FALSE)
528 {
529 foreach ($values as $key => $val)
530 {
531 $valstr[] = $key." = ".$val;
532 }
533
534 $limit = ( ! $limit) ? '' : ' LIMIT '.$limit;
535
536 $orderby = (count($orderby) >= 1)?' ORDER BY '.implode(", ", $orderby):'';
537
538 $sql = "UPDATE ".$table." SET ".implode(', ', $valstr);
539
540 $sql .= ($where != '' AND count($where) >=1) ? " WHERE ".implode(" ", $where) : '';
541
542 $sql .= $orderby.$limit;
543
544 return $sql;
545 }
546
547
548 // --------------------------------------------------------------------
549
550 /**
551 * Truncate statement
552 *
553 * Generates a platform-specific truncate string from the supplied data
554 * If the database does not support the truncate() command
555 * This function maps to "DELETE FROM table"
556 *
557 * @access public
558 * @param string the table name
559 * @return string
560 */
561 function _truncate($table)
562 {
563 return $this->_delete($table);
564 }
565
566 // --------------------------------------------------------------------
567
568 /**
569 * Delete statement
570 *
571 * Generates a platform-specific delete string from the supplied data
572 *
573 * @access public
574 * @param string the table name
575 * @param array the where clause
576 * @param string the limit clause
577 * @return string
578 */
579 function _delete($table, $where = array(), $like = array(), $limit = FALSE)
580 {
581 $conditions = '';
582
583 if (count($where) > 0 OR count($like) > 0)
584 {
585 $conditions = "\nWHERE ";
586 $conditions .= implode("\n", $this->ar_where);
587
588 if (count($where) > 0 && count($like) > 0)
589 {
590 $conditions .= " AND ";
591 }
592 $conditions .= implode("\n", $like);
593 }
594
595 $limit = ( ! $limit) ? '' : ' LIMIT '.$limit;
596
597 return "DELETE FROM ".$table.$conditions.$limit;
598 }
599
600 // --------------------------------------------------------------------
601
602 /**
603 * Limit string
604 *
605 * Generates a platform-specific LIMIT clause
606 *
607 * @access public
608 * @param string the sql query string
609 * @param integer the number of rows to limit the query to
610 * @param integer the offset value
611 * @return string
612 */
613 function _limit($sql, $limit, $offset)
614 {
615 // Does PDO doesn't use the LIMIT clause?
616 return $sql;
617 }
618
619 // --------------------------------------------------------------------
620
621 /**
622 * Close DB Connection
623 *
624 * @access public
625 * @param resource
626 * @return void
627 */
628 function _close($conn_id)
629 {
630 @pdo_close($conn_id);
631 }
632
633
634}
635
636
637
638/* End of file pdo_driver.php */
639/* Location: ./system/database/drivers/pdo/pdo_driver.php */