blob: 92767c42bb61d22183889f8ed74954953626bccc [file] [log] [blame]
admin7eea4f82006-09-24 18:13:17 +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/database/
30 */
31class CI_DB_postgre_driver 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 $port = ($this->port == '') ? '' : " port=".$this->port;
42
43 return pg_connect("host=".$this->hostname.$port." dbname=".$this->database." user=".$this->username." password=".$this->password);
44 }
45
46 // --------------------------------------------------------------------
47
48 /**
49 * Persistent database connection
50 *
51 * @access private called by the base class
52 * @return resource
53 */
54 function db_pconnect()
55 {
56 $port = ($this->port == '') ? '' : " port=".$this->port;
57
58 return pg_pconnect("host=".$this->hostname.$port." dbname=".$this->database." user=".$this->username." password=".$this->password);
59 }
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 */
101 function _prep_query($sql)
102 {
103 return $sql;
104 }
105
106 // --------------------------------------------------------------------
107
108 /**
109 * Begin Transaction
110 *
111 * @access public
112 * @return bool
113 */
114 function trans_begin($test_mode = FALSE)
115 {
116 if ( ! $this->trans_enabled)
117 {
118 return TRUE;
119 }
120
121 // When transactions are nested we only begin/commit/rollback the outermost ones
122 if ($this->_trans_depth > 0)
123 {
124 return TRUE;
125 }
126
127 // Reset the transaction failure flag.
128 // If the $test_mode flag is set to TRUE transactions will be rolled back
129 // even if the queries produce a successful result.
130 $this->_trans_failure = ($test_mode === TRUE) ? TRUE : FALSE;
131
132 return @pg_exec($this->conn_id, "begin");
133 }
134
135 // --------------------------------------------------------------------
136
137 /**
138 * Commit Transaction
139 *
140 * @access public
141 * @return bool
142 */
143 function trans_commit()
144 {
145 if ( ! $this->trans_enabled)
146 {
147 return TRUE;
148 }
149
150 // When transactions are nested we only begin/commit/rollback the outermost ones
151 if ($this->_trans_depth > 0)
152 {
153 return TRUE;
154 }
155
156 return @pg_exec($this->conn_id, "commit");
157 }
158
159 // --------------------------------------------------------------------
160
161 /**
162 * Rollback Transaction
163 *
164 * @access public
165 * @return bool
166 */
167 function trans_rollback()
168 {
169 if ( ! $this->trans_enabled)
170 {
171 return TRUE;
172 }
173
174 // When transactions are nested we only begin/commit/rollback the outermost ones
175 if ($this->_trans_depth > 0)
176 {
177 return TRUE;
178 }
179
180 return @pg_exec($this->conn_id, "rollback");
181 }
182
183 // --------------------------------------------------------------------
184
185 /**
186 * Escape String
187 *
188 * @access public
189 * @param string
190 * @return string
191 */
192 function escape_str($str)
193 {
194 return pg_escape_string($str);
195 }
196
197 // --------------------------------------------------------------------
198
199 /**
200 * Affected Rows
201 *
202 * @access public
203 * @return integer
204 */
205 function affected_rows()
206 {
207 return @pg_affected_rows($this->result_id);
208 }
209
210 // --------------------------------------------------------------------
211
212 /**
213 * Insert ID
214 *
215 * @access public
216 * @return integer
217 */
218 function insert_id()
219 {
220 $v = pg_version($this->conn_id);
221 $v = $v['server'];
222
223 $table = func_num_args() > 0 ? func_get_arg(0) : null;
224 $column = func_num_args() > 1 ? func_get_arg(1) : null;
225
226 if ($table == null && $v >= '8.1')
227 {
228 $sql='SELECT LASTVAL() as ins_id';
229 }
230 elseif ($table != null && $column != null && $v >= '8.0')
231 {
232 $sql = sprintf("SELECT pg_get_serial_sequence('%s','%s') as seq", $table, $column);
233 $query = $this->query($sql);
234 $row = $query->row();
235 $sql = sprintf("SELECT CURRVAL('%s') as ins_id", $row->seq);
236 }
237 elseif ($table != null)
238 {
239 // seq_name passed in table parameter
240 $sql = sprintf("SELECT CURRVAL('%s') as ins_id", $table);
241 }
242 else
243 {
244 return pg_last_oid($this->result_id);
245 }
246 $query = $this->query($sql);
247 $row = $query->row();
248 return $row->ins_id;
249 }
250
251 // --------------------------------------------------------------------
252
253 /**
254 * "Count All" query
255 *
256 * Generates a platform-specific query string that counts all records in
257 * the specified database
258 *
259 * @access public
260 * @param string
261 * @return string
262 */
263 function count_all($table = '')
264 {
265 if ($table == '')
266 return '0';
267
268 $query = $this->query('SELECT COUNT(*) AS numrows FROM "'.$this->dbprefix.$table.'"');
269
270 if ($query->num_rows() == 0)
271 return '0';
272
273 $row = $query->row();
274 return $row->numrows;
275 }
276
277 // --------------------------------------------------------------------
278
279 /**
280 * The error message string
281 *
282 * @access public
283 * @return string
284 */
285 function error_message()
286 {
287 return pg_last_error($this->conn_id);
288 }
289
290 // --------------------------------------------------------------------
291
292 /**
293 * The error message number
294 *
295 * @access public
296 * @return integer
297 */
298 function error_number()
299 {
300 return '';
301 }
302
303 // --------------------------------------------------------------------
304
305 /**
306 * Escape Table Name
307 *
308 * This function adds backticks if the table name has a period
309 * in it. Some DBs will get cranky unless periods are escaped.
310 *
311 * @access public
312 * @param string the table name
313 * @return string
314 */
315 function escape_table($table)
316 {
317 if (stristr($table, '.'))
318 {
319 $table = '"'.preg_replace("/\./", '"."', $table).'"';
320 }
321
322 return $table;
323 }
324
325 // --------------------------------------------------------------------
326
327 /**
328 * Field data query
329 *
330 * Generates a platform-specific query so that the column data can be retrieved
331 *
332 * @access public
333 * @param string the table name
334 * @return object
335 */
336 function _field_data($table)
337 {
338 $sql = "SELECT * FROM ".$this->escape_table($table)." LIMIT 1";
339 $query = $this->query($sql);
340 return $query->field_data();
341 }
342
343 // --------------------------------------------------------------------
344
345 /**
346 * Insert statement
347 *
348 * Generates a platform-specific insert string from the supplied data
349 *
350 * @access public
351 * @param string the table name
352 * @param array the insert keys
353 * @param array the insert values
354 * @return string
355 */
356 function _insert($table, $keys, $values)
357 {
358 return "INSERT INTO ".$this->escape_table($table)." (".implode(', ', $keys).") VALUES (".implode(', ', $values).")";
359 }
360
361 // --------------------------------------------------------------------
362
363 /**
364 * Update statement
365 *
366 * Generates a platform-specific update string from the supplied data
367 *
368 * @access public
369 * @param string the table name
370 * @param array the update data
371 * @param array the where clause
372 * @return string
373 */
374 function _update($table, $values, $where)
375 {
376 foreach($values as $key => $val)
377 {
378 $valstr[] = $key." = ".$val;
379 }
380
381 return "UPDATE ".$this->escape_table($table)." SET ".implode(', ', $valstr)." WHERE ".implode(" ", $where);
382 }
383
384 // --------------------------------------------------------------------
385
386 /**
387 * Delete statement
388 *
389 * Generates a platform-specific delete string from the supplied data
390 *
391 * @access public
392 * @param string the table name
393 * @param array the where clause
394 * @return string
395 */
396 function _delete($table, $where)
397 {
398 return "DELETE FROM ".$this->escape_table($table)." WHERE ".implode(" ", $where);
399 }
400
401 // --------------------------------------------------------------------
402
403 /**
404 * Version number query string
405 *
406 * @access public
407 * @return string
408 */
409 function _version()
410 {
411 return "SELECT version() AS ver";
412 }
413
414 /**
415 * Show table query
416 *
417 * Generates a platform-specific query string so that the table names can be fetched
418 *
419 * @access public
420 * @return string
421 */
422 function _show_tables()
423 {
424 return "SELECT table_name FROM information_schema.tables WHERE table_schema = 'public'";
425 }
426
427 // --------------------------------------------------------------------
428
429 /**
430 * Show columnn query
431 *
432 * Generates a platform-specific query string so that the column names can be fetched
433 *
434 * @access public
435 * @param string the table name
436 * @return string
437 */
438 function _show_columns($table = '')
439 {
440 return "SELECT column_name FROM information_schema.columns WHERE table_name ='".$this->escape_table($table)."'";
441 }
442
443 // --------------------------------------------------------------------
444
445 /**
446 * Limit string
447 *
448 * Generates a platform-specific LIMIT clause
449 *
450 * @access public
451 * @param string the sql query string
452 * @param integer the number of rows to limit the query to
453 * @param integer the offset value
454 * @return string
455 */
456 function _limit($sql, $limit, $offset)
457 {
458 $sql .= "LIMIT ".$limit;
459
460 if ($offset > 0)
461 {
462 $sql .= " OFFSET ".$offset;
463 }
464
465 return $sql;
466 }
467
468 // --------------------------------------------------------------------
469
470 /**
471 * Close DB Connection
472 *
473 * @access public
474 * @param resource
475 * @return void
476 */
477 function _close($conn_id)
478 {
479 pg_close($conn_id);
480 }
481
482}
483
484?>