blob: 9c8a18eb567859db2a3ad85e6ab25f93fa0d5965 [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 *
adminbb1d4392006-09-24 20:14:38 +0000282 * @access private
admin7eea4f82006-09-24 18:13:17 +0000283 * @return string
284 */
adminbb1d4392006-09-24 20:14:38 +0000285 function _error_message()
admin7eea4f82006-09-24 18:13:17 +0000286 {
287 return pg_last_error($this->conn_id);
288 }
289
290 // --------------------------------------------------------------------
291
292 /**
293 * The error message number
294 *
adminbb1d4392006-09-24 20:14:38 +0000295 * @access private
admin7eea4f82006-09-24 18:13:17 +0000296 * @return integer
297 */
adminbb1d4392006-09-24 20:14:38 +0000298 function _error_number()
admin7eea4f82006-09-24 18:13:17 +0000299 {
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 *
adminbb1d4392006-09-24 20:14:38 +0000311 * @access private
admin7eea4f82006-09-24 18:13:17 +0000312 * @param string the table name
313 * @return string
314 */
adminbb1d4392006-09-24 20:14:38 +0000315 function _escape_table($table)
admin7eea4f82006-09-24 18:13:17 +0000316 {
317 if (stristr($table, '.'))
318 {
319 $table = '"'.preg_replace("/\./", '"."', $table).'"';
320 }
321
322 return $table;
323 }
324
325 // --------------------------------------------------------------------
326
327 /**
admin7eea4f82006-09-24 18:13:17 +0000328 * Insert statement
329 *
330 * Generates a platform-specific insert string from the supplied data
331 *
332 * @access public
333 * @param string the table name
334 * @param array the insert keys
335 * @param array the insert values
336 * @return string
337 */
338 function _insert($table, $keys, $values)
339 {
adminbb1d4392006-09-24 20:14:38 +0000340 return "INSERT INTO ".$this->_escape_table($table)." (".implode(', ', $keys).") VALUES (".implode(', ', $values).")";
admin7eea4f82006-09-24 18:13:17 +0000341 }
342
343 // --------------------------------------------------------------------
344
345 /**
346 * Update statement
347 *
348 * Generates a platform-specific update string from the supplied data
349 *
350 * @access public
351 * @param string the table name
352 * @param array the update data
353 * @param array the where clause
354 * @return string
355 */
356 function _update($table, $values, $where)
357 {
358 foreach($values as $key => $val)
359 {
360 $valstr[] = $key." = ".$val;
361 }
362
adminbb1d4392006-09-24 20:14:38 +0000363 return "UPDATE ".$this->_escape_table($table)." SET ".implode(', ', $valstr)." WHERE ".implode(" ", $where);
admin7eea4f82006-09-24 18:13:17 +0000364 }
365
366 // --------------------------------------------------------------------
367
368 /**
369 * Delete statement
370 *
371 * Generates a platform-specific delete string from the supplied data
372 *
373 * @access public
374 * @param string the table name
375 * @param array the where clause
376 * @return string
377 */
378 function _delete($table, $where)
379 {
adminbb1d4392006-09-24 20:14:38 +0000380 return "DELETE FROM ".$this->_escape_table($table)." WHERE ".implode(" ", $where);
admin7eea4f82006-09-24 18:13:17 +0000381 }
adminbb1d4392006-09-24 20:14:38 +0000382
383 // --------------------------------------------------------------------
384
385 /**
386 * Limit string
387 *
388 * Generates a platform-specific LIMIT clause
389 *
390 * @access public
391 * @param string the sql query string
392 * @param integer the number of rows to limit the query to
393 * @param integer the offset value
394 * @return string
395 */
396 function _limit($sql, $limit, $offset)
397 {
398 $sql .= "LIMIT ".$limit;
admin7eea4f82006-09-24 18:13:17 +0000399
adminbb1d4392006-09-24 20:14:38 +0000400 if ($offset > 0)
401 {
402 $sql .= " OFFSET ".$offset;
403 }
404
405 return $sql;
406 }
407
408 // --------------------------------------------------------------------
409
410 /**
411 * Close DB Connection
412 *
413 * @access public
414 * @param resource
415 * @return void
416 */
417 function _close($conn_id)
418 {
419 pg_close($conn_id);
420 }
421
adminbb1d4392006-09-24 20:14:38 +0000422
admin7eea4f82006-09-24 18:13:17 +0000423}
424
425?>