blob: cf59f0fdc5c62dd76337d9b8039b5f17be6cc0f8 [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 {
41 return pg_connect("host=".$this->hostname." dbname=".$this->database." user=".$this->username." password=".$this->password);
42 }
43
44 // --------------------------------------------------------------------
45
46 /**
47 * Persistent database connection
48 *
49 * @access private called by the base class
50 * @return resource
51 */
52 function db_pconnect()
53 {
54 return pg_pconnect("host=".$this->hostname." dbname=".$this->database." user=".$this->username." password=".$this->password);
55 }
56
57 // --------------------------------------------------------------------
58
59 /**
60 * Select the database
61 *
62 * @access private called by the base class
63 * @return resource
64 */
65 function db_select()
66 {
67 // Not needed for Postgre so we'll return TRUE
68 return TRUE;
69 }
70
71 // --------------------------------------------------------------------
72
73 /**
74 * Execute the query
75 *
76 * @access private called by the base class
77 * @param string an SQL query
78 * @return resource
79 */
80 function execute($sql)
81 {
82 $sql = $this->_prep_query($sql);
83 return @pg_query($this->conn_id, $sql);
84 }
85
86 // --------------------------------------------------------------------
87
88 /**
89 * Prep the query
90 *
91 * If needed, each database adapter can prep the query string
92 *
93 * @access private called by execute()
94 * @param string an SQL query
95 * @return string
96 */
adminb071bb52006-08-26 19:28:37 +000097 function _prep_query($sql)
adminb0dd10f2006-08-25 17:25:49 +000098 {
99 return $sql;
100 }
101
102 // --------------------------------------------------------------------
103
104 /**
105 * Escape String
106 *
107 * @access public
108 * @param string
109 * @return string
110 */
111 function escape_str($str)
112 {
adminb0dd10f2006-08-25 17:25:49 +0000113 return pg_escape_string($str);
114 }
115
116 // --------------------------------------------------------------------
117
118 /**
119 * Close DB Connection
120 *
121 * @access public
122 * @param resource
123 * @return void
124 */
125 function destroy($conn_id)
126 {
127 pg_close($conn_id);
128 }
129
130 // --------------------------------------------------------------------
131
132 /**
133 * Affected Rows
134 *
135 * @access public
136 * @return integer
137 */
138 function affected_rows()
139 {
140 return @pg_affected_rows($this->result_id);
141 }
142
143 // --------------------------------------------------------------------
144
145 /**
146 * Insert ID
147 *
148 * @access public
149 * @return integer
150 */
151 function insert_id()
152 {
153 return pg_last_oid($this->result_id);
154 }
155
156 // --------------------------------------------------------------------
157
158 /**
159 * "Count All" query
160 *
161 * Generates a platform-specific query string that counts all records in
162 * the specified database
163 *
164 * @access public
165 * @param string
166 * @return string
167 */
168 function count_all($table = '')
169 {
170 if ($table == '')
171 return '0';
172
adminb071bb52006-08-26 19:28:37 +0000173 $query = $this->query("SELECT COUNT(*) AS numrows FROM ".$this->dbprefix.$table."");
adminb0dd10f2006-08-25 17:25:49 +0000174
175 if ($query->num_rows() == 0)
176 return '0';
177
178 $row = $query->row();
179 return $row->numrows;
180 }
181
182 // --------------------------------------------------------------------
183
184 /**
185 * The error message string
186 *
187 * @access public
188 * @return string
189 */
190 function error_message()
191 {
192 return pg_last_error($this->conn_id);
193 }
194
195 // --------------------------------------------------------------------
196
197 /**
198 * The error message number
199 *
200 * @access public
201 * @return integer
202 */
203 function error_number()
204 {
205 return '';
206 }
207
208 // --------------------------------------------------------------------
209
210 /**
211 * Escape Table Name
212 *
213 * This function adds backticks if the table name has a period
214 * in it. Some DBs will get cranky unless periods are escaped
215 *
216 * @access public
217 * @param string the table name
218 * @return string
219 */
220 function escape_table($table)
221 {
222 if (stristr($table, '.'))
223 {
adminb071bb52006-08-26 19:28:37 +0000224 $table = preg_replace("/\./", ".", $table);
adminb0dd10f2006-08-25 17:25:49 +0000225 }
226
227 return $table;
228 }
229
230 // --------------------------------------------------------------------
231
232 /**
233 * Field data query
234 *
235 * Generates a platform-specific query so that the column data can be retrieved
236 *
237 * @access public
238 * @param string the table name
239 * @return object
240 */
241 function _field_data($table)
242 {
243 $sql = "SELECT * FROM ".$this->escape_table($table)." LIMIT 1";
244 $query = $this->query($sql);
245 return $query->field_data();
246 }
247
248 // --------------------------------------------------------------------
249
250 /**
251 * Insert statement
252 *
253 * Generates a platform-specific insert string from the supplied data
254 *
255 * @access public
256 * @param string the table name
257 * @param array the insert keys
258 * @param array the insert values
259 * @return string
260 */
261 function _insert($table, $keys, $values)
262 {
263 return "INSERT INTO ".$this->escape_table($table)." (".implode(', ', $keys).") VALUES (".implode(', ', $values).")";
264 }
265
266 // --------------------------------------------------------------------
267
268 /**
269 * Update statement
270 *
271 * Generates a platform-specific update string from the supplied data
272 *
273 * @access public
274 * @param string the table name
275 * @param array the update data
276 * @param array the where clause
277 * @return string
278 */
279 function _update($table, $values, $where)
280 {
281 foreach($values as $key => $val)
282 {
283 $valstr[] = $key." = ".$val;
284 }
285
286 return "UPDATE ".$this->escape_table($table)." SET ".implode(', ', $valstr)." WHERE ".implode(" ", $where);
287 }
288
289 // --------------------------------------------------------------------
290
291 /**
292 * Delete statement
293 *
294 * Generates a platform-specific delete string from the supplied data
295 *
296 * @access public
297 * @param string the table name
298 * @param array the where clause
299 * @return string
300 */
301 function _delete($table, $where)
302 {
303 return "DELETE FROM ".$this->escape_table($table)." WHERE ".implode(" ", $where);
304 }
305
306 // --------------------------------------------------------------------
307
308 /**
309 * Version number query string
310 *
311 * @access public
312 * @return string
313 */
314 function _version()
315 {
316 return "SELECT version() AS ver";
317 }
318
319 /**
320 * Show table query
321 *
322 * Generates a platform-specific query string so that the table names can be fetched
323 *
324 * @access public
325 * @return string
326 */
327 function _show_tables()
328 {
329 return "SELECT table_name FROM information_schema.tables WHERE table_schema = 'public'";
330 }
331
332 // --------------------------------------------------------------------
333
334 /**
335 * Show columnn query
336 *
337 * Generates a platform-specific query string so that the column names can be fetched
338 *
339 * @access public
340 * @param string the table name
341 * @return string
342 */
343 function _show_columns($table = '')
344 {
345 return "SELECT column_name FROM information_schema.columns WHERE table_name ='".$this->escape_table($table)."'";
346 }
347
348 // --------------------------------------------------------------------
349
350 /**
351 * Limit string
352 *
353 * Generates a platform-specific LIMIT clause
354 *
355 * @access public
356 * @param string the sql query string
357 * @param integer the number of rows to limit the query to
358 * @param integer the offset value
359 * @return string
360 */
361 function _limit($sql, $limit, $offset)
362 {
363 $sql .= "LIMIT ".$limit;
364
365 if ($offset > 0)
366 {
367 $sql .= " OFFSET ".$offset;
368 }
369
370 return $sql;
371 }
372
373}
374
375
376
377/**
378 * Postgres Result Class
379 *
380 * This class extends the parent result class: CI_DB_result
381 *
382 * @category Database
383 * @author Rick Ellis
384 * @link http://www.codeigniter.com/user_guide/libraries/database/
385 */
386class CI_DB_postgre_result extends CI_DB_result {
387
388 /**
389 * Number of rows in the result set
390 *
391 * @access public
392 * @return integer
393 */
394 function num_rows()
395 {
396 return @pg_num_rows($this->result_id);
397 }
398
399 // --------------------------------------------------------------------
400
401 /**
402 * Number of fields in the result set
403 *
404 * @access public
405 * @return integer
406 */
407 function num_fields()
408 {
409 return @pg_num_fields($this->result_id);
410 }
411
412 // --------------------------------------------------------------------
413
414 /**
415 * Field data
416 *
417 * Generates an array of objects containing field meta-data
418 *
419 * @access public
420 * @return array
421 */
422 function field_data()
423 {
424 $retval = array();
425 for ($i = 0; $i < $this->num_fields(); $i++)
426 {
427 $F = new CI_DB_field();
428 $F->name = pg_field_name($this->result_id, $i);
429 $F->type = pg_field_type($this->result_id, $i);
430 $F->max_length = pg_field_size($this->result_id, $i);
adminb071bb52006-08-26 19:28:37 +0000431 $F->primary_key = $i == 0;
adminb0dd10f2006-08-25 17:25:49 +0000432 $F->default = '';
433
434 $retval[] = $F;
435 }
436
437 return $retval;
438 }
439
440 // --------------------------------------------------------------------
441
442 /**
443 * Result - associative array
444 *
445 * Returns the result set as an array
446 *
447 * @access private
448 * @return array
449 */
450 function _fetch_assoc()
451 {
452 return pg_fetch_assoc($this->result_id);
453 }
454
455 // --------------------------------------------------------------------
456
457 /**
458 * Result - object
459 *
460 * Returns the result set as an object
461 *
462 * @access private
463 * @return object
464 */
465 function _fetch_object()
466 {
467 return pg_fetch_object($this->result_id);
468 }
469
470}
471
472?>