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