blob: 57ef179d91db6db789b6f6b3d1250e0160974522 [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 */
84 function execute($sql)
85 {
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 }
105
106 // --------------------------------------------------------------------
107
108 /**
109 * Escape String
110 *
111 * @access public
112 * @param string
113 * @return string
114 */
115 function escape_str($str)
116 {
adminb0dd10f2006-08-25 17:25:49 +0000117 return pg_escape_string($str);
118 }
119
120 // --------------------------------------------------------------------
121
122 /**
123 * Close DB Connection
124 *
125 * @access public
126 * @param resource
127 * @return void
128 */
129 function destroy($conn_id)
130 {
131 pg_close($conn_id);
132 }
133
134 // --------------------------------------------------------------------
135
136 /**
137 * Affected Rows
138 *
139 * @access public
140 * @return integer
141 */
142 function affected_rows()
143 {
144 return @pg_affected_rows($this->result_id);
145 }
146
147 // --------------------------------------------------------------------
148
149 /**
150 * Insert ID
151 *
152 * @access public
153 * @return integer
154 */
155 function insert_id()
156 {
admin1066dcb2006-09-06 01:55:56 +0000157 $v = pg_version($this->conn_id);
158 $v = $v['server'];
159
160 $table = func_num_args() > 0 ? func_get_arg(0) : null;
161 $column = func_num_args() > 1 ? func_get_arg(1) : null;
162
163 if ($table == null && $v >= '8.1')
164 {
165 $sql='SELECT LASTVAL() as ins_id';
166 }
167 elseif ($table != null && $column != null && $v >= '8.0')
168 {
169 $sql = sprintf("SELECT pg_get_serial_sequence('%s','%s') as seq", $table, $column);
170 $query = $this->query($sql);
171 $row = $query->row();
172 $sql = sprintf("SELECT CURRVAL('%s') as ins_id", $row->seq);
173 }
174 elseif ($table != null)
175 {
176 // seq_name passed in table parameter
177 $sql = sprintf("SELECT CURRVAL('%s') as ins_id", $table);
178 }
179 else
180 {
181 return pg_last_oid($this->result_id);
182 }
183 $query = $this->query($sql);
184 $row = $query->row();
185 return $row->ins_id;
adminb0dd10f2006-08-25 17:25:49 +0000186 }
187
188 // --------------------------------------------------------------------
189
190 /**
191 * "Count All" query
192 *
193 * Generates a platform-specific query string that counts all records in
194 * the specified database
195 *
196 * @access public
197 * @param string
198 * @return string
199 */
200 function count_all($table = '')
201 {
202 if ($table == '')
203 return '0';
204
admin5e7ac0e2006-09-03 18:29:51 +0000205 $query = $this->query('SELECT COUNT(*) AS numrows FROM "'.$this->dbprefix.$table.'"');
adminb0dd10f2006-08-25 17:25:49 +0000206
207 if ($query->num_rows() == 0)
208 return '0';
209
210 $row = $query->row();
211 return $row->numrows;
212 }
213
214 // --------------------------------------------------------------------
215
216 /**
217 * The error message string
218 *
219 * @access public
220 * @return string
221 */
222 function error_message()
223 {
224 return pg_last_error($this->conn_id);
225 }
226
227 // --------------------------------------------------------------------
228
229 /**
230 * The error message number
231 *
232 * @access public
233 * @return integer
234 */
235 function error_number()
236 {
237 return '';
238 }
239
240 // --------------------------------------------------------------------
241
242 /**
243 * Escape Table Name
244 *
245 * This function adds backticks if the table name has a period
admineb6db842006-09-02 02:39:45 +0000246 * in it. Some DBs will get cranky unless periods are escaped.
adminb0dd10f2006-08-25 17:25:49 +0000247 *
248 * @access public
249 * @param string the table name
250 * @return string
251 */
252 function escape_table($table)
253 {
254 if (stristr($table, '.'))
255 {
admin5e7ac0e2006-09-03 18:29:51 +0000256 $table = '"'.preg_replace("/\./", '"."', $table).'"';
adminb0dd10f2006-08-25 17:25:49 +0000257 }
258
259 return $table;
260 }
261
262 // --------------------------------------------------------------------
263
264 /**
265 * Field data query
266 *
267 * Generates a platform-specific query so that the column data can be retrieved
268 *
269 * @access public
270 * @param string the table name
271 * @return object
272 */
273 function _field_data($table)
274 {
275 $sql = "SELECT * FROM ".$this->escape_table($table)." LIMIT 1";
276 $query = $this->query($sql);
277 return $query->field_data();
278 }
279
280 // --------------------------------------------------------------------
281
282 /**
283 * Insert statement
284 *
285 * Generates a platform-specific insert string from the supplied data
286 *
287 * @access public
288 * @param string the table name
289 * @param array the insert keys
290 * @param array the insert values
291 * @return string
292 */
293 function _insert($table, $keys, $values)
294 {
295 return "INSERT INTO ".$this->escape_table($table)." (".implode(', ', $keys).") VALUES (".implode(', ', $values).")";
296 }
297
298 // --------------------------------------------------------------------
299
300 /**
301 * Update statement
302 *
303 * Generates a platform-specific update string from the supplied data
304 *
305 * @access public
306 * @param string the table name
307 * @param array the update data
308 * @param array the where clause
309 * @return string
310 */
311 function _update($table, $values, $where)
312 {
313 foreach($values as $key => $val)
314 {
315 $valstr[] = $key." = ".$val;
316 }
317
318 return "UPDATE ".$this->escape_table($table)." SET ".implode(', ', $valstr)." WHERE ".implode(" ", $where);
319 }
320
321 // --------------------------------------------------------------------
322
323 /**
324 * Delete statement
325 *
326 * Generates a platform-specific delete string from the supplied data
327 *
328 * @access public
329 * @param string the table name
330 * @param array the where clause
331 * @return string
332 */
333 function _delete($table, $where)
334 {
335 return "DELETE FROM ".$this->escape_table($table)." WHERE ".implode(" ", $where);
336 }
337
338 // --------------------------------------------------------------------
339
340 /**
341 * Version number query string
342 *
343 * @access public
344 * @return string
345 */
346 function _version()
347 {
348 return "SELECT version() AS ver";
349 }
350
351 /**
352 * Show table query
353 *
354 * Generates a platform-specific query string so that the table names can be fetched
355 *
356 * @access public
357 * @return string
358 */
359 function _show_tables()
360 {
361 return "SELECT table_name FROM information_schema.tables WHERE table_schema = 'public'";
362 }
363
364 // --------------------------------------------------------------------
365
366 /**
367 * Show columnn query
368 *
369 * Generates a platform-specific query string so that the column names can be fetched
370 *
371 * @access public
372 * @param string the table name
373 * @return string
374 */
375 function _show_columns($table = '')
376 {
377 return "SELECT column_name FROM information_schema.columns WHERE table_name ='".$this->escape_table($table)."'";
378 }
379
380 // --------------------------------------------------------------------
381
382 /**
383 * Limit string
384 *
385 * Generates a platform-specific LIMIT clause
386 *
387 * @access public
388 * @param string the sql query string
389 * @param integer the number of rows to limit the query to
390 * @param integer the offset value
391 * @return string
392 */
393 function _limit($sql, $limit, $offset)
394 {
395 $sql .= "LIMIT ".$limit;
396
397 if ($offset > 0)
398 {
399 $sql .= " OFFSET ".$offset;
400 }
401
402 return $sql;
403 }
404
405}
406
407
408
409/**
410 * Postgres Result Class
411 *
412 * This class extends the parent result class: CI_DB_result
413 *
414 * @category Database
415 * @author Rick Ellis
416 * @link http://www.codeigniter.com/user_guide/libraries/database/
417 */
418class CI_DB_postgre_result extends CI_DB_result {
419
420 /**
421 * Number of rows in the result set
422 *
423 * @access public
424 * @return integer
425 */
426 function num_rows()
427 {
428 return @pg_num_rows($this->result_id);
429 }
430
431 // --------------------------------------------------------------------
432
433 /**
434 * Number of fields in the result set
435 *
436 * @access public
437 * @return integer
438 */
439 function num_fields()
440 {
441 return @pg_num_fields($this->result_id);
442 }
443
444 // --------------------------------------------------------------------
445
446 /**
447 * Field data
448 *
449 * Generates an array of objects containing field meta-data
450 *
451 * @access public
452 * @return array
453 */
454 function field_data()
455 {
456 $retval = array();
457 for ($i = 0; $i < $this->num_fields(); $i++)
458 {
admine348efb2006-09-20 21:13:26 +0000459 $F = new stdClass();
adminb0dd10f2006-08-25 17:25:49 +0000460 $F->name = pg_field_name($this->result_id, $i);
461 $F->type = pg_field_type($this->result_id, $i);
462 $F->max_length = pg_field_size($this->result_id, $i);
adminb071bb52006-08-26 19:28:37 +0000463 $F->primary_key = $i == 0;
adminb0dd10f2006-08-25 17:25:49 +0000464 $F->default = '';
465
466 $retval[] = $F;
467 }
468
469 return $retval;
470 }
471
472 // --------------------------------------------------------------------
473
474 /**
475 * Result - associative array
476 *
477 * Returns the result set as an array
478 *
479 * @access private
480 * @return array
481 */
482 function _fetch_assoc()
483 {
484 return pg_fetch_assoc($this->result_id);
485 }
486
487 // --------------------------------------------------------------------
488
489 /**
490 * Result - object
491 *
492 * Returns the result set as an object
493 *
494 * @access private
495 * @return object
496 */
497 function _fetch_object()
498 {
499 return pg_fetch_object($this->result_id);
500 }
501
502}
503
504?>