blob: f1439563829b355bd237f4b381b8ecfe01b7b44e [file] [log] [blame]
admin7eea4f82006-09-24 18:13:17 +00001<?php if (!defined('BASEPATH')) exit('No direct script access allowed');
2/**
3 * Code Igniter
4 *
5 * An open source application development framework for PHP 4.3.2 or newer
6 *
7 * @package CodeIgniter
8 * @author Rick Ellis
9 * @copyright Copyright (c) 2006, pMachine, Inc.
10 * @license http://www.codeignitor.com/user_guide/license.html
11 * @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 /**
34 * Non-persistent database connection
35 *
36 * @access private called by the base class
37 * @return resource
38 */
39 function db_connect()
40 {
41 $port = ($this->port == '') ? '' : " port=".$this->port;
42
43 return pg_connect("host=".$this->hostname.$port." dbname=".$this->database." user=".$this->username." password=".$this->password);
44 }
45
46 // --------------------------------------------------------------------
47
48 /**
49 * Persistent database connection
50 *
51 * @access private called by the base class
52 * @return resource
53 */
54 function db_pconnect()
55 {
56 $port = ($this->port == '') ? '' : " port=".$this->port;
57
58 return pg_pconnect("host=".$this->hostname.$port." dbname=".$this->database." user=".$this->username." password=".$this->password);
59 }
60
61 // --------------------------------------------------------------------
62
63 /**
64 * Select the database
65 *
66 * @access private called by the base class
67 * @return resource
68 */
69 function db_select()
70 {
71 // Not needed for Postgre so we'll return TRUE
72 return TRUE;
73 }
admin9cd4e8e2006-09-25 23:26:25 +000074
75 // --------------------------------------------------------------------
76
77 /**
78 * Version number query string
79 *
80 * @access public
81 * @return string
82 */
83 function _version()
84 {
85 return "SELECT version() AS ver";
86 }
87
admin7eea4f82006-09-24 18:13:17 +000088 // --------------------------------------------------------------------
89
90 /**
91 * Execute the query
92 *
93 * @access private called by the base class
94 * @param string an SQL query
95 * @return resource
96 */
97 function _execute($sql)
98 {
99 $sql = $this->_prep_query($sql);
100 return @pg_query($this->conn_id, $sql);
101 }
102
103 // --------------------------------------------------------------------
104
105 /**
106 * Prep the query
107 *
108 * If needed, each database adapter can prep the query string
109 *
110 * @access private called by execute()
111 * @param string an SQL query
112 * @return string
113 */
114 function _prep_query($sql)
115 {
116 return $sql;
117 }
118
119 // --------------------------------------------------------------------
120
121 /**
122 * Begin Transaction
123 *
124 * @access public
125 * @return bool
126 */
127 function trans_begin($test_mode = FALSE)
128 {
129 if ( ! $this->trans_enabled)
130 {
131 return TRUE;
132 }
133
134 // When transactions are nested we only begin/commit/rollback the outermost ones
135 if ($this->_trans_depth > 0)
136 {
137 return TRUE;
138 }
139
140 // Reset the transaction failure flag.
141 // If the $test_mode flag is set to TRUE transactions will be rolled back
142 // even if the queries produce a successful result.
143 $this->_trans_failure = ($test_mode === TRUE) ? TRUE : FALSE;
144
145 return @pg_exec($this->conn_id, "begin");
146 }
147
148 // --------------------------------------------------------------------
149
150 /**
151 * Commit Transaction
152 *
153 * @access public
154 * @return bool
155 */
156 function trans_commit()
157 {
158 if ( ! $this->trans_enabled)
159 {
160 return TRUE;
161 }
162
163 // When transactions are nested we only begin/commit/rollback the outermost ones
164 if ($this->_trans_depth > 0)
165 {
166 return TRUE;
167 }
168
169 return @pg_exec($this->conn_id, "commit");
170 }
171
172 // --------------------------------------------------------------------
173
174 /**
175 * Rollback Transaction
176 *
177 * @access public
178 * @return bool
179 */
180 function trans_rollback()
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, "rollback");
194 }
195
196 // --------------------------------------------------------------------
197
198 /**
199 * Escape String
200 *
201 * @access public
202 * @param string
203 * @return string
204 */
205 function escape_str($str)
206 {
207 return pg_escape_string($str);
208 }
209
210 // --------------------------------------------------------------------
211
212 /**
213 * Affected Rows
214 *
215 * @access public
216 * @return integer
217 */
218 function affected_rows()
219 {
220 return @pg_affected_rows($this->result_id);
221 }
222
223 // --------------------------------------------------------------------
224
225 /**
226 * Insert ID
227 *
228 * @access public
229 * @return integer
230 */
231 function insert_id()
232 {
233 $v = pg_version($this->conn_id);
234 $v = $v['server'];
235
236 $table = func_num_args() > 0 ? func_get_arg(0) : null;
237 $column = func_num_args() > 1 ? func_get_arg(1) : null;
238
239 if ($table == null && $v >= '8.1')
240 {
241 $sql='SELECT LASTVAL() as ins_id';
242 }
243 elseif ($table != null && $column != null && $v >= '8.0')
244 {
245 $sql = sprintf("SELECT pg_get_serial_sequence('%s','%s') as seq", $table, $column);
246 $query = $this->query($sql);
247 $row = $query->row();
248 $sql = sprintf("SELECT CURRVAL('%s') as ins_id", $row->seq);
249 }
250 elseif ($table != null)
251 {
252 // seq_name passed in table parameter
253 $sql = sprintf("SELECT CURRVAL('%s') as ins_id", $table);
254 }
255 else
256 {
257 return pg_last_oid($this->result_id);
258 }
259 $query = $this->query($sql);
260 $row = $query->row();
261 return $row->ins_id;
262 }
263
264 // --------------------------------------------------------------------
265
266 /**
267 * "Count All" query
268 *
269 * Generates a platform-specific query string that counts all records in
270 * the specified database
271 *
272 * @access public
273 * @param string
274 * @return string
275 */
276 function count_all($table = '')
277 {
278 if ($table == '')
279 return '0';
280
281 $query = $this->query('SELECT COUNT(*) AS numrows FROM "'.$this->dbprefix.$table.'"');
282
283 if ($query->num_rows() == 0)
284 return '0';
285
286 $row = $query->row();
287 return $row->numrows;
288 }
admin3dd978f2006-09-30 19:24:45 +0000289
290 // --------------------------------------------------------------------
291
292 /**
293 * List databases
294 *
295 * @access private
296 * @return bool
297 */
298 function _list_databases()
299 {
300 return "SELECT datname FROM pg_database";
301 }
302
303 // --------------------------------------------------------------------
304
305 /**
306 * Show table query
307 *
308 * Generates a platform-specific query string so that the table names can be fetched
309 *
310 * @access private
311 * @return string
312 */
313 function _list_tables()
314 {
315 return "SELECT table_name FROM information_schema.tables WHERE table_schema = 'public'";
316 }
admin7eea4f82006-09-24 18:13:17 +0000317
318 // --------------------------------------------------------------------
319
320 /**
admin9cd4e8e2006-09-25 23:26:25 +0000321 * Show columnn query
322 *
323 * Generates a platform-specific query string so that the column names can be fetched
324 *
325 * @access public
326 * @param string the table name
327 * @return string
328 */
329 function _list_columns($table = '')
330 {
331 return "SELECT column_name FROM information_schema.columns WHERE table_name ='".$this->_escape_table($table)."'";
332 }
333
334 // --------------------------------------------------------------------
335
336 /**
337 * Field data query
338 *
339 * Generates a platform-specific query so that the column data can be retrieved
340 *
341 * @access public
342 * @param string the table name
343 * @return object
344 */
345 function _field_data($table)
346 {
347 return "SELECT * FROM ".$this->_escape_table($table)." LIMIT 1";
348 }
349
350 // --------------------------------------------------------------------
351
352 /**
admin7eea4f82006-09-24 18:13:17 +0000353 * The error message string
354 *
adminbb1d4392006-09-24 20:14:38 +0000355 * @access private
admin7eea4f82006-09-24 18:13:17 +0000356 * @return string
357 */
adminbb1d4392006-09-24 20:14:38 +0000358 function _error_message()
admin7eea4f82006-09-24 18:13:17 +0000359 {
360 return pg_last_error($this->conn_id);
361 }
362
363 // --------------------------------------------------------------------
364
365 /**
366 * The error message number
367 *
adminbb1d4392006-09-24 20:14:38 +0000368 * @access private
admin7eea4f82006-09-24 18:13:17 +0000369 * @return integer
370 */
adminbb1d4392006-09-24 20:14:38 +0000371 function _error_number()
admin7eea4f82006-09-24 18:13:17 +0000372 {
373 return '';
374 }
375
376 // --------------------------------------------------------------------
377
378 /**
379 * Escape Table Name
380 *
381 * This function adds backticks if the table name has a period
382 * in it. Some DBs will get cranky unless periods are escaped.
383 *
adminbb1d4392006-09-24 20:14:38 +0000384 * @access private
admin7eea4f82006-09-24 18:13:17 +0000385 * @param string the table name
386 * @return string
387 */
adminbb1d4392006-09-24 20:14:38 +0000388 function _escape_table($table)
admin7eea4f82006-09-24 18:13:17 +0000389 {
390 if (stristr($table, '.'))
391 {
392 $table = '"'.preg_replace("/\./", '"."', $table).'"';
393 }
394
395 return $table;
396 }
397
398 // --------------------------------------------------------------------
399
400 /**
admin7eea4f82006-09-24 18:13:17 +0000401 * Insert statement
402 *
403 * Generates a platform-specific insert string from the supplied data
404 *
405 * @access public
406 * @param string the table name
407 * @param array the insert keys
408 * @param array the insert values
409 * @return string
410 */
411 function _insert($table, $keys, $values)
412 {
adminbb1d4392006-09-24 20:14:38 +0000413 return "INSERT INTO ".$this->_escape_table($table)." (".implode(', ', $keys).") VALUES (".implode(', ', $values).")";
admin7eea4f82006-09-24 18:13:17 +0000414 }
415
416 // --------------------------------------------------------------------
417
418 /**
419 * Update statement
420 *
421 * Generates a platform-specific update string from the supplied data
422 *
423 * @access public
424 * @param string the table name
425 * @param array the update data
426 * @param array the where clause
427 * @return string
428 */
429 function _update($table, $values, $where)
430 {
431 foreach($values as $key => $val)
432 {
433 $valstr[] = $key." = ".$val;
434 }
435
adminbb1d4392006-09-24 20:14:38 +0000436 return "UPDATE ".$this->_escape_table($table)." SET ".implode(', ', $valstr)." WHERE ".implode(" ", $where);
admin7eea4f82006-09-24 18:13:17 +0000437 }
438
439 // --------------------------------------------------------------------
440
441 /**
442 * Delete statement
443 *
444 * Generates a platform-specific delete string from the supplied data
445 *
446 * @access public
447 * @param string the table name
448 * @param array the where clause
449 * @return string
450 */
451 function _delete($table, $where)
452 {
adminbb1d4392006-09-24 20:14:38 +0000453 return "DELETE FROM ".$this->_escape_table($table)." WHERE ".implode(" ", $where);
admin7eea4f82006-09-24 18:13:17 +0000454 }
adminbb1d4392006-09-24 20:14:38 +0000455
456 // --------------------------------------------------------------------
457
458 /**
459 * Limit string
460 *
461 * Generates a platform-specific LIMIT clause
462 *
463 * @access public
464 * @param string the sql query string
465 * @param integer the number of rows to limit the query to
466 * @param integer the offset value
467 * @return string
468 */
469 function _limit($sql, $limit, $offset)
470 {
471 $sql .= "LIMIT ".$limit;
admin7eea4f82006-09-24 18:13:17 +0000472
adminbb1d4392006-09-24 20:14:38 +0000473 if ($offset > 0)
474 {
475 $sql .= " OFFSET ".$offset;
476 }
477
478 return $sql;
479 }
480
481 // --------------------------------------------------------------------
482
483 /**
484 * Close DB Connection
485 *
486 * @access public
487 * @param resource
488 * @return void
489 */
490 function _close($conn_id)
491 {
492 pg_close($conn_id);
493 }
494
adminbb1d4392006-09-24 20:14:38 +0000495
admin7eea4f82006-09-24 18:13:17 +0000496}
497
498?>