Andrey Andreev | da12373 | 2012-03-20 22:27:40 +0200 | [diff] [blame] | 1 | <?php if ( ! defined('BASEPATH')) exit('No direct script access allowed'); |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 2 | /** |
| 3 | * CodeIgniter |
| 4 | * |
Phil Sturgeon | 07c1ac8 | 2012-03-09 17:03:37 +0000 | [diff] [blame] | 5 | * An open source application development framework for PHP 5.2.4 or newer |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 6 | * |
Derek Jones | f4a4bd8 | 2011-10-20 12:18:42 -0500 | [diff] [blame] | 7 | * NOTICE OF LICENSE |
Andrey Andreev | da12373 | 2012-03-20 22:27:40 +0200 | [diff] [blame] | 8 | * |
Derek Jones | f4a4bd8 | 2011-10-20 12:18:42 -0500 | [diff] [blame] | 9 | * Licensed under the Open Software License version 3.0 |
Andrey Andreev | da12373 | 2012-03-20 22:27:40 +0200 | [diff] [blame] | 10 | * |
Derek Jones | f4a4bd8 | 2011-10-20 12:18:42 -0500 | [diff] [blame] | 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 Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 19 | * @package CodeIgniter |
Derek Jones | f4a4bd8 | 2011-10-20 12:18:42 -0500 | [diff] [blame] | 20 | * @author EllisLab Dev Team |
Greg Aker | 0defe5d | 2012-01-01 18:46:41 -0600 | [diff] [blame] | 21 | * @copyright Copyright (c) 2008 - 2012, EllisLab, Inc. (http://ellislab.com/) |
Derek Jones | f4a4bd8 | 2011-10-20 12:18:42 -0500 | [diff] [blame] | 22 | * @license http://opensource.org/licenses/OSL-3.0 Open Software License (OSL 3.0) |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 23 | * @link http://codeigniter.com |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 24 | * @since Version 1.0 |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 25 | * @filesource |
| 26 | */ |
| 27 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 28 | /** |
| 29 | * oci8 Database Adapter Class |
| 30 | * |
| 31 | * Note: _DB is an extender class that the app controller |
| 32 | * creates dynamically based on whether the active record |
| 33 | * class is being used or not. |
| 34 | * |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 35 | * @package CodeIgniter |
Derek Jones | 4b9c629 | 2011-07-01 17:40:48 -0500 | [diff] [blame] | 36 | * @subpackage Drivers |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 37 | * @category Database |
Derek Jones | f4a4bd8 | 2011-10-20 12:18:42 -0500 | [diff] [blame] | 38 | * @author EllisLab Dev Team |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 39 | * @link http://codeigniter.com/user_guide/database/ |
| 40 | */ |
| 41 | |
| 42 | /** |
| 43 | * oci8 Database Adapter Class |
| 44 | * |
| 45 | * This is a modification of the DB_driver class to |
| 46 | * permit access to oracle databases |
| 47 | * |
Derek Jones | 4b9c629 | 2011-07-01 17:40:48 -0500 | [diff] [blame] | 48 | * @author Kelly McArdle |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 49 | * |
| 50 | */ |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 51 | class CI_DB_oci8_driver extends CI_DB { |
| 52 | |
Andrey Andreev | da12373 | 2012-03-20 22:27:40 +0200 | [diff] [blame] | 53 | public $dbdriver = 'oci8'; |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 54 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 55 | // The character used for excaping |
Andrey Andreev | da12373 | 2012-03-20 22:27:40 +0200 | [diff] [blame] | 56 | protected $_escape_char = '"'; |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 57 | |
Derek Jones | e4ed583 | 2009-02-20 21:44:59 +0000 | [diff] [blame] | 58 | // clause and character used for LIKE escape sequences |
Andrey Andreev | da12373 | 2012-03-20 22:27:40 +0200 | [diff] [blame] | 59 | protected $_like_escape_str = " ESCAPE '%s' "; |
| 60 | protected $_like_escape_chr = '!'; |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 61 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 62 | /** |
| 63 | * The syntax to count rows is slightly different across different |
| 64 | * database engines, so this string appears in each driver and is |
| 65 | * used for the count_all() and count_all_results() functions. |
| 66 | */ |
Andrey Andreev | da12373 | 2012-03-20 22:27:40 +0200 | [diff] [blame] | 67 | protected $_count_string = 'SELECT COUNT(1) AS '; |
| 68 | protected $_random_keyword = ' ASC'; // not currently supported |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 69 | |
| 70 | // Set "auto commit" by default |
Andrey Andreev | da12373 | 2012-03-20 22:27:40 +0200 | [diff] [blame] | 71 | protected $_commit = OCI_COMMIT_ON_SUCCESS; |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 72 | |
| 73 | // need to track statement id and cursor id |
Andrey Andreev | da12373 | 2012-03-20 22:27:40 +0200 | [diff] [blame] | 74 | public $stmt_id; |
| 75 | public $curs_id; |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 76 | |
| 77 | // if we use a limit, we will add a field that will |
| 78 | // throw off num_fields later |
Andrey Andreev | da12373 | 2012-03-20 22:27:40 +0200 | [diff] [blame] | 79 | public $limit_used; |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 80 | |
Andrey Andreev | 59ad0af | 2012-03-26 12:47:45 +0300 | [diff] [blame] | 81 | public function __construct($params) |
| 82 | { |
| 83 | parent::__construct($params); |
| 84 | |
| 85 | $valid_dsns = array( |
| 86 | 'tns' => '/^\(DESCRIPTION=(\(.+\)){2,}\)$/', // TNS |
| 87 | // Easy Connect string (Oracle 10g+) |
| 88 | 'ec' => '/^(\/\/)?[a-z0-9.:_-]+(:[1-9][0-9]{0,4})?(\/[a-z0-9$_]+)?(:[^\/])?(\/[a-z0-9$_]+)?$/i', |
| 89 | 'in' => '/^[a-z0-9$_]+$/i' // Instance name (defined in tnsnames.ora) |
| 90 | ); |
| 91 | |
| 92 | /* Space characters don't have any effect when actually |
| 93 | * connecting, but can be a hassle while validating the DSN. |
| 94 | */ |
| 95 | $this->dsn = str_replace(array("\n", "\r", "\t", ' '), '', $this->dsn); |
| 96 | |
| 97 | if ($this->dsn !== '') |
| 98 | { |
| 99 | foreach ($valid_dsns as $regexp) |
| 100 | { |
| 101 | if (preg_match($regexp, $this->dsn)) |
| 102 | { |
| 103 | return; |
| 104 | } |
| 105 | } |
| 106 | } |
| 107 | |
| 108 | // Legacy support for TNS in the hostname configuration field |
| 109 | $this->hostname = str_replace(array("\n", "\r", "\t", ' '), '', $this->hostname); |
| 110 | if (preg_match($valid_dsns['tns'], $this->hostname)) |
| 111 | { |
| 112 | $this->dsn = $this->hostname; |
| 113 | return; |
| 114 | } |
| 115 | elseif ($this->hostname !== '' && strpos($this->hostname, '/') === FALSE && strpos($this->hostname, ':') === FALSE |
| 116 | && (( ! empty($this->port) && ctype_digit($this->port)) OR $this->database !== '')) |
| 117 | { |
| 118 | /* If the hostname field isn't empty, doesn't contain |
| 119 | * ':' and/or '/' and if port and/or database aren't |
| 120 | * empty, then the hostname field is most likely indeed |
| 121 | * just a hostname. Therefore we'll try and build an |
| 122 | * Easy Connect string from these 3 settings, assuming |
| 123 | * that the database field is a service name. |
| 124 | */ |
| 125 | $this->dsn = $this->hostname |
| 126 | .(( ! empty($this->port) && ctype_digit($this->port)) ? ':'.$this->port : '') |
| 127 | .($this->database !== '' ? '/'.ltrim($this->database, '/') : ''); |
| 128 | |
| 129 | if (preg_match($valid_dsns['ec'], $this->dsn)) |
| 130 | { |
| 131 | return; |
| 132 | } |
| 133 | } |
| 134 | |
| 135 | /* At this point, we can only try and validate the hostname and |
| 136 | * database fields separately as DSNs. |
| 137 | */ |
| 138 | if (preg_match($valid_dsns['ec'], $this->hostname) OR preg_match($valid_dsns['in'], $this->hostname)) |
| 139 | { |
| 140 | $this->dsn = $this->hostname; |
| 141 | return; |
| 142 | } |
| 143 | |
| 144 | $this->database = str_replace(array("\n", "\r", "\t", ' '), '', $this->database); |
| 145 | foreach ($valid_dsns as $regexp) |
| 146 | { |
| 147 | if (preg_match($regexp, $this->database)) |
| 148 | { |
| 149 | return; |
| 150 | } |
| 151 | } |
| 152 | |
| 153 | /* Well - OK, an empty string should work as well. |
| 154 | * PHP will try to use environment variables to |
| 155 | * determine which Oracle instance to connect to. |
| 156 | */ |
| 157 | $this->dsn = ''; |
| 158 | } |
| 159 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 160 | /** |
| 161 | * Non-persistent database connection |
| 162 | * |
Andrey Andreev | da12373 | 2012-03-20 22:27:40 +0200 | [diff] [blame] | 163 | * @return resource |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 164 | */ |
Andrey Andreev | 5c3a202 | 2011-10-07 21:04:58 +0300 | [diff] [blame] | 165 | public function db_connect() |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 166 | { |
Andrey Andreev | 59ad0af | 2012-03-26 12:47:45 +0300 | [diff] [blame] | 167 | return ( ! empty($this->char_set)) |
| 168 | ? @oci_connect($this->username, $this->password, $this->dsn, $this->char_set) |
| 169 | : @oci_connect($this->username, $this->password, $this->dsn); |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 170 | } |
| 171 | |
| 172 | // -------------------------------------------------------------------- |
| 173 | |
| 174 | /** |
| 175 | * Persistent database connection |
| 176 | * |
Andrey Andreev | da12373 | 2012-03-20 22:27:40 +0200 | [diff] [blame] | 177 | * @return resource |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 178 | */ |
Andrey Andreev | 5c3a202 | 2011-10-07 21:04:58 +0300 | [diff] [blame] | 179 | public function db_pconnect() |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 180 | { |
Andrey Andreev | 59ad0af | 2012-03-26 12:47:45 +0300 | [diff] [blame] | 181 | return ( ! empty($this->char_set)) |
| 182 | ? @oci_pconnect($this->username, $this->password, $this->dsn, $this->char_set) |
| 183 | : @oci_pconnect($this->username, $this->password, $this->dsn); |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 184 | } |
| 185 | |
| 186 | // -------------------------------------------------------------------- |
| 187 | |
| 188 | /** |
Andrey Andreev | 08856b8 | 2012-03-03 03:19:28 +0200 | [diff] [blame] | 189 | * Database version number |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 190 | * |
Andrey Andreev | 08856b8 | 2012-03-03 03:19:28 +0200 | [diff] [blame] | 191 | * @return string |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 192 | */ |
Andrey Andreev | 08856b8 | 2012-03-03 03:19:28 +0200 | [diff] [blame] | 193 | public function version() |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 194 | { |
Andrey Andreev | 08856b8 | 2012-03-03 03:19:28 +0200 | [diff] [blame] | 195 | return isset($this->data_cache['version']) |
| 196 | ? $this->data_cache['version'] |
| 197 | : $this->data_cache['version'] = oci_server_version($this->conn_id); |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 198 | } |
| 199 | |
| 200 | // -------------------------------------------------------------------- |
| 201 | |
| 202 | /** |
| 203 | * Execute the query |
| 204 | * |
Andrey Andreev | da12373 | 2012-03-20 22:27:40 +0200 | [diff] [blame] | 205 | * @param string an SQL query |
| 206 | * @return resource |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 207 | */ |
Andrey Andreev | bc95e47 | 2011-10-20 09:44:48 +0300 | [diff] [blame] | 208 | protected function _execute($sql) |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 209 | { |
| 210 | // oracle must parse the query before it is run. All of the actions with |
| 211 | // the query are based on the statement id returned by ociparse |
| 212 | $this->stmt_id = FALSE; |
| 213 | $this->_set_stmt_id($sql); |
Andrey Andreev | 5c3a202 | 2011-10-07 21:04:58 +0300 | [diff] [blame] | 214 | oci_set_prefetch($this->stmt_id, 1000); |
| 215 | return @oci_execute($this->stmt_id, $this->_commit); |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 216 | } |
| 217 | |
| 218 | /** |
| 219 | * Generate a statement ID |
| 220 | * |
Andrey Andreev | da12373 | 2012-03-20 22:27:40 +0200 | [diff] [blame] | 221 | * @param string an SQL query |
| 222 | * @return void |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 223 | */ |
Andrey Andreev | da12373 | 2012-03-20 22:27:40 +0200 | [diff] [blame] | 224 | protected function _set_stmt_id($sql) |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 225 | { |
| 226 | if ( ! is_resource($this->stmt_id)) |
| 227 | { |
Timothy Warren | d2ff0bc | 2012-03-19 16:52:10 -0400 | [diff] [blame] | 228 | $this->stmt_id = oci_parse($this->conn_id, $sql); |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 229 | } |
| 230 | } |
Andrey Andreev | da12373 | 2012-03-20 22:27:40 +0200 | [diff] [blame] | 231 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 232 | // -------------------------------------------------------------------- |
| 233 | |
| 234 | /** |
Andrey Andreev | da12373 | 2012-03-20 22:27:40 +0200 | [diff] [blame] | 235 | * Get cursor. Returns a cursor from the database |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 236 | * |
Andrey Andreev | da12373 | 2012-03-20 22:27:40 +0200 | [diff] [blame] | 237 | * @return cursor id |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 238 | */ |
Andrey Andreev | 5c3a202 | 2011-10-07 21:04:58 +0300 | [diff] [blame] | 239 | public function get_cursor() |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 240 | { |
Andrey Andreev | 5c3a202 | 2011-10-07 21:04:58 +0300 | [diff] [blame] | 241 | $this->curs_id = oci_new_cursor($this->conn_id); |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 242 | return $this->curs_id; |
| 243 | } |
| 244 | |
| 245 | // -------------------------------------------------------------------- |
| 246 | |
| 247 | /** |
Derek Jones | 4b9c629 | 2011-07-01 17:40:48 -0500 | [diff] [blame] | 248 | * Stored Procedure. Executes a stored procedure |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 249 | * |
Andrey Andreev | da12373 | 2012-03-20 22:27:40 +0200 | [diff] [blame] | 250 | * @param string package stored procedure is in |
| 251 | * @param string stored procedure to execute |
| 252 | * @param array parameters |
| 253 | * @return object |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 254 | * |
| 255 | * params array keys |
| 256 | * |
Derek Jones | 4b9c629 | 2011-07-01 17:40:48 -0500 | [diff] [blame] | 257 | * KEY OPTIONAL NOTES |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 258 | * name no the name of the parameter should be in :<param_name> format |
Derek Jones | 4b9c629 | 2011-07-01 17:40:48 -0500 | [diff] [blame] | 259 | * value no the value of the parameter. If this is an OUT or IN OUT parameter, |
Timothy Warren | 48e1d2e | 2012-03-19 19:07:07 -0400 | [diff] [blame] | 260 | * this should be a reference to a variable |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 261 | * type yes the type of the parameter |
| 262 | * length yes the max size of the parameter |
| 263 | */ |
Andrey Andreev | 5c3a202 | 2011-10-07 21:04:58 +0300 | [diff] [blame] | 264 | public function stored_procedure($package, $procedure, $params) |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 265 | { |
| 266 | if ($package == '' OR $procedure == '' OR ! is_array($params)) |
| 267 | { |
| 268 | if ($this->db_debug) |
| 269 | { |
| 270 | log_message('error', 'Invalid query: '.$package.'.'.$procedure); |
Derek Allard | fac8fbc | 2010-02-05 16:14:49 +0000 | [diff] [blame] | 271 | return $this->display_error('db_invalid_query'); |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 272 | } |
| 273 | return FALSE; |
| 274 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 275 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 276 | // build the query string |
| 277 | $sql = "begin $package.$procedure("; |
| 278 | |
| 279 | $have_cursor = FALSE; |
Pascal Kriete | c3a4a8d | 2011-02-14 13:40:08 -0500 | [diff] [blame] | 280 | foreach ($params as $param) |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 281 | { |
| 282 | $sql .= $param['name'] . ","; |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 283 | |
Andrey Andreev | 5c3a202 | 2011-10-07 21:04:58 +0300 | [diff] [blame] | 284 | if (array_key_exists('type', $param) && ($param['type'] === OCI_B_CURSOR)) |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 285 | { |
| 286 | $have_cursor = TRUE; |
| 287 | } |
| 288 | } |
| 289 | $sql = trim($sql, ",") . "); end;"; |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 290 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 291 | $this->stmt_id = FALSE; |
| 292 | $this->_set_stmt_id($sql); |
| 293 | $this->_bind_params($params); |
| 294 | $this->query($sql, FALSE, $have_cursor); |
| 295 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 296 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 297 | // -------------------------------------------------------------------- |
| 298 | |
| 299 | /** |
| 300 | * Bind parameters |
| 301 | * |
Andrey Andreev | da12373 | 2012-03-20 22:27:40 +0200 | [diff] [blame] | 302 | * @return void |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 303 | */ |
Andrey Andreev | da12373 | 2012-03-20 22:27:40 +0200 | [diff] [blame] | 304 | protected function _bind_params($params) |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 305 | { |
| 306 | if ( ! is_array($params) OR ! is_resource($this->stmt_id)) |
| 307 | { |
| 308 | return; |
| 309 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 310 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 311 | foreach ($params as $param) |
| 312 | { |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 313 | foreach (array('name', 'value', 'type', 'length') as $val) |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 314 | { |
| 315 | if ( ! isset($param[$val])) |
| 316 | { |
| 317 | $param[$val] = ''; |
| 318 | } |
| 319 | } |
| 320 | |
Andrey Andreev | 5c3a202 | 2011-10-07 21:04:58 +0300 | [diff] [blame] | 321 | oci_bind_by_name($this->stmt_id, $param['name'], $param['value'], $param['length'], $param['type']); |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 322 | } |
| 323 | } |
| 324 | |
| 325 | // -------------------------------------------------------------------- |
| 326 | |
| 327 | /** |
| 328 | * Begin Transaction |
| 329 | * |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 330 | * @return bool |
| 331 | */ |
Andrey Andreev | 5c3a202 | 2011-10-07 21:04:58 +0300 | [diff] [blame] | 332 | public function trans_begin($test_mode = FALSE) |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 333 | { |
| 334 | if ( ! $this->trans_enabled) |
| 335 | { |
| 336 | return TRUE; |
| 337 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 338 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 339 | // When transactions are nested we only begin/commit/rollback the outermost ones |
| 340 | if ($this->_trans_depth > 0) |
| 341 | { |
| 342 | return TRUE; |
| 343 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 344 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 345 | // Reset the transaction failure flag. |
| 346 | // If the $test_mode flag is set to TRUE transactions will be rolled back |
| 347 | // even if the queries produce a successful result. |
| 348 | $this->_trans_failure = ($test_mode === TRUE) ? TRUE : FALSE; |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 349 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 350 | $this->_commit = OCI_DEFAULT; |
| 351 | return TRUE; |
| 352 | } |
| 353 | |
| 354 | // -------------------------------------------------------------------- |
| 355 | |
| 356 | /** |
| 357 | * Commit Transaction |
| 358 | * |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 359 | * @return bool |
| 360 | */ |
Andrey Andreev | 5c3a202 | 2011-10-07 21:04:58 +0300 | [diff] [blame] | 361 | public function trans_commit() |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 362 | { |
| 363 | if ( ! $this->trans_enabled) |
| 364 | { |
| 365 | return TRUE; |
| 366 | } |
| 367 | |
| 368 | // When transactions are nested we only begin/commit/rollback the outermost ones |
| 369 | if ($this->_trans_depth > 0) |
| 370 | { |
| 371 | return TRUE; |
| 372 | } |
| 373 | |
Andrey Andreev | 5c3a202 | 2011-10-07 21:04:58 +0300 | [diff] [blame] | 374 | $ret = oci_commit($this->conn_id); |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 375 | $this->_commit = OCI_COMMIT_ON_SUCCESS; |
| 376 | return $ret; |
| 377 | } |
| 378 | |
| 379 | // -------------------------------------------------------------------- |
| 380 | |
| 381 | /** |
| 382 | * Rollback Transaction |
| 383 | * |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 384 | * @return bool |
| 385 | */ |
Andrey Andreev | 5c3a202 | 2011-10-07 21:04:58 +0300 | [diff] [blame] | 386 | public function trans_rollback() |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 387 | { |
| 388 | if ( ! $this->trans_enabled) |
| 389 | { |
| 390 | return TRUE; |
| 391 | } |
| 392 | |
| 393 | // When transactions are nested we only begin/commit/rollback the outermost ones |
| 394 | if ($this->_trans_depth > 0) |
| 395 | { |
| 396 | return TRUE; |
| 397 | } |
| 398 | |
Andrey Andreev | 5c3a202 | 2011-10-07 21:04:58 +0300 | [diff] [blame] | 399 | $ret = oci_rollback($this->conn_id); |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 400 | $this->_commit = OCI_COMMIT_ON_SUCCESS; |
| 401 | return $ret; |
| 402 | } |
| 403 | |
| 404 | // -------------------------------------------------------------------- |
| 405 | |
| 406 | /** |
| 407 | * Escape String |
| 408 | * |
Andrey Andreev | c2905f5 | 2012-03-01 14:39:26 +0200 | [diff] [blame] | 409 | * @param string |
Derek Jones | e4ed583 | 2009-02-20 21:44:59 +0000 | [diff] [blame] | 410 | * @param bool whether or not the string will be used in a LIKE condition |
Andrey Andreev | c2905f5 | 2012-03-01 14:39:26 +0200 | [diff] [blame] | 411 | * @return string |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 412 | */ |
Andrey Andreev | 5c3a202 | 2011-10-07 21:04:58 +0300 | [diff] [blame] | 413 | public function escape_str($str, $like = FALSE) |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 414 | { |
Derek Jones | e4ed583 | 2009-02-20 21:44:59 +0000 | [diff] [blame] | 415 | if (is_array($str)) |
| 416 | { |
Pascal Kriete | c3a4a8d | 2011-02-14 13:40:08 -0500 | [diff] [blame] | 417 | foreach ($str as $key => $val) |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 418 | { |
Derek Jones | e4ed583 | 2009-02-20 21:44:59 +0000 | [diff] [blame] | 419 | $str[$key] = $this->escape_str($val, $like); |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 420 | } |
| 421 | |
| 422 | return $str; |
| 423 | } |
Derek Jones | e4ed583 | 2009-02-20 21:44:59 +0000 | [diff] [blame] | 424 | |
Andrey Andreev | c2905f5 | 2012-03-01 14:39:26 +0200 | [diff] [blame] | 425 | $str = str_replace("'", "''", remove_invisible_characters($str)); |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 426 | |
Derek Jones | e4ed583 | 2009-02-20 21:44:59 +0000 | [diff] [blame] | 427 | // escape LIKE condition wildcards |
| 428 | if ($like === TRUE) |
| 429 | { |
Andrey Andreev | c2905f5 | 2012-03-01 14:39:26 +0200 | [diff] [blame] | 430 | return str_replace(array($this->_like_escape_chr, '%', '_'), |
| 431 | array($this->_like_escape_chr.$this->_like_escape_chr, $this->_like_escape_chr.'%', $this->_like_escape_chr.'_'), |
| 432 | $str); |
Derek Jones | e4ed583 | 2009-02-20 21:44:59 +0000 | [diff] [blame] | 433 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 434 | |
Derek Jones | e4ed583 | 2009-02-20 21:44:59 +0000 | [diff] [blame] | 435 | return $str; |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 436 | } |
| 437 | |
| 438 | // -------------------------------------------------------------------- |
| 439 | |
| 440 | /** |
| 441 | * Affected Rows |
| 442 | * |
Andrey Andreev | da12373 | 2012-03-20 22:27:40 +0200 | [diff] [blame] | 443 | * @return int |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 444 | */ |
Andrey Andreev | 5c3a202 | 2011-10-07 21:04:58 +0300 | [diff] [blame] | 445 | public function affected_rows() |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 446 | { |
Andrey Andreev | 5c3a202 | 2011-10-07 21:04:58 +0300 | [diff] [blame] | 447 | return @oci_num_rows($this->stmt_id); |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 448 | } |
| 449 | |
| 450 | // -------------------------------------------------------------------- |
| 451 | |
| 452 | /** |
| 453 | * Insert ID |
| 454 | * |
Andrey Andreev | da12373 | 2012-03-20 22:27:40 +0200 | [diff] [blame] | 455 | * @return int |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 456 | */ |
Andrey Andreev | 5c3a202 | 2011-10-07 21:04:58 +0300 | [diff] [blame] | 457 | public function insert_id() |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 458 | { |
| 459 | // not supported in oracle |
Derek Allard | fac8fbc | 2010-02-05 16:14:49 +0000 | [diff] [blame] | 460 | return $this->display_error('db_unsupported_function'); |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 461 | } |
| 462 | |
| 463 | // -------------------------------------------------------------------- |
| 464 | |
| 465 | /** |
| 466 | * "Count All" query |
| 467 | * |
| 468 | * Generates a platform-specific query string that counts all records in |
| 469 | * the specified database |
| 470 | * |
Andrey Andreev | da12373 | 2012-03-20 22:27:40 +0200 | [diff] [blame] | 471 | * @param string |
| 472 | * @return string |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 473 | */ |
Andrey Andreev | 5c3a202 | 2011-10-07 21:04:58 +0300 | [diff] [blame] | 474 | public function count_all($table = '') |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 475 | { |
| 476 | if ($table == '') |
Derek Allard | e37ab38 | 2009-02-03 16:13:57 +0000 | [diff] [blame] | 477 | { |
| 478 | return 0; |
| 479 | } |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 480 | |
Andrey Andreev | 032e7ea | 2012-03-06 19:48:35 +0200 | [diff] [blame] | 481 | $query = $this->query($this->_count_string.$this->protect_identifiers('numrows').' FROM '.$this->protect_identifiers($table, TRUE, NULL, FALSE)); |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 482 | if ($query == FALSE) |
Derek Allard | e37ab38 | 2009-02-03 16:13:57 +0000 | [diff] [blame] | 483 | { |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 484 | return 0; |
Derek Allard | e37ab38 | 2009-02-03 16:13:57 +0000 | [diff] [blame] | 485 | } |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 486 | |
| 487 | $row = $query->row(); |
Greg Aker | 90248ab | 2011-08-20 14:23:14 -0500 | [diff] [blame] | 488 | $this->_reset_select(); |
Derek Allard | e37ab38 | 2009-02-03 16:13:57 +0000 | [diff] [blame] | 489 | return (int) $row->numrows; |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 490 | } |
| 491 | |
| 492 | // -------------------------------------------------------------------- |
| 493 | |
| 494 | /** |
| 495 | * Show table query |
| 496 | * |
| 497 | * Generates a platform-specific query string so that the table names can be fetched |
| 498 | * |
Andrey Andreev | da12373 | 2012-03-20 22:27:40 +0200 | [diff] [blame] | 499 | * @param bool |
Andrey Andreev | 5c3a202 | 2011-10-07 21:04:58 +0300 | [diff] [blame] | 500 | * @return string |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 501 | */ |
Andrey Andreev | bc95e47 | 2011-10-20 09:44:48 +0300 | [diff] [blame] | 502 | protected function _list_tables($prefix_limit = FALSE) |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 503 | { |
| 504 | $sql = "SELECT TABLE_NAME FROM ALL_TABLES"; |
| 505 | |
| 506 | if ($prefix_limit !== FALSE AND $this->dbprefix != '') |
| 507 | { |
Greg Aker | 0d42489 | 2010-01-26 02:14:44 +0000 | [diff] [blame] | 508 | $sql .= " WHERE TABLE_NAME LIKE '".$this->escape_like_str($this->dbprefix)."%' ".sprintf($this->_like_escape_str, $this->_like_escape_chr); |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 509 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 510 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 511 | return $sql; |
| 512 | } |
| 513 | |
| 514 | // -------------------------------------------------------------------- |
| 515 | |
| 516 | /** |
| 517 | * Show column query |
| 518 | * |
| 519 | * Generates a platform-specific query string so that the column names can be fetched |
| 520 | * |
Andrey Andreev | da12373 | 2012-03-20 22:27:40 +0200 | [diff] [blame] | 521 | * @param string the table name |
| 522 | * @return string |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 523 | */ |
Andrey Andreev | bc95e47 | 2011-10-20 09:44:48 +0300 | [diff] [blame] | 524 | protected function _list_columns($table = '') |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 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 | * |
Andrey Andreev | da12373 | 2012-03-20 22:27:40 +0200 | [diff] [blame] | 536 | * @param string the table name |
| 537 | * @return string |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 538 | */ |
Andrey Andreev | bc95e47 | 2011-10-20 09:44:48 +0300 | [diff] [blame] | 539 | protected function _field_data($table) |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 540 | { |
| 541 | return "SELECT * FROM ".$table." where rownum = 1"; |
| 542 | } |
| 543 | |
| 544 | // -------------------------------------------------------------------- |
| 545 | |
| 546 | /** |
Andrey Andreev | 4be5de1 | 2012-03-02 15:45:41 +0200 | [diff] [blame] | 547 | * Error |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 548 | * |
Andrey Andreev | 4be5de1 | 2012-03-02 15:45:41 +0200 | [diff] [blame] | 549 | * Returns an array containing code and message of the last |
| 550 | * database error that has occured. |
Andrey Andreev | 601f8b2 | 2012-03-01 20:11:15 +0200 | [diff] [blame] | 551 | * |
| 552 | * @return array |
| 553 | */ |
Andrey Andreev | 4be5de1 | 2012-03-02 15:45:41 +0200 | [diff] [blame] | 554 | public function error() |
Andrey Andreev | 601f8b2 | 2012-03-01 20:11:15 +0200 | [diff] [blame] | 555 | { |
Andrey Andreev | 4be5de1 | 2012-03-02 15:45:41 +0200 | [diff] [blame] | 556 | /* oci_error() returns an array that already contains the |
| 557 | * 'code' and 'message' keys, so we can just return it. |
| 558 | */ |
Andrey Andreev | 601f8b2 | 2012-03-01 20:11:15 +0200 | [diff] [blame] | 559 | if (is_resource($this->curs_id)) |
| 560 | { |
| 561 | return oci_error($this->curs_id); |
| 562 | } |
| 563 | elseif (is_resource($this->stmt_id)) |
| 564 | { |
| 565 | return oci_error($this->stmt_id); |
| 566 | } |
| 567 | elseif (is_resource($this->conn_id)) |
| 568 | { |
| 569 | return oci_error($this->conn_id); |
| 570 | } |
| 571 | |
| 572 | return oci_error(); |
| 573 | } |
| 574 | |
| 575 | // -------------------------------------------------------------------- |
| 576 | |
| 577 | /** |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 578 | * Escape the SQL Identifiers |
| 579 | * |
| 580 | * This function escapes column and table names |
| 581 | * |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 582 | * @param string |
| 583 | * @return string |
| 584 | */ |
Andrey Andreev | da12373 | 2012-03-20 22:27:40 +0200 | [diff] [blame] | 585 | public function _escape_identifiers($item) |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 586 | { |
| 587 | if ($this->_escape_char == '') |
| 588 | { |
| 589 | return $item; |
| 590 | } |
| 591 | |
| 592 | foreach ($this->_reserved_identifiers as $id) |
| 593 | { |
| 594 | if (strpos($item, '.'.$id) !== FALSE) |
| 595 | { |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 596 | $str = $this->_escape_char. str_replace('.', $this->_escape_char.'.', $item); |
| 597 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 598 | // remove duplicates if the user already included the escape |
| 599 | return preg_replace('/['.$this->_escape_char.']+/', $this->_escape_char, $str); |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 600 | } |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 601 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 602 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 603 | if (strpos($item, '.') !== FALSE) |
| 604 | { |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 605 | $str = $this->_escape_char.str_replace('.', $this->_escape_char.'.'.$this->_escape_char, $item).$this->_escape_char; |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 606 | } |
| 607 | else |
| 608 | { |
| 609 | $str = $this->_escape_char.$item.$this->_escape_char; |
| 610 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 611 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 612 | // remove duplicates if the user already included the escape |
| 613 | return preg_replace('/['.$this->_escape_char.']+/', $this->_escape_char, $str); |
| 614 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 615 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 616 | // -------------------------------------------------------------------- |
| 617 | |
| 618 | /** |
| 619 | * From Tables |
| 620 | * |
| 621 | * This function implicitly groups FROM tables so there is no confusion |
| 622 | * about operator precedence in harmony with SQL standards |
| 623 | * |
Andrey Andreev | da12373 | 2012-03-20 22:27:40 +0200 | [diff] [blame] | 624 | * @param array |
| 625 | * @return string |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 626 | */ |
Andrey Andreev | bc95e47 | 2011-10-20 09:44:48 +0300 | [diff] [blame] | 627 | protected function _from_tables($tables) |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 628 | { |
| 629 | if ( ! is_array($tables)) |
| 630 | { |
| 631 | $tables = array($tables); |
| 632 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 633 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 634 | return implode(', ', $tables); |
| 635 | } |
| 636 | |
| 637 | // -------------------------------------------------------------------- |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 638 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 639 | /** |
| 640 | * Insert statement |
| 641 | * |
| 642 | * Generates a platform-specific insert string from the supplied data |
| 643 | * |
Andrey Andreev | da12373 | 2012-03-20 22:27:40 +0200 | [diff] [blame] | 644 | * @param string the table name |
| 645 | * @param array the insert keys |
| 646 | * @param array the insert values |
| 647 | * @return string |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 648 | */ |
Andrey Andreev | bc95e47 | 2011-10-20 09:44:48 +0300 | [diff] [blame] | 649 | protected function _insert($table, $keys, $values) |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 650 | { |
Andrey Andreev | 5c3a202 | 2011-10-07 21:04:58 +0300 | [diff] [blame] | 651 | return "INSERT INTO ".$table." (".implode(', ', $keys).") VALUES (".implode(', ', $values).")"; |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 652 | } |
| 653 | |
| 654 | // -------------------------------------------------------------------- |
| 655 | |
| 656 | /** |
Andrey Andreev | 99c6dd4 | 2011-09-23 03:07:01 +0300 | [diff] [blame] | 657 | * Insert_batch statement |
| 658 | * |
| 659 | * Generates a platform-specific insert string from the supplied data |
| 660 | * |
Andrey Andreev | da12373 | 2012-03-20 22:27:40 +0200 | [diff] [blame] | 661 | * @param string the table name |
| 662 | * @param array the insert keys |
| 663 | * @param array the insert values |
| 664 | * @return string |
Andrey Andreev | 99c6dd4 | 2011-09-23 03:07:01 +0300 | [diff] [blame] | 665 | */ |
Andrey Andreev | bc95e47 | 2011-10-20 09:44:48 +0300 | [diff] [blame] | 666 | protected function _insert_batch($table, $keys, $values) |
Andrey Andreev | 99c6dd4 | 2011-09-23 03:07:01 +0300 | [diff] [blame] | 667 | { |
| 668 | $keys = implode(', ', $keys); |
| 669 | $sql = "INSERT ALL\n"; |
| 670 | |
| 671 | for ($i = 0, $c = count($values); $i < $c; $i++) |
Andrey Andreev | b83c408 | 2011-09-23 03:32:45 +0300 | [diff] [blame] | 672 | { |
Andrey Andreev | 99c6dd4 | 2011-09-23 03:07:01 +0300 | [diff] [blame] | 673 | $sql .= ' INTO ' . $table . ' (' . $keys . ') VALUES ' . $values[$i] . "\n"; |
Andrey Andreev | b83c408 | 2011-09-23 03:32:45 +0300 | [diff] [blame] | 674 | } |
Andrey Andreev | 99c6dd4 | 2011-09-23 03:07:01 +0300 | [diff] [blame] | 675 | |
| 676 | $sql .= 'SELECT * FROM dual'; |
| 677 | |
| 678 | return $sql; |
| 679 | } |
| 680 | |
| 681 | // -------------------------------------------------------------------- |
| 682 | |
| 683 | /** |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 684 | * Update statement |
| 685 | * |
| 686 | * Generates a platform-specific update string from the supplied data |
| 687 | * |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 688 | * @param string the table name |
| 689 | * @param array the update data |
| 690 | * @param array the where clause |
| 691 | * @param array the orderby clause |
| 692 | * @param array the limit clause |
| 693 | * @return string |
| 694 | */ |
Andrey Andreev | bc95e47 | 2011-10-20 09:44:48 +0300 | [diff] [blame] | 695 | protected function _update($table, $values, $where, $orderby = array(), $limit = FALSE) |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 696 | { |
Pascal Kriete | c3a4a8d | 2011-02-14 13:40:08 -0500 | [diff] [blame] | 697 | foreach ($values as $key => $val) |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 698 | { |
| 699 | $valstr[] = $key." = ".$val; |
| 700 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 701 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 702 | $limit = ( ! $limit) ? '' : ' LIMIT '.$limit; |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 703 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 704 | $orderby = (count($orderby) >= 1)?' ORDER BY '.implode(", ", $orderby):''; |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 705 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 706 | $sql = "UPDATE ".$table." SET ".implode(', ', $valstr); |
| 707 | |
| 708 | $sql .= ($where != '' AND count($where) >=1) ? " WHERE ".implode(" ", $where) : ''; |
| 709 | |
| 710 | $sql .= $orderby.$limit; |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 711 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 712 | return $sql; |
| 713 | } |
| 714 | |
| 715 | // -------------------------------------------------------------------- |
| 716 | |
| 717 | /** |
| 718 | * Truncate statement |
| 719 | * |
| 720 | * Generates a platform-specific truncate string from the supplied data |
| 721 | * If the database does not support the truncate() command |
| 722 | * This function maps to "DELETE FROM table" |
| 723 | * |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 724 | * @param string the table name |
| 725 | * @return string |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 726 | */ |
Andrey Andreev | bc95e47 | 2011-10-20 09:44:48 +0300 | [diff] [blame] | 727 | protected function _truncate($table) |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 728 | { |
| 729 | return "TRUNCATE TABLE ".$table; |
| 730 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 731 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 732 | // -------------------------------------------------------------------- |
| 733 | |
| 734 | /** |
| 735 | * Delete statement |
| 736 | * |
| 737 | * Generates a platform-specific delete string from the supplied data |
| 738 | * |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 739 | * @param string the table name |
| 740 | * @param array the where clause |
| 741 | * @param string the limit clause |
| 742 | * @return string |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 743 | */ |
Andrey Andreev | bc95e47 | 2011-10-20 09:44:48 +0300 | [diff] [blame] | 744 | protected function _delete($table, $where = array(), $like = array(), $limit = FALSE) |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 745 | { |
| 746 | $conditions = ''; |
| 747 | |
| 748 | if (count($where) > 0 OR count($like) > 0) |
| 749 | { |
| 750 | $conditions = "\nWHERE "; |
| 751 | $conditions .= implode("\n", $this->ar_where); |
| 752 | |
| 753 | if (count($where) > 0 && count($like) > 0) |
| 754 | { |
| 755 | $conditions .= " AND "; |
| 756 | } |
| 757 | $conditions .= implode("\n", $like); |
| 758 | } |
| 759 | |
| 760 | $limit = ( ! $limit) ? '' : ' LIMIT '.$limit; |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 761 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 762 | return "DELETE FROM ".$table.$conditions.$limit; |
| 763 | } |
| 764 | |
| 765 | // -------------------------------------------------------------------- |
| 766 | |
| 767 | /** |
| 768 | * Limit string |
| 769 | * |
| 770 | * Generates a platform-specific LIMIT clause |
| 771 | * |
Andrey Andreev | da12373 | 2012-03-20 22:27:40 +0200 | [diff] [blame] | 772 | * @param string the sql query string |
| 773 | * @param int the number of rows to limit the query to |
| 774 | * @param int the offset value |
| 775 | * @return string |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 776 | */ |
Andrey Andreev | bc95e47 | 2011-10-20 09:44:48 +0300 | [diff] [blame] | 777 | protected function _limit($sql, $limit, $offset) |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 778 | { |
| 779 | $limit = $offset + $limit; |
| 780 | $newsql = "SELECT * FROM (select inner_query.*, rownum rnum FROM ($sql) inner_query WHERE rownum < $limit)"; |
| 781 | |
| 782 | if ($offset != 0) |
| 783 | { |
| 784 | $newsql .= " WHERE rnum >= $offset"; |
| 785 | } |
| 786 | |
| 787 | // remember that we used limits |
| 788 | $this->limit_used = TRUE; |
| 789 | |
| 790 | return $newsql; |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 791 | } |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 792 | |
| 793 | // -------------------------------------------------------------------- |
| 794 | |
| 795 | /** |
| 796 | * Close DB Connection |
| 797 | * |
Andrey Andreev | da12373 | 2012-03-20 22:27:40 +0200 | [diff] [blame] | 798 | * @param resource |
| 799 | * @return void |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 800 | */ |
Andrey Andreev | bc95e47 | 2011-10-20 09:44:48 +0300 | [diff] [blame] | 801 | protected function _close($conn_id) |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 802 | { |
Andrey Andreev | 5c3a202 | 2011-10-07 21:04:58 +0300 | [diff] [blame] | 803 | @oci_close($conn_id); |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 804 | } |
| 805 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 806 | } |
| 807 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 808 | /* End of file oci8_driver.php */ |
Timothy Warren | 215890b | 2012-03-20 09:38:16 -0400 | [diff] [blame] | 809 | /* Location: ./system/database/drivers/oci8/oci8_driver.php */ |