blob: 06c24d66f8fc1560576e6b327311ec11d3ee29f8 [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
darwinel871754a2014-02-11 17:34:57 +010021 * @copyright Copyright (c) 2008 - 2014, 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 *
Andrey Andreev2e171022014-02-25 15:21:41 +0200107 * @param bool $persistent
Derek Allard2067d1a2008-11-13 22:59:24 +0000108 * @return resource
Barry Mienydd671972010-10-04 16:33:58 +0200109 */
Andrey Andreev2e171022014-02-25 15:21:41 +0200110 public function db_connect($persistent = FALSE)
Derek Allard2067d1a2008-11-13 22:59:24 +0000111 {
Andrey Andreev2e171022014-02-25 15:21:41 +0200112 return ($persistent === TRUE)
113 ? odbc_pconnect($this->dsn, $this->username, $this->password)
114 : odbc_connect($this->dsn, $this->username, $this->password);
Derek Allard2067d1a2008-11-13 22:59:24 +0000115 }
Barry Mienydd671972010-10-04 16:33:58 +0200116
Derek Allard2067d1a2008-11-13 22:59:24 +0000117 // --------------------------------------------------------------------
118
119 /**
Derek Allard2067d1a2008-11-13 22:59:24 +0000120 * Execute the query
121 *
Andrey Andreeva24e52e2012-11-02 03:54:12 +0200122 * @param string $sql an SQL query
Derek Allard2067d1a2008-11-13 22:59:24 +0000123 * @return resource
Barry Mienydd671972010-10-04 16:33:58 +0200124 */
Andrey Andreevb76029d2012-01-26 15:13:19 +0200125 protected function _execute($sql)
Derek Allard2067d1a2008-11-13 22:59:24 +0000126 {
Andrey Andreev2bbbd1a2014-05-09 10:24:14 +0300127 return odbc_exec($this->conn_id, $sql);
Derek Allard2067d1a2008-11-13 22:59:24 +0000128 }
129
130 // --------------------------------------------------------------------
131
132 /**
133 * Begin Transaction
134 *
Andrey Andreeva24e52e2012-11-02 03:54:12 +0200135 * @param bool $test_mode
Barry Mienydd671972010-10-04 16:33:58 +0200136 * @return bool
137 */
Andrey Andreevb76029d2012-01-26 15:13:19 +0200138 public function trans_begin($test_mode = FALSE)
Derek Allard2067d1a2008-11-13 22:59:24 +0000139 {
Derek Allard2067d1a2008-11-13 22:59:24 +0000140 // When transactions are nested we only begin/commit/rollback the outermost ones
Andrey Andreevb76029d2012-01-26 15:13:19 +0200141 if ( ! $this->trans_enabled OR $this->_trans_depth > 0)
Derek Allard2067d1a2008-11-13 22:59:24 +0000142 {
143 return TRUE;
144 }
145
146 // Reset the transaction failure flag.
147 // If the $test_mode flag is set to TRUE transactions will be rolled back
148 // even if the queries produce a successful result.
Andrey Andreevb76029d2012-01-26 15:13:19 +0200149 $this->_trans_failure = ($test_mode === TRUE);
Derek Allard2067d1a2008-11-13 22:59:24 +0000150
151 return odbc_autocommit($this->conn_id, FALSE);
152 }
153
154 // --------------------------------------------------------------------
155
156 /**
157 * Commit Transaction
158 *
Barry Mienydd671972010-10-04 16:33:58 +0200159 * @return bool
160 */
Andrey Andreevb76029d2012-01-26 15:13:19 +0200161 public function trans_commit()
Derek Allard2067d1a2008-11-13 22:59:24 +0000162 {
Derek Allard2067d1a2008-11-13 22:59:24 +0000163 // When transactions are nested we only begin/commit/rollback the outermost ones
Andrey Andreevb76029d2012-01-26 15:13:19 +0200164 if ( ! $this->trans_enabled OR $this->_trans_depth > 0)
Derek Allard2067d1a2008-11-13 22:59:24 +0000165 {
166 return TRUE;
167 }
168
169 $ret = odbc_commit($this->conn_id);
170 odbc_autocommit($this->conn_id, TRUE);
171 return $ret;
172 }
173
174 // --------------------------------------------------------------------
175
176 /**
177 * Rollback Transaction
178 *
Barry Mienydd671972010-10-04 16:33:58 +0200179 * @return bool
180 */
Andrey Andreevb76029d2012-01-26 15:13:19 +0200181 public function trans_rollback()
Derek Allard2067d1a2008-11-13 22:59:24 +0000182 {
Derek Allard2067d1a2008-11-13 22:59:24 +0000183 // When transactions are nested we only begin/commit/rollback the outermost ones
Andrey Andreevb76029d2012-01-26 15:13:19 +0200184 if ( ! $this->trans_enabled OR $this->_trans_depth > 0)
Derek Allard2067d1a2008-11-13 22:59:24 +0000185 {
186 return TRUE;
187 }
188
189 $ret = odbc_rollback($this->conn_id);
190 odbc_autocommit($this->conn_id, TRUE);
191 return $ret;
192 }
193
194 // --------------------------------------------------------------------
195
196 /**
Andrey Andreev0b6a4922013-01-10 16:53:44 +0200197 * Platform-dependant string escape
Derek Allard2067d1a2008-11-13 22:59:24 +0000198 *
Andrey Andreev0b6a4922013-01-10 16:53:44 +0200199 * @param string
Derek Allard2067d1a2008-11-13 22:59:24 +0000200 * @return string
201 */
Andrey Andreev0b6a4922013-01-10 16:53:44 +0200202 protected function _escape_str($str)
Derek Allard2067d1a2008-11-13 22:59:24 +0000203 {
Andrey Andreev0b6a4922013-01-10 16:53:44 +0200204 return remove_invisible_characters($str);
Derek Allard2067d1a2008-11-13 22:59:24 +0000205 }
Barry Mienydd671972010-10-04 16:33:58 +0200206
Derek Allard2067d1a2008-11-13 22:59:24 +0000207 // --------------------------------------------------------------------
208
209 /**
210 * Affected Rows
211 *
Andrey Andreevb76029d2012-01-26 15:13:19 +0200212 * @return int
Derek Allard2067d1a2008-11-13 22:59:24 +0000213 */
Andrey Andreevb76029d2012-01-26 15:13:19 +0200214 public function affected_rows()
Derek Allard2067d1a2008-11-13 22:59:24 +0000215 {
Andrey Andreev2bbbd1a2014-05-09 10:24:14 +0300216 return odbc_num_rows($this->result_id);
Derek Allard2067d1a2008-11-13 22:59:24 +0000217 }
Barry Mienydd671972010-10-04 16:33:58 +0200218
Derek Allard2067d1a2008-11-13 22:59:24 +0000219 // --------------------------------------------------------------------
220
221 /**
222 * Insert ID
223 *
Andrey Andreev8af76662012-03-05 14:33:41 +0200224 * @return bool
Derek Allard2067d1a2008-11-13 22:59:24 +0000225 */
Andrey Andreevb76029d2012-01-26 15:13:19 +0200226 public function insert_id()
Derek Allard2067d1a2008-11-13 22:59:24 +0000227 {
Andrey Andreev8d3afde2012-11-06 12:53:47 +0200228 return ($this->db->db_debug) ? $this->db->display_error('db_unsupported_feature') : FALSE;
Derek Allard2067d1a2008-11-13 22:59:24 +0000229 }
230
231 // --------------------------------------------------------------------
232
233 /**
Derek Allard2067d1a2008-11-13 22:59:24 +0000234 * Show table query
235 *
236 * Generates a platform-specific query string so that the table names can be fetched
237 *
Andrey Andreeva24e52e2012-11-02 03:54:12 +0200238 * @param bool $prefix_limit
Derek Allard2067d1a2008-11-13 22:59:24 +0000239 * @return string
240 */
Andrey Andreevb76029d2012-01-26 15:13:19 +0200241 protected function _list_tables($prefix_limit = FALSE)
Derek Allard2067d1a2008-11-13 22:59:24 +0000242 {
Andrey Andreev485a3482012-10-27 03:22:43 +0300243 $sql = "SELECT table_name FROM information_schema.tables WHERE table_schema = '".$this->schema."'";
Derek Allard2067d1a2008-11-13 22:59:24 +0000244
Andrey Andreeve4c30192012-06-04 15:08:24 +0300245 if ($prefix_limit !== FALSE && $this->dbprefix !== '')
Derek Allard2067d1a2008-11-13 22:59:24 +0000246 {
Andrey Andreev485a3482012-10-27 03:22:43 +0300247 return $sql." AND table_name LIKE '".$this->escape_like_str($this->dbprefix)."%' "
248 .sprintf($this->_like_escape_str, $this->_like_escape_chr);
Derek Allard2067d1a2008-11-13 22:59:24 +0000249 }
Barry Mienydd671972010-10-04 16:33:58 +0200250
Derek Allard2067d1a2008-11-13 22:59:24 +0000251 return $sql;
252 }
Barry Mienydd671972010-10-04 16:33:58 +0200253
Derek Allard2067d1a2008-11-13 22:59:24 +0000254 // --------------------------------------------------------------------
255
256 /**
257 * Show column query
258 *
259 * Generates a platform-specific query string so that the column names can be fetched
260 *
Andrey Andreeva24e52e2012-11-02 03:54:12 +0200261 * @param string $table
Derek Allard2067d1a2008-11-13 22:59:24 +0000262 * @return string
263 */
Andrey Andreevb76029d2012-01-26 15:13:19 +0200264 protected function _list_columns($table = '')
Derek Allard2067d1a2008-11-13 22:59:24 +0000265 {
Andrey Andreevb76029d2012-01-26 15:13:19 +0200266 return 'SHOW COLUMNS FROM '.$table;
Derek Allard2067d1a2008-11-13 22:59:24 +0000267 }
268
269 // --------------------------------------------------------------------
270
271 /**
272 * Field data query
273 *
274 * Generates a platform-specific query so that the column data can be retrieved
275 *
Andrey Andreeva24e52e2012-11-02 03:54:12 +0200276 * @param string $table
Andrey Andreevb76029d2012-01-26 15:13:19 +0200277 * @return string
Derek Allard2067d1a2008-11-13 22:59:24 +0000278 */
Andrey Andreevb76029d2012-01-26 15:13:19 +0200279 protected function _field_data($table)
Derek Allard2067d1a2008-11-13 22:59:24 +0000280 {
Andrey Andreevb76029d2012-01-26 15:13:19 +0200281 return 'SELECT TOP 1 FROM '.$table;
Derek Allard2067d1a2008-11-13 22:59:24 +0000282 }
283
284 // --------------------------------------------------------------------
285
286 /**
Andrey Andreev4be5de12012-03-02 15:45:41 +0200287 * Error
Derek Allard2067d1a2008-11-13 22:59:24 +0000288 *
Andrey Andreev4be5de12012-03-02 15:45:41 +0200289 * Returns an array containing code and message of the last
290 * database error that has occured.
Derek Allard2067d1a2008-11-13 22:59:24 +0000291 *
Andrey Andreev4be5de12012-03-02 15:45:41 +0200292 * @return array
Derek Allard2067d1a2008-11-13 22:59:24 +0000293 */
Andrey Andreev4be5de12012-03-02 15:45:41 +0200294 public function error()
Derek Allard2067d1a2008-11-13 22:59:24 +0000295 {
Andrey Andreev4be5de12012-03-02 15:45:41 +0200296 return array('code' => odbc_error($this->conn_id), 'message' => odbc_errormsg($this->conn_id));
Derek Allard2067d1a2008-11-13 22:59:24 +0000297 }
298
299 // --------------------------------------------------------------------
300
301 /**
Andrey Andreevb0478652012-07-18 15:34:46 +0300302 * Update statement
303 *
304 * Generates a platform-specific update string from the supplied data
305 *
Andrey Andreeva24e52e2012-11-02 03:54:12 +0200306 * @param string $table
307 * @param array $values
Andrey Andreevb0478652012-07-18 15:34:46 +0300308 * @return string
Andrey Andreev838a9d62012-12-03 14:37:47 +0200309 */
Andrey Andreevb0478652012-07-18 15:34:46 +0300310 protected function _update($table, $values)
311 {
312 $this->qb_limit = FALSE;
313 $this->qb_orderby = array();
314 return parent::_update($table, $values);
315 }
316
317 // --------------------------------------------------------------------
318
319 /**
Derek Allard2067d1a2008-11-13 22:59:24 +0000320 * Truncate statement
321 *
322 * Generates a platform-specific truncate string from the supplied data
Andrey Andreeva6fe36e2012-04-05 16:00:32 +0300323 *
Andrey Andreeva24e52e2012-11-02 03:54:12 +0200324 * If the database does not support the TRUNCATE statement,
Andrey Andreeva6fe36e2012-04-05 16:00:32 +0300325 * then this method maps to 'DELETE FROM table'
Derek Allard2067d1a2008-11-13 22:59:24 +0000326 *
Andrey Andreeva24e52e2012-11-02 03:54:12 +0200327 * @param string $table
Derek Allard2067d1a2008-11-13 22:59:24 +0000328 * @return string
Barry Mienydd671972010-10-04 16:33:58 +0200329 */
Andrey Andreevb76029d2012-01-26 15:13:19 +0200330 protected function _truncate($table)
Derek Allard2067d1a2008-11-13 22:59:24 +0000331 {
Andrey Andreeva6fe36e2012-04-05 16:00:32 +0300332 return 'DELETE FROM '.$table;
Derek Allard2067d1a2008-11-13 22:59:24 +0000333 }
Barry Mienydd671972010-10-04 16:33:58 +0200334
Derek Allard2067d1a2008-11-13 22:59:24 +0000335 // --------------------------------------------------------------------
336
337 /**
Andrey Andreevb0478652012-07-18 15:34:46 +0300338 * Delete statement
339 *
340 * Generates a platform-specific delete string from the supplied data
341 *
Andrey Andreeva24e52e2012-11-02 03:54:12 +0200342 * @param string $table
Andrey Andreevb0478652012-07-18 15:34:46 +0300343 * @return string
344 */
345 protected function _delete($table)
346 {
347 $this->qb_limit = FALSE;
348 return parent::_delete($table);
349 }
350
351 // --------------------------------------------------------------------
352
353 /**
Derek Allard2067d1a2008-11-13 22:59:24 +0000354 * Close DB Connection
355 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000356 * @return void
357 */
Andrey Andreev79922c02012-05-23 12:27:17 +0300358 protected function _close()
Derek Allard2067d1a2008-11-13 22:59:24 +0000359 {
Andrey Andreev2bbbd1a2014-05-09 10:24:14 +0300360 odbc_close($this->conn_id);
Derek Allard2067d1a2008-11-13 22:59:24 +0000361 }
362
Derek Allard2067d1a2008-11-13 22:59:24 +0000363}
364
Derek Allard2067d1a2008-11-13 22:59:24 +0000365/* End of file odbc_driver.php */
Andrey Andreev79922c02012-05-23 12:27:17 +0300366/* Location: ./system/database/drivers/odbc/odbc_driver.php */