blob: 0243448bae399ad7db0476ae072521aa4c5bdf61 [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 *
Derek Jonesf4a4bd82011-10-20 12:18:42 -05007 * NOTICE OF LICENSE
8 *
9 * Licensed under the Open Software License version 3.0
10 *
11 * This source file is subject to the Open Software License (OSL 3.0) that is
12 * bundled with this package in the files license.txt / license.rst. It is
13 * also available through the world wide web at this URL:
14 * http://opensource.org/licenses/OSL-3.0
15 * If you did not receive a copy of the license and are unable to obtain it
16 * through the world wide web, please send an email to
17 * licensing@ellislab.com so we can send you a copy immediately.
18 *
Barry Mienydd671972010-10-04 16:33:58 +020019 * @package CodeIgniter
Derek Jonesf4a4bd82011-10-20 12:18:42 -050020 * @author EllisLab Dev Team
21 * @copyright Copyright (c) 2008 - 2011, EllisLab, Inc. (http://ellislab.com/)
22 * @license http://opensource.org/licenses/OSL-3.0 Open Software License (OSL 3.0)
Derek Allard2067d1a2008-11-13 22:59:24 +000023 * @link http://codeigniter.com
Barry Mienydd671972010-10-04 16:33:58 +020024 * @since Version 1.0
Derek Allard2067d1a2008-11-13 22:59:24 +000025 * @filesource
26 */
27
28// ------------------------------------------------------------------------
29
30/**
31 * oci8 Database Adapter Class
32 *
33 * Note: _DB is an extender class that the app controller
34 * creates dynamically based on whether the active record
35 * class is being used or not.
36 *
Barry Mienydd671972010-10-04 16:33:58 +020037 * @package CodeIgniter
Derek Jones4b9c6292011-07-01 17:40:48 -050038 * @subpackage Drivers
Derek Allard2067d1a2008-11-13 22:59:24 +000039 * @category Database
Derek Jonesf4a4bd82011-10-20 12:18:42 -050040 * @author EllisLab Dev Team
Derek Allard2067d1a2008-11-13 22:59:24 +000041 * @link http://codeigniter.com/user_guide/database/
42 */
43
44/**
45 * oci8 Database Adapter Class
46 *
47 * This is a modification of the DB_driver class to
48 * permit access to oracle databases
49 *
50 * NOTE: this uses the PHP 4 oci methods
51 *
Derek Jones4b9c6292011-07-01 17:40:48 -050052 * @author Kelly McArdle
Derek Allard2067d1a2008-11-13 22:59:24 +000053 *
54 */
55
56class CI_DB_oci8_driver extends CI_DB {
57
58 var $dbdriver = 'oci8';
Barry Mienydd671972010-10-04 16:33:58 +020059
Derek Allard2067d1a2008-11-13 22:59:24 +000060 // The character used for excaping
61 var $_escape_char = '"';
Barry Mienydd671972010-10-04 16:33:58 +020062
Derek Jonese4ed5832009-02-20 21:44:59 +000063 // clause and character used for LIKE escape sequences
64 var $_like_escape_str = " escape '%s' ";
65 var $_like_escape_chr = '!';
Barry Mienydd671972010-10-04 16:33:58 +020066
Derek Allard2067d1a2008-11-13 22:59:24 +000067 /**
68 * The syntax to count rows is slightly different across different
69 * database engines, so this string appears in each driver and is
70 * used for the count_all() and count_all_results() functions.
71 */
72 var $_count_string = "SELECT COUNT(1) AS ";
73 var $_random_keyword = ' ASC'; // not currently supported
74
75 // Set "auto commit" by default
76 var $_commit = OCI_COMMIT_ON_SUCCESS;
77
78 // need to track statement id and cursor id
79 var $stmt_id;
80 var $curs_id;
81
82 // if we use a limit, we will add a field that will
83 // throw off num_fields later
84 var $limit_used;
85
86 /**
87 * Non-persistent database connection
88 *
Derek Jones4b9c6292011-07-01 17:40:48 -050089 * @access private called by the base class
90 * @return resource
Derek Allard2067d1a2008-11-13 22:59:24 +000091 */
92 function db_connect()
93 {
narfbg068e3de2011-09-17 21:38:46 +030094 return @ocilogon($this->username, $this->password, $this->hostname, $this->char_set);
Derek Allard2067d1a2008-11-13 22:59:24 +000095 }
96
97 // --------------------------------------------------------------------
98
99 /**
100 * Persistent database connection
101 *
Derek Jones4b9c6292011-07-01 17:40:48 -0500102 * @access private called by the base class
103 * @return resource
Derek Allard2067d1a2008-11-13 22:59:24 +0000104 */
105 function db_pconnect()
106 {
narfbg068e3de2011-09-17 21:38:46 +0300107 return @ociplogon($this->username, $this->password, $this->hostname, $this->char_set);
Derek Allard2067d1a2008-11-13 22:59:24 +0000108 }
109
110 // --------------------------------------------------------------------
111
112 /**
Derek Jones87cbafc2009-02-27 16:29:59 +0000113 * Reconnect
114 *
115 * Keep / reestablish the db connection if no queries have been
116 * sent for a length of time exceeding the server's idle timeout
117 *
118 * @access public
119 * @return void
120 */
121 function reconnect()
122 {
123 // not implemented in oracle
124 }
125
126 // --------------------------------------------------------------------
127
128 /**
Derek Allard2067d1a2008-11-13 22:59:24 +0000129 * Select the database
130 *
Derek Jones4b9c6292011-07-01 17:40:48 -0500131 * @access private called by the base class
132 * @return resource
Derek Allard2067d1a2008-11-13 22:59:24 +0000133 */
134 function db_select()
135 {
136 return TRUE;
137 }
138
139 // --------------------------------------------------------------------
140
141 /**
142 * Set client character set
143 *
144 * @access public
145 * @param string
146 * @param string
147 * @return resource
148 */
149 function db_set_charset($charset, $collation)
150 {
narfbg068e3de2011-09-17 21:38:46 +0300151 // this is done upon connect
Derek Allard2067d1a2008-11-13 22:59:24 +0000152 return TRUE;
153 }
154
155 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200156
Derek Allard2067d1a2008-11-13 22:59:24 +0000157 /**
158 * Version number query string
159 *
Derek Jones4b9c6292011-07-01 17:40:48 -0500160 * @access public
161 * @return string
Derek Allard2067d1a2008-11-13 22:59:24 +0000162 */
163 function _version()
164 {
165 return ociserverversion($this->conn_id);
166 }
167
168 // --------------------------------------------------------------------
169
170 /**
171 * Execute the query
172 *
Derek Jones4b9c6292011-07-01 17:40:48 -0500173 * @access private called by the base class
174 * @param string an SQL query
175 * @return resource
Derek Allard2067d1a2008-11-13 22:59:24 +0000176 */
177 function _execute($sql)
178 {
179 // oracle must parse the query before it is run. All of the actions with
180 // the query are based on the statement id returned by ociparse
181 $this->stmt_id = FALSE;
182 $this->_set_stmt_id($sql);
183 ocisetprefetch($this->stmt_id, 1000);
184 return @ociexecute($this->stmt_id, $this->_commit);
185 }
186
187 /**
188 * Generate a statement ID
189 *
Derek Jones4b9c6292011-07-01 17:40:48 -0500190 * @access private
191 * @param string an SQL query
192 * @return none
Derek Allard2067d1a2008-11-13 22:59:24 +0000193 */
194 function _set_stmt_id($sql)
195 {
196 if ( ! is_resource($this->stmt_id))
197 {
198 $this->stmt_id = ociparse($this->conn_id, $this->_prep_query($sql));
199 }
200 }
201
202 // --------------------------------------------------------------------
203
204 /**
205 * Prep the query
206 *
207 * If needed, each database adapter can prep the query string
208 *
Derek Jones4b9c6292011-07-01 17:40:48 -0500209 * @access private called by execute()
210 * @param string an SQL query
211 * @return string
Derek Allard2067d1a2008-11-13 22:59:24 +0000212 */
213 function _prep_query($sql)
214 {
215 return $sql;
216 }
217
218 // --------------------------------------------------------------------
219
220 /**
Derek Jones4b9c6292011-07-01 17:40:48 -0500221 * getCursor. Returns a cursor from the datbase
Derek Allard2067d1a2008-11-13 22:59:24 +0000222 *
Derek Jones4b9c6292011-07-01 17:40:48 -0500223 * @access public
224 * @return cursor id
Derek Allard2067d1a2008-11-13 22:59:24 +0000225 */
226 function get_cursor()
227 {
228 $this->curs_id = ocinewcursor($this->conn_id);
229 return $this->curs_id;
230 }
231
232 // --------------------------------------------------------------------
233
234 /**
Derek Jones4b9c6292011-07-01 17:40:48 -0500235 * Stored Procedure. Executes a stored procedure
Derek Allard2067d1a2008-11-13 22:59:24 +0000236 *
Derek Jones4b9c6292011-07-01 17:40:48 -0500237 * @access public
238 * @param package package stored procedure is in
239 * @param procedure stored procedure to execute
240 * @param params array of parameters
241 * @return array
Derek Allard2067d1a2008-11-13 22:59:24 +0000242 *
243 * params array keys
244 *
Derek Jones4b9c6292011-07-01 17:40:48 -0500245 * KEY OPTIONAL NOTES
Derek Allard2067d1a2008-11-13 22:59:24 +0000246 * name no the name of the parameter should be in :<param_name> format
Derek Jones4b9c6292011-07-01 17:40:48 -0500247 * value no the value of the parameter. If this is an OUT or IN OUT parameter,
Derek Allard2067d1a2008-11-13 22:59:24 +0000248 * this should be a reference to a variable
249 * type yes the type of the parameter
250 * length yes the max size of the parameter
251 */
252 function stored_procedure($package, $procedure, $params)
253 {
254 if ($package == '' OR $procedure == '' OR ! is_array($params))
255 {
256 if ($this->db_debug)
257 {
258 log_message('error', 'Invalid query: '.$package.'.'.$procedure);
Derek Allardfac8fbc2010-02-05 16:14:49 +0000259 return $this->display_error('db_invalid_query');
Derek Allard2067d1a2008-11-13 22:59:24 +0000260 }
261 return FALSE;
262 }
Barry Mienydd671972010-10-04 16:33:58 +0200263
Derek Allard2067d1a2008-11-13 22:59:24 +0000264 // build the query string
265 $sql = "begin $package.$procedure(";
266
267 $have_cursor = FALSE;
Pascal Krietec3a4a8d2011-02-14 13:40:08 -0500268 foreach ($params as $param)
Derek Allard2067d1a2008-11-13 22:59:24 +0000269 {
270 $sql .= $param['name'] . ",";
Barry Mienydd671972010-10-04 16:33:58 +0200271
Derek Allard2067d1a2008-11-13 22:59:24 +0000272 if (array_key_exists('type', $param) && ($param['type'] == OCI_B_CURSOR))
273 {
274 $have_cursor = TRUE;
275 }
276 }
277 $sql = trim($sql, ",") . "); end;";
Barry Mienydd671972010-10-04 16:33:58 +0200278
Derek Allard2067d1a2008-11-13 22:59:24 +0000279 $this->stmt_id = FALSE;
280 $this->_set_stmt_id($sql);
281 $this->_bind_params($params);
282 $this->query($sql, FALSE, $have_cursor);
283 }
Barry Mienydd671972010-10-04 16:33:58 +0200284
Derek Allard2067d1a2008-11-13 22:59:24 +0000285 // --------------------------------------------------------------------
286
287 /**
288 * Bind parameters
289 *
Derek Jones4b9c6292011-07-01 17:40:48 -0500290 * @access private
291 * @return none
Derek Allard2067d1a2008-11-13 22:59:24 +0000292 */
293 function _bind_params($params)
294 {
295 if ( ! is_array($params) OR ! is_resource($this->stmt_id))
296 {
297 return;
298 }
Barry Mienydd671972010-10-04 16:33:58 +0200299
Derek Allard2067d1a2008-11-13 22:59:24 +0000300 foreach ($params as $param)
301 {
Barry Mienydd671972010-10-04 16:33:58 +0200302 foreach (array('name', 'value', 'type', 'length') as $val)
Derek Allard2067d1a2008-11-13 22:59:24 +0000303 {
304 if ( ! isset($param[$val]))
305 {
306 $param[$val] = '';
307 }
308 }
309
310 ocibindbyname($this->stmt_id, $param['name'], $param['value'], $param['length'], $param['type']);
311 }
312 }
313
314 // --------------------------------------------------------------------
315
316 /**
317 * Begin Transaction
318 *
319 * @access public
Barry Mienydd671972010-10-04 16:33:58 +0200320 * @return bool
321 */
Derek Allard2067d1a2008-11-13 22:59:24 +0000322 function trans_begin($test_mode = FALSE)
323 {
324 if ( ! $this->trans_enabled)
325 {
326 return TRUE;
327 }
Barry Mienydd671972010-10-04 16:33:58 +0200328
Derek Allard2067d1a2008-11-13 22:59:24 +0000329 // When transactions are nested we only begin/commit/rollback the outermost ones
330 if ($this->_trans_depth > 0)
331 {
332 return TRUE;
333 }
Barry Mienydd671972010-10-04 16:33:58 +0200334
Derek Allard2067d1a2008-11-13 22:59:24 +0000335 // Reset the transaction failure flag.
336 // If the $test_mode flag is set to TRUE transactions will be rolled back
337 // even if the queries produce a successful result.
338 $this->_trans_failure = ($test_mode === TRUE) ? TRUE : FALSE;
Barry Mienydd671972010-10-04 16:33:58 +0200339
Derek Allard2067d1a2008-11-13 22:59:24 +0000340 $this->_commit = OCI_DEFAULT;
341 return TRUE;
342 }
343
344 // --------------------------------------------------------------------
345
346 /**
347 * Commit Transaction
348 *
349 * @access public
Barry Mienydd671972010-10-04 16:33:58 +0200350 * @return bool
351 */
Derek Allard2067d1a2008-11-13 22:59:24 +0000352 function trans_commit()
353 {
354 if ( ! $this->trans_enabled)
355 {
356 return TRUE;
357 }
358
359 // When transactions are nested we only begin/commit/rollback the outermost ones
360 if ($this->_trans_depth > 0)
361 {
362 return TRUE;
363 }
364
365 $ret = OCIcommit($this->conn_id);
366 $this->_commit = OCI_COMMIT_ON_SUCCESS;
367 return $ret;
368 }
369
370 // --------------------------------------------------------------------
371
372 /**
373 * Rollback Transaction
374 *
375 * @access public
Barry Mienydd671972010-10-04 16:33:58 +0200376 * @return bool
377 */
Derek Allard2067d1a2008-11-13 22:59:24 +0000378 function trans_rollback()
379 {
380 if ( ! $this->trans_enabled)
381 {
382 return TRUE;
383 }
384
385 // When transactions are nested we only begin/commit/rollback the outermost ones
386 if ($this->_trans_depth > 0)
387 {
388 return TRUE;
389 }
390
391 $ret = OCIrollback($this->conn_id);
392 $this->_commit = OCI_COMMIT_ON_SUCCESS;
393 return $ret;
394 }
395
396 // --------------------------------------------------------------------
397
398 /**
399 * Escape String
400 *
Derek Jones4b9c6292011-07-01 17:40:48 -0500401 * @access public
402 * @param string
Derek Jonese4ed5832009-02-20 21:44:59 +0000403 * @param bool whether or not the string will be used in a LIKE condition
Derek Jones4b9c6292011-07-01 17:40:48 -0500404 * @return string
Derek Allard2067d1a2008-11-13 22:59:24 +0000405 */
Derek Jonese4ed5832009-02-20 21:44:59 +0000406 function escape_str($str, $like = FALSE)
Derek Allard2067d1a2008-11-13 22:59:24 +0000407 {
Derek Jonese4ed5832009-02-20 21:44:59 +0000408 if (is_array($str))
409 {
Pascal Krietec3a4a8d2011-02-14 13:40:08 -0500410 foreach ($str as $key => $val)
Barry Mienydd671972010-10-04 16:33:58 +0200411 {
Derek Jonese4ed5832009-02-20 21:44:59 +0000412 $str[$key] = $this->escape_str($val, $like);
Barry Mienydd671972010-10-04 16:33:58 +0200413 }
414
415 return $str;
416 }
Derek Jonese4ed5832009-02-20 21:44:59 +0000417
Greg Aker757dda62010-04-14 19:06:19 -0500418 $str = remove_invisible_characters($str);
Michiel Vugteveeneaa55412011-08-25 21:22:49 +0200419 $str = str_replace("'", "''", $str);
Barry Mienydd671972010-10-04 16:33:58 +0200420
Derek Jonese4ed5832009-02-20 21:44:59 +0000421 // escape LIKE condition wildcards
422 if ($like === TRUE)
423 {
424 $str = str_replace( array('%', '_', $this->_like_escape_chr),
425 array($this->_like_escape_chr.'%', $this->_like_escape_chr.'_', $this->_like_escape_chr.$this->_like_escape_chr),
426 $str);
427 }
Barry Mienydd671972010-10-04 16:33:58 +0200428
Derek Jonese4ed5832009-02-20 21:44:59 +0000429 return $str;
Derek Allard2067d1a2008-11-13 22:59:24 +0000430 }
431
432 // --------------------------------------------------------------------
433
434 /**
435 * Affected Rows
436 *
Derek Jones4b9c6292011-07-01 17:40:48 -0500437 * @access public
438 * @return integer
Derek Allard2067d1a2008-11-13 22:59:24 +0000439 */
440 function affected_rows()
441 {
442 return @ocirowcount($this->stmt_id);
443 }
444
445 // --------------------------------------------------------------------
446
447 /**
448 * Insert ID
449 *
Derek Jones4b9c6292011-07-01 17:40:48 -0500450 * @access public
451 * @return integer
Derek Allard2067d1a2008-11-13 22:59:24 +0000452 */
453 function insert_id()
454 {
455 // not supported in oracle
Derek Allardfac8fbc2010-02-05 16:14:49 +0000456 return $this->display_error('db_unsupported_function');
Derek Allard2067d1a2008-11-13 22:59:24 +0000457 }
458
459 // --------------------------------------------------------------------
460
461 /**
462 * "Count All" query
463 *
464 * Generates a platform-specific query string that counts all records in
465 * the specified database
466 *
Derek Jones4b9c6292011-07-01 17:40:48 -0500467 * @access public
468 * @param string
469 * @return string
Derek Allard2067d1a2008-11-13 22:59:24 +0000470 */
471 function count_all($table = '')
472 {
473 if ($table == '')
Derek Allarde37ab382009-02-03 16:13:57 +0000474 {
475 return 0;
476 }
Derek Allard2067d1a2008-11-13 22:59:24 +0000477
Derek Allarde37ab382009-02-03 16:13:57 +0000478 $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 +0000479
480 if ($query == FALSE)
Derek Allarde37ab382009-02-03 16:13:57 +0000481 {
Derek Allard2067d1a2008-11-13 22:59:24 +0000482 return 0;
Derek Allarde37ab382009-02-03 16:13:57 +0000483 }
Derek Allard2067d1a2008-11-13 22:59:24 +0000484
485 $row = $query->row();
Greg Aker90248ab2011-08-20 14:23:14 -0500486 $this->_reset_select();
Derek Allarde37ab382009-02-03 16:13:57 +0000487 return (int) $row->numrows;
Derek Allard2067d1a2008-11-13 22:59:24 +0000488 }
489
490 // --------------------------------------------------------------------
491
492 /**
493 * Show table query
494 *
495 * Generates a platform-specific query string so that the table names can be fetched
496 *
Derek Jones4b9c6292011-07-01 17:40:48 -0500497 * @access private
Derek Allard2067d1a2008-11-13 22:59:24 +0000498 * @param boolean
Derek Jones4b9c6292011-07-01 17:40:48 -0500499 * @return string
Derek Allard2067d1a2008-11-13 22:59:24 +0000500 */
501 function _list_tables($prefix_limit = FALSE)
502 {
503 $sql = "SELECT TABLE_NAME FROM ALL_TABLES";
504
505 if ($prefix_limit !== FALSE AND $this->dbprefix != '')
506 {
Greg Aker0d424892010-01-26 02:14:44 +0000507 $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 +0000508 }
Barry Mienydd671972010-10-04 16:33:58 +0200509
Derek Allard2067d1a2008-11-13 22:59:24 +0000510 return $sql;
511 }
512
513 // --------------------------------------------------------------------
514
515 /**
516 * Show column query
517 *
518 * Generates a platform-specific query string so that the column names can be fetched
519 *
Derek Jones4b9c6292011-07-01 17:40:48 -0500520 * @access public
521 * @param string the table name
522 * @return string
Derek Allard2067d1a2008-11-13 22:59:24 +0000523 */
524 function _list_columns($table = '')
525 {
526 return "SELECT COLUMN_NAME FROM all_tab_columns WHERE table_name = '$table'";
527 }
528
529 // --------------------------------------------------------------------
530
531 /**
532 * Field data query
533 *
534 * Generates a platform-specific query so that the column data can be retrieved
535 *
Derek Jones4b9c6292011-07-01 17:40:48 -0500536 * @access public
537 * @param string the table name
538 * @return object
Derek Allard2067d1a2008-11-13 22:59:24 +0000539 */
540 function _field_data($table)
541 {
542 return "SELECT * FROM ".$table." where rownum = 1";
543 }
544
545 // --------------------------------------------------------------------
546
547 /**
548 * The error message string
549 *
Derek Jones4b9c6292011-07-01 17:40:48 -0500550 * @access private
551 * @return string
Derek Allard2067d1a2008-11-13 22:59:24 +0000552 */
553 function _error_message()
554 {
555 $error = ocierror($this->conn_id);
556 return $error['message'];
557 }
558
559 // --------------------------------------------------------------------
560
561 /**
562 * The error message number
563 *
Derek Jones4b9c6292011-07-01 17:40:48 -0500564 * @access private
565 * @return integer
Derek Allard2067d1a2008-11-13 22:59:24 +0000566 */
567 function _error_number()
568 {
569 $error = ocierror($this->conn_id);
570 return $error['code'];
571 }
Barry Mienydd671972010-10-04 16:33:58 +0200572
Derek Allard2067d1a2008-11-13 22:59:24 +0000573 // --------------------------------------------------------------------
574
575 /**
576 * Escape the SQL Identifiers
577 *
578 * This function escapes column and table names
579 *
580 * @access private
581 * @param string
582 * @return string
583 */
584 function _escape_identifiers($item)
585 {
586 if ($this->_escape_char == '')
587 {
588 return $item;
589 }
590
591 foreach ($this->_reserved_identifiers as $id)
592 {
593 if (strpos($item, '.'.$id) !== FALSE)
594 {
Barry Mienydd671972010-10-04 16:33:58 +0200595 $str = $this->_escape_char. str_replace('.', $this->_escape_char.'.', $item);
596
Derek Allard2067d1a2008-11-13 22:59:24 +0000597 // remove duplicates if the user already included the escape
598 return preg_replace('/['.$this->_escape_char.']+/', $this->_escape_char, $str);
Barry Mienydd671972010-10-04 16:33:58 +0200599 }
Derek Allard2067d1a2008-11-13 22:59:24 +0000600 }
Barry Mienydd671972010-10-04 16:33:58 +0200601
Derek Allard2067d1a2008-11-13 22:59:24 +0000602 if (strpos($item, '.') !== FALSE)
603 {
Barry Mienydd671972010-10-04 16:33:58 +0200604 $str = $this->_escape_char.str_replace('.', $this->_escape_char.'.'.$this->_escape_char, $item).$this->_escape_char;
Derek Allard2067d1a2008-11-13 22:59:24 +0000605 }
606 else
607 {
608 $str = $this->_escape_char.$item.$this->_escape_char;
609 }
Barry Mienydd671972010-10-04 16:33:58 +0200610
Derek Allard2067d1a2008-11-13 22:59:24 +0000611 // remove duplicates if the user already included the escape
612 return preg_replace('/['.$this->_escape_char.']+/', $this->_escape_char, $str);
613 }
Barry Mienydd671972010-10-04 16:33:58 +0200614
Derek Allard2067d1a2008-11-13 22:59:24 +0000615 // --------------------------------------------------------------------
616
617 /**
618 * From Tables
619 *
620 * This function implicitly groups FROM tables so there is no confusion
621 * about operator precedence in harmony with SQL standards
622 *
623 * @access public
624 * @param type
625 * @return type
626 */
627 function _from_tables($tables)
628 {
629 if ( ! is_array($tables))
630 {
631 $tables = array($tables);
632 }
Barry Mienydd671972010-10-04 16:33:58 +0200633
Derek Allard2067d1a2008-11-13 22:59:24 +0000634 return implode(', ', $tables);
635 }
636
637 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200638
Derek Allard2067d1a2008-11-13 22:59:24 +0000639 /**
640 * Insert statement
641 *
642 * Generates a platform-specific insert string from the supplied data
643 *
Derek Jones4b9c6292011-07-01 17:40:48 -0500644 * @access public
645 * @param string the table name
646 * @param array the insert keys
647 * @param array the insert values
648 * @return string
Derek Allard2067d1a2008-11-13 22:59:24 +0000649 */
650 function _insert($table, $keys, $values)
651 {
652 return "INSERT INTO ".$table." (".implode(', ', $keys).") VALUES (".implode(', ', $values).")";
653 }
654
655 // --------------------------------------------------------------------
656
657 /**
Andrey Andreev99c6dd42011-09-23 03:07:01 +0300658 * Insert_batch statement
659 *
660 * Generates a platform-specific insert string from the supplied data
661 *
662 * @access public
663 * @param string the table name
664 * @param array the insert keys
665 * @param array the insert values
666 * @return string
667 */
668 function _insert_batch($table, $keys, $values)
669 {
670 $keys = implode(', ', $keys);
671 $sql = "INSERT ALL\n";
672
673 for ($i = 0, $c = count($values); $i < $c; $i++)
Andrey Andreevb83c4082011-09-23 03:32:45 +0300674 {
Andrey Andreev99c6dd42011-09-23 03:07:01 +0300675 $sql .= ' INTO ' . $table . ' (' . $keys . ') VALUES ' . $values[$i] . "\n";
Andrey Andreevb83c4082011-09-23 03:32:45 +0300676 }
Andrey Andreev99c6dd42011-09-23 03:07:01 +0300677
678 $sql .= 'SELECT * FROM dual';
679
680 return $sql;
681 }
682
683 // --------------------------------------------------------------------
684
685 /**
Derek Allard2067d1a2008-11-13 22:59:24 +0000686 * Update statement
687 *
688 * Generates a platform-specific update string from the supplied data
689 *
690 * @access public
691 * @param string the table name
692 * @param array the update data
693 * @param array the where clause
694 * @param array the orderby clause
695 * @param array the limit clause
696 * @return string
697 */
698 function _update($table, $values, $where, $orderby = array(), $limit = FALSE)
699 {
Pascal Krietec3a4a8d2011-02-14 13:40:08 -0500700 foreach ($values as $key => $val)
Derek Allard2067d1a2008-11-13 22:59:24 +0000701 {
702 $valstr[] = $key." = ".$val;
703 }
Barry Mienydd671972010-10-04 16:33:58 +0200704
Derek Allard2067d1a2008-11-13 22:59:24 +0000705 $limit = ( ! $limit) ? '' : ' LIMIT '.$limit;
Barry Mienydd671972010-10-04 16:33:58 +0200706
Derek Allard2067d1a2008-11-13 22:59:24 +0000707 $orderby = (count($orderby) >= 1)?' ORDER BY '.implode(", ", $orderby):'';
Barry Mienydd671972010-10-04 16:33:58 +0200708
Derek Allard2067d1a2008-11-13 22:59:24 +0000709 $sql = "UPDATE ".$table." SET ".implode(', ', $valstr);
710
711 $sql .= ($where != '' AND count($where) >=1) ? " WHERE ".implode(" ", $where) : '';
712
713 $sql .= $orderby.$limit;
Barry Mienydd671972010-10-04 16:33:58 +0200714
Derek Allard2067d1a2008-11-13 22:59:24 +0000715 return $sql;
716 }
717
718 // --------------------------------------------------------------------
719
720 /**
721 * Truncate statement
722 *
723 * Generates a platform-specific truncate string from the supplied data
724 * If the database does not support the truncate() command
725 * This function maps to "DELETE FROM table"
726 *
727 * @access public
728 * @param string the table name
729 * @return string
Barry Mienydd671972010-10-04 16:33:58 +0200730 */
Derek Allard2067d1a2008-11-13 22:59:24 +0000731 function _truncate($table)
732 {
733 return "TRUNCATE TABLE ".$table;
734 }
Barry Mienydd671972010-10-04 16:33:58 +0200735
Derek Allard2067d1a2008-11-13 22:59:24 +0000736 // --------------------------------------------------------------------
737
738 /**
739 * Delete statement
740 *
741 * Generates a platform-specific delete string from the supplied data
742 *
743 * @access public
744 * @param string the table name
745 * @param array the where clause
746 * @param string the limit clause
747 * @return string
Barry Mienydd671972010-10-04 16:33:58 +0200748 */
Derek Allard2067d1a2008-11-13 22:59:24 +0000749 function _delete($table, $where = array(), $like = array(), $limit = FALSE)
750 {
751 $conditions = '';
752
753 if (count($where) > 0 OR count($like) > 0)
754 {
755 $conditions = "\nWHERE ";
756 $conditions .= implode("\n", $this->ar_where);
757
758 if (count($where) > 0 && count($like) > 0)
759 {
760 $conditions .= " AND ";
761 }
762 $conditions .= implode("\n", $like);
763 }
764
765 $limit = ( ! $limit) ? '' : ' LIMIT '.$limit;
Barry Mienydd671972010-10-04 16:33:58 +0200766
Derek Allard2067d1a2008-11-13 22:59:24 +0000767 return "DELETE FROM ".$table.$conditions.$limit;
768 }
769
770 // --------------------------------------------------------------------
771
772 /**
773 * Limit string
774 *
775 * Generates a platform-specific LIMIT clause
776 *
Derek Jones4b9c6292011-07-01 17:40:48 -0500777 * @access public
778 * @param string the sql query string
779 * @param integer the number of rows to limit the query to
780 * @param integer the offset value
781 * @return string
Derek Allard2067d1a2008-11-13 22:59:24 +0000782 */
783 function _limit($sql, $limit, $offset)
784 {
785 $limit = $offset + $limit;
786 $newsql = "SELECT * FROM (select inner_query.*, rownum rnum FROM ($sql) inner_query WHERE rownum < $limit)";
787
788 if ($offset != 0)
789 {
790 $newsql .= " WHERE rnum >= $offset";
791 }
792
793 // remember that we used limits
794 $this->limit_used = TRUE;
795
796 return $newsql;
Barry Mienydd671972010-10-04 16:33:58 +0200797 }
Derek Allard2067d1a2008-11-13 22:59:24 +0000798
799 // --------------------------------------------------------------------
800
801 /**
802 * Close DB Connection
803 *
Derek Jones4b9c6292011-07-01 17:40:48 -0500804 * @access public
805 * @param resource
806 * @return void
Derek Allard2067d1a2008-11-13 22:59:24 +0000807 */
808 function _close($conn_id)
809 {
810 @ocilogoff($conn_id);
811 }
812
813
814}
815
816
817
818/* End of file oci8_driver.php */
Andrey Andreev99c6dd42011-09-23 03:07:01 +0300819/* Location: ./system/database/drivers/oci8/oci8_driver.php */