blob: 6f635bdfb38e3177c2b3d4b2bc9e0606a7270ac9 [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 *
Derek Jonesf4a4bd82011-10-20 12:18:42 -05007 * NOTICE OF LICENSE
Andrey Andreevb76029d2012-01-26 15:13:19 +02008 *
Derek Jonesf4a4bd82011-10-20 12:18:42 -05009 * Licensed under the Open Software License version 3.0
Andrey Andreevb76029d2012-01-26 15:13:19 +020010 *
Derek Jonesf4a4bd82011-10-20 12:18:42 -050011 * 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 Allard2067d1a2008-11-13 22:59:24 +000019 * @package CodeIgniter
Derek Jonesf4a4bd82011-10-20 12:18:42 -050020 * @author EllisLab Dev Team
Andrey Andreev80500af2013-01-01 08:16:53 +020021 * @copyright Copyright (c) 2008 - 2013, EllisLab, Inc. (http://ellislab.com/)
Derek Jonesf4a4bd82011-10-20 12:18:42 -050022 * @license http://opensource.org/licenses/OSL-3.0 Open Software License (OSL 3.0)
Derek Allard2067d1a2008-11-13 22:59:24 +000023 * @link http://codeigniter.com
24 * @since Version 1.0
25 * @filesource
26 */
Andrey Andreevc5536aa2012-11-01 17:33:58 +020027defined('BASEPATH') OR exit('No direct script access allowed');
Derek Allard2067d1a2008-11-13 22:59:24 +000028
Derek Allard2067d1a2008-11-13 22:59:24 +000029/**
30 * ODBC Database Adapter Class
31 *
32 * Note: _DB is an extender class that the app controller
Jamie Rumbelow7efad202012-02-19 12:37:00 +000033 * creates dynamically based on whether the query builder
Derek Allard2067d1a2008-11-13 22:59:24 +000034 * class is being used or not.
35 *
36 * @package CodeIgniter
37 * @subpackage Drivers
38 * @category Database
Derek Jonesf4a4bd82011-10-20 12:18:42 -050039 * @author EllisLab Dev Team
Derek Allard2067d1a2008-11-13 22:59:24 +000040 * @link http://codeigniter.com/user_guide/database/
41 */
42class CI_DB_odbc_driver extends CI_DB {
43
Andrey Andreeva24e52e2012-11-02 03:54:12 +020044 /**
45 * Database driver
46 *
47 * @var string
48 */
Andrey Andreevb76029d2012-01-26 15:13:19 +020049 public $dbdriver = 'odbc';
Barry Mienydd671972010-10-04 16:33:58 +020050
Andrey Andreev5fd3ae82012-10-24 14:55:35 +030051 /**
Andrey Andreev3e9d2b82012-10-27 14:28:51 +030052 * Database schema
53 *
54 * @var string
Andrey Andreev485a3482012-10-27 03:22:43 +030055 */
56 public $schema = 'public';
57
Andrey Andreeva24e52e2012-11-02 03:54:12 +020058 // --------------------------------------------------------------------
59
Andrey Andreev485a3482012-10-27 03:22:43 +030060 /**
Andrey Andreeva24e52e2012-11-02 03:54:12 +020061 * Identifier escape character
62 *
63 * Must be empty for ODBC.
64 *
65 * @var string
66 */
67 protected $_escape_char = '';
68
69 /**
70 * ESCAPE statement string
71 *
72 * @var string
73 */
74 protected $_like_escape_str = " {escape '%s'} ";
75
Andrey Andreev98e46cf2012-11-13 03:01:42 +020076 /**
77 * ORDER BY random keyword
78 *
79 * @var array
80 */
81 protected $_random_keyword = array('RND()', 'RND(%d)');
82
Andrey Andreeva24e52e2012-11-02 03:54:12 +020083 // --------------------------------------------------------------------
84
85 /**
86 * Class constructor
Andrey Andreev5fd3ae82012-10-24 14:55:35 +030087 *
88 * @param array $params
89 * @return void
90 */
Andrey Andreev5f356bf2012-03-20 14:32:27 +020091 public function __construct($params)
Derek Allard2067d1a2008-11-13 22:59:24 +000092 {
Timothy Warrena2097a02011-10-10 10:10:46 -040093 parent::__construct($params);
Barry Mienydd671972010-10-04 16:33:58 +020094
Andrey Andreev46f6dbc2012-02-13 01:39:53 +020095 // Legacy support for DSN in the hostname field
Andrey Andreeve4c30192012-06-04 15:08:24 +030096 if (empty($this->dsn))
Andrey Andreev46f6dbc2012-02-13 01:39:53 +020097 {
98 $this->dsn = $this->hostname;
99 }
Derek Allard2067d1a2008-11-13 22:59:24 +0000100 }
101
Andrey Andreev5fd3ae82012-10-24 14:55:35 +0300102 // --------------------------------------------------------------------
103
Derek Allard2067d1a2008-11-13 22:59:24 +0000104 /**
105 * Non-persistent database connection
106 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000107 * @return resource
Barry Mienydd671972010-10-04 16:33:58 +0200108 */
Andrey Andreevb76029d2012-01-26 15:13:19 +0200109 public function db_connect()
Derek Allard2067d1a2008-11-13 22:59:24 +0000110 {
Andrey Andreev46f6dbc2012-02-13 01:39:53 +0200111 return @odbc_connect($this->dsn, $this->username, $this->password);
Derek Allard2067d1a2008-11-13 22:59:24 +0000112 }
Barry Mienydd671972010-10-04 16:33:58 +0200113
Derek Allard2067d1a2008-11-13 22:59:24 +0000114 // --------------------------------------------------------------------
115
116 /**
117 * Persistent database connection
118 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000119 * @return resource
Barry Mienydd671972010-10-04 16:33:58 +0200120 */
Andrey Andreevb76029d2012-01-26 15:13:19 +0200121 public function db_pconnect()
Derek Allard2067d1a2008-11-13 22:59:24 +0000122 {
Andrey Andreev46f6dbc2012-02-13 01:39:53 +0200123 return @odbc_pconnect($this->dsn, $this->username, $this->password);
Derek Allard2067d1a2008-11-13 22:59:24 +0000124 }
Barry Mienydd671972010-10-04 16:33:58 +0200125
Derek Allard2067d1a2008-11-13 22:59:24 +0000126 // --------------------------------------------------------------------
127
128 /**
Derek Allard2067d1a2008-11-13 22:59:24 +0000129 * Execute the query
130 *
Andrey Andreeva24e52e2012-11-02 03:54:12 +0200131 * @param string $sql an SQL query
Derek Allard2067d1a2008-11-13 22:59:24 +0000132 * @return resource
Barry Mienydd671972010-10-04 16:33:58 +0200133 */
Andrey Andreevb76029d2012-01-26 15:13:19 +0200134 protected function _execute($sql)
Derek Allard2067d1a2008-11-13 22:59:24 +0000135 {
Derek Allard2067d1a2008-11-13 22:59:24 +0000136 return @odbc_exec($this->conn_id, $sql);
Derek Allard2067d1a2008-11-13 22:59:24 +0000137 }
138
139 // --------------------------------------------------------------------
140
141 /**
142 * Begin Transaction
143 *
Andrey Andreeva24e52e2012-11-02 03:54:12 +0200144 * @param bool $test_mode
Barry Mienydd671972010-10-04 16:33:58 +0200145 * @return bool
146 */
Andrey Andreevb76029d2012-01-26 15:13:19 +0200147 public function trans_begin($test_mode = FALSE)
Derek Allard2067d1a2008-11-13 22:59:24 +0000148 {
Derek Allard2067d1a2008-11-13 22:59:24 +0000149 // When transactions are nested we only begin/commit/rollback the outermost ones
Andrey Andreevb76029d2012-01-26 15:13:19 +0200150 if ( ! $this->trans_enabled OR $this->_trans_depth > 0)
Derek Allard2067d1a2008-11-13 22:59:24 +0000151 {
152 return TRUE;
153 }
154
155 // Reset the transaction failure flag.
156 // If the $test_mode flag is set to TRUE transactions will be rolled back
157 // even if the queries produce a successful result.
Andrey Andreevb76029d2012-01-26 15:13:19 +0200158 $this->_trans_failure = ($test_mode === TRUE);
Derek Allard2067d1a2008-11-13 22:59:24 +0000159
160 return odbc_autocommit($this->conn_id, FALSE);
161 }
162
163 // --------------------------------------------------------------------
164
165 /**
166 * Commit Transaction
167 *
Barry Mienydd671972010-10-04 16:33:58 +0200168 * @return bool
169 */
Andrey Andreevb76029d2012-01-26 15:13:19 +0200170 public function trans_commit()
Derek Allard2067d1a2008-11-13 22:59:24 +0000171 {
Derek Allard2067d1a2008-11-13 22:59:24 +0000172 // When transactions are nested we only begin/commit/rollback the outermost ones
Andrey Andreevb76029d2012-01-26 15:13:19 +0200173 if ( ! $this->trans_enabled OR $this->_trans_depth > 0)
Derek Allard2067d1a2008-11-13 22:59:24 +0000174 {
175 return TRUE;
176 }
177
178 $ret = odbc_commit($this->conn_id);
179 odbc_autocommit($this->conn_id, TRUE);
180 return $ret;
181 }
182
183 // --------------------------------------------------------------------
184
185 /**
186 * Rollback Transaction
187 *
Barry Mienydd671972010-10-04 16:33:58 +0200188 * @return bool
189 */
Andrey Andreevb76029d2012-01-26 15:13:19 +0200190 public function trans_rollback()
Derek Allard2067d1a2008-11-13 22:59:24 +0000191 {
Derek Allard2067d1a2008-11-13 22:59:24 +0000192 // When transactions are nested we only begin/commit/rollback the outermost ones
Andrey Andreevb76029d2012-01-26 15:13:19 +0200193 if ( ! $this->trans_enabled OR $this->_trans_depth > 0)
Derek Allard2067d1a2008-11-13 22:59:24 +0000194 {
195 return TRUE;
196 }
197
198 $ret = odbc_rollback($this->conn_id);
199 odbc_autocommit($this->conn_id, TRUE);
200 return $ret;
201 }
202
203 // --------------------------------------------------------------------
204
205 /**
Andrey Andreev0b6a4922013-01-10 16:53:44 +0200206 * Platform-dependant string escape
Derek Allard2067d1a2008-11-13 22:59:24 +0000207 *
Andrey Andreev0b6a4922013-01-10 16:53:44 +0200208 * @param string
Derek Allard2067d1a2008-11-13 22:59:24 +0000209 * @return string
210 */
Andrey Andreev0b6a4922013-01-10 16:53:44 +0200211 protected function _escape_str($str)
Derek Allard2067d1a2008-11-13 22:59:24 +0000212 {
Andrey Andreev0b6a4922013-01-10 16:53:44 +0200213 return remove_invisible_characters($str);
Derek Allard2067d1a2008-11-13 22:59:24 +0000214 }
Barry Mienydd671972010-10-04 16:33:58 +0200215
Derek Allard2067d1a2008-11-13 22:59:24 +0000216 // --------------------------------------------------------------------
217
218 /**
219 * Affected Rows
220 *
Andrey Andreevb76029d2012-01-26 15:13:19 +0200221 * @return int
Derek Allard2067d1a2008-11-13 22:59:24 +0000222 */
Andrey Andreevb76029d2012-01-26 15:13:19 +0200223 public function affected_rows()
Derek Allard2067d1a2008-11-13 22:59:24 +0000224 {
Andrey Andreevf6779f52014-02-05 23:15:26 +0200225 return @odbc_num_rows($this->result_id);
Derek Allard2067d1a2008-11-13 22:59:24 +0000226 }
Barry Mienydd671972010-10-04 16:33:58 +0200227
Derek Allard2067d1a2008-11-13 22:59:24 +0000228 // --------------------------------------------------------------------
229
230 /**
231 * Insert ID
232 *
Andrey Andreev8af76662012-03-05 14:33:41 +0200233 * @return bool
Derek Allard2067d1a2008-11-13 22:59:24 +0000234 */
Andrey Andreevb76029d2012-01-26 15:13:19 +0200235 public function insert_id()
Derek Allard2067d1a2008-11-13 22:59:24 +0000236 {
Andrey Andreev8d3afde2012-11-06 12:53:47 +0200237 return ($this->db->db_debug) ? $this->db->display_error('db_unsupported_feature') : FALSE;
Derek Allard2067d1a2008-11-13 22:59:24 +0000238 }
239
240 // --------------------------------------------------------------------
241
242 /**
Derek Allard2067d1a2008-11-13 22:59:24 +0000243 * Show table query
244 *
245 * Generates a platform-specific query string so that the table names can be fetched
246 *
Andrey Andreeva24e52e2012-11-02 03:54:12 +0200247 * @param bool $prefix_limit
Derek Allard2067d1a2008-11-13 22:59:24 +0000248 * @return string
249 */
Andrey Andreevb76029d2012-01-26 15:13:19 +0200250 protected function _list_tables($prefix_limit = FALSE)
Derek Allard2067d1a2008-11-13 22:59:24 +0000251 {
Andrey Andreev485a3482012-10-27 03:22:43 +0300252 $sql = "SELECT table_name FROM information_schema.tables WHERE table_schema = '".$this->schema."'";
Derek Allard2067d1a2008-11-13 22:59:24 +0000253
Andrey Andreeve4c30192012-06-04 15:08:24 +0300254 if ($prefix_limit !== FALSE && $this->dbprefix !== '')
Derek Allard2067d1a2008-11-13 22:59:24 +0000255 {
Andrey Andreev485a3482012-10-27 03:22:43 +0300256 return $sql." AND table_name LIKE '".$this->escape_like_str($this->dbprefix)."%' "
257 .sprintf($this->_like_escape_str, $this->_like_escape_chr);
Derek Allard2067d1a2008-11-13 22:59:24 +0000258 }
Barry Mienydd671972010-10-04 16:33:58 +0200259
Derek Allard2067d1a2008-11-13 22:59:24 +0000260 return $sql;
261 }
Barry Mienydd671972010-10-04 16:33:58 +0200262
Derek Allard2067d1a2008-11-13 22:59:24 +0000263 // --------------------------------------------------------------------
264
265 /**
266 * Show column query
267 *
268 * Generates a platform-specific query string so that the column names can be fetched
269 *
Andrey Andreeva24e52e2012-11-02 03:54:12 +0200270 * @param string $table
Derek Allard2067d1a2008-11-13 22:59:24 +0000271 * @return string
272 */
Andrey Andreevb76029d2012-01-26 15:13:19 +0200273 protected function _list_columns($table = '')
Derek Allard2067d1a2008-11-13 22:59:24 +0000274 {
Andrey Andreevb76029d2012-01-26 15:13:19 +0200275 return 'SHOW COLUMNS FROM '.$table;
Derek Allard2067d1a2008-11-13 22:59:24 +0000276 }
277
278 // --------------------------------------------------------------------
279
280 /**
281 * Field data query
282 *
283 * Generates a platform-specific query so that the column data can be retrieved
284 *
Andrey Andreeva24e52e2012-11-02 03:54:12 +0200285 * @param string $table
Andrey Andreevb76029d2012-01-26 15:13:19 +0200286 * @return string
Derek Allard2067d1a2008-11-13 22:59:24 +0000287 */
Andrey Andreevb76029d2012-01-26 15:13:19 +0200288 protected function _field_data($table)
Derek Allard2067d1a2008-11-13 22:59:24 +0000289 {
Andrey Andreevb76029d2012-01-26 15:13:19 +0200290 return 'SELECT TOP 1 FROM '.$table;
Derek Allard2067d1a2008-11-13 22:59:24 +0000291 }
292
293 // --------------------------------------------------------------------
294
295 /**
Andrey Andreev4be5de12012-03-02 15:45:41 +0200296 * Error
Derek Allard2067d1a2008-11-13 22:59:24 +0000297 *
Andrey Andreev4be5de12012-03-02 15:45:41 +0200298 * Returns an array containing code and message of the last
299 * database error that has occured.
Derek Allard2067d1a2008-11-13 22:59:24 +0000300 *
Andrey Andreev4be5de12012-03-02 15:45:41 +0200301 * @return array
Derek Allard2067d1a2008-11-13 22:59:24 +0000302 */
Andrey Andreev4be5de12012-03-02 15:45:41 +0200303 public function error()
Derek Allard2067d1a2008-11-13 22:59:24 +0000304 {
Andrey Andreev4be5de12012-03-02 15:45:41 +0200305 return array('code' => odbc_error($this->conn_id), 'message' => odbc_errormsg($this->conn_id));
Derek Allard2067d1a2008-11-13 22:59:24 +0000306 }
307
308 // --------------------------------------------------------------------
309
310 /**
Andrey Andreevb0478652012-07-18 15:34:46 +0300311 * Update statement
312 *
313 * Generates a platform-specific update string from the supplied data
314 *
Andrey Andreeva24e52e2012-11-02 03:54:12 +0200315 * @param string $table
316 * @param array $values
Andrey Andreevb0478652012-07-18 15:34:46 +0300317 * @return string
Andrey Andreev838a9d62012-12-03 14:37:47 +0200318 */
Andrey Andreevb0478652012-07-18 15:34:46 +0300319 protected function _update($table, $values)
320 {
321 $this->qb_limit = FALSE;
322 $this->qb_orderby = array();
323 return parent::_update($table, $values);
324 }
325
326 // --------------------------------------------------------------------
327
328 /**
Derek Allard2067d1a2008-11-13 22:59:24 +0000329 * Truncate statement
330 *
331 * Generates a platform-specific truncate string from the supplied data
Andrey Andreeva6fe36e2012-04-05 16:00:32 +0300332 *
Andrey Andreeva24e52e2012-11-02 03:54:12 +0200333 * If the database does not support the TRUNCATE statement,
Andrey Andreeva6fe36e2012-04-05 16:00:32 +0300334 * then this method maps to 'DELETE FROM table'
Derek Allard2067d1a2008-11-13 22:59:24 +0000335 *
Andrey Andreeva24e52e2012-11-02 03:54:12 +0200336 * @param string $table
Derek Allard2067d1a2008-11-13 22:59:24 +0000337 * @return string
Barry Mienydd671972010-10-04 16:33:58 +0200338 */
Andrey Andreevb76029d2012-01-26 15:13:19 +0200339 protected function _truncate($table)
Derek Allard2067d1a2008-11-13 22:59:24 +0000340 {
Andrey Andreeva6fe36e2012-04-05 16:00:32 +0300341 return 'DELETE FROM '.$table;
Derek Allard2067d1a2008-11-13 22:59:24 +0000342 }
Barry Mienydd671972010-10-04 16:33:58 +0200343
Derek Allard2067d1a2008-11-13 22:59:24 +0000344 // --------------------------------------------------------------------
345
346 /**
Andrey Andreevb0478652012-07-18 15:34:46 +0300347 * Delete statement
348 *
349 * Generates a platform-specific delete string from the supplied data
350 *
Andrey Andreeva24e52e2012-11-02 03:54:12 +0200351 * @param string $table
Andrey Andreevb0478652012-07-18 15:34:46 +0300352 * @return string
353 */
354 protected function _delete($table)
355 {
356 $this->qb_limit = FALSE;
357 return parent::_delete($table);
358 }
359
360 // --------------------------------------------------------------------
361
362 /**
Derek Allard2067d1a2008-11-13 22:59:24 +0000363 * Close DB Connection
364 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000365 * @return void
366 */
Andrey Andreev79922c02012-05-23 12:27:17 +0300367 protected function _close()
Derek Allard2067d1a2008-11-13 22:59:24 +0000368 {
Andrey Andreev79922c02012-05-23 12:27:17 +0300369 @odbc_close($this->conn_id);
Derek Allard2067d1a2008-11-13 22:59:24 +0000370 }
371
Derek Allard2067d1a2008-11-13 22:59:24 +0000372}
373
Derek Allard2067d1a2008-11-13 22:59:24 +0000374/* End of file odbc_driver.php */
Andrey Andreev79922c02012-05-23 12:27:17 +0300375/* Location: ./system/database/drivers/odbc/odbc_driver.php */