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 | * |
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 |
| 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 | * |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [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 |
| 24 | * @since Version 1.0 |
| 25 | * @filesource |
| 26 | */ |
| 27 | |
| 28 | // ------------------------------------------------------------------------ |
| 29 | |
| 30 | /** |
| 31 | * ODBC Database Adapter Class |
| 32 | * |
| 33 | * Note: _DB is an extender class that the app controller |
| 34 | * creates dynamically based on whether the active record |
| 35 | * class is being used or not. |
| 36 | * |
| 37 | * @package CodeIgniter |
| 38 | * @subpackage Drivers |
| 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 | class CI_DB_odbc_driver extends CI_DB { |
| 44 | |
| 45 | var $dbdriver = 'odbc'; |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 46 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 47 | // the character used to excape - not necessary for ODBC |
| 48 | var $_escape_char = ''; |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 49 | |
Derek Jones | e4ed583 | 2009-02-20 21:44:59 +0000 | [diff] [blame] | 50 | // clause and character used for LIKE escape sequences |
Timothy Warren | 142ca8e | 2012-03-19 18:00:35 -0400 | [diff] [blame^] | 51 | protected $_like_escape_str = " {escape '%s'} "; |
| 52 | protected $_like_escape_chr = '!'; |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 53 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 54 | /** |
| 55 | * The syntax to count rows is slightly different across different |
| 56 | * database engines, so this string appears in each driver and is |
Timothy Warren | 142ca8e | 2012-03-19 18:00:35 -0400 | [diff] [blame^] | 57 | * used for the count_all() and count_all_results() public functions. |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 58 | */ |
Timothy Warren | 142ca8e | 2012-03-19 18:00:35 -0400 | [diff] [blame^] | 59 | protected $_count_string = "SELECT COUNT(*) AS "; |
| 60 | protected $_random_keyword; |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 61 | |
Timothy Warren | 142ca8e | 2012-03-19 18:00:35 -0400 | [diff] [blame^] | 62 | /** |
| 63 | * Constructor, to define random keyword |
| 64 | */ |
| 65 | public function __construct($params) |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 66 | { |
Timothy Warren | a2097a0 | 2011-10-10 10:10:46 -0400 | [diff] [blame] | 67 | parent::__construct($params); |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 68 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 69 | $this->_random_keyword = ' RND('.time().')'; // database specific random keyword |
| 70 | } |
Timothy Warren | 142ca8e | 2012-03-19 18:00:35 -0400 | [diff] [blame^] | 71 | |
| 72 | // -------------------------------------------------------------------------- |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 73 | |
| 74 | /** |
| 75 | * Non-persistent database connection |
| 76 | * |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 77 | * @return resource |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 78 | */ |
Timothy Warren | 142ca8e | 2012-03-19 18:00:35 -0400 | [diff] [blame^] | 79 | protected function db_connect() |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 80 | { |
| 81 | return @odbc_connect($this->hostname, $this->username, $this->password); |
| 82 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 83 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 84 | // -------------------------------------------------------------------- |
| 85 | |
| 86 | /** |
| 87 | * Persistent database connection |
| 88 | * |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 89 | * @return resource |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 90 | */ |
Timothy Warren | 142ca8e | 2012-03-19 18:00:35 -0400 | [diff] [blame^] | 91 | protected function db_pconnect() |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 92 | { |
| 93 | return @odbc_pconnect($this->hostname, $this->username, $this->password); |
| 94 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 95 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 96 | // -------------------------------------------------------------------- |
| 97 | |
| 98 | /** |
Derek Jones | 87cbafc | 2009-02-27 16:29:59 +0000 | [diff] [blame] | 99 | * Reconnect |
| 100 | * |
| 101 | * Keep / reestablish the db connection if no queries have been |
| 102 | * sent for a length of time exceeding the server's idle timeout |
| 103 | * |
Derek Jones | 87cbafc | 2009-02-27 16:29:59 +0000 | [diff] [blame] | 104 | * @return void |
| 105 | */ |
Timothy Warren | 142ca8e | 2012-03-19 18:00:35 -0400 | [diff] [blame^] | 106 | public function reconnect() |
Derek Jones | 87cbafc | 2009-02-27 16:29:59 +0000 | [diff] [blame] | 107 | { |
| 108 | // not implemented in odbc |
| 109 | } |
| 110 | |
| 111 | // -------------------------------------------------------------------- |
| 112 | |
| 113 | /** |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 114 | * Select the database |
| 115 | * |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 116 | * @return resource |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 117 | */ |
Timothy Warren | 142ca8e | 2012-03-19 18:00:35 -0400 | [diff] [blame^] | 118 | protected function db_select() |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 119 | { |
| 120 | // Not needed for ODBC |
| 121 | return TRUE; |
| 122 | } |
| 123 | |
| 124 | // -------------------------------------------------------------------- |
| 125 | |
| 126 | /** |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 127 | * Execute the query |
| 128 | * |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 129 | * @param string an SQL query |
| 130 | * @return resource |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 131 | */ |
Timothy Warren | 142ca8e | 2012-03-19 18:00:35 -0400 | [diff] [blame^] | 132 | protected function _execute($sql) |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 133 | { |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 134 | return @odbc_exec($this->conn_id, $sql); |
| 135 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 136 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 137 | // -------------------------------------------------------------------- |
| 138 | |
| 139 | /** |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 140 | * Begin Transaction |
| 141 | * |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 142 | * @return bool |
| 143 | */ |
Timothy Warren | 142ca8e | 2012-03-19 18:00:35 -0400 | [diff] [blame^] | 144 | public function trans_begin($test_mode = FALSE) |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 145 | { |
| 146 | if ( ! $this->trans_enabled) |
| 147 | { |
| 148 | return TRUE; |
| 149 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 150 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 151 | // When transactions are nested we only begin/commit/rollback the outermost ones |
| 152 | if ($this->_trans_depth > 0) |
| 153 | { |
| 154 | return TRUE; |
| 155 | } |
| 156 | |
| 157 | // Reset the transaction failure flag. |
| 158 | // If the $test_mode flag is set to TRUE transactions will be rolled back |
| 159 | // even if the queries produce a successful result. |
| 160 | $this->_trans_failure = ($test_mode === TRUE) ? TRUE : FALSE; |
| 161 | |
| 162 | return odbc_autocommit($this->conn_id, FALSE); |
| 163 | } |
| 164 | |
| 165 | // -------------------------------------------------------------------- |
| 166 | |
| 167 | /** |
| 168 | * Commit Transaction |
| 169 | * |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 170 | * @return bool |
| 171 | */ |
Timothy Warren | 142ca8e | 2012-03-19 18:00:35 -0400 | [diff] [blame^] | 172 | public function trans_commit() |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 173 | { |
| 174 | if ( ! $this->trans_enabled) |
| 175 | { |
| 176 | return TRUE; |
| 177 | } |
| 178 | |
| 179 | // When transactions are nested we only begin/commit/rollback the outermost ones |
| 180 | if ($this->_trans_depth > 0) |
| 181 | { |
| 182 | return TRUE; |
| 183 | } |
| 184 | |
| 185 | $ret = odbc_commit($this->conn_id); |
| 186 | odbc_autocommit($this->conn_id, TRUE); |
| 187 | return $ret; |
| 188 | } |
| 189 | |
| 190 | // -------------------------------------------------------------------- |
| 191 | |
| 192 | /** |
| 193 | * Rollback Transaction |
| 194 | * |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 195 | * @return bool |
| 196 | */ |
Timothy Warren | 142ca8e | 2012-03-19 18:00:35 -0400 | [diff] [blame^] | 197 | public function trans_rollback() |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 198 | { |
| 199 | if ( ! $this->trans_enabled) |
| 200 | { |
| 201 | return TRUE; |
| 202 | } |
| 203 | |
| 204 | // When transactions are nested we only begin/commit/rollback the outermost ones |
| 205 | if ($this->_trans_depth > 0) |
| 206 | { |
| 207 | return TRUE; |
| 208 | } |
| 209 | |
| 210 | $ret = odbc_rollback($this->conn_id); |
| 211 | odbc_autocommit($this->conn_id, TRUE); |
| 212 | return $ret; |
| 213 | } |
| 214 | |
| 215 | // -------------------------------------------------------------------- |
| 216 | |
| 217 | /** |
| 218 | * Escape String |
| 219 | * |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 220 | * @param string |
Derek Jones | e4ed583 | 2009-02-20 21:44:59 +0000 | [diff] [blame] | 221 | * @param bool whether or not the string will be used in a LIKE condition |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 222 | * @return string |
| 223 | */ |
Timothy Warren | 142ca8e | 2012-03-19 18:00:35 -0400 | [diff] [blame^] | 224 | public function escape_str($str, $like = FALSE) |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 225 | { |
Derek Jones | e4ed583 | 2009-02-20 21:44:59 +0000 | [diff] [blame] | 226 | if (is_array($str)) |
| 227 | { |
Pascal Kriete | c3a4a8d | 2011-02-14 13:40:08 -0500 | [diff] [blame] | 228 | foreach ($str as $key => $val) |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 229 | { |
Derek Jones | e4ed583 | 2009-02-20 21:44:59 +0000 | [diff] [blame] | 230 | $str[$key] = $this->escape_str($val, $like); |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 231 | } |
| 232 | |
| 233 | return $str; |
| 234 | } |
| 235 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 236 | // ODBC doesn't require escaping |
Greg Aker | 757dda6 | 2010-04-14 19:06:19 -0500 | [diff] [blame] | 237 | $str = remove_invisible_characters($str); |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 238 | |
Derek Jones | e4ed583 | 2009-02-20 21:44:59 +0000 | [diff] [blame] | 239 | // escape LIKE condition wildcards |
| 240 | if ($like === TRUE) |
| 241 | { |
| 242 | $str = str_replace( array('%', '_', $this->_like_escape_chr), |
| 243 | array($this->_like_escape_chr.'%', $this->_like_escape_chr.'_', $this->_like_escape_chr.$this->_like_escape_chr), |
| 244 | $str); |
| 245 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 246 | |
Derek Jones | e4ed583 | 2009-02-20 21:44:59 +0000 | [diff] [blame] | 247 | return $str; |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 248 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 249 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 250 | // -------------------------------------------------------------------- |
| 251 | |
| 252 | /** |
| 253 | * Affected Rows |
| 254 | * |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 255 | * @return integer |
| 256 | */ |
Timothy Warren | 142ca8e | 2012-03-19 18:00:35 -0400 | [diff] [blame^] | 257 | public function affected_rows() |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 258 | { |
| 259 | return @odbc_num_rows($this->conn_id); |
| 260 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 261 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 262 | // -------------------------------------------------------------------- |
| 263 | |
| 264 | /** |
| 265 | * Insert ID |
| 266 | * |
Andrey Andreev | 8af7666 | 2012-03-05 14:33:41 +0200 | [diff] [blame] | 267 | * @return bool |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 268 | */ |
Andrey Andreev | 8af7666 | 2012-03-05 14:33:41 +0200 | [diff] [blame] | 269 | public function insert_id() |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 270 | { |
Andrey Andreev | 8af7666 | 2012-03-05 14:33:41 +0200 | [diff] [blame] | 271 | return ($this->db->db_debug) ? $this->db->display_error('db_unsuported_feature') : FALSE; |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 272 | } |
| 273 | |
| 274 | // -------------------------------------------------------------------- |
| 275 | |
| 276 | /** |
| 277 | * "Count All" query |
| 278 | * |
| 279 | * Generates a platform-specific query string that counts all records in |
| 280 | * the specified database |
| 281 | * |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 282 | * @param string |
| 283 | * @return string |
| 284 | */ |
Timothy Warren | 142ca8e | 2012-03-19 18:00:35 -0400 | [diff] [blame^] | 285 | public function count_all($table = '') |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 286 | { |
| 287 | if ($table == '') |
Derek Allard | e37ab38 | 2009-02-03 16:13:57 +0000 | [diff] [blame] | 288 | { |
| 289 | return 0; |
| 290 | } |
| 291 | |
Andrey Andreev | 032e7ea | 2012-03-06 19:48:35 +0200 | [diff] [blame] | 292 | $query = $this->query($this->_count_string . $this->protect_identifiers('numrows') . " FROM " . $this->protect_identifiers($table, TRUE, NULL, FALSE)); |
Derek Allard | e37ab38 | 2009-02-03 16:13:57 +0000 | [diff] [blame] | 293 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 294 | if ($query->num_rows() == 0) |
Derek Allard | e37ab38 | 2009-02-03 16:13:57 +0000 | [diff] [blame] | 295 | { |
| 296 | return 0; |
| 297 | } |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 298 | |
| 299 | $row = $query->row(); |
Greg Aker | 90248ab | 2011-08-20 14:23:14 -0500 | [diff] [blame] | 300 | $this->_reset_select(); |
Derek Allard | e37ab38 | 2009-02-03 16:13:57 +0000 | [diff] [blame] | 301 | return (int) $row->numrows; |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 302 | } |
| 303 | |
| 304 | // -------------------------------------------------------------------- |
| 305 | |
| 306 | /** |
| 307 | * Show table query |
| 308 | * |
| 309 | * Generates a platform-specific query string so that the table names can be fetched |
| 310 | * |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 311 | * @param boolean |
| 312 | * @return string |
| 313 | */ |
Timothy Warren | 142ca8e | 2012-03-19 18:00:35 -0400 | [diff] [blame^] | 314 | protected function _list_tables($prefix_limit = FALSE) |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 315 | { |
| 316 | $sql = "SHOW TABLES FROM `".$this->database."`"; |
| 317 | |
| 318 | if ($prefix_limit !== FALSE AND $this->dbprefix != '') |
| 319 | { |
Greg Aker | 0d42489 | 2010-01-26 02:14:44 +0000 | [diff] [blame] | 320 | //$sql .= " 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] | 321 | return FALSE; // not currently supported |
| 322 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 323 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 324 | return $sql; |
| 325 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 326 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 327 | // -------------------------------------------------------------------- |
| 328 | |
| 329 | /** |
| 330 | * Show column query |
| 331 | * |
| 332 | * Generates a platform-specific query string so that the column names can be fetched |
| 333 | * |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 334 | * @param string the table name |
| 335 | * @return string |
| 336 | */ |
Timothy Warren | 142ca8e | 2012-03-19 18:00:35 -0400 | [diff] [blame^] | 337 | public function _list_columns($table = '') |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 338 | { |
| 339 | return "SHOW COLUMNS FROM ".$table; |
| 340 | } |
| 341 | |
| 342 | // -------------------------------------------------------------------- |
| 343 | |
| 344 | /** |
| 345 | * Field data query |
| 346 | * |
| 347 | * Generates a platform-specific query so that the column data can be retrieved |
| 348 | * |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 349 | * @param string the table name |
| 350 | * @return object |
| 351 | */ |
Timothy Warren | 142ca8e | 2012-03-19 18:00:35 -0400 | [diff] [blame^] | 352 | public function _field_data($table) |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 353 | { |
| 354 | return "SELECT TOP 1 FROM ".$table; |
| 355 | } |
| 356 | |
| 357 | // -------------------------------------------------------------------- |
| 358 | |
| 359 | /** |
Andrey Andreev | 4be5de1 | 2012-03-02 15:45:41 +0200 | [diff] [blame] | 360 | * Error |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 361 | * |
Andrey Andreev | 4be5de1 | 2012-03-02 15:45:41 +0200 | [diff] [blame] | 362 | * Returns an array containing code and message of the last |
| 363 | * database error that has occured. |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 364 | * |
Andrey Andreev | 4be5de1 | 2012-03-02 15:45:41 +0200 | [diff] [blame] | 365 | * @return array |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 366 | */ |
Andrey Andreev | 4be5de1 | 2012-03-02 15:45:41 +0200 | [diff] [blame] | 367 | public function error() |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 368 | { |
Andrey Andreev | 4be5de1 | 2012-03-02 15:45:41 +0200 | [diff] [blame] | 369 | return array('code' => odbc_error($this->conn_id), 'message' => odbc_errormsg($this->conn_id)); |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 370 | } |
| 371 | |
| 372 | // -------------------------------------------------------------------- |
| 373 | |
| 374 | /** |
| 375 | * Escape the SQL Identifiers |
| 376 | * |
Timothy Warren | 142ca8e | 2012-03-19 18:00:35 -0400 | [diff] [blame^] | 377 | * This public function escapes column and table names |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 378 | * |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 379 | * @param string |
| 380 | * @return string |
| 381 | */ |
Timothy Warren | 142ca8e | 2012-03-19 18:00:35 -0400 | [diff] [blame^] | 382 | protected function _escape_identifiers($item) |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 383 | { |
| 384 | if ($this->_escape_char == '') |
| 385 | { |
| 386 | return $item; |
| 387 | } |
| 388 | |
| 389 | foreach ($this->_reserved_identifiers as $id) |
| 390 | { |
| 391 | if (strpos($item, '.'.$id) !== FALSE) |
| 392 | { |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 393 | $str = $this->_escape_char. str_replace('.', $this->_escape_char.'.', $item); |
| 394 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 395 | // remove duplicates if the user already included the escape |
| 396 | return preg_replace('/['.$this->_escape_char.']+/', $this->_escape_char, $str); |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 397 | } |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 398 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 399 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 400 | if (strpos($item, '.') !== FALSE) |
| 401 | { |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 402 | $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] | 403 | } |
| 404 | else |
| 405 | { |
| 406 | $str = $this->_escape_char.$item.$this->_escape_char; |
| 407 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 408 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 409 | // remove duplicates if the user already included the escape |
| 410 | return preg_replace('/['.$this->_escape_char.']+/', $this->_escape_char, $str); |
| 411 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 412 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 413 | // -------------------------------------------------------------------- |
| 414 | |
| 415 | /** |
| 416 | * From Tables |
| 417 | * |
Timothy Warren | 142ca8e | 2012-03-19 18:00:35 -0400 | [diff] [blame^] | 418 | * This public function implicitly groups FROM tables so there is no confusion |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 419 | * about operator precedence in harmony with SQL standards |
| 420 | * |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 421 | * @param type |
| 422 | * @return type |
| 423 | */ |
Timothy Warren | 142ca8e | 2012-03-19 18:00:35 -0400 | [diff] [blame^] | 424 | public function _from_tables($tables) |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 425 | { |
| 426 | if ( ! is_array($tables)) |
| 427 | { |
| 428 | $tables = array($tables); |
| 429 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 430 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 431 | return '('.implode(', ', $tables).')'; |
| 432 | } |
| 433 | |
| 434 | // -------------------------------------------------------------------- |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 435 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 436 | /** |
| 437 | * Insert statement |
| 438 | * |
| 439 | * Generates a platform-specific insert string from the supplied data |
| 440 | * |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 441 | * @param string the table name |
| 442 | * @param array the insert keys |
| 443 | * @param array the insert values |
| 444 | * @return string |
| 445 | */ |
Timothy Warren | 142ca8e | 2012-03-19 18:00:35 -0400 | [diff] [blame^] | 446 | public function _insert($table, $keys, $values) |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 447 | { |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 448 | return "INSERT INTO ".$table." (".implode(', ', $keys).") VALUES (".implode(', ', $values).")"; |
| 449 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 450 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 451 | // -------------------------------------------------------------------- |
| 452 | |
| 453 | /** |
| 454 | * Update statement |
| 455 | * |
| 456 | * Generates a platform-specific update string from the supplied data |
| 457 | * |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 458 | * @param string the table name |
| 459 | * @param array the update data |
| 460 | * @param array the where clause |
| 461 | * @param array the orderby clause |
| 462 | * @param array the limit clause |
| 463 | * @return string |
| 464 | */ |
Timothy Warren | 142ca8e | 2012-03-19 18:00:35 -0400 | [diff] [blame^] | 465 | public function _update($table, $values, $where, $orderby = array(), $limit = FALSE) |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 466 | { |
Pascal Kriete | c3a4a8d | 2011-02-14 13:40:08 -0500 | [diff] [blame] | 467 | foreach ($values as $key => $val) |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 468 | { |
| 469 | $valstr[] = $key." = ".$val; |
| 470 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 471 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 472 | $limit = ( ! $limit) ? '' : ' LIMIT '.$limit; |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 473 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 474 | $orderby = (count($orderby) >= 1)?' ORDER BY '.implode(", ", $orderby):''; |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 475 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 476 | $sql = "UPDATE ".$table." SET ".implode(', ', $valstr); |
| 477 | |
| 478 | $sql .= ($where != '' AND count($where) >=1) ? " WHERE ".implode(" ", $where) : ''; |
| 479 | |
| 480 | $sql .= $orderby.$limit; |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 481 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 482 | return $sql; |
| 483 | } |
| 484 | |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 485 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 486 | // -------------------------------------------------------------------- |
| 487 | |
| 488 | /** |
| 489 | * Truncate statement |
| 490 | * |
| 491 | * Generates a platform-specific truncate string from the supplied data |
| 492 | * If the database does not support the truncate() command |
Timothy Warren | 142ca8e | 2012-03-19 18:00:35 -0400 | [diff] [blame^] | 493 | * This public function maps to "DELETE FROM table" |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 494 | * |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 495 | * @param string the table name |
| 496 | * @return string |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 497 | */ |
Timothy Warren | 142ca8e | 2012-03-19 18:00:35 -0400 | [diff] [blame^] | 498 | public function _truncate($table) |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 499 | { |
| 500 | return $this->_delete($table); |
| 501 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 502 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 503 | // -------------------------------------------------------------------- |
| 504 | |
| 505 | /** |
| 506 | * Delete statement |
| 507 | * |
| 508 | * Generates a platform-specific delete string from the supplied data |
| 509 | * |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 510 | * @param string the table name |
| 511 | * @param array the where clause |
| 512 | * @param string the limit clause |
| 513 | * @return string |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 514 | */ |
Timothy Warren | 142ca8e | 2012-03-19 18:00:35 -0400 | [diff] [blame^] | 515 | public function _delete($table, $where = array(), $like = array(), $limit = FALSE) |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 516 | { |
| 517 | $conditions = ''; |
| 518 | |
| 519 | if (count($where) > 0 OR count($like) > 0) |
| 520 | { |
| 521 | $conditions = "\nWHERE "; |
| 522 | $conditions .= implode("\n", $this->ar_where); |
| 523 | |
| 524 | if (count($where) > 0 && count($like) > 0) |
| 525 | { |
| 526 | $conditions .= " AND "; |
| 527 | } |
| 528 | $conditions .= implode("\n", $like); |
| 529 | } |
| 530 | |
| 531 | $limit = ( ! $limit) ? '' : ' LIMIT '.$limit; |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 532 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 533 | return "DELETE FROM ".$table.$conditions.$limit; |
| 534 | } |
| 535 | |
| 536 | // -------------------------------------------------------------------- |
| 537 | |
| 538 | /** |
| 539 | * Limit string |
| 540 | * |
| 541 | * Generates a platform-specific LIMIT clause |
| 542 | * |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 543 | * @param string the sql query string |
| 544 | * @param integer the number of rows to limit the query to |
| 545 | * @param integer the offset value |
| 546 | * @return string |
| 547 | */ |
Timothy Warren | 142ca8e | 2012-03-19 18:00:35 -0400 | [diff] [blame^] | 548 | public function _limit($sql, $limit, $offset) |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 549 | { |
| 550 | // Does ODBC doesn't use the LIMIT clause? |
| 551 | return $sql; |
| 552 | } |
| 553 | |
| 554 | // -------------------------------------------------------------------- |
| 555 | |
| 556 | /** |
| 557 | * Close DB Connection |
| 558 | * |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 559 | * @param resource |
| 560 | * @return void |
| 561 | */ |
Timothy Warren | 142ca8e | 2012-03-19 18:00:35 -0400 | [diff] [blame^] | 562 | public function _close($conn_id) |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 563 | { |
| 564 | @odbc_close($conn_id); |
| 565 | } |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 566 | } |
| 567 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 568 | /* End of file odbc_driver.php */ |
Timothy Warren | 142ca8e | 2012-03-19 18:00:35 -0400 | [diff] [blame^] | 569 | /* Location: ./system/database/drivers/odbc/odbc_driver.php */ |