Derek Jones | 4b9c629 | 2011-07-01 17:40:48 -0500 | [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 | * |
Greg Aker | 741de1c | 2010-11-10 14:52:57 -0600 | [diff] [blame] | 5 | * An open source application development framework for PHP 5.1.6 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 |
| 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 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 | |
| 28 | // ------------------------------------------------------------------------ |
| 29 | |
| 30 | /** |
| 31 | * oci8 Database Adapter Class |
| 32 | * |
| 33 | * Note: _DB is an extender class that the app controller |
Jamie Rumbelow | 7efad20 | 2012-02-19 12:37:00 +0000 | [diff] [blame^] | 34 | * creates dynamically based on whether the query builder |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 35 | * class is being used or not. |
| 36 | * |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 37 | * @package CodeIgniter |
Derek Jones | 4b9c629 | 2011-07-01 17:40:48 -0500 | [diff] [blame] | 38 | * @subpackage Drivers |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 39 | * @category Database |
Derek Jones | f4a4bd8 | 2011-10-20 12:18:42 -0500 | [diff] [blame] | 40 | * @author EllisLab Dev Team |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 41 | * @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 | * |
Derek Jones | 4b9c629 | 2011-07-01 17:40:48 -0500 | [diff] [blame] | 50 | * @author Kelly McArdle |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 51 | * |
| 52 | */ |
| 53 | |
| 54 | class CI_DB_oci8_driver extends CI_DB { |
| 55 | |
| 56 | var $dbdriver = 'oci8'; |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 57 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 58 | // The character used for excaping |
| 59 | var $_escape_char = '"'; |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 60 | |
Derek Jones | e4ed583 | 2009-02-20 21:44:59 +0000 | [diff] [blame] | 61 | // clause and character used for LIKE escape sequences |
| 62 | var $_like_escape_str = " escape '%s' "; |
| 63 | var $_like_escape_chr = '!'; |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 64 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 65 | /** |
| 66 | * The syntax to count rows is slightly different across different |
| 67 | * database engines, so this string appears in each driver and is |
| 68 | * used for the count_all() and count_all_results() functions. |
| 69 | */ |
| 70 | var $_count_string = "SELECT COUNT(1) AS "; |
| 71 | var $_random_keyword = ' ASC'; // not currently supported |
| 72 | |
| 73 | // Set "auto commit" by default |
| 74 | var $_commit = OCI_COMMIT_ON_SUCCESS; |
| 75 | |
| 76 | // need to track statement id and cursor id |
| 77 | var $stmt_id; |
| 78 | var $curs_id; |
| 79 | |
| 80 | // if we use a limit, we will add a field that will |
| 81 | // throw off num_fields later |
| 82 | var $limit_used; |
| 83 | |
| 84 | /** |
| 85 | * Non-persistent database connection |
| 86 | * |
Derek Jones | 4b9c629 | 2011-07-01 17:40:48 -0500 | [diff] [blame] | 87 | * @access private called by the base class |
| 88 | * @return resource |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 89 | */ |
Andrey Andreev | 5c3a202 | 2011-10-07 21:04:58 +0300 | [diff] [blame] | 90 | public function db_connect() |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 91 | { |
Andrey Andreev | 5c3a202 | 2011-10-07 21:04:58 +0300 | [diff] [blame] | 92 | return @oci_connect($this->username, $this->password, $this->hostname, $this->char_set); |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 93 | } |
| 94 | |
| 95 | // -------------------------------------------------------------------- |
| 96 | |
| 97 | /** |
| 98 | * Persistent database connection |
| 99 | * |
Derek Jones | 4b9c629 | 2011-07-01 17:40:48 -0500 | [diff] [blame] | 100 | * @access private called by the base class |
| 101 | * @return resource |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 102 | */ |
Andrey Andreev | 5c3a202 | 2011-10-07 21:04:58 +0300 | [diff] [blame] | 103 | public function db_pconnect() |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 104 | { |
Andrey Andreev | 5c3a202 | 2011-10-07 21:04:58 +0300 | [diff] [blame] | 105 | return @oci_pconnect($this->username, $this->password, $this->hostname, $this->char_set); |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 106 | } |
| 107 | |
| 108 | // -------------------------------------------------------------------- |
| 109 | |
| 110 | /** |
Derek Jones | 87cbafc | 2009-02-27 16:29:59 +0000 | [diff] [blame] | 111 | * Reconnect |
| 112 | * |
| 113 | * Keep / reestablish the db connection if no queries have been |
| 114 | * sent for a length of time exceeding the server's idle timeout |
| 115 | * |
| 116 | * @access public |
| 117 | * @return void |
| 118 | */ |
Andrey Andreev | 5c3a202 | 2011-10-07 21:04:58 +0300 | [diff] [blame] | 119 | public function reconnect() |
Derek Jones | 87cbafc | 2009-02-27 16:29:59 +0000 | [diff] [blame] | 120 | { |
| 121 | // not implemented in oracle |
Andrey Andreev | 5c3a202 | 2011-10-07 21:04:58 +0300 | [diff] [blame] | 122 | return; |
Derek Jones | 87cbafc | 2009-02-27 16:29:59 +0000 | [diff] [blame] | 123 | } |
| 124 | |
| 125 | // -------------------------------------------------------------------- |
| 126 | |
| 127 | /** |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 128 | * Select the database |
| 129 | * |
Derek Jones | 4b9c629 | 2011-07-01 17:40:48 -0500 | [diff] [blame] | 130 | * @access private called by the base class |
| 131 | * @return resource |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 132 | */ |
Andrey Andreev | 5c3a202 | 2011-10-07 21:04:58 +0300 | [diff] [blame] | 133 | public function db_select() |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 134 | { |
Andrey Andreev | 5c3a202 | 2011-10-07 21:04:58 +0300 | [diff] [blame] | 135 | // Not in Oracle - schemas are actually usernames |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 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 | */ |
Andrey Andreev | 5c3a202 | 2011-10-07 21:04:58 +0300 | [diff] [blame] | 149 | public function db_set_charset($charset, $collation) |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 150 | { |
narfbg | 068e3de | 2011-09-17 21:38:46 +0300 | [diff] [blame] | 151 | // this is done upon connect |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 152 | return TRUE; |
| 153 | } |
| 154 | |
| 155 | // -------------------------------------------------------------------- |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 156 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 157 | /** |
| 158 | * Version number query string |
| 159 | * |
Andrey Andreev | bc95e47 | 2011-10-20 09:44:48 +0300 | [diff] [blame] | 160 | * @access protected |
Derek Jones | 4b9c629 | 2011-07-01 17:40:48 -0500 | [diff] [blame] | 161 | * @return string |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 162 | */ |
Andrey Andreev | bc95e47 | 2011-10-20 09:44:48 +0300 | [diff] [blame] | 163 | protected function _version() |
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 | return oci_server_version($this->conn_id); |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 166 | } |
| 167 | |
| 168 | // -------------------------------------------------------------------- |
| 169 | |
| 170 | /** |
| 171 | * Execute the query |
| 172 | * |
Andrey Andreev | bc95e47 | 2011-10-20 09:44:48 +0300 | [diff] [blame] | 173 | * @access protected called by the base class |
Derek Jones | 4b9c629 | 2011-07-01 17:40:48 -0500 | [diff] [blame] | 174 | * @param string an SQL query |
| 175 | * @return resource |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 176 | */ |
Andrey Andreev | bc95e47 | 2011-10-20 09:44:48 +0300 | [diff] [blame] | 177 | protected function _execute($sql) |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 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); |
Andrey Andreev | 5c3a202 | 2011-10-07 21:04:58 +0300 | [diff] [blame] | 183 | oci_set_prefetch($this->stmt_id, 1000); |
| 184 | return @oci_execute($this->stmt_id, $this->_commit); |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 185 | } |
| 186 | |
| 187 | /** |
| 188 | * Generate a statement ID |
| 189 | * |
Derek Jones | 4b9c629 | 2011-07-01 17:40:48 -0500 | [diff] [blame] | 190 | * @access private |
| 191 | * @param string an SQL query |
| 192 | * @return none |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 193 | */ |
Andrey Andreev | 5c3a202 | 2011-10-07 21:04:58 +0300 | [diff] [blame] | 194 | private function _set_stmt_id($sql) |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 195 | { |
| 196 | if ( ! is_resource($this->stmt_id)) |
| 197 | { |
Andrey Andreev | 5c3a202 | 2011-10-07 21:04:58 +0300 | [diff] [blame] | 198 | $this->stmt_id = oci_parse($this->conn_id, $this->_prep_query($sql)); |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 199 | } |
| 200 | } |
| 201 | |
| 202 | // -------------------------------------------------------------------- |
| 203 | |
| 204 | /** |
| 205 | * Prep the query |
| 206 | * |
| 207 | * If needed, each database adapter can prep the query string |
| 208 | * |
Derek Jones | 4b9c629 | 2011-07-01 17:40:48 -0500 | [diff] [blame] | 209 | * @access private called by execute() |
| 210 | * @param string an SQL query |
| 211 | * @return string |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 212 | */ |
Andrey Andreev | 5c3a202 | 2011-10-07 21:04:58 +0300 | [diff] [blame] | 213 | private function _prep_query($sql) |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 214 | { |
| 215 | return $sql; |
| 216 | } |
| 217 | |
| 218 | // -------------------------------------------------------------------- |
| 219 | |
| 220 | /** |
Derek Jones | 4b9c629 | 2011-07-01 17:40:48 -0500 | [diff] [blame] | 221 | * getCursor. Returns a cursor from the datbase |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 222 | * |
Derek Jones | 4b9c629 | 2011-07-01 17:40:48 -0500 | [diff] [blame] | 223 | * @access public |
| 224 | * @return cursor id |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 225 | */ |
Andrey Andreev | 5c3a202 | 2011-10-07 21:04:58 +0300 | [diff] [blame] | 226 | public function get_cursor() |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 227 | { |
Andrey Andreev | 5c3a202 | 2011-10-07 21:04:58 +0300 | [diff] [blame] | 228 | $this->curs_id = oci_new_cursor($this->conn_id); |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 229 | return $this->curs_id; |
| 230 | } |
| 231 | |
| 232 | // -------------------------------------------------------------------- |
| 233 | |
| 234 | /** |
Derek Jones | 4b9c629 | 2011-07-01 17:40:48 -0500 | [diff] [blame] | 235 | * Stored Procedure. Executes a stored procedure |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 236 | * |
Derek Jones | 4b9c629 | 2011-07-01 17:40:48 -0500 | [diff] [blame] | 237 | * @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 Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 242 | * |
| 243 | * params array keys |
| 244 | * |
Derek Jones | 4b9c629 | 2011-07-01 17:40:48 -0500 | [diff] [blame] | 245 | * KEY OPTIONAL NOTES |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 246 | * 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] | 247 | * value no the value of the parameter. If this is an OUT or IN OUT parameter, |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 248 | * 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 | */ |
Andrey Andreev | 5c3a202 | 2011-10-07 21:04:58 +0300 | [diff] [blame] | 252 | public function stored_procedure($package, $procedure, $params) |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 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 Allard | fac8fbc | 2010-02-05 16:14:49 +0000 | [diff] [blame] | 259 | return $this->display_error('db_invalid_query'); |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 260 | } |
| 261 | return FALSE; |
| 262 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 263 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 264 | // build the query string |
| 265 | $sql = "begin $package.$procedure("; |
| 266 | |
| 267 | $have_cursor = FALSE; |
Pascal Kriete | c3a4a8d | 2011-02-14 13:40:08 -0500 | [diff] [blame] | 268 | foreach ($params as $param) |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 269 | { |
| 270 | $sql .= $param['name'] . ","; |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 271 | |
Andrey Andreev | 5c3a202 | 2011-10-07 21:04:58 +0300 | [diff] [blame] | 272 | if (array_key_exists('type', $param) && ($param['type'] === OCI_B_CURSOR)) |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 273 | { |
| 274 | $have_cursor = TRUE; |
| 275 | } |
| 276 | } |
| 277 | $sql = trim($sql, ",") . "); end;"; |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 278 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 279 | $this->stmt_id = FALSE; |
| 280 | $this->_set_stmt_id($sql); |
| 281 | $this->_bind_params($params); |
| 282 | $this->query($sql, FALSE, $have_cursor); |
| 283 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 284 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 285 | // -------------------------------------------------------------------- |
| 286 | |
| 287 | /** |
| 288 | * Bind parameters |
| 289 | * |
Derek Jones | 4b9c629 | 2011-07-01 17:40:48 -0500 | [diff] [blame] | 290 | * @access private |
| 291 | * @return none |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 292 | */ |
Andrey Andreev | 5c3a202 | 2011-10-07 21:04:58 +0300 | [diff] [blame] | 293 | private function _bind_params($params) |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 294 | { |
| 295 | if ( ! is_array($params) OR ! is_resource($this->stmt_id)) |
| 296 | { |
| 297 | return; |
| 298 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 299 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 300 | foreach ($params as $param) |
| 301 | { |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 302 | foreach (array('name', 'value', 'type', 'length') as $val) |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 303 | { |
| 304 | if ( ! isset($param[$val])) |
| 305 | { |
| 306 | $param[$val] = ''; |
| 307 | } |
| 308 | } |
| 309 | |
Andrey Andreev | 5c3a202 | 2011-10-07 21:04:58 +0300 | [diff] [blame] | 310 | 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] | 311 | } |
| 312 | } |
| 313 | |
| 314 | // -------------------------------------------------------------------- |
| 315 | |
| 316 | /** |
| 317 | * Begin Transaction |
| 318 | * |
| 319 | * @access public |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 320 | * @return bool |
| 321 | */ |
Andrey Andreev | 5c3a202 | 2011-10-07 21:04:58 +0300 | [diff] [blame] | 322 | public function trans_begin($test_mode = FALSE) |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 323 | { |
| 324 | if ( ! $this->trans_enabled) |
| 325 | { |
| 326 | return TRUE; |
| 327 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 328 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 329 | // When transactions are nested we only begin/commit/rollback the outermost ones |
| 330 | if ($this->_trans_depth > 0) |
| 331 | { |
| 332 | return TRUE; |
| 333 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 334 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 335 | // 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 Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 339 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 340 | $this->_commit = OCI_DEFAULT; |
| 341 | return TRUE; |
| 342 | } |
| 343 | |
| 344 | // -------------------------------------------------------------------- |
| 345 | |
| 346 | /** |
| 347 | * Commit Transaction |
| 348 | * |
| 349 | * @access public |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 350 | * @return bool |
| 351 | */ |
Andrey Andreev | 5c3a202 | 2011-10-07 21:04:58 +0300 | [diff] [blame] | 352 | public function trans_commit() |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 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 | |
Andrey Andreev | 5c3a202 | 2011-10-07 21:04:58 +0300 | [diff] [blame] | 365 | $ret = oci_commit($this->conn_id); |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 366 | $this->_commit = OCI_COMMIT_ON_SUCCESS; |
| 367 | return $ret; |
| 368 | } |
| 369 | |
| 370 | // -------------------------------------------------------------------- |
| 371 | |
| 372 | /** |
| 373 | * Rollback Transaction |
| 374 | * |
| 375 | * @access public |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 376 | * @return bool |
| 377 | */ |
Andrey Andreev | 5c3a202 | 2011-10-07 21:04:58 +0300 | [diff] [blame] | 378 | public function trans_rollback() |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 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 | |
Andrey Andreev | 5c3a202 | 2011-10-07 21:04:58 +0300 | [diff] [blame] | 391 | $ret = oci_rollback($this->conn_id); |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 392 | $this->_commit = OCI_COMMIT_ON_SUCCESS; |
| 393 | return $ret; |
| 394 | } |
| 395 | |
| 396 | // -------------------------------------------------------------------- |
| 397 | |
| 398 | /** |
| 399 | * Escape String |
| 400 | * |
Derek Jones | 4b9c629 | 2011-07-01 17:40:48 -0500 | [diff] [blame] | 401 | * @access public |
| 402 | * @param string |
Derek Jones | e4ed583 | 2009-02-20 21:44:59 +0000 | [diff] [blame] | 403 | * @param bool whether or not the string will be used in a LIKE condition |
Derek Jones | 4b9c629 | 2011-07-01 17:40:48 -0500 | [diff] [blame] | 404 | * @return string |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 405 | */ |
Andrey Andreev | 5c3a202 | 2011-10-07 21:04:58 +0300 | [diff] [blame] | 406 | public function escape_str($str, $like = FALSE) |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 407 | { |
Derek Jones | e4ed583 | 2009-02-20 21:44:59 +0000 | [diff] [blame] | 408 | if (is_array($str)) |
| 409 | { |
Pascal Kriete | c3a4a8d | 2011-02-14 13:40:08 -0500 | [diff] [blame] | 410 | foreach ($str as $key => $val) |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 411 | { |
Derek Jones | e4ed583 | 2009-02-20 21:44:59 +0000 | [diff] [blame] | 412 | $str[$key] = $this->escape_str($val, $like); |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 413 | } |
| 414 | |
| 415 | return $str; |
| 416 | } |
Derek Jones | e4ed583 | 2009-02-20 21:44:59 +0000 | [diff] [blame] | 417 | |
Greg Aker | 757dda6 | 2010-04-14 19:06:19 -0500 | [diff] [blame] | 418 | $str = remove_invisible_characters($str); |
Michiel Vugteveen | eaa5541 | 2011-08-25 21:22:49 +0200 | [diff] [blame] | 419 | $str = str_replace("'", "''", $str); |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 420 | |
Derek Jones | e4ed583 | 2009-02-20 21:44:59 +0000 | [diff] [blame] | 421 | // 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 Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 428 | |
Derek Jones | e4ed583 | 2009-02-20 21:44:59 +0000 | [diff] [blame] | 429 | return $str; |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 430 | } |
| 431 | |
| 432 | // -------------------------------------------------------------------- |
| 433 | |
| 434 | /** |
| 435 | * Affected Rows |
| 436 | * |
Derek Jones | 4b9c629 | 2011-07-01 17:40:48 -0500 | [diff] [blame] | 437 | * @access public |
| 438 | * @return integer |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 439 | */ |
Andrey Andreev | 5c3a202 | 2011-10-07 21:04:58 +0300 | [diff] [blame] | 440 | public function affected_rows() |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 441 | { |
Andrey Andreev | 5c3a202 | 2011-10-07 21:04:58 +0300 | [diff] [blame] | 442 | return @oci_num_rows($this->stmt_id); |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 443 | } |
| 444 | |
| 445 | // -------------------------------------------------------------------- |
| 446 | |
| 447 | /** |
| 448 | * Insert ID |
| 449 | * |
Derek Jones | 4b9c629 | 2011-07-01 17:40:48 -0500 | [diff] [blame] | 450 | * @access public |
| 451 | * @return integer |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 452 | */ |
Andrey Andreev | 5c3a202 | 2011-10-07 21:04:58 +0300 | [diff] [blame] | 453 | public function insert_id() |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 454 | { |
| 455 | // not supported in oracle |
Derek Allard | fac8fbc | 2010-02-05 16:14:49 +0000 | [diff] [blame] | 456 | return $this->display_error('db_unsupported_function'); |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 457 | } |
| 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 Jones | 4b9c629 | 2011-07-01 17:40:48 -0500 | [diff] [blame] | 467 | * @access public |
| 468 | * @param string |
| 469 | * @return string |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 470 | */ |
Andrey Andreev | 5c3a202 | 2011-10-07 21:04:58 +0300 | [diff] [blame] | 471 | public function count_all($table = '') |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 472 | { |
| 473 | if ($table == '') |
Derek Allard | e37ab38 | 2009-02-03 16:13:57 +0000 | [diff] [blame] | 474 | { |
| 475 | return 0; |
| 476 | } |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 477 | |
Derek Allard | e37ab38 | 2009-02-03 16:13:57 +0000 | [diff] [blame] | 478 | $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] | 479 | |
| 480 | if ($query == FALSE) |
Derek Allard | e37ab38 | 2009-02-03 16:13:57 +0000 | [diff] [blame] | 481 | { |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 482 | return 0; |
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 | |
| 485 | $row = $query->row(); |
Greg Aker | 90248ab | 2011-08-20 14:23:14 -0500 | [diff] [blame] | 486 | $this->_reset_select(); |
Derek Allard | e37ab38 | 2009-02-03 16:13:57 +0000 | [diff] [blame] | 487 | return (int) $row->numrows; |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 488 | } |
| 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 | * |
Andrey Andreev | bc95e47 | 2011-10-20 09:44:48 +0300 | [diff] [blame] | 497 | * @access protected |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 498 | * @param boolean |
Andrey Andreev | 5c3a202 | 2011-10-07 21:04:58 +0300 | [diff] [blame] | 499 | * @return string |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 500 | */ |
Andrey Andreev | bc95e47 | 2011-10-20 09:44:48 +0300 | [diff] [blame] | 501 | protected function _list_tables($prefix_limit = FALSE) |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 502 | { |
| 503 | $sql = "SELECT TABLE_NAME FROM ALL_TABLES"; |
| 504 | |
| 505 | if ($prefix_limit !== FALSE AND $this->dbprefix != '') |
| 506 | { |
Greg Aker | 0d42489 | 2010-01-26 02:14:44 +0000 | [diff] [blame] | 507 | $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] | 508 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 509 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 510 | 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 | * |
Andrey Andreev | bc95e47 | 2011-10-20 09:44:48 +0300 | [diff] [blame] | 520 | * @access protected |
Derek Jones | 4b9c629 | 2011-07-01 17:40:48 -0500 | [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 | * |
Derek Jones | 4b9c629 | 2011-07-01 17:40:48 -0500 | [diff] [blame] | 536 | * @access public |
| 537 | * @param string the table name |
| 538 | * @return object |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 539 | */ |
Andrey Andreev | bc95e47 | 2011-10-20 09:44:48 +0300 | [diff] [blame] | 540 | protected function _field_data($table) |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 541 | { |
| 542 | return "SELECT * FROM ".$table." where rownum = 1"; |
| 543 | } |
| 544 | |
| 545 | // -------------------------------------------------------------------- |
| 546 | |
| 547 | /** |
| 548 | * The error message string |
| 549 | * |
Andrey Andreev | bc95e47 | 2011-10-20 09:44:48 +0300 | [diff] [blame] | 550 | * @access protected |
Derek Jones | 4b9c629 | 2011-07-01 17:40:48 -0500 | [diff] [blame] | 551 | * @return string |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 552 | */ |
Andrey Andreev | bc95e47 | 2011-10-20 09:44:48 +0300 | [diff] [blame] | 553 | protected function _error_message() |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 554 | { |
Andrey Andreev | 5c3a202 | 2011-10-07 21:04:58 +0300 | [diff] [blame] | 555 | // If the error was during connection, no conn_id should be passed |
| 556 | $error = is_resource($this->conn_id) ? oci_error($this->conn_id) : oci_error(); |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 557 | return $error['message']; |
| 558 | } |
| 559 | |
| 560 | // -------------------------------------------------------------------- |
| 561 | |
| 562 | /** |
| 563 | * The error message number |
| 564 | * |
Andrey Andreev | bc95e47 | 2011-10-20 09:44:48 +0300 | [diff] [blame] | 565 | * @access protected |
Derek Jones | 4b9c629 | 2011-07-01 17:40:48 -0500 | [diff] [blame] | 566 | * @return integer |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 567 | */ |
Andrey Andreev | bc95e47 | 2011-10-20 09:44:48 +0300 | [diff] [blame] | 568 | protected function _error_number() |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 569 | { |
Andrey Andreev | 5c3a202 | 2011-10-07 21:04:58 +0300 | [diff] [blame] | 570 | // Same as _error_message() |
| 571 | $error = is_resource($this->conn_id) ? oci_error($this->conn_id) : oci_error(); |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 572 | return $error['code']; |
| 573 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 574 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 575 | // -------------------------------------------------------------------- |
| 576 | |
| 577 | /** |
| 578 | * Escape the SQL Identifiers |
| 579 | * |
| 580 | * This function escapes column and table names |
| 581 | * |
Andrey Andreev | bc95e47 | 2011-10-20 09:44:48 +0300 | [diff] [blame] | 582 | * @access protected |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 583 | * @param string |
| 584 | * @return string |
| 585 | */ |
Andrey Andreev | bc95e47 | 2011-10-20 09:44:48 +0300 | [diff] [blame] | 586 | protected function _escape_identifiers($item) |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 587 | { |
| 588 | if ($this->_escape_char == '') |
| 589 | { |
| 590 | return $item; |
| 591 | } |
| 592 | |
| 593 | foreach ($this->_reserved_identifiers as $id) |
| 594 | { |
| 595 | if (strpos($item, '.'.$id) !== FALSE) |
| 596 | { |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 597 | $str = $this->_escape_char. str_replace('.', $this->_escape_char.'.', $item); |
| 598 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 599 | // remove duplicates if the user already included the escape |
| 600 | return preg_replace('/['.$this->_escape_char.']+/', $this->_escape_char, $str); |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 601 | } |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 602 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 603 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 604 | if (strpos($item, '.') !== FALSE) |
| 605 | { |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 606 | $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] | 607 | } |
| 608 | else |
| 609 | { |
| 610 | $str = $this->_escape_char.$item.$this->_escape_char; |
| 611 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 612 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 613 | // remove duplicates if the user already included the escape |
| 614 | return preg_replace('/['.$this->_escape_char.']+/', $this->_escape_char, $str); |
| 615 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 616 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 617 | // -------------------------------------------------------------------- |
| 618 | |
| 619 | /** |
| 620 | * From Tables |
| 621 | * |
| 622 | * This function implicitly groups FROM tables so there is no confusion |
| 623 | * about operator precedence in harmony with SQL standards |
| 624 | * |
Andrey Andreev | bc95e47 | 2011-10-20 09:44:48 +0300 | [diff] [blame] | 625 | * @access protected |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 626 | * @param type |
| 627 | * @return type |
| 628 | */ |
Andrey Andreev | bc95e47 | 2011-10-20 09:44:48 +0300 | [diff] [blame] | 629 | protected function _from_tables($tables) |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 630 | { |
| 631 | if ( ! is_array($tables)) |
| 632 | { |
| 633 | $tables = array($tables); |
| 634 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 635 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 636 | return implode(', ', $tables); |
| 637 | } |
| 638 | |
| 639 | // -------------------------------------------------------------------- |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 640 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 641 | /** |
| 642 | * Insert statement |
| 643 | * |
| 644 | * Generates a platform-specific insert string from the supplied data |
| 645 | * |
Derek Jones | 4b9c629 | 2011-07-01 17:40:48 -0500 | [diff] [blame] | 646 | * @access public |
| 647 | * @param string the table name |
| 648 | * @param array the insert keys |
| 649 | * @param array the insert values |
| 650 | * @return string |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 651 | */ |
Andrey Andreev | bc95e47 | 2011-10-20 09:44:48 +0300 | [diff] [blame] | 652 | protected function _insert($table, $keys, $values) |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 653 | { |
Andrey Andreev | 5c3a202 | 2011-10-07 21:04:58 +0300 | [diff] [blame] | 654 | return "INSERT INTO ".$table." (".implode(', ', $keys).") VALUES (".implode(', ', $values).")"; |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 655 | } |
| 656 | |
| 657 | // -------------------------------------------------------------------- |
| 658 | |
| 659 | /** |
Andrey Andreev | 99c6dd4 | 2011-09-23 03:07:01 +0300 | [diff] [blame] | 660 | * Insert_batch statement |
| 661 | * |
| 662 | * Generates a platform-specific insert string from the supplied data |
| 663 | * |
Greg Aker | 03abee3 | 2011-12-25 00:31:29 -0600 | [diff] [blame] | 664 | * @param string the table name |
| 665 | * @param array the insert keys |
| 666 | * @param array the insert values |
| 667 | * @return string |
Andrey Andreev | 99c6dd4 | 2011-09-23 03:07:01 +0300 | [diff] [blame] | 668 | */ |
Andrey Andreev | bc95e47 | 2011-10-20 09:44:48 +0300 | [diff] [blame] | 669 | protected function _insert_batch($table, $keys, $values) |
Andrey Andreev | 99c6dd4 | 2011-09-23 03:07:01 +0300 | [diff] [blame] | 670 | { |
| 671 | $keys = implode(', ', $keys); |
| 672 | $sql = "INSERT ALL\n"; |
| 673 | |
| 674 | for ($i = 0, $c = count($values); $i < $c; $i++) |
Andrey Andreev | b83c408 | 2011-09-23 03:32:45 +0300 | [diff] [blame] | 675 | { |
Andrey Andreev | 99c6dd4 | 2011-09-23 03:07:01 +0300 | [diff] [blame] | 676 | $sql .= ' INTO ' . $table . ' (' . $keys . ') VALUES ' . $values[$i] . "\n"; |
Andrey Andreev | b83c408 | 2011-09-23 03:32:45 +0300 | [diff] [blame] | 677 | } |
Andrey Andreev | 99c6dd4 | 2011-09-23 03:07:01 +0300 | [diff] [blame] | 678 | |
| 679 | $sql .= 'SELECT * FROM dual'; |
| 680 | |
| 681 | return $sql; |
| 682 | } |
| 683 | |
| 684 | // -------------------------------------------------------------------- |
| 685 | |
| 686 | /** |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 687 | * Update statement |
| 688 | * |
| 689 | * Generates a platform-specific update string from the supplied data |
| 690 | * |
Andrey Andreev | bc95e47 | 2011-10-20 09:44:48 +0300 | [diff] [blame] | 691 | * @access protected |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 692 | * @param string the table name |
| 693 | * @param array the update data |
| 694 | * @param array the where clause |
| 695 | * @param array the orderby clause |
| 696 | * @param array the limit clause |
| 697 | * @return string |
| 698 | */ |
Andrey Andreev | bc95e47 | 2011-10-20 09:44:48 +0300 | [diff] [blame] | 699 | protected function _update($table, $values, $where, $orderby = array(), $limit = FALSE) |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 700 | { |
Pascal Kriete | c3a4a8d | 2011-02-14 13:40:08 -0500 | [diff] [blame] | 701 | foreach ($values as $key => $val) |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 702 | { |
| 703 | $valstr[] = $key." = ".$val; |
| 704 | } |
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 | $limit = ( ! $limit) ? '' : ' LIMIT '.$limit; |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 707 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 708 | $orderby = (count($orderby) >= 1)?' ORDER BY '.implode(", ", $orderby):''; |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 709 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 710 | $sql = "UPDATE ".$table." SET ".implode(', ', $valstr); |
| 711 | |
| 712 | $sql .= ($where != '' AND count($where) >=1) ? " WHERE ".implode(" ", $where) : ''; |
| 713 | |
| 714 | $sql .= $orderby.$limit; |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 715 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 716 | return $sql; |
| 717 | } |
| 718 | |
| 719 | // -------------------------------------------------------------------- |
| 720 | |
| 721 | /** |
| 722 | * Truncate statement |
| 723 | * |
| 724 | * Generates a platform-specific truncate string from the supplied data |
| 725 | * If the database does not support the truncate() command |
| 726 | * This function maps to "DELETE FROM table" |
| 727 | * |
Andrey Andreev | bc95e47 | 2011-10-20 09:44:48 +0300 | [diff] [blame] | 728 | * @access protected |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 729 | * @param string the table name |
| 730 | * @return string |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 731 | */ |
Andrey Andreev | bc95e47 | 2011-10-20 09:44:48 +0300 | [diff] [blame] | 732 | protected function _truncate($table) |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 733 | { |
| 734 | return "TRUNCATE TABLE ".$table; |
| 735 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 736 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 737 | // -------------------------------------------------------------------- |
| 738 | |
| 739 | /** |
| 740 | * Delete statement |
| 741 | * |
| 742 | * Generates a platform-specific delete string from the supplied data |
| 743 | * |
Andrey Andreev | bc95e47 | 2011-10-20 09:44:48 +0300 | [diff] [blame] | 744 | * @access protected |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 745 | * @param string the table name |
| 746 | * @param array the where clause |
| 747 | * @param string the limit clause |
| 748 | * @return string |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 749 | */ |
Andrey Andreev | bc95e47 | 2011-10-20 09:44:48 +0300 | [diff] [blame] | 750 | protected function _delete($table, $where = array(), $like = array(), $limit = FALSE) |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 751 | { |
| 752 | $conditions = ''; |
| 753 | |
| 754 | if (count($where) > 0 OR count($like) > 0) |
| 755 | { |
| 756 | $conditions = "\nWHERE "; |
Jamie Rumbelow | 7efad20 | 2012-02-19 12:37:00 +0000 | [diff] [blame^] | 757 | $conditions .= implode("\n", $this->qb_where); |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 758 | |
| 759 | if (count($where) > 0 && count($like) > 0) |
| 760 | { |
| 761 | $conditions .= " AND "; |
| 762 | } |
| 763 | $conditions .= implode("\n", $like); |
| 764 | } |
| 765 | |
| 766 | $limit = ( ! $limit) ? '' : ' LIMIT '.$limit; |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 767 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 768 | return "DELETE FROM ".$table.$conditions.$limit; |
| 769 | } |
| 770 | |
| 771 | // -------------------------------------------------------------------- |
| 772 | |
| 773 | /** |
| 774 | * Limit string |
| 775 | * |
| 776 | * Generates a platform-specific LIMIT clause |
| 777 | * |
Andrey Andreev | bc95e47 | 2011-10-20 09:44:48 +0300 | [diff] [blame] | 778 | * @access protected |
Derek Jones | 4b9c629 | 2011-07-01 17:40:48 -0500 | [diff] [blame] | 779 | * @param string the sql query string |
| 780 | * @param integer the number of rows to limit the query to |
| 781 | * @param integer the offset value |
| 782 | * @return string |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 783 | */ |
Andrey Andreev | bc95e47 | 2011-10-20 09:44:48 +0300 | [diff] [blame] | 784 | protected function _limit($sql, $limit, $offset) |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 785 | { |
| 786 | $limit = $offset + $limit; |
| 787 | $newsql = "SELECT * FROM (select inner_query.*, rownum rnum FROM ($sql) inner_query WHERE rownum < $limit)"; |
| 788 | |
| 789 | if ($offset != 0) |
| 790 | { |
| 791 | $newsql .= " WHERE rnum >= $offset"; |
| 792 | } |
| 793 | |
| 794 | // remember that we used limits |
| 795 | $this->limit_used = TRUE; |
| 796 | |
| 797 | return $newsql; |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 798 | } |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 799 | |
| 800 | // -------------------------------------------------------------------- |
| 801 | |
| 802 | /** |
| 803 | * Close DB Connection |
| 804 | * |
Andrey Andreev | bc95e47 | 2011-10-20 09:44:48 +0300 | [diff] [blame] | 805 | * @access protected |
Derek Jones | 4b9c629 | 2011-07-01 17:40:48 -0500 | [diff] [blame] | 806 | * @param resource |
| 807 | * @return void |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 808 | */ |
Andrey Andreev | bc95e47 | 2011-10-20 09:44:48 +0300 | [diff] [blame] | 809 | protected function _close($conn_id) |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 810 | { |
Andrey Andreev | 5c3a202 | 2011-10-07 21:04:58 +0300 | [diff] [blame] | 811 | @oci_close($conn_id); |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 812 | } |
| 813 | |
| 814 | |
| 815 | } |
| 816 | |
| 817 | |
| 818 | |
| 819 | /* End of file oci8_driver.php */ |
Andrey Andreev | 99c6dd4 | 2011-09-23 03:07:01 +0300 | [diff] [blame] | 820 | /* Location: ./system/database/drivers/oci8/oci8_driver.php */ |