Andrey Andreev | b76029d | 2012-01-26 15:13:19 +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 | b76029d | 2012-01-26 15:13:19 +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 | b76029d | 2012-01-26 15:13:19 +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 | * |
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 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 28 | /** |
| 29 | * ODBC 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 | * |
| 35 | * @package CodeIgniter |
| 36 | * @subpackage Drivers |
| 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 | class CI_DB_odbc_driver extends CI_DB { |
| 42 | |
Andrey Andreev | b76029d | 2012-01-26 15:13:19 +0200 | [diff] [blame] | 43 | public $dbdriver = 'odbc'; |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 44 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 45 | // the character used to excape - not necessary for ODBC |
Andrey Andreev | b76029d | 2012-01-26 15:13:19 +0200 | [diff] [blame] | 46 | protected $_escape_char = ''; |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 47 | |
Derek Jones | e4ed583 | 2009-02-20 21:44:59 +0000 | [diff] [blame] | 48 | // clause and character used for LIKE escape sequences |
Andrey Andreev | 5f356bf | 2012-03-20 14:32:27 +0200 | [diff] [blame] | 49 | protected $_like_escape_str = " {escape '%s'} "; |
Andrey Andreev | b76029d | 2012-01-26 15:13:19 +0200 | [diff] [blame] | 50 | protected $_like_escape_chr = '!'; |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 51 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 52 | /** |
| 53 | * The syntax to count rows is slightly different across different |
| 54 | * database engines, so this string appears in each driver and is |
| 55 | * used for the count_all() and count_all_results() functions. |
| 56 | */ |
Andrey Andreev | b76029d | 2012-01-26 15:13:19 +0200 | [diff] [blame] | 57 | protected $_count_string = 'SELECT COUNT(*) AS '; |
| 58 | protected $_random_keyword; |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 59 | |
Andrey Andreev | 5f356bf | 2012-03-20 14:32:27 +0200 | [diff] [blame] | 60 | public function __construct($params) |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 61 | { |
Timothy Warren | a2097a0 | 2011-10-10 10:10:46 -0400 | [diff] [blame] | 62 | parent::__construct($params); |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 63 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 64 | $this->_random_keyword = ' RND('.time().')'; // database specific random keyword |
Andrey Andreev | 46f6dbc | 2012-02-13 01:39:53 +0200 | [diff] [blame] | 65 | |
| 66 | // Legacy support for DSN in the hostname field |
| 67 | if ($this->dsn == '') |
| 68 | { |
| 69 | $this->dsn = $this->hostname; |
| 70 | } |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 71 | } |
| 72 | |
| 73 | /** |
| 74 | * Non-persistent database connection |
| 75 | * |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 76 | * @return resource |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 77 | */ |
Andrey Andreev | b76029d | 2012-01-26 15:13:19 +0200 | [diff] [blame] | 78 | public function db_connect() |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 79 | { |
Andrey Andreev | 46f6dbc | 2012-02-13 01:39:53 +0200 | [diff] [blame] | 80 | return @odbc_connect($this->dsn, $this->username, $this->password); |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 81 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 82 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 83 | // -------------------------------------------------------------------- |
| 84 | |
| 85 | /** |
| 86 | * Persistent database connection |
| 87 | * |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 88 | * @return resource |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 89 | */ |
Andrey Andreev | b76029d | 2012-01-26 15:13:19 +0200 | [diff] [blame] | 90 | public function db_pconnect() |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 91 | { |
Andrey Andreev | 46f6dbc | 2012-02-13 01:39:53 +0200 | [diff] [blame] | 92 | return @odbc_pconnect($this->dsn, $this->username, $this->password); |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 93 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 94 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 95 | // -------------------------------------------------------------------- |
| 96 | |
| 97 | /** |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 98 | * Execute the query |
| 99 | * |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 100 | * @param string an SQL query |
| 101 | * @return resource |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 102 | */ |
Andrey Andreev | b76029d | 2012-01-26 15:13:19 +0200 | [diff] [blame] | 103 | protected function _execute($sql) |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 104 | { |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 105 | return @odbc_exec($this->conn_id, $sql); |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 106 | } |
| 107 | |
| 108 | // -------------------------------------------------------------------- |
| 109 | |
| 110 | /** |
| 111 | * Begin Transaction |
| 112 | * |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 113 | * @return bool |
| 114 | */ |
Andrey Andreev | b76029d | 2012-01-26 15:13:19 +0200 | [diff] [blame] | 115 | public function trans_begin($test_mode = FALSE) |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 116 | { |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 117 | // When transactions are nested we only begin/commit/rollback the outermost ones |
Andrey Andreev | b76029d | 2012-01-26 15:13:19 +0200 | [diff] [blame] | 118 | if ( ! $this->trans_enabled OR $this->_trans_depth > 0) |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 119 | { |
| 120 | return TRUE; |
| 121 | } |
| 122 | |
| 123 | // Reset the transaction failure flag. |
| 124 | // If the $test_mode flag is set to TRUE transactions will be rolled back |
| 125 | // even if the queries produce a successful result. |
Andrey Andreev | b76029d | 2012-01-26 15:13:19 +0200 | [diff] [blame] | 126 | $this->_trans_failure = ($test_mode === TRUE); |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 127 | |
| 128 | return odbc_autocommit($this->conn_id, FALSE); |
| 129 | } |
| 130 | |
| 131 | // -------------------------------------------------------------------- |
| 132 | |
| 133 | /** |
| 134 | * Commit Transaction |
| 135 | * |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 136 | * @return bool |
| 137 | */ |
Andrey Andreev | b76029d | 2012-01-26 15:13:19 +0200 | [diff] [blame] | 138 | public function trans_commit() |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 139 | { |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 140 | // When transactions are nested we only begin/commit/rollback the outermost ones |
Andrey Andreev | b76029d | 2012-01-26 15:13:19 +0200 | [diff] [blame] | 141 | if ( ! $this->trans_enabled OR $this->_trans_depth > 0) |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 142 | { |
| 143 | return TRUE; |
| 144 | } |
| 145 | |
| 146 | $ret = odbc_commit($this->conn_id); |
| 147 | odbc_autocommit($this->conn_id, TRUE); |
| 148 | return $ret; |
| 149 | } |
| 150 | |
| 151 | // -------------------------------------------------------------------- |
| 152 | |
| 153 | /** |
| 154 | * Rollback Transaction |
| 155 | * |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 156 | * @return bool |
| 157 | */ |
Andrey Andreev | b76029d | 2012-01-26 15:13:19 +0200 | [diff] [blame] | 158 | public function trans_rollback() |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 159 | { |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 160 | // When transactions are nested we only begin/commit/rollback the outermost ones |
Andrey Andreev | b76029d | 2012-01-26 15:13:19 +0200 | [diff] [blame] | 161 | if ( ! $this->trans_enabled OR $this->_trans_depth > 0) |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 162 | { |
| 163 | return TRUE; |
| 164 | } |
| 165 | |
| 166 | $ret = odbc_rollback($this->conn_id); |
| 167 | odbc_autocommit($this->conn_id, TRUE); |
| 168 | return $ret; |
| 169 | } |
| 170 | |
| 171 | // -------------------------------------------------------------------- |
| 172 | |
| 173 | /** |
| 174 | * Escape String |
| 175 | * |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 176 | * @param string |
Derek Jones | e4ed583 | 2009-02-20 21:44:59 +0000 | [diff] [blame] | 177 | * @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] | 178 | * @return string |
| 179 | */ |
Andrey Andreev | b76029d | 2012-01-26 15:13:19 +0200 | [diff] [blame] | 180 | public function escape_str($str, $like = FALSE) |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 181 | { |
Derek Jones | e4ed583 | 2009-02-20 21:44:59 +0000 | [diff] [blame] | 182 | if (is_array($str)) |
| 183 | { |
Pascal Kriete | c3a4a8d | 2011-02-14 13:40:08 -0500 | [diff] [blame] | 184 | foreach ($str as $key => $val) |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 185 | { |
Derek Jones | e4ed583 | 2009-02-20 21:44:59 +0000 | [diff] [blame] | 186 | $str[$key] = $this->escape_str($val, $like); |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 187 | } |
| 188 | |
| 189 | return $str; |
| 190 | } |
| 191 | |
Greg Aker | 757dda6 | 2010-04-14 19:06:19 -0500 | [diff] [blame] | 192 | $str = remove_invisible_characters($str); |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 193 | |
Derek Jones | e4ed583 | 2009-02-20 21:44:59 +0000 | [diff] [blame] | 194 | // escape LIKE condition wildcards |
| 195 | if ($like === TRUE) |
| 196 | { |
Andrey Andreev | 5e18e89 | 2012-03-09 14:25:00 +0200 | [diff] [blame] | 197 | return str_replace(array($this->_like_escape_chr, '%', '_'), |
| 198 | array($this->_like_escape_chr.$this->_like_escape_chr, $this->_like_escape_chr.'%', $this->_like_escape_chr.'_'), |
Andrey Andreev | b76029d | 2012-01-26 15:13:19 +0200 | [diff] [blame] | 199 | $str); |
Derek Jones | e4ed583 | 2009-02-20 21:44:59 +0000 | [diff] [blame] | 200 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 201 | |
Derek Jones | e4ed583 | 2009-02-20 21:44:59 +0000 | [diff] [blame] | 202 | return $str; |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 203 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 204 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 205 | // -------------------------------------------------------------------- |
| 206 | |
| 207 | /** |
| 208 | * Affected Rows |
| 209 | * |
Andrey Andreev | b76029d | 2012-01-26 15:13:19 +0200 | [diff] [blame] | 210 | * @return int |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 211 | */ |
Andrey Andreev | b76029d | 2012-01-26 15:13:19 +0200 | [diff] [blame] | 212 | public function affected_rows() |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 213 | { |
| 214 | return @odbc_num_rows($this->conn_id); |
| 215 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 216 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 217 | // -------------------------------------------------------------------- |
| 218 | |
| 219 | /** |
| 220 | * Insert ID |
| 221 | * |
Andrey Andreev | 8af7666 | 2012-03-05 14:33:41 +0200 | [diff] [blame] | 222 | * @return bool |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 223 | */ |
Andrey Andreev | b76029d | 2012-01-26 15:13:19 +0200 | [diff] [blame] | 224 | public function insert_id() |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 225 | { |
Andrey Andreev | 8af7666 | 2012-03-05 14:33:41 +0200 | [diff] [blame] | 226 | 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] | 227 | } |
| 228 | |
| 229 | // -------------------------------------------------------------------- |
| 230 | |
| 231 | /** |
| 232 | * "Count All" query |
| 233 | * |
| 234 | * Generates a platform-specific query string that counts all records in |
| 235 | * the specified database |
| 236 | * |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 237 | * @param string |
Andrey Andreev | b76029d | 2012-01-26 15:13:19 +0200 | [diff] [blame] | 238 | * @return int |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 239 | */ |
Andrey Andreev | b76029d | 2012-01-26 15:13:19 +0200 | [diff] [blame] | 240 | public function count_all($table = '') |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 241 | { |
| 242 | if ($table == '') |
Derek Allard | e37ab38 | 2009-02-03 16:13:57 +0000 | [diff] [blame] | 243 | { |
| 244 | return 0; |
| 245 | } |
| 246 | |
Andrey Andreev | 9771548 | 2012-03-09 14:21:54 +0200 | [diff] [blame] | 247 | $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] | 248 | if ($query->num_rows() == 0) |
Derek Allard | e37ab38 | 2009-02-03 16:13:57 +0000 | [diff] [blame] | 249 | { |
| 250 | return 0; |
| 251 | } |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 252 | |
Andrey Andreev | b76029d | 2012-01-26 15:13:19 +0200 | [diff] [blame] | 253 | $query = $query->row(); |
Greg Aker | 90248ab | 2011-08-20 14:23:14 -0500 | [diff] [blame] | 254 | $this->_reset_select(); |
Andrey Andreev | b76029d | 2012-01-26 15:13:19 +0200 | [diff] [blame] | 255 | return (int) $query->numrows; |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 256 | } |
| 257 | |
| 258 | // -------------------------------------------------------------------- |
| 259 | |
| 260 | /** |
| 261 | * Show table query |
| 262 | * |
| 263 | * Generates a platform-specific query string so that the table names can be fetched |
| 264 | * |
Andrey Andreev | 5f356bf | 2012-03-20 14:32:27 +0200 | [diff] [blame] | 265 | * @param bool |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 266 | * @return string |
| 267 | */ |
Andrey Andreev | b76029d | 2012-01-26 15:13:19 +0200 | [diff] [blame] | 268 | protected function _list_tables($prefix_limit = FALSE) |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 269 | { |
Andrey Andreev | b76029d | 2012-01-26 15:13:19 +0200 | [diff] [blame] | 270 | $sql = 'SHOW TABLES FROM `'.$this->database.'`'; |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 271 | |
Andrey Andreev | b76029d | 2012-01-26 15:13:19 +0200 | [diff] [blame] | 272 | if ($prefix_limit !== FALSE && $this->dbprefix != '') |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 273 | { |
Greg Aker | 0d42489 | 2010-01-26 02:14:44 +0000 | [diff] [blame] | 274 | //$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] | 275 | return FALSE; // not currently supported |
| 276 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 277 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 278 | return $sql; |
| 279 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 280 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 281 | // -------------------------------------------------------------------- |
| 282 | |
| 283 | /** |
| 284 | * Show column query |
| 285 | * |
| 286 | * Generates a platform-specific query string so that the column names can be fetched |
| 287 | * |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 288 | * @param string the table name |
| 289 | * @return string |
| 290 | */ |
Andrey Andreev | b76029d | 2012-01-26 15:13:19 +0200 | [diff] [blame] | 291 | protected function _list_columns($table = '') |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 292 | { |
Andrey Andreev | b76029d | 2012-01-26 15:13:19 +0200 | [diff] [blame] | 293 | return 'SHOW COLUMNS FROM '.$table; |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 294 | } |
| 295 | |
| 296 | // -------------------------------------------------------------------- |
| 297 | |
| 298 | /** |
| 299 | * Field data query |
| 300 | * |
| 301 | * Generates a platform-specific query so that the column data can be retrieved |
| 302 | * |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 303 | * @param string the table name |
Andrey Andreev | b76029d | 2012-01-26 15:13:19 +0200 | [diff] [blame] | 304 | * @return string |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 305 | */ |
Andrey Andreev | b76029d | 2012-01-26 15:13:19 +0200 | [diff] [blame] | 306 | protected function _field_data($table) |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 307 | { |
Andrey Andreev | b76029d | 2012-01-26 15:13:19 +0200 | [diff] [blame] | 308 | return 'SELECT TOP 1 FROM '.$table; |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 309 | } |
| 310 | |
| 311 | // -------------------------------------------------------------------- |
| 312 | |
| 313 | /** |
Andrey Andreev | 4be5de1 | 2012-03-02 15:45:41 +0200 | [diff] [blame] | 314 | * Error |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 315 | * |
Andrey Andreev | 4be5de1 | 2012-03-02 15:45:41 +0200 | [diff] [blame] | 316 | * Returns an array containing code and message of the last |
| 317 | * database error that has occured. |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 318 | * |
Andrey Andreev | 4be5de1 | 2012-03-02 15:45:41 +0200 | [diff] [blame] | 319 | * @return array |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 320 | */ |
Andrey Andreev | 4be5de1 | 2012-03-02 15:45:41 +0200 | [diff] [blame] | 321 | public function error() |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 322 | { |
Andrey Andreev | 4be5de1 | 2012-03-02 15:45:41 +0200 | [diff] [blame] | 323 | 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] | 324 | } |
| 325 | |
| 326 | // -------------------------------------------------------------------- |
| 327 | |
| 328 | /** |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 329 | * From Tables |
| 330 | * |
| 331 | * This function implicitly groups FROM tables so there is no confusion |
| 332 | * about operator precedence in harmony with SQL standards |
| 333 | * |
Andrey Andreev | 5f356bf | 2012-03-20 14:32:27 +0200 | [diff] [blame] | 334 | * @param array |
Andrey Andreev | b76029d | 2012-01-26 15:13:19 +0200 | [diff] [blame] | 335 | * @return string |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 336 | */ |
Andrey Andreev | b76029d | 2012-01-26 15:13:19 +0200 | [diff] [blame] | 337 | protected function _from_tables($tables) |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 338 | { |
| 339 | if ( ! is_array($tables)) |
| 340 | { |
| 341 | $tables = array($tables); |
| 342 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 343 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 344 | return '('.implode(', ', $tables).')'; |
| 345 | } |
| 346 | |
| 347 | // -------------------------------------------------------------------- |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 348 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 349 | /** |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 350 | * Truncate statement |
| 351 | * |
| 352 | * Generates a platform-specific truncate string from the supplied data |
Andrey Andreev | a6fe36e | 2012-04-05 16:00:32 +0300 | [diff] [blame] | 353 | * |
| 354 | * If the database does not support the truncate() command, |
| 355 | * then this method maps to 'DELETE FROM table' |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 356 | * |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 357 | * @param string the table name |
| 358 | * @return string |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 359 | */ |
Andrey Andreev | b76029d | 2012-01-26 15:13:19 +0200 | [diff] [blame] | 360 | protected function _truncate($table) |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 361 | { |
Andrey Andreev | a6fe36e | 2012-04-05 16:00:32 +0300 | [diff] [blame] | 362 | return 'DELETE FROM '.$table; |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 363 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 364 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 365 | // -------------------------------------------------------------------- |
| 366 | |
| 367 | /** |
| 368 | * Delete statement |
| 369 | * |
| 370 | * Generates a platform-specific delete string from the supplied data |
| 371 | * |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 372 | * @param string the table name |
| 373 | * @param array the where clause |
| 374 | * @param string the limit clause |
| 375 | * @return string |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 376 | */ |
Andrey Andreev | b76029d | 2012-01-26 15:13:19 +0200 | [diff] [blame] | 377 | protected function _delete($table, $where = array(), $like = array(), $limit = FALSE) |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 378 | { |
| 379 | $conditions = ''; |
| 380 | |
| 381 | if (count($where) > 0 OR count($like) > 0) |
| 382 | { |
Andrey Andreev | b76029d | 2012-01-26 15:13:19 +0200 | [diff] [blame] | 383 | $conditions = "\nWHERE ".implode("\n", $this->ar_where); |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 384 | |
| 385 | if (count($where) > 0 && count($like) > 0) |
| 386 | { |
Andrey Andreev | b76029d | 2012-01-26 15:13:19 +0200 | [diff] [blame] | 387 | $conditions .= ' AND '; |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 388 | } |
| 389 | $conditions .= implode("\n", $like); |
| 390 | } |
| 391 | |
Andrey Andreev | b76029d | 2012-01-26 15:13:19 +0200 | [diff] [blame] | 392 | return 'DELETE FROM '.$table.$conditions.( ! $limit ? '' : ' LIMIT '.$limit); |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 393 | } |
| 394 | |
| 395 | // -------------------------------------------------------------------- |
| 396 | |
| 397 | /** |
| 398 | * Limit string |
| 399 | * |
| 400 | * Generates a platform-specific LIMIT clause |
| 401 | * |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 402 | * @param string the sql query string |
Andrey Andreev | b76029d | 2012-01-26 15:13:19 +0200 | [diff] [blame] | 403 | * @param int the number of rows to limit the query to |
| 404 | * @param int the offset value |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 405 | * @return string |
| 406 | */ |
Andrey Andreev | b76029d | 2012-01-26 15:13:19 +0200 | [diff] [blame] | 407 | protected function _limit($sql, $limit, $offset) |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 408 | { |
Andrey Andreev | b76029d | 2012-01-26 15:13:19 +0200 | [diff] [blame] | 409 | return $sql.($offset == 0 ? '' : $offset.', ').$limit; |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 410 | } |
| 411 | |
| 412 | // -------------------------------------------------------------------- |
| 413 | |
| 414 | /** |
| 415 | * Close DB Connection |
| 416 | * |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 417 | * @param resource |
| 418 | * @return void |
| 419 | */ |
Andrey Andreev | b76029d | 2012-01-26 15:13:19 +0200 | [diff] [blame] | 420 | protected function _close($conn_id) |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 421 | { |
| 422 | @odbc_close($conn_id); |
| 423 | } |
| 424 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 425 | } |
| 426 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 427 | /* End of file odbc_driver.php */ |
Andrey Andreev | 19aee03 | 2012-03-20 15:31:42 +0200 | [diff] [blame] | 428 | /* Location: ./system/database/drivers/odbc/odbc_driver.php */ |