blob: 673dea3358c161827fcb16be7af5f42fca652767 [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 {
157 return pg_last_oid($this->result_id);
158 }
159
160 // --------------------------------------------------------------------
161
162 /**
163 * "Count All" query
164 *
165 * Generates a platform-specific query string that counts all records in
166 * the specified database
167 *
168 * @access public
169 * @param string
170 * @return string
171 */
172 function count_all($table = '')
173 {
174 if ($table == '')
175 return '0';
176
adminb071bb52006-08-26 19:28:37 +0000177 $query = $this->query("SELECT COUNT(*) AS numrows FROM ".$this->dbprefix.$table."");
adminb0dd10f2006-08-25 17:25:49 +0000178
179 if ($query->num_rows() == 0)
180 return '0';
181
182 $row = $query->row();
183 return $row->numrows;
184 }
185
186 // --------------------------------------------------------------------
187
188 /**
189 * The error message string
190 *
191 * @access public
192 * @return string
193 */
194 function error_message()
195 {
196 return pg_last_error($this->conn_id);
197 }
198
199 // --------------------------------------------------------------------
200
201 /**
202 * The error message number
203 *
204 * @access public
205 * @return integer
206 */
207 function error_number()
208 {
209 return '';
210 }
211
212 // --------------------------------------------------------------------
213
214 /**
215 * Escape Table Name
216 *
217 * This function adds backticks if the table name has a period
admineb6db842006-09-02 02:39:45 +0000218 * in it. Some DBs will get cranky unless periods are escaped.
219 * NOT NEEDED FOR POSTGRE
adminb0dd10f2006-08-25 17:25:49 +0000220 *
221 * @access public
222 * @param string the table name
223 * @return string
224 */
225 function escape_table($table)
226 {
admineb6db842006-09-02 02:39:45 +0000227 /*
adminb0dd10f2006-08-25 17:25:49 +0000228 if (stristr($table, '.'))
229 {
admineb6db842006-09-02 02:39:45 +0000230 $table = preg_replace("/\./", "`.`", $table);
adminb0dd10f2006-08-25 17:25:49 +0000231 }
admineb6db842006-09-02 02:39:45 +0000232 */
adminb0dd10f2006-08-25 17:25:49 +0000233
234 return $table;
235 }
236
237 // --------------------------------------------------------------------
238
239 /**
240 * Field data query
241 *
242 * Generates a platform-specific query so that the column data can be retrieved
243 *
244 * @access public
245 * @param string the table name
246 * @return object
247 */
248 function _field_data($table)
249 {
250 $sql = "SELECT * FROM ".$this->escape_table($table)." LIMIT 1";
251 $query = $this->query($sql);
252 return $query->field_data();
253 }
254
255 // --------------------------------------------------------------------
256
257 /**
258 * Insert statement
259 *
260 * Generates a platform-specific insert string from the supplied data
261 *
262 * @access public
263 * @param string the table name
264 * @param array the insert keys
265 * @param array the insert values
266 * @return string
267 */
268 function _insert($table, $keys, $values)
269 {
270 return "INSERT INTO ".$this->escape_table($table)." (".implode(', ', $keys).") VALUES (".implode(', ', $values).")";
271 }
272
273 // --------------------------------------------------------------------
274
275 /**
276 * Update statement
277 *
278 * Generates a platform-specific update string from the supplied data
279 *
280 * @access public
281 * @param string the table name
282 * @param array the update data
283 * @param array the where clause
284 * @return string
285 */
286 function _update($table, $values, $where)
287 {
288 foreach($values as $key => $val)
289 {
290 $valstr[] = $key." = ".$val;
291 }
292
293 return "UPDATE ".$this->escape_table($table)." SET ".implode(', ', $valstr)." WHERE ".implode(" ", $where);
294 }
295
296 // --------------------------------------------------------------------
297
298 /**
299 * Delete statement
300 *
301 * Generates a platform-specific delete string from the supplied data
302 *
303 * @access public
304 * @param string the table name
305 * @param array the where clause
306 * @return string
307 */
308 function _delete($table, $where)
309 {
310 return "DELETE FROM ".$this->escape_table($table)." WHERE ".implode(" ", $where);
311 }
312
313 // --------------------------------------------------------------------
314
315 /**
316 * Version number query string
317 *
318 * @access public
319 * @return string
320 */
321 function _version()
322 {
323 return "SELECT version() AS ver";
324 }
325
326 /**
327 * Show table query
328 *
329 * Generates a platform-specific query string so that the table names can be fetched
330 *
331 * @access public
332 * @return string
333 */
334 function _show_tables()
335 {
336 return "SELECT table_name FROM information_schema.tables WHERE table_schema = 'public'";
337 }
338
339 // --------------------------------------------------------------------
340
341 /**
342 * Show columnn query
343 *
344 * Generates a platform-specific query string so that the column names can be fetched
345 *
346 * @access public
347 * @param string the table name
348 * @return string
349 */
350 function _show_columns($table = '')
351 {
352 return "SELECT column_name FROM information_schema.columns WHERE table_name ='".$this->escape_table($table)."'";
353 }
354
355 // --------------------------------------------------------------------
356
357 /**
358 * Limit string
359 *
360 * Generates a platform-specific LIMIT clause
361 *
362 * @access public
363 * @param string the sql query string
364 * @param integer the number of rows to limit the query to
365 * @param integer the offset value
366 * @return string
367 */
368 function _limit($sql, $limit, $offset)
369 {
370 $sql .= "LIMIT ".$limit;
371
372 if ($offset > 0)
373 {
374 $sql .= " OFFSET ".$offset;
375 }
376
377 return $sql;
378 }
379
380}
381
382
383
384/**
385 * Postgres Result Class
386 *
387 * This class extends the parent result class: CI_DB_result
388 *
389 * @category Database
390 * @author Rick Ellis
391 * @link http://www.codeigniter.com/user_guide/libraries/database/
392 */
393class CI_DB_postgre_result extends CI_DB_result {
394
395 /**
396 * Number of rows in the result set
397 *
398 * @access public
399 * @return integer
400 */
401 function num_rows()
402 {
403 return @pg_num_rows($this->result_id);
404 }
405
406 // --------------------------------------------------------------------
407
408 /**
409 * Number of fields in the result set
410 *
411 * @access public
412 * @return integer
413 */
414 function num_fields()
415 {
416 return @pg_num_fields($this->result_id);
417 }
418
419 // --------------------------------------------------------------------
420
421 /**
422 * Field data
423 *
424 * Generates an array of objects containing field meta-data
425 *
426 * @access public
427 * @return array
428 */
429 function field_data()
430 {
431 $retval = array();
432 for ($i = 0; $i < $this->num_fields(); $i++)
433 {
434 $F = new CI_DB_field();
435 $F->name = pg_field_name($this->result_id, $i);
436 $F->type = pg_field_type($this->result_id, $i);
437 $F->max_length = pg_field_size($this->result_id, $i);
adminb071bb52006-08-26 19:28:37 +0000438 $F->primary_key = $i == 0;
adminb0dd10f2006-08-25 17:25:49 +0000439 $F->default = '';
440
441 $retval[] = $F;
442 }
443
444 return $retval;
445 }
446
447 // --------------------------------------------------------------------
448
449 /**
450 * Result - associative array
451 *
452 * Returns the result set as an array
453 *
454 * @access private
455 * @return array
456 */
457 function _fetch_assoc()
458 {
459 return pg_fetch_assoc($this->result_id);
460 }
461
462 // --------------------------------------------------------------------
463
464 /**
465 * Result - object
466 *
467 * Returns the result set as an object
468 *
469 * @access private
470 * @return object
471 */
472 function _fetch_object()
473 {
474 return pg_fetch_object($this->result_id);
475 }
476
477}
478
479?>