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