blob: fa27f03d5a295cb326c025903c67d2b8320c9195 [file] [log] [blame]
Derek Allardba0dd632007-03-07 12:10:58 +00001<?php if (!defined('BASEPATH')) exit('No direct script access allowed');
2/**
Derek Allardd2df9bc2007-04-15 17:41:17 +00003 * CodeIgniter
Derek Allardba0dd632007-03-07 12:10:58 +00004 *
5 * An open source application development framework for PHP 4.3.2 or newer
6 *
7 * @package CodeIgniter
8 * @author Rick Ellis
Derek Allardd2df9bc2007-04-15 17:41:17 +00009 * @copyright Copyright (c) 2006, EllisLab, Inc.
Derek Allard6838f002007-10-04 19:29:59 +000010 * @license http://www.codeigniter.com/user_guide/license.html
Derek Allardba0dd632007-03-07 12:10:58 +000011 * @link http://www.codeigniter.com
12 * @since Version 1.0
13 * @filesource
14 */
15
16// ------------------------------------------------------------------------
17
18/**
19 * Postgre 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 Rick Ellis
29 * @link http://www.codeigniter.com/user_guide/database/
30 */
31class CI_DB_postgre_driver extends CI_DB {
32
33 /**
Derek Allard694b5b82007-12-18 15:58:03 +000034 * The syntax to count rows is slightly different across different
35 * database engines, so this string appears in each driver and is
36 * used for the count_all() and count_all_results() functions.
37 */
Derek Allard39b622d2008-01-16 21:10:09 +000038 var $_count_string = "SELECT COUNT(*) AS ";
Derek Allard6ddb5a12007-12-18 17:22:50 +000039 var $_random_keyword = ' RANDOM()'; // database specific random keyword
Derek Allard694b5b82007-12-18 15:58:03 +000040
41 /**
Derek Allardba0dd632007-03-07 12:10:58 +000042 * Non-persistent database connection
43 *
44 * @access private called by the base class
45 * @return resource
46 */
47 function db_connect()
48 {
49 $port = ($this->port == '') ? '' : " port=".$this->port;
50
51 return @pg_connect("host=".$this->hostname.$port." dbname=".$this->database." user=".$this->username." password=".$this->password);
52 }
53
54 // --------------------------------------------------------------------
55
56 /**
57 * Persistent database connection
58 *
59 * @access private called by the base class
60 * @return resource
61 */
62 function db_pconnect()
63 {
64 $port = ($this->port == '') ? '' : " port=".$this->port;
65
66 return @pg_pconnect("host=".$this->hostname.$port." dbname=".$this->database." user=".$this->username." password=".$this->password);
67 }
68
69 // --------------------------------------------------------------------
70
71 /**
72 * Select the database
73 *
74 * @access private called by the base class
75 * @return resource
76 */
77 function db_select()
78 {
79 // Not needed for Postgre so we'll return TRUE
80 return TRUE;
81 }
82
83 // --------------------------------------------------------------------
84
85 /**
Derek Allard39b622d2008-01-16 21:10:09 +000086 * Set client character set
87 *
88 * @access public
89 * @param string
90 * @param string
91 * @return resource
92 */
93 function db_set_charset($charset, $collation)
94 {
95 // TODO - add support if needed
96 return TRUE;
97 }
98
99 // --------------------------------------------------------------------
100
101 /**
Derek Allardba0dd632007-03-07 12:10:58 +0000102 * Version number query string
103 *
104 * @access public
105 * @return string
106 */
107 function _version()
108 {
109 return "SELECT version() AS ver";
110 }
111
112 // --------------------------------------------------------------------
113
114 /**
115 * Execute the query
116 *
117 * @access private called by the base class
118 * @param string an SQL query
119 * @return resource
120 */
121 function _execute($sql)
122 {
123 $sql = $this->_prep_query($sql);
124 return @pg_query($this->conn_id, $sql);
125 }
126
127 // --------------------------------------------------------------------
128
129 /**
130 * Prep the query
131 *
132 * If needed, each database adapter can prep the query string
133 *
134 * @access private called by execute()
135 * @param string an SQL query
136 * @return string
137 */
138 function _prep_query($sql)
139 {
140 return $sql;
141 }
142
143 // --------------------------------------------------------------------
144
145 /**
146 * Begin Transaction
147 *
148 * @access public
149 * @return bool
150 */
151 function trans_begin($test_mode = FALSE)
152 {
153 if ( ! $this->trans_enabled)
154 {
155 return TRUE;
156 }
157
158 // When transactions are nested we only begin/commit/rollback the outermost ones
159 if ($this->_trans_depth > 0)
160 {
161 return TRUE;
162 }
163
164 // Reset the transaction failure flag.
165 // If the $test_mode flag is set to TRUE transactions will be rolled back
166 // even if the queries produce a successful result.
167 $this->_trans_failure = ($test_mode === TRUE) ? TRUE : FALSE;
168
169 return @pg_exec($this->conn_id, "begin");
170 }
171
172 // --------------------------------------------------------------------
173
174 /**
175 * Commit Transaction
176 *
177 * @access public
178 * @return bool
179 */
180 function trans_commit()
181 {
182 if ( ! $this->trans_enabled)
183 {
184 return TRUE;
185 }
186
187 // When transactions are nested we only begin/commit/rollback the outermost ones
188 if ($this->_trans_depth > 0)
189 {
190 return TRUE;
191 }
192
193 return @pg_exec($this->conn_id, "commit");
194 }
195
196 // --------------------------------------------------------------------
197
198 /**
199 * Rollback Transaction
200 *
201 * @access public
202 * @return bool
203 */
204 function trans_rollback()
205 {
206 if ( ! $this->trans_enabled)
207 {
208 return TRUE;
209 }
210
211 // When transactions are nested we only begin/commit/rollback the outermost ones
212 if ($this->_trans_depth > 0)
213 {
214 return TRUE;
215 }
216
217 return @pg_exec($this->conn_id, "rollback");
218 }
219
220 // --------------------------------------------------------------------
221
222 /**
223 * Escape String
224 *
225 * @access public
226 * @param string
227 * @return string
228 */
229 function escape_str($str)
230 {
231 return pg_escape_string($str);
232 }
233
234 // --------------------------------------------------------------------
235
236 /**
237 * Affected Rows
238 *
239 * @access public
240 * @return integer
241 */
242 function affected_rows()
243 {
244 return @pg_affected_rows($this->result_id);
245 }
246
247 // --------------------------------------------------------------------
248
249 /**
250 * Insert ID
251 *
252 * @access public
253 * @return integer
254 */
255 function insert_id()
256 {
Derek Jonesf9a4e9e2007-07-12 13:56:21 +0000257 $v = $this->_version();
Derek Allardba0dd632007-03-07 12:10:58 +0000258 $v = $v['server'];
259
260 $table = func_num_args() > 0 ? func_get_arg(0) : null;
261 $column = func_num_args() > 1 ? func_get_arg(1) : null;
262
263 if ($table == null && $v >= '8.1')
264 {
265 $sql='SELECT LASTVAL() as ins_id';
266 }
267 elseif ($table != null && $column != null && $v >= '8.0')
268 {
269 $sql = sprintf("SELECT pg_get_serial_sequence('%s','%s') as seq", $table, $column);
270 $query = $this->query($sql);
271 $row = $query->row();
272 $sql = sprintf("SELECT CURRVAL('%s') as ins_id", $row->seq);
273 }
274 elseif ($table != null)
275 {
276 // seq_name passed in table parameter
277 $sql = sprintf("SELECT CURRVAL('%s') as ins_id", $table);
278 }
279 else
280 {
281 return pg_last_oid($this->result_id);
282 }
283 $query = $this->query($sql);
284 $row = $query->row();
285 return $row->ins_id;
286 }
287
288 // --------------------------------------------------------------------
289
290 /**
291 * "Count All" query
292 *
293 * Generates a platform-specific query string that counts all records in
294 * the specified database
295 *
296 * @access public
297 * @param string
298 * @return string
299 */
300 function count_all($table = '')
301 {
302 if ($table == '')
303 return '0';
Derek Allard694b5b82007-12-18 15:58:03 +0000304
Derek Allard6ddb5a12007-12-18 17:22:50 +0000305 $query = $this->query($this->_count_string .'FROM "'.$this->dbprefix.$table.'"');
306// original query before _count_string was used. Kept for reference
Derek Allard694b5b82007-12-18 15:58:03 +0000307// $query = $this->query('SELECT COUNT(*) AS numrows FROM "'.$this->dbprefix.$table.'"');
308
Derek Allardba0dd632007-03-07 12:10:58 +0000309 if ($query->num_rows() == 0)
310 return '0';
311
312 $row = $query->row();
313 return $row->numrows;
314 }
315
316 // --------------------------------------------------------------------
317
318 /**
319 * Show table query
320 *
321 * Generates a platform-specific query string so that the table names can be fetched
322 *
323 * @access private
Derek Allard39b622d2008-01-16 21:10:09 +0000324 * @param boolean
Derek Allardba0dd632007-03-07 12:10:58 +0000325 * @return string
326 */
Derek Allard39b622d2008-01-16 21:10:09 +0000327 function _list_tables($prefix_limit = FALSE)
Derek Allardba0dd632007-03-07 12:10:58 +0000328 {
Derek Allard39b622d2008-01-16 21:10:09 +0000329 $sql = "SELECT table_name FROM information_schema.tables WHERE table_schema = 'public'";
330
331 if ($prefix_limit !== FALSE AND $this->dbprefix != '')
332 {
333 $sql .= " AND table_name LIKE '".$this->dbprefix."%'";
334 }
335
336 return $sql;
Derek Allardba0dd632007-03-07 12:10:58 +0000337 }
338
339 // --------------------------------------------------------------------
340
341 /**
342 * Show column query
343 *
344 * Generates a platform-specific query string so that the column names can be fetched
345 *
346 * @access public
347 * @param string the table name
348 * @return string
349 */
350 function _list_columns($table = '')
351 {
Derek Allard694b5b82007-12-18 15:58:03 +0000352 return "SELECT column_name FROM information_schema.columns WHERE table_name ='".$this->_escape_table($table)."'";
Derek Allardba0dd632007-03-07 12:10:58 +0000353 }
354
355 // --------------------------------------------------------------------
356
357 /**
358 * Field data query
359 *
360 * Generates a platform-specific query so that the column data can be retrieved
361 *
362 * @access public
363 * @param string the table name
364 * @return object
365 */
366 function _field_data($table)
367 {
368 return "SELECT * FROM ".$this->_escape_table($table)." LIMIT 1";
369 }
370
371 // --------------------------------------------------------------------
372
373 /**
374 * The error message string
375 *
376 * @access private
377 * @return string
378 */
379 function _error_message()
380 {
381 return pg_last_error($this->conn_id);
382 }
383
384 // --------------------------------------------------------------------
385
386 /**
387 * The error message number
388 *
389 * @access private
390 * @return integer
391 */
392 function _error_number()
393 {
394 return '';
395 }
396
397 // --------------------------------------------------------------------
398
399 /**
400 * Escape Table Name
401 *
402 * This function adds backticks if the table name has a period
403 * in it. Some DBs will get cranky unless periods are escaped.
404 *
405 * @access private
406 * @param string the table name
407 * @return string
408 */
409 function _escape_table($table)
410 {
411 if (stristr($table, '.'))
412 {
413 $table = '"'.preg_replace("/\./", '"."', $table).'"';
414 }
415
416 return $table;
417 }
418
419 // --------------------------------------------------------------------
420
421 /**
Derek Allard39b622d2008-01-16 21:10:09 +0000422 * Protect Identifiers
423 *
424 * This function adds backticks if appropriate based on db type
425 *
426 * @access private
427 * @param mixed the item to escape
428 * @param boolean only affect the first word
429 * @return mixed the item with backticks
430 */
431 function _protect_identifiers($item, $first_word_only = FALSE)
432 {
433 if (is_array($item))
434 {
435 $escaped_array = array();
436
437 foreach($item as $k=>$v)
438 {
439 $escaped_array[$this->_protect_identifiers($k)] = $this->_protect_identifiers($v, $first_word_only);
440 }
441
442 return $escaped_array;
443 }
444
445 // This function may get "item1 item2" as a string, and so
446 // we may need ""item1" "item2"" and not ""item1 item2""
Derek Allard61579382008-01-16 22:22:42 +0000447 if (ctype_alnum($item) === FALSE)
Derek Allard39b622d2008-01-16 21:10:09 +0000448 {
449 // This function may get "field >= 1", and need it to return ""field" >= 1"
Derek Allard61579382008-01-16 22:22:42 +0000450 $lbound = ($first_word_only === TRUE) ? '' : '|\s|\(';
Derek Allard39b622d2008-01-16 21:10:09 +0000451
Derek Allard61579382008-01-16 22:22:42 +0000452 $item = preg_replace('/(^'.$lbound.')([\w\d\-\_]+?)(\s|\)|$)/iS', '$1"$2"$3', $item);
453 }
454 else
455 {
456 return ""{$item}"";
Derek Allard39b622d2008-01-16 21:10:09 +0000457 }
458
459 $exceptions = array('AS', '/', '-', '%', '+', '*');
460
461 foreach ($exceptions as $exception)
462 {
Derek Allard61579382008-01-16 22:22:42 +0000463
Derek Allard39b622d2008-01-16 21:10:09 +0000464 if (stristr($item, " "{$exception}" ") !== FALSE)
465 {
466 $item = preg_replace('/ "('.preg_quote($exception).')" /i', ' $1 ', $item);
467 }
468 }
Derek Allard39b622d2008-01-16 21:10:09 +0000469 return $item;
470 }
471
472 // --------------------------------------------------------------------
473
474 /**
Derek Allardba0dd632007-03-07 12:10:58 +0000475 * Insert statement
476 *
477 * Generates a platform-specific insert string from the supplied data
478 *
479 * @access public
480 * @param string the table name
481 * @param array the insert keys
482 * @param array the insert values
483 * @return string
484 */
485 function _insert($table, $keys, $values)
486 {
487 return "INSERT INTO ".$this->_escape_table($table)." (".implode(', ', $keys).") VALUES (".implode(', ', $values).")";
488 }
489
490 // --------------------------------------------------------------------
491
492 /**
493 * Update statement
494 *
495 * Generates a platform-specific update string from the supplied data
496 *
497 * @access public
498 * @param string the table name
499 * @param array the update data
500 * @param array the where clause
Derek Allard39b622d2008-01-16 21:10:09 +0000501 * @param array the orderby clause
502 * @param array the limit clause
Derek Allardba0dd632007-03-07 12:10:58 +0000503 * @return string
504 */
Derek Allard39b622d2008-01-16 21:10:09 +0000505 function _update($table, $values, $where, $orderby = array(), $limit = FALSE)
Derek Allardba0dd632007-03-07 12:10:58 +0000506 {
507 foreach($values as $key => $val)
508 {
509 $valstr[] = $key." = ".$val;
510 }
Derek Allardda6d2402007-12-19 14:49:29 +0000511
512 $limit = (!$limit) ? '' : ' LIMIT '.$limit;
Derek Allard39b622d2008-01-16 21:10:09 +0000513
514 $orderby = (count($orderby) >= 1)?' ORDER BY '.implode(", ", $orderby):'';
Derek Allardba0dd632007-03-07 12:10:58 +0000515
Derek Allard39b622d2008-01-16 21:10:09 +0000516 return "UPDATE ".$this->_escape_table($table)." SET ".implode(', ', $valstr)." WHERE ".implode(" ", $where).$orderby.$limit;
517 }
518
519
520 // --------------------------------------------------------------------
521
522 /**
523 * Truncate statement
524 *
525 * Generates a platform-specific truncate string from the supplied data
526 * If the database does not support the truncate() command
527 * This function maps to "DELETE FROM table"
528 *
529 * @access public
530 * @param string the table name
531 * @return string
532 */
533 function _truncate($table)
534 {
535 return "TRUNCATE ".$this->_escape_table($table);
Derek Allardba0dd632007-03-07 12:10:58 +0000536 }
537
538 // --------------------------------------------------------------------
539
540 /**
541 * Delete statement
542 *
543 * Generates a platform-specific delete string from the supplied data
544 *
545 * @access public
546 * @param string the table name
547 * @param array the where clause
Derek Allard39b622d2008-01-16 21:10:09 +0000548 * @param string the limit clause
Derek Allardba0dd632007-03-07 12:10:58 +0000549 * @return string
550 */
Derek Allard39b622d2008-01-16 21:10:09 +0000551 function _delete($table, $where = array(), $like = array(), $limit = FALSE)
Derek Allardba0dd632007-03-07 12:10:58 +0000552 {
Derek Allard39b622d2008-01-16 21:10:09 +0000553 $conditions = '';
554
555 if (count($where) > 0 || count($like) > 0)
556 {
557 $conditions = "\nWHERE ";
558 $conditions .= implode("\n", $this->ar_where);
559
560 if (count($where) > 0 && count($like) > 0)
561 {
562 $conditions .= " AND ";
563 }
564 $conditions .= implode("\n", $like);
565 }
566
Derek Allarde77d77c2007-12-19 15:01:55 +0000567 $limit = (!$limit) ? '' : ' LIMIT '.$limit;
568
Derek Allard39b622d2008-01-16 21:10:09 +0000569 return "DELETE FROM ".$table.$conditions.$limit;
Derek Allardba0dd632007-03-07 12:10:58 +0000570 }
571
572 // --------------------------------------------------------------------
Derek Allardba0dd632007-03-07 12:10:58 +0000573 /**
574 * Limit string
575 *
576 * Generates a platform-specific LIMIT clause
577 *
578 * @access public
579 * @param string the sql query string
580 * @param integer the number of rows to limit the query to
581 * @param integer the offset value
582 * @return string
583 */
584 function _limit($sql, $limit, $offset)
585 {
586 $sql .= "LIMIT ".$limit;
587
588 if ($offset > 0)
589 {
590 $sql .= " OFFSET ".$offset;
591 }
592
593 return $sql;
594 }
595
596 // --------------------------------------------------------------------
597
598 /**
599 * Close DB Connection
600 *
601 * @access public
602 * @param resource
603 * @return void
604 */
605 function _close($conn_id)
606 {
607 @pg_close($conn_id);
608 }
609
610
611}
612
admin7eea4f82006-09-24 18:13:17 +0000613?>