blob: fe7c704f953f54e8f9dcdf3272e80cb97208fc41 [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 */
114 function trans_begin()
115 {
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
127 return @pg_exec($this->conn_id, "begin");
128 }
129
130 // --------------------------------------------------------------------
131
132 /**
133 * Commit Transaction
134 *
135 * @access public
136 * @return bool
137 */
138 function trans_commit()
139 {
140 if ( ! $this->trans_enabled)
141 {
142 return TRUE;
143 }
144
145 // When transactions are nested we only begin/commit/rollback the outermost ones
146 if ($this->_trans_depth > 0)
147 {
148 return TRUE;
149 }
150
151 return @pg_exec($this->conn_id, "commit");
152 }
153
154 // --------------------------------------------------------------------
155
156 /**
157 * Rollback Transaction
158 *
159 * @access public
160 * @return bool
161 */
162 function trans_rollback()
163 {
164 if ( ! $this->trans_enabled)
165 {
166 return TRUE;
167 }
168
169 // When transactions are nested we only begin/commit/rollback the outermost ones
170 if ($this->_trans_depth > 0)
171 {
172 return TRUE;
173 }
174
175 return @pg_exec($this->conn_id, "rollback");
176 }
177
adminb0dd10f2006-08-25 17:25:49 +0000178 // --------------------------------------------------------------------
179
180 /**
181 * Escape String
182 *
183 * @access public
184 * @param string
185 * @return string
186 */
187 function escape_str($str)
188 {
adminb0dd10f2006-08-25 17:25:49 +0000189 return pg_escape_string($str);
190 }
191
192 // --------------------------------------------------------------------
193
194 /**
195 * Close DB Connection
196 *
197 * @access public
198 * @param resource
199 * @return void
200 */
201 function destroy($conn_id)
202 {
203 pg_close($conn_id);
204 }
205
206 // --------------------------------------------------------------------
207
208 /**
209 * Affected Rows
210 *
211 * @access public
212 * @return integer
213 */
214 function affected_rows()
215 {
216 return @pg_affected_rows($this->result_id);
217 }
218
219 // --------------------------------------------------------------------
220
221 /**
222 * Insert ID
223 *
224 * @access public
225 * @return integer
226 */
227 function insert_id()
228 {
admin1066dcb2006-09-06 01:55:56 +0000229 $v = pg_version($this->conn_id);
230 $v = $v['server'];
231
232 $table = func_num_args() > 0 ? func_get_arg(0) : null;
233 $column = func_num_args() > 1 ? func_get_arg(1) : null;
234
235 if ($table == null && $v >= '8.1')
236 {
237 $sql='SELECT LASTVAL() as ins_id';
238 }
239 elseif ($table != null && $column != null && $v >= '8.0')
240 {
241 $sql = sprintf("SELECT pg_get_serial_sequence('%s','%s') as seq", $table, $column);
242 $query = $this->query($sql);
243 $row = $query->row();
244 $sql = sprintf("SELECT CURRVAL('%s') as ins_id", $row->seq);
245 }
246 elseif ($table != null)
247 {
248 // seq_name passed in table parameter
249 $sql = sprintf("SELECT CURRVAL('%s') as ins_id", $table);
250 }
251 else
252 {
253 return pg_last_oid($this->result_id);
254 }
255 $query = $this->query($sql);
256 $row = $query->row();
257 return $row->ins_id;
adminb0dd10f2006-08-25 17:25:49 +0000258 }
259
260 // --------------------------------------------------------------------
261
262 /**
263 * "Count All" query
264 *
265 * Generates a platform-specific query string that counts all records in
266 * the specified database
267 *
268 * @access public
269 * @param string
270 * @return string
271 */
272 function count_all($table = '')
273 {
274 if ($table == '')
275 return '0';
276
admin5e7ac0e2006-09-03 18:29:51 +0000277 $query = $this->query('SELECT COUNT(*) AS numrows FROM "'.$this->dbprefix.$table.'"');
adminb0dd10f2006-08-25 17:25:49 +0000278
279 if ($query->num_rows() == 0)
280 return '0';
281
282 $row = $query->row();
283 return $row->numrows;
284 }
285
286 // --------------------------------------------------------------------
287
288 /**
289 * The error message string
290 *
291 * @access public
292 * @return string
293 */
294 function error_message()
295 {
296 return pg_last_error($this->conn_id);
297 }
298
299 // --------------------------------------------------------------------
300
301 /**
302 * The error message number
303 *
304 * @access public
305 * @return integer
306 */
307 function error_number()
308 {
309 return '';
310 }
311
312 // --------------------------------------------------------------------
313
314 /**
315 * Escape Table Name
316 *
317 * This function adds backticks if the table name has a period
admineb6db842006-09-02 02:39:45 +0000318 * in it. Some DBs will get cranky unless periods are escaped.
adminb0dd10f2006-08-25 17:25:49 +0000319 *
320 * @access public
321 * @param string the table name
322 * @return string
323 */
324 function escape_table($table)
325 {
326 if (stristr($table, '.'))
327 {
admin5e7ac0e2006-09-03 18:29:51 +0000328 $table = '"'.preg_replace("/\./", '"."', $table).'"';
adminb0dd10f2006-08-25 17:25:49 +0000329 }
330
331 return $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 $sql = "SELECT * FROM ".$this->escape_table($table)." LIMIT 1";
348 $query = $this->query($sql);
349 return $query->field_data();
350 }
351
352 // --------------------------------------------------------------------
353
354 /**
355 * Insert statement
356 *
357 * Generates a platform-specific insert string from the supplied data
358 *
359 * @access public
360 * @param string the table name
361 * @param array the insert keys
362 * @param array the insert values
363 * @return string
364 */
365 function _insert($table, $keys, $values)
366 {
367 return "INSERT INTO ".$this->escape_table($table)." (".implode(', ', $keys).") VALUES (".implode(', ', $values).")";
368 }
369
370 // --------------------------------------------------------------------
371
372 /**
373 * Update statement
374 *
375 * Generates a platform-specific update string from the supplied data
376 *
377 * @access public
378 * @param string the table name
379 * @param array the update data
380 * @param array the where clause
381 * @return string
382 */
383 function _update($table, $values, $where)
384 {
385 foreach($values as $key => $val)
386 {
387 $valstr[] = $key." = ".$val;
388 }
389
390 return "UPDATE ".$this->escape_table($table)." SET ".implode(', ', $valstr)." WHERE ".implode(" ", $where);
391 }
392
393 // --------------------------------------------------------------------
394
395 /**
396 * Delete statement
397 *
398 * Generates a platform-specific delete string from the supplied data
399 *
400 * @access public
401 * @param string the table name
402 * @param array the where clause
403 * @return string
404 */
405 function _delete($table, $where)
406 {
407 return "DELETE FROM ".$this->escape_table($table)." WHERE ".implode(" ", $where);
408 }
409
410 // --------------------------------------------------------------------
411
412 /**
413 * Version number query string
414 *
415 * @access public
416 * @return string
417 */
418 function _version()
419 {
420 return "SELECT version() AS ver";
421 }
422
423 /**
424 * Show table query
425 *
426 * Generates a platform-specific query string so that the table names can be fetched
427 *
428 * @access public
429 * @return string
430 */
431 function _show_tables()
432 {
433 return "SELECT table_name FROM information_schema.tables WHERE table_schema = 'public'";
434 }
435
436 // --------------------------------------------------------------------
437
438 /**
439 * Show columnn query
440 *
441 * Generates a platform-specific query string so that the column names can be fetched
442 *
443 * @access public
444 * @param string the table name
445 * @return string
446 */
447 function _show_columns($table = '')
448 {
449 return "SELECT column_name FROM information_schema.columns WHERE table_name ='".$this->escape_table($table)."'";
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
481/**
482 * Postgres Result Class
483 *
484 * This class extends the parent result class: CI_DB_result
485 *
486 * @category Database
487 * @author Rick Ellis
488 * @link http://www.codeigniter.com/user_guide/libraries/database/
489 */
490class CI_DB_postgre_result extends CI_DB_result {
491
492 /**
493 * Number of rows in the result set
494 *
495 * @access public
496 * @return integer
497 */
498 function num_rows()
499 {
500 return @pg_num_rows($this->result_id);
501 }
502
503 // --------------------------------------------------------------------
504
505 /**
506 * Number of fields in the result set
507 *
508 * @access public
509 * @return integer
510 */
511 function num_fields()
512 {
513 return @pg_num_fields($this->result_id);
514 }
515
516 // --------------------------------------------------------------------
517
518 /**
519 * Field data
520 *
521 * Generates an array of objects containing field meta-data
522 *
523 * @access public
524 * @return array
525 */
526 function field_data()
527 {
528 $retval = array();
529 for ($i = 0; $i < $this->num_fields(); $i++)
530 {
admine348efb2006-09-20 21:13:26 +0000531 $F = new stdClass();
adminb0dd10f2006-08-25 17:25:49 +0000532 $F->name = pg_field_name($this->result_id, $i);
533 $F->type = pg_field_type($this->result_id, $i);
534 $F->max_length = pg_field_size($this->result_id, $i);
adminb071bb52006-08-26 19:28:37 +0000535 $F->primary_key = $i == 0;
adminb0dd10f2006-08-25 17:25:49 +0000536 $F->default = '';
537
538 $retval[] = $F;
539 }
540
541 return $retval;
542 }
543
544 // --------------------------------------------------------------------
545
546 /**
547 * Result - associative array
548 *
549 * Returns the result set as an array
550 *
551 * @access private
552 * @return array
553 */
554 function _fetch_assoc()
555 {
556 return pg_fetch_assoc($this->result_id);
557 }
558
559 // --------------------------------------------------------------------
560
561 /**
562 * Result - object
563 *
564 * Returns the result set as an object
565 *
566 * @access private
567 * @return object
568 */
569 function _fetch_object()
570 {
571 return pg_fetch_object($this->result_id);
572 }
573
574}
575
576?>