blob: bd5fbac248226cd3db0b382abdfc0ff2037eecd4 [file] [log] [blame]
adminb0dd10f2006-08-25 17:25:49 +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/libraries/database/
30 */
31class CI_DB_postgre 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 {
admin2ed76d52006-09-02 17:34:52 +000041 $port = ($this->port == '') ? '' : " port=".$this->port;
42
43 return pg_connect("host=".$this->hostname.$port." dbname=".$this->database." user=".$this->username." password=".$this->password);
adminb0dd10f2006-08-25 17:25:49 +000044 }
admin2ed76d52006-09-02 17:34:52 +000045
adminb0dd10f2006-08-25 17:25:49 +000046 // --------------------------------------------------------------------
47
48 /**
49 * Persistent database connection
50 *
51 * @access private called by the base class
52 * @return resource
53 */
54 function db_pconnect()
55 {
admin2ed76d52006-09-02 17:34:52 +000056 $port = ($this->port == '') ? '' : " port=".$this->port;
57
58 return pg_pconnect("host=".$this->hostname.$port." dbname=".$this->database." user=".$this->username." password=".$this->password);
adminb0dd10f2006-08-25 17:25:49 +000059 }
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 }
74
75 // --------------------------------------------------------------------
76
77 /**
78 * Execute the query
79 *
80 * @access private called by the base class
81 * @param string an SQL query
82 * @return resource
83 */
admine885d782006-09-23 20:25:05 +000084 function _execute($sql)
adminb0dd10f2006-08-25 17:25:49 +000085 {
86 $sql = $this->_prep_query($sql);
87 return @pg_query($this->conn_id, $sql);
88 }
89
90 // --------------------------------------------------------------------
91
92 /**
93 * Prep the query
94 *
95 * If needed, each database adapter can prep the query string
96 *
97 * @access private called by execute()
98 * @param string an SQL query
99 * @return string
100 */
adminb071bb52006-08-26 19:28:37 +0000101 function _prep_query($sql)
adminb0dd10f2006-08-25 17:25:49 +0000102 {
103 return $sql;
104 }
admine885d782006-09-23 20:25:05 +0000105
106 // --------------------------------------------------------------------
107
108 /**
109 * Begin Transaction
110 *
111 * @access public
112 * @return bool
113 */
admin8b180be2006-09-24 01:12:22 +0000114 function trans_begin($test_mode = FALSE)
admine885d782006-09-23 20:25:05 +0000115 {
116 if ( ! $this->trans_enabled)
117 {
118 return TRUE;
119 }
120
121 // When transactions are nested we only begin/commit/rollback the outermost ones
122 if ($this->_trans_depth > 0)
123 {
124 return TRUE;
125 }
126
admin8b180be2006-09-24 01:12:22 +0000127 // Reset the transaction failure flag.
128 // If the $test_mode flag is set to TRUE transactions will be rolled back
129 // even if the queries produce a successful result.
130 $this->_trans_failure = ($test_mode === TRUE) ? TRUE : FALSE;
131
admine885d782006-09-23 20:25:05 +0000132 return @pg_exec($this->conn_id, "begin");
133 }
134
135 // --------------------------------------------------------------------
136
137 /**
138 * Commit Transaction
139 *
140 * @access public
141 * @return bool
142 */
143 function trans_commit()
144 {
145 if ( ! $this->trans_enabled)
146 {
147 return TRUE;
148 }
149
150 // When transactions are nested we only begin/commit/rollback the outermost ones
151 if ($this->_trans_depth > 0)
152 {
153 return TRUE;
154 }
155
156 return @pg_exec($this->conn_id, "commit");
157 }
158
159 // --------------------------------------------------------------------
160
161 /**
162 * Rollback Transaction
163 *
164 * @access public
165 * @return bool
166 */
167 function trans_rollback()
168 {
169 if ( ! $this->trans_enabled)
170 {
171 return TRUE;
172 }
173
174 // When transactions are nested we only begin/commit/rollback the outermost ones
175 if ($this->_trans_depth > 0)
176 {
177 return TRUE;
178 }
179
180 return @pg_exec($this->conn_id, "rollback");
181 }
182
adminb0dd10f2006-08-25 17:25:49 +0000183 // --------------------------------------------------------------------
184
185 /**
186 * Escape String
187 *
188 * @access public
189 * @param string
190 * @return string
191 */
192 function escape_str($str)
193 {
adminb0dd10f2006-08-25 17:25:49 +0000194 return pg_escape_string($str);
195 }
196
197 // --------------------------------------------------------------------
198
199 /**
200 * Close DB Connection
201 *
202 * @access public
203 * @param resource
204 * @return void
205 */
206 function destroy($conn_id)
207 {
208 pg_close($conn_id);
209 }
210
211 // --------------------------------------------------------------------
212
213 /**
214 * Affected Rows
215 *
216 * @access public
217 * @return integer
218 */
219 function affected_rows()
220 {
221 return @pg_affected_rows($this->result_id);
222 }
223
224 // --------------------------------------------------------------------
225
226 /**
227 * Insert ID
228 *
229 * @access public
230 * @return integer
231 */
232 function insert_id()
233 {
admin1066dcb2006-09-06 01:55:56 +0000234 $v = pg_version($this->conn_id);
235 $v = $v['server'];
236
237 $table = func_num_args() > 0 ? func_get_arg(0) : null;
238 $column = func_num_args() > 1 ? func_get_arg(1) : null;
239
240 if ($table == null && $v >= '8.1')
241 {
242 $sql='SELECT LASTVAL() as ins_id';
243 }
244 elseif ($table != null && $column != null && $v >= '8.0')
245 {
246 $sql = sprintf("SELECT pg_get_serial_sequence('%s','%s') as seq", $table, $column);
247 $query = $this->query($sql);
248 $row = $query->row();
249 $sql = sprintf("SELECT CURRVAL('%s') as ins_id", $row->seq);
250 }
251 elseif ($table != null)
252 {
253 // seq_name passed in table parameter
254 $sql = sprintf("SELECT CURRVAL('%s') as ins_id", $table);
255 }
256 else
257 {
258 return pg_last_oid($this->result_id);
259 }
260 $query = $this->query($sql);
261 $row = $query->row();
262 return $row->ins_id;
adminb0dd10f2006-08-25 17:25:49 +0000263 }
264
265 // --------------------------------------------------------------------
266
267 /**
268 * "Count All" query
269 *
270 * Generates a platform-specific query string that counts all records in
271 * the specified database
272 *
273 * @access public
274 * @param string
275 * @return string
276 */
277 function count_all($table = '')
278 {
279 if ($table == '')
280 return '0';
281
admin5e7ac0e2006-09-03 18:29:51 +0000282 $query = $this->query('SELECT COUNT(*) AS numrows FROM "'.$this->dbprefix.$table.'"');
adminb0dd10f2006-08-25 17:25:49 +0000283
284 if ($query->num_rows() == 0)
285 return '0';
286
287 $row = $query->row();
288 return $row->numrows;
289 }
290
291 // --------------------------------------------------------------------
292
293 /**
294 * The error message string
295 *
296 * @access public
297 * @return string
298 */
299 function error_message()
300 {
301 return pg_last_error($this->conn_id);
302 }
303
304 // --------------------------------------------------------------------
305
306 /**
307 * The error message number
308 *
309 * @access public
310 * @return integer
311 */
312 function error_number()
313 {
314 return '';
315 }
316
317 // --------------------------------------------------------------------
318
319 /**
320 * Escape Table Name
321 *
322 * This function adds backticks if the table name has a period
admineb6db842006-09-02 02:39:45 +0000323 * in it. Some DBs will get cranky unless periods are escaped.
adminb0dd10f2006-08-25 17:25:49 +0000324 *
325 * @access public
326 * @param string the table name
327 * @return string
328 */
329 function escape_table($table)
330 {
331 if (stristr($table, '.'))
332 {
admin5e7ac0e2006-09-03 18:29:51 +0000333 $table = '"'.preg_replace("/\./", '"."', $table).'"';
adminb0dd10f2006-08-25 17:25:49 +0000334 }
335
336 return $table;
337 }
338
339 // --------------------------------------------------------------------
340
341 /**
342 * Field data query
343 *
344 * Generates a platform-specific query so that the column data can be retrieved
345 *
346 * @access public
347 * @param string the table name
348 * @return object
349 */
350 function _field_data($table)
351 {
352 $sql = "SELECT * FROM ".$this->escape_table($table)." LIMIT 1";
353 $query = $this->query($sql);
354 return $query->field_data();
355 }
356
357 // --------------------------------------------------------------------
358
359 /**
360 * Insert statement
361 *
362 * Generates a platform-specific insert string from the supplied data
363 *
364 * @access public
365 * @param string the table name
366 * @param array the insert keys
367 * @param array the insert values
368 * @return string
369 */
370 function _insert($table, $keys, $values)
371 {
372 return "INSERT INTO ".$this->escape_table($table)." (".implode(', ', $keys).") VALUES (".implode(', ', $values).")";
373 }
374
375 // --------------------------------------------------------------------
376
377 /**
378 * Update statement
379 *
380 * Generates a platform-specific update string from the supplied data
381 *
382 * @access public
383 * @param string the table name
384 * @param array the update data
385 * @param array the where clause
386 * @return string
387 */
388 function _update($table, $values, $where)
389 {
390 foreach($values as $key => $val)
391 {
392 $valstr[] = $key." = ".$val;
393 }
394
395 return "UPDATE ".$this->escape_table($table)." SET ".implode(', ', $valstr)." WHERE ".implode(" ", $where);
396 }
397
398 // --------------------------------------------------------------------
399
400 /**
401 * Delete statement
402 *
403 * Generates a platform-specific delete string from the supplied data
404 *
405 * @access public
406 * @param string the table name
407 * @param array the where clause
408 * @return string
409 */
410 function _delete($table, $where)
411 {
412 return "DELETE FROM ".$this->escape_table($table)." WHERE ".implode(" ", $where);
413 }
414
415 // --------------------------------------------------------------------
416
417 /**
418 * Version number query string
419 *
420 * @access public
421 * @return string
422 */
423 function _version()
424 {
425 return "SELECT version() AS ver";
426 }
427
428 /**
429 * Show table query
430 *
431 * Generates a platform-specific query string so that the table names can be fetched
432 *
433 * @access public
434 * @return string
435 */
436 function _show_tables()
437 {
438 return "SELECT table_name FROM information_schema.tables WHERE table_schema = 'public'";
439 }
440
441 // --------------------------------------------------------------------
442
443 /**
444 * Show columnn query
445 *
446 * Generates a platform-specific query string so that the column names can be fetched
447 *
448 * @access public
449 * @param string the table name
450 * @return string
451 */
452 function _show_columns($table = '')
453 {
454 return "SELECT column_name FROM information_schema.columns WHERE table_name ='".$this->escape_table($table)."'";
455 }
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
486/**
487 * Postgres Result Class
488 *
489 * This class extends the parent result class: CI_DB_result
490 *
491 * @category Database
492 * @author Rick Ellis
493 * @link http://www.codeigniter.com/user_guide/libraries/database/
494 */
495class CI_DB_postgre_result extends CI_DB_result {
496
497 /**
498 * Number of rows in the result set
499 *
500 * @access public
501 * @return integer
502 */
503 function num_rows()
504 {
505 return @pg_num_rows($this->result_id);
506 }
507
508 // --------------------------------------------------------------------
509
510 /**
511 * Number of fields in the result set
512 *
513 * @access public
514 * @return integer
515 */
516 function num_fields()
517 {
518 return @pg_num_fields($this->result_id);
519 }
520
521 // --------------------------------------------------------------------
522
523 /**
524 * Field data
525 *
526 * Generates an array of objects containing field meta-data
527 *
528 * @access public
529 * @return array
530 */
531 function field_data()
532 {
533 $retval = array();
534 for ($i = 0; $i < $this->num_fields(); $i++)
535 {
admine348efb2006-09-20 21:13:26 +0000536 $F = new stdClass();
adminb0dd10f2006-08-25 17:25:49 +0000537 $F->name = pg_field_name($this->result_id, $i);
538 $F->type = pg_field_type($this->result_id, $i);
539 $F->max_length = pg_field_size($this->result_id, $i);
adminb071bb52006-08-26 19:28:37 +0000540 $F->primary_key = $i == 0;
adminb0dd10f2006-08-25 17:25:49 +0000541 $F->default = '';
542
543 $retval[] = $F;
544 }
545
546 return $retval;
547 }
548
549 // --------------------------------------------------------------------
550
551 /**
552 * Result - associative array
553 *
554 * Returns the result set as an array
555 *
556 * @access private
557 * @return array
558 */
559 function _fetch_assoc()
560 {
561 return pg_fetch_assoc($this->result_id);
562 }
563
564 // --------------------------------------------------------------------
565
566 /**
567 * Result - object
568 *
569 * Returns the result set as an object
570 *
571 * @access private
572 * @return object
573 */
574 function _fetch_object()
575 {
576 return pg_fetch_object($this->result_id);
577 }
578
579}
580
581?>