blob: 3829b04afa2cdf7eb6c79bd432c23632a5a51817 [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 {
113 if (get_magic_quotes_gpc())
114 {
115 $str = stripslashes($str);
116 }
117 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
218 * in it. Some DBs will get cranky unless periods are escaped
219 *
220 * @access public
221 * @param string the table name
222 * @return string
223 */
224 function escape_table($table)
225 {
226 if (stristr($table, '.'))
227 {
adminb071bb52006-08-26 19:28:37 +0000228 $table = preg_replace("/\./", ".", $table);
adminb0dd10f2006-08-25 17:25:49 +0000229 }
230
231 return $table;
232 }
233
234 // --------------------------------------------------------------------
235
236 /**
237 * Field data query
238 *
239 * Generates a platform-specific query so that the column data can be retrieved
240 *
241 * @access public
242 * @param string the table name
243 * @return object
244 */
245 function _field_data($table)
246 {
247 $sql = "SELECT * FROM ".$this->escape_table($table)." LIMIT 1";
248 $query = $this->query($sql);
249 return $query->field_data();
250 }
251
252 // --------------------------------------------------------------------
253
254 /**
255 * Insert statement
256 *
257 * Generates a platform-specific insert string from the supplied data
258 *
259 * @access public
260 * @param string the table name
261 * @param array the insert keys
262 * @param array the insert values
263 * @return string
264 */
265 function _insert($table, $keys, $values)
266 {
267 return "INSERT INTO ".$this->escape_table($table)." (".implode(', ', $keys).") VALUES (".implode(', ', $values).")";
268 }
269
270 // --------------------------------------------------------------------
271
272 /**
273 * Update statement
274 *
275 * Generates a platform-specific update string from the supplied data
276 *
277 * @access public
278 * @param string the table name
279 * @param array the update data
280 * @param array the where clause
281 * @return string
282 */
283 function _update($table, $values, $where)
284 {
285 foreach($values as $key => $val)
286 {
287 $valstr[] = $key." = ".$val;
288 }
289
290 return "UPDATE ".$this->escape_table($table)." SET ".implode(', ', $valstr)." WHERE ".implode(" ", $where);
291 }
292
293 // --------------------------------------------------------------------
294
295 /**
296 * Delete statement
297 *
298 * Generates a platform-specific delete string from the supplied data
299 *
300 * @access public
301 * @param string the table name
302 * @param array the where clause
303 * @return string
304 */
305 function _delete($table, $where)
306 {
307 return "DELETE FROM ".$this->escape_table($table)." WHERE ".implode(" ", $where);
308 }
309
310 // --------------------------------------------------------------------
311
312 /**
313 * Version number query string
314 *
315 * @access public
316 * @return string
317 */
318 function _version()
319 {
320 return "SELECT version() AS ver";
321 }
322
323 /**
324 * Show table query
325 *
326 * Generates a platform-specific query string so that the table names can be fetched
327 *
328 * @access public
329 * @return string
330 */
331 function _show_tables()
332 {
333 return "SELECT table_name FROM information_schema.tables WHERE table_schema = 'public'";
334 }
335
336 // --------------------------------------------------------------------
337
338 /**
339 * Show columnn query
340 *
341 * Generates a platform-specific query string so that the column names can be fetched
342 *
343 * @access public
344 * @param string the table name
345 * @return string
346 */
347 function _show_columns($table = '')
348 {
349 return "SELECT column_name FROM information_schema.columns WHERE table_name ='".$this->escape_table($table)."'";
350 }
351
352 // --------------------------------------------------------------------
353
354 /**
355 * Limit string
356 *
357 * Generates a platform-specific LIMIT clause
358 *
359 * @access public
360 * @param string the sql query string
361 * @param integer the number of rows to limit the query to
362 * @param integer the offset value
363 * @return string
364 */
365 function _limit($sql, $limit, $offset)
366 {
367 $sql .= "LIMIT ".$limit;
368
369 if ($offset > 0)
370 {
371 $sql .= " OFFSET ".$offset;
372 }
373
374 return $sql;
375 }
376
377}
378
379
380
381/**
382 * Postgres Result Class
383 *
384 * This class extends the parent result class: CI_DB_result
385 *
386 * @category Database
387 * @author Rick Ellis
388 * @link http://www.codeigniter.com/user_guide/libraries/database/
389 */
390class CI_DB_postgre_result extends CI_DB_result {
391
392 /**
393 * Number of rows in the result set
394 *
395 * @access public
396 * @return integer
397 */
398 function num_rows()
399 {
400 return @pg_num_rows($this->result_id);
401 }
402
403 // --------------------------------------------------------------------
404
405 /**
406 * Number of fields in the result set
407 *
408 * @access public
409 * @return integer
410 */
411 function num_fields()
412 {
413 return @pg_num_fields($this->result_id);
414 }
415
416 // --------------------------------------------------------------------
417
418 /**
419 * Field data
420 *
421 * Generates an array of objects containing field meta-data
422 *
423 * @access public
424 * @return array
425 */
426 function field_data()
427 {
428 $retval = array();
429 for ($i = 0; $i < $this->num_fields(); $i++)
430 {
431 $F = new CI_DB_field();
432 $F->name = pg_field_name($this->result_id, $i);
433 $F->type = pg_field_type($this->result_id, $i);
434 $F->max_length = pg_field_size($this->result_id, $i);
adminb071bb52006-08-26 19:28:37 +0000435 $F->primary_key = $i == 0;
adminb0dd10f2006-08-25 17:25:49 +0000436 $F->default = '';
437
438 $retval[] = $F;
439 }
440
441 return $retval;
442 }
443
444 // --------------------------------------------------------------------
445
446 /**
447 * Result - associative array
448 *
449 * Returns the result set as an array
450 *
451 * @access private
452 * @return array
453 */
454 function _fetch_assoc()
455 {
456 return pg_fetch_assoc($this->result_id);
457 }
458
459 // --------------------------------------------------------------------
460
461 /**
462 * Result - object
463 *
464 * Returns the result set as an object
465 *
466 * @access private
467 * @return object
468 */
469 function _fetch_object()
470 {
471 return pg_fetch_object($this->result_id);
472 }
473
474}
475
476?>