blob: 42cfaaefbae051833f024303a6e824b539a2c054 [file] [log] [blame]
Derek Jones4b9c6292011-07-01 17:40:48 -05001<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
Derek Allard2067d1a2008-11-13 22:59:24 +00002/**
3 * CodeIgniter
4 *
Greg Aker741de1c2010-11-10 14:52:57 -06005 * An open source application development framework for PHP 5.1.6 or newer
Derek Allard2067d1a2008-11-13 22:59:24 +00006 *
Barry Mienydd671972010-10-04 16:33:58 +02007 * @package CodeIgniter
8 * @author ExpressionEngine Dev Team
Derek Jones4b9c6292011-07-01 17:40:48 -05009 * @copyright Copyright (c) 2008 - 2011, EllisLab, Inc.
Barry Mienydd671972010-10-04 16:33:58 +020010 * @license http://codeigniter.com/user_guide/license.html
Derek Allard2067d1a2008-11-13 22:59:24 +000011 * @link http://codeigniter.com
Barry Mienydd671972010-10-04 16:33:58 +020012 * @since Version 1.0
Derek Allard2067d1a2008-11-13 22:59:24 +000013 * @filesource
14 */
15
16// ------------------------------------------------------------------------
17
18/**
19 * oci8 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 *
Barry Mienydd671972010-10-04 16:33:58 +020025 * @package CodeIgniter
Derek Jones4b9c6292011-07-01 17:40:48 -050026 * @subpackage Drivers
Derek Allard2067d1a2008-11-13 22:59:24 +000027 * @category Database
Barry Mienydd671972010-10-04 16:33:58 +020028 * @author ExpressionEngine Dev Team
Derek Allard2067d1a2008-11-13 22:59:24 +000029 * @link http://codeigniter.com/user_guide/database/
30 */
31
32/**
33 * oci8 Database Adapter Class
34 *
35 * This is a modification of the DB_driver class to
36 * permit access to oracle databases
37 *
38 * NOTE: this uses the PHP 4 oci methods
39 *
Derek Jones4b9c6292011-07-01 17:40:48 -050040 * @author Kelly McArdle
Derek Allard2067d1a2008-11-13 22:59:24 +000041 *
42 */
43
44class CI_DB_oci8_driver extends CI_DB {
45
46 var $dbdriver = 'oci8';
Barry Mienydd671972010-10-04 16:33:58 +020047
Derek Allard2067d1a2008-11-13 22:59:24 +000048 // The character used for excaping
49 var $_escape_char = '"';
Barry Mienydd671972010-10-04 16:33:58 +020050
Derek Jonese4ed5832009-02-20 21:44:59 +000051 // clause and character used for LIKE escape sequences
52 var $_like_escape_str = " escape '%s' ";
53 var $_like_escape_chr = '!';
Barry Mienydd671972010-10-04 16:33:58 +020054
Derek Allard2067d1a2008-11-13 22:59:24 +000055 /**
56 * The syntax to count rows is slightly different across different
57 * database engines, so this string appears in each driver and is
58 * used for the count_all() and count_all_results() functions.
59 */
60 var $_count_string = "SELECT COUNT(1) AS ";
61 var $_random_keyword = ' ASC'; // not currently supported
62
63 // Set "auto commit" by default
64 var $_commit = OCI_COMMIT_ON_SUCCESS;
65
66 // need to track statement id and cursor id
67 var $stmt_id;
68 var $curs_id;
69
70 // if we use a limit, we will add a field that will
71 // throw off num_fields later
72 var $limit_used;
73
74 /**
75 * Non-persistent database connection
76 *
Derek Jones4b9c6292011-07-01 17:40:48 -050077 * @access private called by the base class
78 * @return resource
Derek Allard2067d1a2008-11-13 22:59:24 +000079 */
80 function db_connect()
81 {
82 return @ocilogon($this->username, $this->password, $this->hostname);
83 }
84
85 // --------------------------------------------------------------------
86
87 /**
88 * Persistent database connection
89 *
Derek Jones4b9c6292011-07-01 17:40:48 -050090 * @access private called by the base class
91 * @return resource
Derek Allard2067d1a2008-11-13 22:59:24 +000092 */
93 function db_pconnect()
94 {
95 return @ociplogon($this->username, $this->password, $this->hostname);
96 }
97
98 // --------------------------------------------------------------------
99
100 /**
Derek Jones87cbafc2009-02-27 16:29:59 +0000101 * Reconnect
102 *
103 * Keep / reestablish the db connection if no queries have been
104 * sent for a length of time exceeding the server's idle timeout
105 *
106 * @access public
107 * @return void
108 */
109 function reconnect()
110 {
111 // not implemented in oracle
112 }
113
114 // --------------------------------------------------------------------
115
116 /**
Derek Allard2067d1a2008-11-13 22:59:24 +0000117 * Select the database
118 *
Derek Jones4b9c6292011-07-01 17:40:48 -0500119 * @access private called by the base class
120 * @return resource
Derek Allard2067d1a2008-11-13 22:59:24 +0000121 */
122 function db_select()
123 {
124 return TRUE;
125 }
126
127 // --------------------------------------------------------------------
128
129 /**
130 * Set client character set
131 *
132 * @access public
133 * @param string
134 * @param string
135 * @return resource
136 */
137 function db_set_charset($charset, $collation)
138 {
139 // @todo - add support if needed
140 return TRUE;
141 }
142
143 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200144
Derek Allard2067d1a2008-11-13 22:59:24 +0000145 /**
146 * Version number query string
147 *
Derek Jones4b9c6292011-07-01 17:40:48 -0500148 * @access public
149 * @return string
Derek Allard2067d1a2008-11-13 22:59:24 +0000150 */
151 function _version()
152 {
153 return ociserverversion($this->conn_id);
154 }
155
156 // --------------------------------------------------------------------
157
158 /**
159 * Execute the query
160 *
Derek Jones4b9c6292011-07-01 17:40:48 -0500161 * @access private called by the base class
162 * @param string an SQL query
163 * @return resource
Derek Allard2067d1a2008-11-13 22:59:24 +0000164 */
165 function _execute($sql)
166 {
167 // oracle must parse the query before it is run. All of the actions with
168 // the query are based on the statement id returned by ociparse
169 $this->stmt_id = FALSE;
170 $this->_set_stmt_id($sql);
171 ocisetprefetch($this->stmt_id, 1000);
172 return @ociexecute($this->stmt_id, $this->_commit);
173 }
174
175 /**
176 * Generate a statement ID
177 *
Derek Jones4b9c6292011-07-01 17:40:48 -0500178 * @access private
179 * @param string an SQL query
180 * @return none
Derek Allard2067d1a2008-11-13 22:59:24 +0000181 */
182 function _set_stmt_id($sql)
183 {
184 if ( ! is_resource($this->stmt_id))
185 {
186 $this->stmt_id = ociparse($this->conn_id, $this->_prep_query($sql));
187 }
188 }
189
190 // --------------------------------------------------------------------
191
192 /**
193 * Prep the query
194 *
195 * If needed, each database adapter can prep the query string
196 *
Derek Jones4b9c6292011-07-01 17:40:48 -0500197 * @access private called by execute()
198 * @param string an SQL query
199 * @return string
Derek Allard2067d1a2008-11-13 22:59:24 +0000200 */
201 function _prep_query($sql)
202 {
203 return $sql;
204 }
205
206 // --------------------------------------------------------------------
207
208 /**
Derek Jones4b9c6292011-07-01 17:40:48 -0500209 * getCursor. Returns a cursor from the datbase
Derek Allard2067d1a2008-11-13 22:59:24 +0000210 *
Derek Jones4b9c6292011-07-01 17:40:48 -0500211 * @access public
212 * @return cursor id
Derek Allard2067d1a2008-11-13 22:59:24 +0000213 */
214 function get_cursor()
215 {
216 $this->curs_id = ocinewcursor($this->conn_id);
217 return $this->curs_id;
218 }
219
220 // --------------------------------------------------------------------
221
222 /**
Derek Jones4b9c6292011-07-01 17:40:48 -0500223 * Stored Procedure. Executes a stored procedure
Derek Allard2067d1a2008-11-13 22:59:24 +0000224 *
Derek Jones4b9c6292011-07-01 17:40:48 -0500225 * @access public
226 * @param package package stored procedure is in
227 * @param procedure stored procedure to execute
228 * @param params array of parameters
229 * @return array
Derek Allard2067d1a2008-11-13 22:59:24 +0000230 *
231 * params array keys
232 *
Derek Jones4b9c6292011-07-01 17:40:48 -0500233 * KEY OPTIONAL NOTES
Derek Allard2067d1a2008-11-13 22:59:24 +0000234 * name no the name of the parameter should be in :<param_name> format
Derek Jones4b9c6292011-07-01 17:40:48 -0500235 * value no the value of the parameter. If this is an OUT or IN OUT parameter,
Derek Allard2067d1a2008-11-13 22:59:24 +0000236 * this should be a reference to a variable
237 * type yes the type of the parameter
238 * length yes the max size of the parameter
239 */
240 function stored_procedure($package, $procedure, $params)
241 {
242 if ($package == '' OR $procedure == '' OR ! is_array($params))
243 {
244 if ($this->db_debug)
245 {
246 log_message('error', 'Invalid query: '.$package.'.'.$procedure);
Derek Allardfac8fbc2010-02-05 16:14:49 +0000247 return $this->display_error('db_invalid_query');
Derek Allard2067d1a2008-11-13 22:59:24 +0000248 }
249 return FALSE;
250 }
Barry Mienydd671972010-10-04 16:33:58 +0200251
Derek Allard2067d1a2008-11-13 22:59:24 +0000252 // build the query string
253 $sql = "begin $package.$procedure(";
254
255 $have_cursor = FALSE;
Pascal Krietec3a4a8d2011-02-14 13:40:08 -0500256 foreach ($params as $param)
Derek Allard2067d1a2008-11-13 22:59:24 +0000257 {
258 $sql .= $param['name'] . ",";
Barry Mienydd671972010-10-04 16:33:58 +0200259
Derek Allard2067d1a2008-11-13 22:59:24 +0000260 if (array_key_exists('type', $param) && ($param['type'] == OCI_B_CURSOR))
261 {
262 $have_cursor = TRUE;
263 }
264 }
265 $sql = trim($sql, ",") . "); end;";
Barry Mienydd671972010-10-04 16:33:58 +0200266
Derek Allard2067d1a2008-11-13 22:59:24 +0000267 $this->stmt_id = FALSE;
268 $this->_set_stmt_id($sql);
269 $this->_bind_params($params);
270 $this->query($sql, FALSE, $have_cursor);
271 }
Barry Mienydd671972010-10-04 16:33:58 +0200272
Derek Allard2067d1a2008-11-13 22:59:24 +0000273 // --------------------------------------------------------------------
274
275 /**
276 * Bind parameters
277 *
Derek Jones4b9c6292011-07-01 17:40:48 -0500278 * @access private
279 * @return none
Derek Allard2067d1a2008-11-13 22:59:24 +0000280 */
281 function _bind_params($params)
282 {
283 if ( ! is_array($params) OR ! is_resource($this->stmt_id))
284 {
285 return;
286 }
Barry Mienydd671972010-10-04 16:33:58 +0200287
Derek Allard2067d1a2008-11-13 22:59:24 +0000288 foreach ($params as $param)
289 {
Barry Mienydd671972010-10-04 16:33:58 +0200290 foreach (array('name', 'value', 'type', 'length') as $val)
Derek Allard2067d1a2008-11-13 22:59:24 +0000291 {
292 if ( ! isset($param[$val]))
293 {
294 $param[$val] = '';
295 }
296 }
297
298 ocibindbyname($this->stmt_id, $param['name'], $param['value'], $param['length'], $param['type']);
299 }
300 }
301
302 // --------------------------------------------------------------------
303
304 /**
305 * Begin Transaction
306 *
307 * @access public
Barry Mienydd671972010-10-04 16:33:58 +0200308 * @return bool
309 */
Derek Allard2067d1a2008-11-13 22:59:24 +0000310 function trans_begin($test_mode = FALSE)
311 {
312 if ( ! $this->trans_enabled)
313 {
314 return TRUE;
315 }
Barry Mienydd671972010-10-04 16:33:58 +0200316
Derek Allard2067d1a2008-11-13 22:59:24 +0000317 // When transactions are nested we only begin/commit/rollback the outermost ones
318 if ($this->_trans_depth > 0)
319 {
320 return TRUE;
321 }
Barry Mienydd671972010-10-04 16:33:58 +0200322
Derek Allard2067d1a2008-11-13 22:59:24 +0000323 // Reset the transaction failure flag.
324 // If the $test_mode flag is set to TRUE transactions will be rolled back
325 // even if the queries produce a successful result.
326 $this->_trans_failure = ($test_mode === TRUE) ? TRUE : FALSE;
Barry Mienydd671972010-10-04 16:33:58 +0200327
Derek Allard2067d1a2008-11-13 22:59:24 +0000328 $this->_commit = OCI_DEFAULT;
329 return TRUE;
330 }
331
332 // --------------------------------------------------------------------
333
334 /**
335 * Commit Transaction
336 *
337 * @access public
Barry Mienydd671972010-10-04 16:33:58 +0200338 * @return bool
339 */
Derek Allard2067d1a2008-11-13 22:59:24 +0000340 function trans_commit()
341 {
342 if ( ! $this->trans_enabled)
343 {
344 return TRUE;
345 }
346
347 // When transactions are nested we only begin/commit/rollback the outermost ones
348 if ($this->_trans_depth > 0)
349 {
350 return TRUE;
351 }
352
353 $ret = OCIcommit($this->conn_id);
354 $this->_commit = OCI_COMMIT_ON_SUCCESS;
355 return $ret;
356 }
357
358 // --------------------------------------------------------------------
359
360 /**
361 * Rollback Transaction
362 *
363 * @access public
Barry Mienydd671972010-10-04 16:33:58 +0200364 * @return bool
365 */
Derek Allard2067d1a2008-11-13 22:59:24 +0000366 function trans_rollback()
367 {
368 if ( ! $this->trans_enabled)
369 {
370 return TRUE;
371 }
372
373 // When transactions are nested we only begin/commit/rollback the outermost ones
374 if ($this->_trans_depth > 0)
375 {
376 return TRUE;
377 }
378
379 $ret = OCIrollback($this->conn_id);
380 $this->_commit = OCI_COMMIT_ON_SUCCESS;
381 return $ret;
382 }
383
384 // --------------------------------------------------------------------
385
386 /**
387 * Escape String
388 *
Derek Jones4b9c6292011-07-01 17:40:48 -0500389 * @access public
390 * @param string
Derek Jonese4ed5832009-02-20 21:44:59 +0000391 * @param bool whether or not the string will be used in a LIKE condition
Derek Jones4b9c6292011-07-01 17:40:48 -0500392 * @return string
Derek Allard2067d1a2008-11-13 22:59:24 +0000393 */
Derek Jonese4ed5832009-02-20 21:44:59 +0000394 function escape_str($str, $like = FALSE)
Derek Allard2067d1a2008-11-13 22:59:24 +0000395 {
Derek Jonese4ed5832009-02-20 21:44:59 +0000396 if (is_array($str))
397 {
Pascal Krietec3a4a8d2011-02-14 13:40:08 -0500398 foreach ($str as $key => $val)
Barry Mienydd671972010-10-04 16:33:58 +0200399 {
Derek Jonese4ed5832009-02-20 21:44:59 +0000400 $str[$key] = $this->escape_str($val, $like);
Barry Mienydd671972010-10-04 16:33:58 +0200401 }
402
403 return $str;
404 }
Derek Jonese4ed5832009-02-20 21:44:59 +0000405
Greg Aker757dda62010-04-14 19:06:19 -0500406 $str = remove_invisible_characters($str);
Barry Mienydd671972010-10-04 16:33:58 +0200407
Derek Jonese4ed5832009-02-20 21:44:59 +0000408 // escape LIKE condition wildcards
409 if ($like === TRUE)
410 {
411 $str = str_replace( array('%', '_', $this->_like_escape_chr),
412 array($this->_like_escape_chr.'%', $this->_like_escape_chr.'_', $this->_like_escape_chr.$this->_like_escape_chr),
413 $str);
414 }
Barry Mienydd671972010-10-04 16:33:58 +0200415
Derek Jonese4ed5832009-02-20 21:44:59 +0000416 return $str;
Derek Allard2067d1a2008-11-13 22:59:24 +0000417 }
418
419 // --------------------------------------------------------------------
420
421 /**
422 * Affected Rows
423 *
Derek Jones4b9c6292011-07-01 17:40:48 -0500424 * @access public
425 * @return integer
Derek Allard2067d1a2008-11-13 22:59:24 +0000426 */
427 function affected_rows()
428 {
429 return @ocirowcount($this->stmt_id);
430 }
431
432 // --------------------------------------------------------------------
433
434 /**
435 * Insert ID
436 *
Derek Jones4b9c6292011-07-01 17:40:48 -0500437 * @access public
438 * @return integer
Derek Allard2067d1a2008-11-13 22:59:24 +0000439 */
440 function insert_id()
441 {
442 // not supported in oracle
Derek Allardfac8fbc2010-02-05 16:14:49 +0000443 return $this->display_error('db_unsupported_function');
Derek Allard2067d1a2008-11-13 22:59:24 +0000444 }
445
446 // --------------------------------------------------------------------
447
448 /**
449 * "Count All" query
450 *
451 * Generates a platform-specific query string that counts all records in
452 * the specified database
453 *
Derek Jones4b9c6292011-07-01 17:40:48 -0500454 * @access public
455 * @param string
456 * @return string
Derek Allard2067d1a2008-11-13 22:59:24 +0000457 */
458 function count_all($table = '')
459 {
460 if ($table == '')
Derek Allarde37ab382009-02-03 16:13:57 +0000461 {
462 return 0;
463 }
Derek Allard2067d1a2008-11-13 22:59:24 +0000464
Derek Allarde37ab382009-02-03 16:13:57 +0000465 $query = $this->query($this->_count_string . $this->_protect_identifiers('numrows') . " FROM " . $this->_protect_identifiers($table, TRUE, NULL, FALSE));
Derek Allard2067d1a2008-11-13 22:59:24 +0000466
467 if ($query == FALSE)
Derek Allarde37ab382009-02-03 16:13:57 +0000468 {
Derek Allard2067d1a2008-11-13 22:59:24 +0000469 return 0;
Derek Allarde37ab382009-02-03 16:13:57 +0000470 }
Derek Allard2067d1a2008-11-13 22:59:24 +0000471
472 $row = $query->row();
Greg Aker90248ab2011-08-20 14:23:14 -0500473 $this->_reset_select();
Derek Allarde37ab382009-02-03 16:13:57 +0000474 return (int) $row->numrows;
Derek Allard2067d1a2008-11-13 22:59:24 +0000475 }
476
477 // --------------------------------------------------------------------
478
479 /**
480 * Show table query
481 *
482 * Generates a platform-specific query string so that the table names can be fetched
483 *
Derek Jones4b9c6292011-07-01 17:40:48 -0500484 * @access private
Derek Allard2067d1a2008-11-13 22:59:24 +0000485 * @param boolean
Derek Jones4b9c6292011-07-01 17:40:48 -0500486 * @return string
Derek Allard2067d1a2008-11-13 22:59:24 +0000487 */
488 function _list_tables($prefix_limit = FALSE)
489 {
490 $sql = "SELECT TABLE_NAME FROM ALL_TABLES";
491
492 if ($prefix_limit !== FALSE AND $this->dbprefix != '')
493 {
Greg Aker0d424892010-01-26 02:14:44 +0000494 $sql .= " WHERE TABLE_NAME LIKE '".$this->escape_like_str($this->dbprefix)."%' ".sprintf($this->_like_escape_str, $this->_like_escape_chr);
Derek Allard2067d1a2008-11-13 22:59:24 +0000495 }
Barry Mienydd671972010-10-04 16:33:58 +0200496
Derek Allard2067d1a2008-11-13 22:59:24 +0000497 return $sql;
498 }
499
500 // --------------------------------------------------------------------
501
502 /**
503 * Show column query
504 *
505 * Generates a platform-specific query string so that the column names can be fetched
506 *
Derek Jones4b9c6292011-07-01 17:40:48 -0500507 * @access public
508 * @param string the table name
509 * @return string
Derek Allard2067d1a2008-11-13 22:59:24 +0000510 */
511 function _list_columns($table = '')
512 {
513 return "SELECT COLUMN_NAME FROM all_tab_columns WHERE table_name = '$table'";
514 }
515
516 // --------------------------------------------------------------------
517
518 /**
519 * Field data query
520 *
521 * Generates a platform-specific query so that the column data can be retrieved
522 *
Derek Jones4b9c6292011-07-01 17:40:48 -0500523 * @access public
524 * @param string the table name
525 * @return object
Derek Allard2067d1a2008-11-13 22:59:24 +0000526 */
527 function _field_data($table)
528 {
529 return "SELECT * FROM ".$table." where rownum = 1";
530 }
531
532 // --------------------------------------------------------------------
533
534 /**
535 * The error message string
536 *
Derek Jones4b9c6292011-07-01 17:40:48 -0500537 * @access private
538 * @return string
Derek Allard2067d1a2008-11-13 22:59:24 +0000539 */
540 function _error_message()
541 {
542 $error = ocierror($this->conn_id);
543 return $error['message'];
544 }
545
546 // --------------------------------------------------------------------
547
548 /**
549 * The error message number
550 *
Derek Jones4b9c6292011-07-01 17:40:48 -0500551 * @access private
552 * @return integer
Derek Allard2067d1a2008-11-13 22:59:24 +0000553 */
554 function _error_number()
555 {
556 $error = ocierror($this->conn_id);
557 return $error['code'];
558 }
Barry Mienydd671972010-10-04 16:33:58 +0200559
Derek Allard2067d1a2008-11-13 22:59:24 +0000560 // --------------------------------------------------------------------
561
562 /**
563 * Escape the SQL Identifiers
564 *
565 * This function escapes column and table names
566 *
567 * @access private
568 * @param string
569 * @return string
570 */
571 function _escape_identifiers($item)
572 {
573 if ($this->_escape_char == '')
574 {
575 return $item;
576 }
577
578 foreach ($this->_reserved_identifiers as $id)
579 {
580 if (strpos($item, '.'.$id) !== FALSE)
581 {
Barry Mienydd671972010-10-04 16:33:58 +0200582 $str = $this->_escape_char. str_replace('.', $this->_escape_char.'.', $item);
583
Derek Allard2067d1a2008-11-13 22:59:24 +0000584 // remove duplicates if the user already included the escape
585 return preg_replace('/['.$this->_escape_char.']+/', $this->_escape_char, $str);
Barry Mienydd671972010-10-04 16:33:58 +0200586 }
Derek Allard2067d1a2008-11-13 22:59:24 +0000587 }
Barry Mienydd671972010-10-04 16:33:58 +0200588
Derek Allard2067d1a2008-11-13 22:59:24 +0000589 if (strpos($item, '.') !== FALSE)
590 {
Barry Mienydd671972010-10-04 16:33:58 +0200591 $str = $this->_escape_char.str_replace('.', $this->_escape_char.'.'.$this->_escape_char, $item).$this->_escape_char;
Derek Allard2067d1a2008-11-13 22:59:24 +0000592 }
593 else
594 {
595 $str = $this->_escape_char.$item.$this->_escape_char;
596 }
Barry Mienydd671972010-10-04 16:33:58 +0200597
Derek Allard2067d1a2008-11-13 22:59:24 +0000598 // remove duplicates if the user already included the escape
599 return preg_replace('/['.$this->_escape_char.']+/', $this->_escape_char, $str);
600 }
Barry Mienydd671972010-10-04 16:33:58 +0200601
Derek Allard2067d1a2008-11-13 22:59:24 +0000602 // --------------------------------------------------------------------
603
604 /**
605 * From Tables
606 *
607 * This function implicitly groups FROM tables so there is no confusion
608 * about operator precedence in harmony with SQL standards
609 *
610 * @access public
611 * @param type
612 * @return type
613 */
614 function _from_tables($tables)
615 {
616 if ( ! is_array($tables))
617 {
618 $tables = array($tables);
619 }
Barry Mienydd671972010-10-04 16:33:58 +0200620
Derek Allard2067d1a2008-11-13 22:59:24 +0000621 return implode(', ', $tables);
622 }
623
624 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200625
Derek Allard2067d1a2008-11-13 22:59:24 +0000626 /**
627 * Insert statement
628 *
629 * Generates a platform-specific insert string from the supplied data
630 *
Derek Jones4b9c6292011-07-01 17:40:48 -0500631 * @access public
632 * @param string the table name
633 * @param array the insert keys
634 * @param array the insert values
635 * @return string
Derek Allard2067d1a2008-11-13 22:59:24 +0000636 */
637 function _insert($table, $keys, $values)
638 {
639 return "INSERT INTO ".$table." (".implode(', ', $keys).") VALUES (".implode(', ', $values).")";
640 }
641
642 // --------------------------------------------------------------------
643
644 /**
645 * Update statement
646 *
647 * Generates a platform-specific update string from the supplied data
648 *
649 * @access public
650 * @param string the table name
651 * @param array the update data
652 * @param array the where clause
653 * @param array the orderby clause
654 * @param array the limit clause
655 * @return string
656 */
657 function _update($table, $values, $where, $orderby = array(), $limit = FALSE)
658 {
Pascal Krietec3a4a8d2011-02-14 13:40:08 -0500659 foreach ($values as $key => $val)
Derek Allard2067d1a2008-11-13 22:59:24 +0000660 {
661 $valstr[] = $key." = ".$val;
662 }
Barry Mienydd671972010-10-04 16:33:58 +0200663
Derek Allard2067d1a2008-11-13 22:59:24 +0000664 $limit = ( ! $limit) ? '' : ' LIMIT '.$limit;
Barry Mienydd671972010-10-04 16:33:58 +0200665
Derek Allard2067d1a2008-11-13 22:59:24 +0000666 $orderby = (count($orderby) >= 1)?' ORDER BY '.implode(", ", $orderby):'';
Barry Mienydd671972010-10-04 16:33:58 +0200667
Derek Allard2067d1a2008-11-13 22:59:24 +0000668 $sql = "UPDATE ".$table." SET ".implode(', ', $valstr);
669
670 $sql .= ($where != '' AND count($where) >=1) ? " WHERE ".implode(" ", $where) : '';
671
672 $sql .= $orderby.$limit;
Barry Mienydd671972010-10-04 16:33:58 +0200673
Derek Allard2067d1a2008-11-13 22:59:24 +0000674 return $sql;
675 }
676
677 // --------------------------------------------------------------------
678
679 /**
680 * Truncate statement
681 *
682 * Generates a platform-specific truncate string from the supplied data
683 * If the database does not support the truncate() command
684 * This function maps to "DELETE FROM table"
685 *
686 * @access public
687 * @param string the table name
688 * @return string
Barry Mienydd671972010-10-04 16:33:58 +0200689 */
Derek Allard2067d1a2008-11-13 22:59:24 +0000690 function _truncate($table)
691 {
692 return "TRUNCATE TABLE ".$table;
693 }
Barry Mienydd671972010-10-04 16:33:58 +0200694
Derek Allard2067d1a2008-11-13 22:59:24 +0000695 // --------------------------------------------------------------------
696
697 /**
698 * Delete statement
699 *
700 * Generates a platform-specific delete string from the supplied data
701 *
702 * @access public
703 * @param string the table name
704 * @param array the where clause
705 * @param string the limit clause
706 * @return string
Barry Mienydd671972010-10-04 16:33:58 +0200707 */
Derek Allard2067d1a2008-11-13 22:59:24 +0000708 function _delete($table, $where = array(), $like = array(), $limit = FALSE)
709 {
710 $conditions = '';
711
712 if (count($where) > 0 OR count($like) > 0)
713 {
714 $conditions = "\nWHERE ";
715 $conditions .= implode("\n", $this->ar_where);
716
717 if (count($where) > 0 && count($like) > 0)
718 {
719 $conditions .= " AND ";
720 }
721 $conditions .= implode("\n", $like);
722 }
723
724 $limit = ( ! $limit) ? '' : ' LIMIT '.$limit;
Barry Mienydd671972010-10-04 16:33:58 +0200725
Derek Allard2067d1a2008-11-13 22:59:24 +0000726 return "DELETE FROM ".$table.$conditions.$limit;
727 }
728
729 // --------------------------------------------------------------------
730
731 /**
732 * Limit string
733 *
734 * Generates a platform-specific LIMIT clause
735 *
Derek Jones4b9c6292011-07-01 17:40:48 -0500736 * @access public
737 * @param string the sql query string
738 * @param integer the number of rows to limit the query to
739 * @param integer the offset value
740 * @return string
Derek Allard2067d1a2008-11-13 22:59:24 +0000741 */
742 function _limit($sql, $limit, $offset)
743 {
744 $limit = $offset + $limit;
745 $newsql = "SELECT * FROM (select inner_query.*, rownum rnum FROM ($sql) inner_query WHERE rownum < $limit)";
746
747 if ($offset != 0)
748 {
749 $newsql .= " WHERE rnum >= $offset";
750 }
751
752 // remember that we used limits
753 $this->limit_used = TRUE;
754
755 return $newsql;
Barry Mienydd671972010-10-04 16:33:58 +0200756 }
Derek Allard2067d1a2008-11-13 22:59:24 +0000757
758 // --------------------------------------------------------------------
759
760 /**
761 * Close DB Connection
762 *
Derek Jones4b9c6292011-07-01 17:40:48 -0500763 * @access public
764 * @param resource
765 * @return void
Derek Allard2067d1a2008-11-13 22:59:24 +0000766 */
767 function _close($conn_id)
768 {
769 @ocilogoff($conn_id);
770 }
771
772
773}
774
775
776
777/* End of file oci8_driver.php */
Derek Jonesa3ffbbb2008-05-11 18:18:29 +0000778/* Location: ./system/database/drivers/oci8/oci8_driver.php */