blob: 88f08b2d72ee24a9f5155ed1f8244d6a98302c94 [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 Allard6ddb5a12007-12-18 17:22:50 +000038 var $_count_string = "SELECT COUNT(*) AS numrows ";
39 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 /**
86 * Version number query string
87 *
88 * @access public
89 * @return string
90 */
91 function _version()
92 {
93 return "SELECT version() AS ver";
94 }
95
96 // --------------------------------------------------------------------
97
98 /**
99 * Execute the query
100 *
101 * @access private called by the base class
102 * @param string an SQL query
103 * @return resource
104 */
105 function _execute($sql)
106 {
107 $sql = $this->_prep_query($sql);
108 return @pg_query($this->conn_id, $sql);
109 }
110
111 // --------------------------------------------------------------------
112
113 /**
114 * Prep the query
115 *
116 * If needed, each database adapter can prep the query string
117 *
118 * @access private called by execute()
119 * @param string an SQL query
120 * @return string
121 */
122 function _prep_query($sql)
123 {
124 return $sql;
125 }
126
127 // --------------------------------------------------------------------
128
129 /**
130 * Begin Transaction
131 *
132 * @access public
133 * @return bool
134 */
135 function trans_begin($test_mode = FALSE)
136 {
137 if ( ! $this->trans_enabled)
138 {
139 return TRUE;
140 }
141
142 // When transactions are nested we only begin/commit/rollback the outermost ones
143 if ($this->_trans_depth > 0)
144 {
145 return TRUE;
146 }
147
148 // Reset the transaction failure flag.
149 // If the $test_mode flag is set to TRUE transactions will be rolled back
150 // even if the queries produce a successful result.
151 $this->_trans_failure = ($test_mode === TRUE) ? TRUE : FALSE;
152
153 return @pg_exec($this->conn_id, "begin");
154 }
155
156 // --------------------------------------------------------------------
157
158 /**
159 * Commit Transaction
160 *
161 * @access public
162 * @return bool
163 */
164 function trans_commit()
165 {
166 if ( ! $this->trans_enabled)
167 {
168 return TRUE;
169 }
170
171 // When transactions are nested we only begin/commit/rollback the outermost ones
172 if ($this->_trans_depth > 0)
173 {
174 return TRUE;
175 }
176
177 return @pg_exec($this->conn_id, "commit");
178 }
179
180 // --------------------------------------------------------------------
181
182 /**
183 * Rollback Transaction
184 *
185 * @access public
186 * @return bool
187 */
188 function trans_rollback()
189 {
190 if ( ! $this->trans_enabled)
191 {
192 return TRUE;
193 }
194
195 // When transactions are nested we only begin/commit/rollback the outermost ones
196 if ($this->_trans_depth > 0)
197 {
198 return TRUE;
199 }
200
201 return @pg_exec($this->conn_id, "rollback");
202 }
203
204 // --------------------------------------------------------------------
205
206 /**
207 * Escape String
208 *
209 * @access public
210 * @param string
211 * @return string
212 */
213 function escape_str($str)
214 {
215 return pg_escape_string($str);
216 }
217
218 // --------------------------------------------------------------------
219
220 /**
221 * Affected Rows
222 *
223 * @access public
224 * @return integer
225 */
226 function affected_rows()
227 {
228 return @pg_affected_rows($this->result_id);
229 }
230
231 // --------------------------------------------------------------------
232
233 /**
234 * Insert ID
235 *
236 * @access public
237 * @return integer
238 */
239 function insert_id()
240 {
Derek Jonesf9a4e9e2007-07-12 13:56:21 +0000241 $v = $this->_version();
Derek Allardba0dd632007-03-07 12:10:58 +0000242 $v = $v['server'];
243
244 $table = func_num_args() > 0 ? func_get_arg(0) : null;
245 $column = func_num_args() > 1 ? func_get_arg(1) : null;
246
247 if ($table == null && $v >= '8.1')
248 {
249 $sql='SELECT LASTVAL() as ins_id';
250 }
251 elseif ($table != null && $column != null && $v >= '8.0')
252 {
253 $sql = sprintf("SELECT pg_get_serial_sequence('%s','%s') as seq", $table, $column);
254 $query = $this->query($sql);
255 $row = $query->row();
256 $sql = sprintf("SELECT CURRVAL('%s') as ins_id", $row->seq);
257 }
258 elseif ($table != null)
259 {
260 // seq_name passed in table parameter
261 $sql = sprintf("SELECT CURRVAL('%s') as ins_id", $table);
262 }
263 else
264 {
265 return pg_last_oid($this->result_id);
266 }
267 $query = $this->query($sql);
268 $row = $query->row();
269 return $row->ins_id;
270 }
271
272 // --------------------------------------------------------------------
273
274 /**
275 * "Count All" query
276 *
277 * Generates a platform-specific query string that counts all records in
278 * the specified database
279 *
280 * @access public
281 * @param string
282 * @return string
283 */
284 function count_all($table = '')
285 {
286 if ($table == '')
287 return '0';
Derek Allard694b5b82007-12-18 15:58:03 +0000288
Derek Allard6ddb5a12007-12-18 17:22:50 +0000289 $query = $this->query($this->_count_string .'FROM "'.$this->dbprefix.$table.'"');
290// original query before _count_string was used. Kept for reference
Derek Allard694b5b82007-12-18 15:58:03 +0000291// $query = $this->query('SELECT COUNT(*) AS numrows FROM "'.$this->dbprefix.$table.'"');
292
Derek Allardba0dd632007-03-07 12:10:58 +0000293 if ($query->num_rows() == 0)
294 return '0';
295
296 $row = $query->row();
297 return $row->numrows;
298 }
299
300 // --------------------------------------------------------------------
301
302 /**
303 * Show table query
304 *
305 * Generates a platform-specific query string so that the table names can be fetched
306 *
307 * @access private
308 * @return string
309 */
310 function _list_tables()
311 {
312 return "SELECT table_name FROM information_schema.tables WHERE table_schema = 'public'";
313 }
314
315 // --------------------------------------------------------------------
316
317 /**
318 * Show column query
319 *
320 * Generates a platform-specific query string so that the column names can be fetched
321 *
322 * @access public
323 * @param string the table name
324 * @return string
325 */
326 function _list_columns($table = '')
327 {
Derek Allard694b5b82007-12-18 15:58:03 +0000328 return "SELECT column_name FROM information_schema.columns WHERE table_name ='".$this->_escape_table($table)."'";
Derek Allardba0dd632007-03-07 12:10:58 +0000329 }
330
331 // --------------------------------------------------------------------
332
333 /**
334 * Field data query
335 *
336 * Generates a platform-specific query so that the column data can be retrieved
337 *
338 * @access public
339 * @param string the table name
340 * @return object
341 */
342 function _field_data($table)
343 {
344 return "SELECT * FROM ".$this->_escape_table($table)." LIMIT 1";
345 }
346
347 // --------------------------------------------------------------------
348
349 /**
350 * The error message string
351 *
352 * @access private
353 * @return string
354 */
355 function _error_message()
356 {
357 return pg_last_error($this->conn_id);
358 }
359
360 // --------------------------------------------------------------------
361
362 /**
363 * The error message number
364 *
365 * @access private
366 * @return integer
367 */
368 function _error_number()
369 {
370 return '';
371 }
372
373 // --------------------------------------------------------------------
374
375 /**
376 * Escape Table Name
377 *
378 * This function adds backticks if the table name has a period
379 * in it. Some DBs will get cranky unless periods are escaped.
380 *
381 * @access private
382 * @param string the table name
383 * @return string
384 */
385 function _escape_table($table)
386 {
387 if (stristr($table, '.'))
388 {
389 $table = '"'.preg_replace("/\./", '"."', $table).'"';
390 }
391
392 return $table;
393 }
394
395 // --------------------------------------------------------------------
396
397 /**
398 * Insert statement
399 *
400 * Generates a platform-specific insert string from the supplied data
401 *
402 * @access public
403 * @param string the table name
404 * @param array the insert keys
405 * @param array the insert values
406 * @return string
407 */
408 function _insert($table, $keys, $values)
409 {
410 return "INSERT INTO ".$this->_escape_table($table)." (".implode(', ', $keys).") VALUES (".implode(', ', $values).")";
411 }
412
413 // --------------------------------------------------------------------
414
415 /**
416 * Update statement
417 *
418 * Generates a platform-specific update string from the supplied data
419 *
420 * @access public
421 * @param string the table name
422 * @param array the update data
423 * @param array the where clause
424 * @return string
425 */
Derek Allardda6d2402007-12-19 14:49:29 +0000426 function _update($table, $values, $where, $limit = FALSE)
Derek Allardba0dd632007-03-07 12:10:58 +0000427 {
428 foreach($values as $key => $val)
429 {
430 $valstr[] = $key." = ".$val;
431 }
Derek Allardda6d2402007-12-19 14:49:29 +0000432
433 $limit = (!$limit) ? '' : ' LIMIT '.$limit;
Derek Allardba0dd632007-03-07 12:10:58 +0000434
Derek Allardda6d2402007-12-19 14:49:29 +0000435 return "UPDATE ".$this->_escape_table($table)." SET ".implode(', ', $valstr)." WHERE ".implode(" ", $where).$limit;
Derek Allardba0dd632007-03-07 12:10:58 +0000436 }
437
438 // --------------------------------------------------------------------
439
440 /**
441 * Delete statement
442 *
443 * Generates a platform-specific delete string from the supplied data
444 *
445 * @access public
446 * @param string the table name
447 * @param array the where clause
448 * @return string
449 */
Derek Allarde77d77c2007-12-19 15:01:55 +0000450 function _delete($table, $where, $limit = FALSE)
Derek Allardba0dd632007-03-07 12:10:58 +0000451 {
Derek Allarde77d77c2007-12-19 15:01:55 +0000452 $limit = (!$limit) ? '' : ' LIMIT '.$limit;
453
454 return "DELETE FROM ".$this->_escape_table($table)." WHERE ".implode(" ", $where).$limit;
Derek Allardba0dd632007-03-07 12:10:58 +0000455 }
456
457 // --------------------------------------------------------------------
458
459 /**
460 * Limit string
461 *
462 * Generates a platform-specific LIMIT clause
463 *
464 * @access public
465 * @param string the sql query string
466 * @param integer the number of rows to limit the query to
467 * @param integer the offset value
468 * @return string
469 */
470 function _limit($sql, $limit, $offset)
471 {
472 $sql .= "LIMIT ".$limit;
473
474 if ($offset > 0)
475 {
476 $sql .= " OFFSET ".$offset;
477 }
478
479 return $sql;
480 }
481
482 // --------------------------------------------------------------------
483
484 /**
485 * Close DB Connection
486 *
487 * @access public
488 * @param resource
489 * @return void
490 */
491 function _close($conn_id)
492 {
493 @pg_close($conn_id);
494 }
495
496
497}
498
admin7eea4f82006-09-24 18:13:17 +0000499?>