blob: 4474136500202c19b1067cf5b1b8a271f9171f30 [file] [log] [blame]
Andrey Andreevc5536aa2012-11-01 17:33:58 +02001<?php
Derek Allard2067d1a2008-11-13 22:59:24 +00002/**
3 * CodeIgniter
4 *
Phil Sturgeon07c1ac82012-03-09 17:03:37 +00005 * An open source application development framework for PHP 5.2.4 or newer
Derek Allard2067d1a2008-11-13 22:59:24 +00006 *
Andrey Andreevbdb96ca2014-10-28 00:13:31 +02007 * This content is released under the MIT License (MIT)
Andrey Andreevb76029d2012-01-26 15:13:19 +02008 *
Andrey Andreevbdb96ca2014-10-28 00:13:31 +02009 * Copyright (c) 2014, British Columbia Institute of Technology
Andrey Andreevb76029d2012-01-26 15:13:19 +020010 *
Andrey Andreevbdb96ca2014-10-28 00:13:31 +020011 * Permission is hereby granted, free of charge, to any person obtaining a copy
12 * of this software and associated documentation files (the "Software"), to deal
13 * in the Software without restriction, including without limitation the rights
14 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
15 * copies of the Software, and to permit persons to whom the Software is
16 * furnished to do so, subject to the following conditions:
Derek Jonesf4a4bd82011-10-20 12:18:42 -050017 *
Andrey Andreevbdb96ca2014-10-28 00:13:31 +020018 * The above copyright notice and this permission notice shall be included in
19 * all copies or substantial portions of the Software.
20 *
21 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
22 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
23 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
24 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
25 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
26 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
27 * THE SOFTWARE.
28 *
29 * @package CodeIgniter
30 * @author EllisLab Dev Team
darwinel871754a2014-02-11 17:34:57 +010031 * @copyright Copyright (c) 2008 - 2014, EllisLab, Inc. (http://ellislab.com/)
Andrey Andreevbdb96ca2014-10-28 00:13:31 +020032 * @copyright Copyright (c) 2014, British Columbia Institute of Technology (http://bcit.ca/)
33 * @license http://opensource.org/licenses/MIT MIT License
34 * @link http://codeigniter.com
35 * @since Version 1.3.0
Derek Allard2067d1a2008-11-13 22:59:24 +000036 * @filesource
37 */
Andrey Andreevc5536aa2012-11-01 17:33:58 +020038defined('BASEPATH') OR exit('No direct script access allowed');
Derek Allard2067d1a2008-11-13 22:59:24 +000039
Derek Allard2067d1a2008-11-13 22:59:24 +000040/**
41 * ODBC Database Adapter Class
42 *
43 * Note: _DB is an extender class that the app controller
Jamie Rumbelow7efad202012-02-19 12:37:00 +000044 * creates dynamically based on whether the query builder
Derek Allard2067d1a2008-11-13 22:59:24 +000045 * class is being used or not.
46 *
47 * @package CodeIgniter
48 * @subpackage Drivers
49 * @category Database
Derek Jonesf4a4bd82011-10-20 12:18:42 -050050 * @author EllisLab Dev Team
Derek Allard2067d1a2008-11-13 22:59:24 +000051 * @link http://codeigniter.com/user_guide/database/
52 */
53class CI_DB_odbc_driver extends CI_DB {
54
Andrey Andreeva24e52e2012-11-02 03:54:12 +020055 /**
56 * Database driver
57 *
58 * @var string
59 */
Andrey Andreevb76029d2012-01-26 15:13:19 +020060 public $dbdriver = 'odbc';
Barry Mienydd671972010-10-04 16:33:58 +020061
Andrey Andreev5fd3ae82012-10-24 14:55:35 +030062 /**
Andrey Andreev3e9d2b82012-10-27 14:28:51 +030063 * Database schema
64 *
65 * @var string
Andrey Andreev485a3482012-10-27 03:22:43 +030066 */
67 public $schema = 'public';
68
Andrey Andreeva24e52e2012-11-02 03:54:12 +020069 // --------------------------------------------------------------------
70
Andrey Andreev485a3482012-10-27 03:22:43 +030071 /**
Andrey Andreeva24e52e2012-11-02 03:54:12 +020072 * Identifier escape character
73 *
74 * Must be empty for ODBC.
75 *
76 * @var string
77 */
78 protected $_escape_char = '';
79
80 /**
81 * ESCAPE statement string
82 *
83 * @var string
84 */
85 protected $_like_escape_str = " {escape '%s'} ";
86
Andrey Andreev98e46cf2012-11-13 03:01:42 +020087 /**
88 * ORDER BY random keyword
89 *
90 * @var array
91 */
92 protected $_random_keyword = array('RND()', 'RND(%d)');
93
Andrey Andreeva24e52e2012-11-02 03:54:12 +020094 // --------------------------------------------------------------------
95
96 /**
97 * Class constructor
Andrey Andreev5fd3ae82012-10-24 14:55:35 +030098 *
99 * @param array $params
100 * @return void
101 */
Andrey Andreev5f356bf2012-03-20 14:32:27 +0200102 public function __construct($params)
Derek Allard2067d1a2008-11-13 22:59:24 +0000103 {
Timothy Warrena2097a02011-10-10 10:10:46 -0400104 parent::__construct($params);
Barry Mienydd671972010-10-04 16:33:58 +0200105
Andrey Andreev46f6dbc2012-02-13 01:39:53 +0200106 // Legacy support for DSN in the hostname field
Andrey Andreeve4c30192012-06-04 15:08:24 +0300107 if (empty($this->dsn))
Andrey Andreev46f6dbc2012-02-13 01:39:53 +0200108 {
109 $this->dsn = $this->hostname;
110 }
Derek Allard2067d1a2008-11-13 22:59:24 +0000111 }
112
Andrey Andreev5fd3ae82012-10-24 14:55:35 +0300113 // --------------------------------------------------------------------
114
Derek Allard2067d1a2008-11-13 22:59:24 +0000115 /**
116 * Non-persistent database connection
117 *
Andrey Andreev2e171022014-02-25 15:21:41 +0200118 * @param bool $persistent
Derek Allard2067d1a2008-11-13 22:59:24 +0000119 * @return resource
Barry Mienydd671972010-10-04 16:33:58 +0200120 */
Andrey Andreev2e171022014-02-25 15:21:41 +0200121 public function db_connect($persistent = FALSE)
Derek Allard2067d1a2008-11-13 22:59:24 +0000122 {
Andrey Andreev2e171022014-02-25 15:21:41 +0200123 return ($persistent === TRUE)
124 ? odbc_pconnect($this->dsn, $this->username, $this->password)
125 : odbc_connect($this->dsn, $this->username, $this->password);
Derek Allard2067d1a2008-11-13 22:59:24 +0000126 }
Barry Mienydd671972010-10-04 16:33:58 +0200127
Derek Allard2067d1a2008-11-13 22:59:24 +0000128 // --------------------------------------------------------------------
129
130 /**
Derek Allard2067d1a2008-11-13 22:59:24 +0000131 * Execute the query
132 *
Andrey Andreeva24e52e2012-11-02 03:54:12 +0200133 * @param string $sql an SQL query
Derek Allard2067d1a2008-11-13 22:59:24 +0000134 * @return resource
Barry Mienydd671972010-10-04 16:33:58 +0200135 */
Andrey Andreevb76029d2012-01-26 15:13:19 +0200136 protected function _execute($sql)
Derek Allard2067d1a2008-11-13 22:59:24 +0000137 {
Andrey Andreev2bbbd1a2014-05-09 10:24:14 +0300138 return odbc_exec($this->conn_id, $sql);
Derek Allard2067d1a2008-11-13 22:59:24 +0000139 }
140
141 // --------------------------------------------------------------------
142
143 /**
144 * Begin Transaction
145 *
Andrey Andreeva24e52e2012-11-02 03:54:12 +0200146 * @param bool $test_mode
Barry Mienydd671972010-10-04 16:33:58 +0200147 * @return bool
148 */
Andrey Andreevb76029d2012-01-26 15:13:19 +0200149 public function trans_begin($test_mode = FALSE)
Derek Allard2067d1a2008-11-13 22:59:24 +0000150 {
Derek Allard2067d1a2008-11-13 22:59:24 +0000151 // When transactions are nested we only begin/commit/rollback the outermost ones
Andrey Andreevb76029d2012-01-26 15:13:19 +0200152 if ( ! $this->trans_enabled OR $this->_trans_depth > 0)
Derek Allard2067d1a2008-11-13 22:59:24 +0000153 {
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.
Andrey Andreevb76029d2012-01-26 15:13:19 +0200160 $this->_trans_failure = ($test_mode === TRUE);
Derek Allard2067d1a2008-11-13 22:59:24 +0000161
162 return odbc_autocommit($this->conn_id, FALSE);
163 }
164
165 // --------------------------------------------------------------------
166
167 /**
168 * Commit Transaction
169 *
Barry Mienydd671972010-10-04 16:33:58 +0200170 * @return bool
171 */
Andrey Andreevb76029d2012-01-26 15:13:19 +0200172 public function trans_commit()
Derek Allard2067d1a2008-11-13 22:59:24 +0000173 {
Derek Allard2067d1a2008-11-13 22:59:24 +0000174 // When transactions are nested we only begin/commit/rollback the outermost ones
Andrey Andreevb76029d2012-01-26 15:13:19 +0200175 if ( ! $this->trans_enabled OR $this->_trans_depth > 0)
Derek Allard2067d1a2008-11-13 22:59:24 +0000176 {
177 return TRUE;
178 }
179
180 $ret = odbc_commit($this->conn_id);
181 odbc_autocommit($this->conn_id, TRUE);
182 return $ret;
183 }
184
185 // --------------------------------------------------------------------
186
187 /**
188 * Rollback Transaction
189 *
Barry Mienydd671972010-10-04 16:33:58 +0200190 * @return bool
191 */
Andrey Andreevb76029d2012-01-26 15:13:19 +0200192 public function trans_rollback()
Derek Allard2067d1a2008-11-13 22:59:24 +0000193 {
Derek Allard2067d1a2008-11-13 22:59:24 +0000194 // When transactions are nested we only begin/commit/rollback the outermost ones
Andrey Andreevb76029d2012-01-26 15:13:19 +0200195 if ( ! $this->trans_enabled OR $this->_trans_depth > 0)
Derek Allard2067d1a2008-11-13 22:59:24 +0000196 {
197 return TRUE;
198 }
199
200 $ret = odbc_rollback($this->conn_id);
201 odbc_autocommit($this->conn_id, TRUE);
202 return $ret;
203 }
204
205 // --------------------------------------------------------------------
206
207 /**
Andrey Andreev0b6a4922013-01-10 16:53:44 +0200208 * Platform-dependant string escape
Derek Allard2067d1a2008-11-13 22:59:24 +0000209 *
Andrey Andreev0b6a4922013-01-10 16:53:44 +0200210 * @param string
Derek Allard2067d1a2008-11-13 22:59:24 +0000211 * @return string
212 */
Andrey Andreev0b6a4922013-01-10 16:53:44 +0200213 protected function _escape_str($str)
Derek Allard2067d1a2008-11-13 22:59:24 +0000214 {
Andrey Andreev0b6a4922013-01-10 16:53:44 +0200215 return remove_invisible_characters($str);
Derek Allard2067d1a2008-11-13 22:59:24 +0000216 }
Barry Mienydd671972010-10-04 16:33:58 +0200217
Derek Allard2067d1a2008-11-13 22:59:24 +0000218 // --------------------------------------------------------------------
219
220 /**
221 * Affected Rows
222 *
Andrey Andreevb76029d2012-01-26 15:13:19 +0200223 * @return int
Derek Allard2067d1a2008-11-13 22:59:24 +0000224 */
Andrey Andreevb76029d2012-01-26 15:13:19 +0200225 public function affected_rows()
Derek Allard2067d1a2008-11-13 22:59:24 +0000226 {
Andrey Andreev2bbbd1a2014-05-09 10:24:14 +0300227 return odbc_num_rows($this->result_id);
Derek Allard2067d1a2008-11-13 22:59:24 +0000228 }
Barry Mienydd671972010-10-04 16:33:58 +0200229
Derek Allard2067d1a2008-11-13 22:59:24 +0000230 // --------------------------------------------------------------------
231
232 /**
233 * Insert ID
234 *
Andrey Andreev8af76662012-03-05 14:33:41 +0200235 * @return bool
Derek Allard2067d1a2008-11-13 22:59:24 +0000236 */
Andrey Andreevb76029d2012-01-26 15:13:19 +0200237 public function insert_id()
Derek Allard2067d1a2008-11-13 22:59:24 +0000238 {
Andrey Andreev8d3afde2012-11-06 12:53:47 +0200239 return ($this->db->db_debug) ? $this->db->display_error('db_unsupported_feature') : FALSE;
Derek Allard2067d1a2008-11-13 22:59:24 +0000240 }
241
242 // --------------------------------------------------------------------
243
244 /**
Derek Allard2067d1a2008-11-13 22:59:24 +0000245 * Show table query
246 *
247 * Generates a platform-specific query string so that the table names can be fetched
248 *
Andrey Andreeva24e52e2012-11-02 03:54:12 +0200249 * @param bool $prefix_limit
Derek Allard2067d1a2008-11-13 22:59:24 +0000250 * @return string
251 */
Andrey Andreevb76029d2012-01-26 15:13:19 +0200252 protected function _list_tables($prefix_limit = FALSE)
Derek Allard2067d1a2008-11-13 22:59:24 +0000253 {
Andrey Andreev485a3482012-10-27 03:22:43 +0300254 $sql = "SELECT table_name FROM information_schema.tables WHERE table_schema = '".$this->schema."'";
Derek Allard2067d1a2008-11-13 22:59:24 +0000255
Andrey Andreeve4c30192012-06-04 15:08:24 +0300256 if ($prefix_limit !== FALSE && $this->dbprefix !== '')
Derek Allard2067d1a2008-11-13 22:59:24 +0000257 {
Andrey Andreev485a3482012-10-27 03:22:43 +0300258 return $sql." AND table_name LIKE '".$this->escape_like_str($this->dbprefix)."%' "
259 .sprintf($this->_like_escape_str, $this->_like_escape_chr);
Derek Allard2067d1a2008-11-13 22:59:24 +0000260 }
Barry Mienydd671972010-10-04 16:33:58 +0200261
Derek Allard2067d1a2008-11-13 22:59:24 +0000262 return $sql;
263 }
Barry Mienydd671972010-10-04 16:33:58 +0200264
Derek Allard2067d1a2008-11-13 22:59:24 +0000265 // --------------------------------------------------------------------
266
267 /**
268 * Show column query
269 *
270 * Generates a platform-specific query string so that the column names can be fetched
271 *
Andrey Andreeva24e52e2012-11-02 03:54:12 +0200272 * @param string $table
Derek Allard2067d1a2008-11-13 22:59:24 +0000273 * @return string
274 */
Andrey Andreevb76029d2012-01-26 15:13:19 +0200275 protected function _list_columns($table = '')
Derek Allard2067d1a2008-11-13 22:59:24 +0000276 {
Andrey Andreevb76029d2012-01-26 15:13:19 +0200277 return 'SHOW COLUMNS FROM '.$table;
Derek Allard2067d1a2008-11-13 22:59:24 +0000278 }
279
280 // --------------------------------------------------------------------
281
282 /**
283 * Field data query
284 *
285 * Generates a platform-specific query so that the column data can be retrieved
286 *
Andrey Andreeva24e52e2012-11-02 03:54:12 +0200287 * @param string $table
Andrey Andreevb76029d2012-01-26 15:13:19 +0200288 * @return string
Derek Allard2067d1a2008-11-13 22:59:24 +0000289 */
Andrey Andreevb76029d2012-01-26 15:13:19 +0200290 protected function _field_data($table)
Derek Allard2067d1a2008-11-13 22:59:24 +0000291 {
Andrey Andreevb76029d2012-01-26 15:13:19 +0200292 return 'SELECT TOP 1 FROM '.$table;
Derek Allard2067d1a2008-11-13 22:59:24 +0000293 }
294
295 // --------------------------------------------------------------------
296
297 /**
Andrey Andreev4be5de12012-03-02 15:45:41 +0200298 * Error
Derek Allard2067d1a2008-11-13 22:59:24 +0000299 *
Andrey Andreev4be5de12012-03-02 15:45:41 +0200300 * Returns an array containing code and message of the last
301 * database error that has occured.
Derek Allard2067d1a2008-11-13 22:59:24 +0000302 *
Andrey Andreev4be5de12012-03-02 15:45:41 +0200303 * @return array
Derek Allard2067d1a2008-11-13 22:59:24 +0000304 */
Andrey Andreev4be5de12012-03-02 15:45:41 +0200305 public function error()
Derek Allard2067d1a2008-11-13 22:59:24 +0000306 {
Andrey Andreev4be5de12012-03-02 15:45:41 +0200307 return array('code' => odbc_error($this->conn_id), 'message' => odbc_errormsg($this->conn_id));
Derek Allard2067d1a2008-11-13 22:59:24 +0000308 }
309
310 // --------------------------------------------------------------------
311
312 /**
Andrey Andreevb0478652012-07-18 15:34:46 +0300313 * Update statement
314 *
315 * Generates a platform-specific update string from the supplied data
316 *
Andrey Andreeva24e52e2012-11-02 03:54:12 +0200317 * @param string $table
318 * @param array $values
Andrey Andreevb0478652012-07-18 15:34:46 +0300319 * @return string
Andrey Andreev838a9d62012-12-03 14:37:47 +0200320 */
Andrey Andreevb0478652012-07-18 15:34:46 +0300321 protected function _update($table, $values)
322 {
323 $this->qb_limit = FALSE;
324 $this->qb_orderby = array();
325 return parent::_update($table, $values);
326 }
327
328 // --------------------------------------------------------------------
329
330 /**
Derek Allard2067d1a2008-11-13 22:59:24 +0000331 * Truncate statement
332 *
333 * Generates a platform-specific truncate string from the supplied data
Andrey Andreeva6fe36e2012-04-05 16:00:32 +0300334 *
Andrey Andreeva24e52e2012-11-02 03:54:12 +0200335 * If the database does not support the TRUNCATE statement,
Andrey Andreeva6fe36e2012-04-05 16:00:32 +0300336 * then this method maps to 'DELETE FROM table'
Derek Allard2067d1a2008-11-13 22:59:24 +0000337 *
Andrey Andreeva24e52e2012-11-02 03:54:12 +0200338 * @param string $table
Derek Allard2067d1a2008-11-13 22:59:24 +0000339 * @return string
Barry Mienydd671972010-10-04 16:33:58 +0200340 */
Andrey Andreevb76029d2012-01-26 15:13:19 +0200341 protected function _truncate($table)
Derek Allard2067d1a2008-11-13 22:59:24 +0000342 {
Andrey Andreeva6fe36e2012-04-05 16:00:32 +0300343 return 'DELETE FROM '.$table;
Derek Allard2067d1a2008-11-13 22:59:24 +0000344 }
Barry Mienydd671972010-10-04 16:33:58 +0200345
Derek Allard2067d1a2008-11-13 22:59:24 +0000346 // --------------------------------------------------------------------
347
348 /**
Andrey Andreevb0478652012-07-18 15:34:46 +0300349 * Delete statement
350 *
351 * Generates a platform-specific delete string from the supplied data
352 *
Andrey Andreeva24e52e2012-11-02 03:54:12 +0200353 * @param string $table
Andrey Andreevb0478652012-07-18 15:34:46 +0300354 * @return string
355 */
356 protected function _delete($table)
357 {
358 $this->qb_limit = FALSE;
359 return parent::_delete($table);
360 }
361
362 // --------------------------------------------------------------------
363
364 /**
Derek Allard2067d1a2008-11-13 22:59:24 +0000365 * Close DB Connection
366 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000367 * @return void
368 */
Andrey Andreev79922c02012-05-23 12:27:17 +0300369 protected function _close()
Derek Allard2067d1a2008-11-13 22:59:24 +0000370 {
Andrey Andreev2bbbd1a2014-05-09 10:24:14 +0300371 odbc_close($this->conn_id);
Derek Allard2067d1a2008-11-13 22:59:24 +0000372 }
373
Derek Allard2067d1a2008-11-13 22:59:24 +0000374}
375
Derek Allard2067d1a2008-11-13 22:59:24 +0000376/* End of file odbc_driver.php */
Andrey Andreev79922c02012-05-23 12:27:17 +0300377/* Location: ./system/database/drivers/odbc/odbc_driver.php */