blob: 063a04b98b18b83e842e7830391c2ee0d19176eb [file] [log] [blame]
Andrey Andreevb76029d2012-01-26 15:13:19 +02001<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
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
Greg Aker0defe5d2012-01-01 18:46:41 -060021 * @copyright Copyright (c) 2008 - 2012, 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 */
27
Derek Allard2067d1a2008-11-13 22:59:24 +000028/**
29 * ODBC Database Adapter Class
30 *
31 * Note: _DB is an extender class that the app controller
Jamie Rumbelow7efad202012-02-19 12:37:00 +000032 * creates dynamically based on whether the query builder
Derek Allard2067d1a2008-11-13 22:59:24 +000033 * class is being used or not.
34 *
35 * @package CodeIgniter
36 * @subpackage Drivers
37 * @category Database
Derek Jonesf4a4bd82011-10-20 12:18:42 -050038 * @author EllisLab Dev Team
Derek Allard2067d1a2008-11-13 22:59:24 +000039 * @link http://codeigniter.com/user_guide/database/
40 */
41class CI_DB_odbc_driver extends CI_DB {
42
Andrey Andreevb76029d2012-01-26 15:13:19 +020043 public $dbdriver = 'odbc';
Barry Mienydd671972010-10-04 16:33:58 +020044
Derek Allard2067d1a2008-11-13 22:59:24 +000045 // the character used to excape - not necessary for ODBC
Andrey Andreevb76029d2012-01-26 15:13:19 +020046 protected $_escape_char = '';
Barry Mienydd671972010-10-04 16:33:58 +020047
Andrey Andreev5f356bf2012-03-20 14:32:27 +020048 protected $_like_escape_str = " {escape '%s'} ";
Barry Mienydd671972010-10-04 16:33:58 +020049
Andrey Andreevb76029d2012-01-26 15:13:19 +020050 protected $_random_keyword;
Derek Allard2067d1a2008-11-13 22:59:24 +000051
Andrey Andreev5fd3ae82012-10-24 14:55:35 +030052 /**
53 * Constructor
54 *
55 * @param array $params
56 * @return void
57 */
Andrey Andreev5f356bf2012-03-20 14:32:27 +020058 public function __construct($params)
Derek Allard2067d1a2008-11-13 22:59:24 +000059 {
Timothy Warrena2097a02011-10-10 10:10:46 -040060 parent::__construct($params);
Barry Mienydd671972010-10-04 16:33:58 +020061
Derek Allard2067d1a2008-11-13 22:59:24 +000062 $this->_random_keyword = ' RND('.time().')'; // database specific random keyword
Andrey Andreev46f6dbc2012-02-13 01:39:53 +020063
64 // Legacy support for DSN in the hostname field
Andrey Andreeve4c30192012-06-04 15:08:24 +030065 if (empty($this->dsn))
Andrey Andreev46f6dbc2012-02-13 01:39:53 +020066 {
67 $this->dsn = $this->hostname;
68 }
Derek Allard2067d1a2008-11-13 22:59:24 +000069 }
70
Andrey Andreev5fd3ae82012-10-24 14:55:35 +030071 // --------------------------------------------------------------------
72
Derek Allard2067d1a2008-11-13 22:59:24 +000073 /**
74 * Non-persistent database connection
75 *
Derek Allard2067d1a2008-11-13 22:59:24 +000076 * @return resource
Barry Mienydd671972010-10-04 16:33:58 +020077 */
Andrey Andreevb76029d2012-01-26 15:13:19 +020078 public function db_connect()
Derek Allard2067d1a2008-11-13 22:59:24 +000079 {
Andrey Andreev46f6dbc2012-02-13 01:39:53 +020080 return @odbc_connect($this->dsn, $this->username, $this->password);
Derek Allard2067d1a2008-11-13 22:59:24 +000081 }
Barry Mienydd671972010-10-04 16:33:58 +020082
Derek Allard2067d1a2008-11-13 22:59:24 +000083 // --------------------------------------------------------------------
84
85 /**
86 * Persistent database connection
87 *
Derek Allard2067d1a2008-11-13 22:59:24 +000088 * @return resource
Barry Mienydd671972010-10-04 16:33:58 +020089 */
Andrey Andreevb76029d2012-01-26 15:13:19 +020090 public function db_pconnect()
Derek Allard2067d1a2008-11-13 22:59:24 +000091 {
Andrey Andreev46f6dbc2012-02-13 01:39:53 +020092 return @odbc_pconnect($this->dsn, $this->username, $this->password);
Derek Allard2067d1a2008-11-13 22:59:24 +000093 }
Barry Mienydd671972010-10-04 16:33:58 +020094
Derek Allard2067d1a2008-11-13 22:59:24 +000095 // --------------------------------------------------------------------
96
97 /**
Derek Allard2067d1a2008-11-13 22:59:24 +000098 * Execute the query
99 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000100 * @param string an SQL query
101 * @return resource
Barry Mienydd671972010-10-04 16:33:58 +0200102 */
Andrey Andreevb76029d2012-01-26 15:13:19 +0200103 protected function _execute($sql)
Derek Allard2067d1a2008-11-13 22:59:24 +0000104 {
Derek Allard2067d1a2008-11-13 22:59:24 +0000105 return @odbc_exec($this->conn_id, $sql);
Derek Allard2067d1a2008-11-13 22:59:24 +0000106 }
107
108 // --------------------------------------------------------------------
109
110 /**
111 * Begin Transaction
112 *
Andrey Andreev5fd3ae82012-10-24 14:55:35 +0300113 * @param bool $test_mode = FALSE
Barry Mienydd671972010-10-04 16:33:58 +0200114 * @return bool
115 */
Andrey Andreevb76029d2012-01-26 15:13:19 +0200116 public function trans_begin($test_mode = FALSE)
Derek Allard2067d1a2008-11-13 22:59:24 +0000117 {
Derek Allard2067d1a2008-11-13 22:59:24 +0000118 // When transactions are nested we only begin/commit/rollback the outermost ones
Andrey Andreevb76029d2012-01-26 15:13:19 +0200119 if ( ! $this->trans_enabled OR $this->_trans_depth > 0)
Derek Allard2067d1a2008-11-13 22:59:24 +0000120 {
121 return TRUE;
122 }
123
124 // Reset the transaction failure flag.
125 // If the $test_mode flag is set to TRUE transactions will be rolled back
126 // even if the queries produce a successful result.
Andrey Andreevb76029d2012-01-26 15:13:19 +0200127 $this->_trans_failure = ($test_mode === TRUE);
Derek Allard2067d1a2008-11-13 22:59:24 +0000128
129 return odbc_autocommit($this->conn_id, FALSE);
130 }
131
132 // --------------------------------------------------------------------
133
134 /**
135 * Commit Transaction
136 *
Barry Mienydd671972010-10-04 16:33:58 +0200137 * @return bool
138 */
Andrey Andreevb76029d2012-01-26 15:13:19 +0200139 public function trans_commit()
Derek Allard2067d1a2008-11-13 22:59:24 +0000140 {
Derek Allard2067d1a2008-11-13 22:59:24 +0000141 // When transactions are nested we only begin/commit/rollback the outermost ones
Andrey Andreevb76029d2012-01-26 15:13:19 +0200142 if ( ! $this->trans_enabled OR $this->_trans_depth > 0)
Derek Allard2067d1a2008-11-13 22:59:24 +0000143 {
144 return TRUE;
145 }
146
147 $ret = odbc_commit($this->conn_id);
148 odbc_autocommit($this->conn_id, TRUE);
149 return $ret;
150 }
151
152 // --------------------------------------------------------------------
153
154 /**
155 * Rollback Transaction
156 *
Barry Mienydd671972010-10-04 16:33:58 +0200157 * @return bool
158 */
Andrey Andreevb76029d2012-01-26 15:13:19 +0200159 public function trans_rollback()
Derek Allard2067d1a2008-11-13 22:59:24 +0000160 {
Derek Allard2067d1a2008-11-13 22:59:24 +0000161 // When transactions are nested we only begin/commit/rollback the outermost ones
Andrey Andreevb76029d2012-01-26 15:13:19 +0200162 if ( ! $this->trans_enabled OR $this->_trans_depth > 0)
Derek Allard2067d1a2008-11-13 22:59:24 +0000163 {
164 return TRUE;
165 }
166
167 $ret = odbc_rollback($this->conn_id);
168 odbc_autocommit($this->conn_id, TRUE);
169 return $ret;
170 }
171
172 // --------------------------------------------------------------------
173
174 /**
175 * Escape String
176 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000177 * @param string
Derek Jonese4ed5832009-02-20 21:44:59 +0000178 * @param bool whether or not the string will be used in a LIKE condition
Derek Allard2067d1a2008-11-13 22:59:24 +0000179 * @return string
180 */
Andrey Andreevb76029d2012-01-26 15:13:19 +0200181 public function escape_str($str, $like = FALSE)
Derek Allard2067d1a2008-11-13 22:59:24 +0000182 {
Derek Jonese4ed5832009-02-20 21:44:59 +0000183 if (is_array($str))
184 {
Pascal Krietec3a4a8d2011-02-14 13:40:08 -0500185 foreach ($str as $key => $val)
Barry Mienydd671972010-10-04 16:33:58 +0200186 {
Derek Jonese4ed5832009-02-20 21:44:59 +0000187 $str[$key] = $this->escape_str($val, $like);
Barry Mienydd671972010-10-04 16:33:58 +0200188 }
189
190 return $str;
191 }
192
Greg Aker757dda62010-04-14 19:06:19 -0500193 $str = remove_invisible_characters($str);
Barry Mienydd671972010-10-04 16:33:58 +0200194
Derek Jonese4ed5832009-02-20 21:44:59 +0000195 // escape LIKE condition wildcards
196 if ($like === TRUE)
197 {
Andrey Andreev5e18e892012-03-09 14:25:00 +0200198 return str_replace(array($this->_like_escape_chr, '%', '_'),
199 array($this->_like_escape_chr.$this->_like_escape_chr, $this->_like_escape_chr.'%', $this->_like_escape_chr.'_'),
Andrey Andreevb76029d2012-01-26 15:13:19 +0200200 $str);
Derek Jonese4ed5832009-02-20 21:44:59 +0000201 }
Barry Mienydd671972010-10-04 16:33:58 +0200202
Derek Jonese4ed5832009-02-20 21:44:59 +0000203 return $str;
Derek Allard2067d1a2008-11-13 22:59:24 +0000204 }
Barry Mienydd671972010-10-04 16:33:58 +0200205
Derek Allard2067d1a2008-11-13 22:59:24 +0000206 // --------------------------------------------------------------------
207
208 /**
209 * Affected Rows
210 *
Andrey Andreevb76029d2012-01-26 15:13:19 +0200211 * @return int
Derek Allard2067d1a2008-11-13 22:59:24 +0000212 */
Andrey Andreevb76029d2012-01-26 15:13:19 +0200213 public function affected_rows()
Derek Allard2067d1a2008-11-13 22:59:24 +0000214 {
215 return @odbc_num_rows($this->conn_id);
216 }
Barry Mienydd671972010-10-04 16:33:58 +0200217
Derek Allard2067d1a2008-11-13 22:59:24 +0000218 // --------------------------------------------------------------------
219
220 /**
221 * Insert ID
222 *
Andrey Andreev8af76662012-03-05 14:33:41 +0200223 * @return bool
Derek Allard2067d1a2008-11-13 22:59:24 +0000224 */
Andrey Andreevb76029d2012-01-26 15:13:19 +0200225 public function insert_id()
Derek Allard2067d1a2008-11-13 22:59:24 +0000226 {
Andrey Andreev8af76662012-03-05 14:33:41 +0200227 return ($this->db->db_debug) ? $this->db->display_error('db_unsuported_feature') : FALSE;
Derek Allard2067d1a2008-11-13 22:59:24 +0000228 }
229
230 // --------------------------------------------------------------------
231
232 /**
Derek Allard2067d1a2008-11-13 22:59:24 +0000233 * Show table query
234 *
235 * Generates a platform-specific query string so that the table names can be fetched
236 *
Andrey Andreev5f356bf2012-03-20 14:32:27 +0200237 * @param bool
Derek Allard2067d1a2008-11-13 22:59:24 +0000238 * @return string
239 */
Andrey Andreevb76029d2012-01-26 15:13:19 +0200240 protected function _list_tables($prefix_limit = FALSE)
Derek Allard2067d1a2008-11-13 22:59:24 +0000241 {
Andrey Andreeve4c30192012-06-04 15:08:24 +0300242 $sql = 'SHOW TABLES FROM '.$this->database;
Derek Allard2067d1a2008-11-13 22:59:24 +0000243
Andrey Andreeve4c30192012-06-04 15:08:24 +0300244 if ($prefix_limit !== FALSE && $this->dbprefix !== '')
Derek Allard2067d1a2008-11-13 22:59:24 +0000245 {
Greg Aker0d424892010-01-26 02:14:44 +0000246 //$sql .= " LIKE '".$this->escape_like_str($this->dbprefix)."%' ".sprintf($this->_like_escape_str, $this->_like_escape_chr);
Derek Allard2067d1a2008-11-13 22:59:24 +0000247 return FALSE; // not currently supported
248 }
Barry Mienydd671972010-10-04 16:33:58 +0200249
Derek Allard2067d1a2008-11-13 22:59:24 +0000250 return $sql;
251 }
Barry Mienydd671972010-10-04 16:33:58 +0200252
Derek Allard2067d1a2008-11-13 22:59:24 +0000253 // --------------------------------------------------------------------
254
255 /**
256 * Show column query
257 *
258 * Generates a platform-specific query string so that the column names can be fetched
259 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000260 * @param string the table name
261 * @return string
262 */
Andrey Andreevb76029d2012-01-26 15:13:19 +0200263 protected function _list_columns($table = '')
Derek Allard2067d1a2008-11-13 22:59:24 +0000264 {
Andrey Andreevb76029d2012-01-26 15:13:19 +0200265 return 'SHOW COLUMNS FROM '.$table;
Derek Allard2067d1a2008-11-13 22:59:24 +0000266 }
267
268 // --------------------------------------------------------------------
269
270 /**
271 * Field data query
272 *
273 * Generates a platform-specific query so that the column data can be retrieved
274 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000275 * @param string the table name
Andrey Andreevb76029d2012-01-26 15:13:19 +0200276 * @return string
Derek Allard2067d1a2008-11-13 22:59:24 +0000277 */
Andrey Andreevb76029d2012-01-26 15:13:19 +0200278 protected function _field_data($table)
Derek Allard2067d1a2008-11-13 22:59:24 +0000279 {
Andrey Andreevb76029d2012-01-26 15:13:19 +0200280 return 'SELECT TOP 1 FROM '.$table;
Derek Allard2067d1a2008-11-13 22:59:24 +0000281 }
282
283 // --------------------------------------------------------------------
284
285 /**
Andrey Andreev4be5de12012-03-02 15:45:41 +0200286 * Error
Derek Allard2067d1a2008-11-13 22:59:24 +0000287 *
Andrey Andreev4be5de12012-03-02 15:45:41 +0200288 * Returns an array containing code and message of the last
289 * database error that has occured.
Derek Allard2067d1a2008-11-13 22:59:24 +0000290 *
Andrey Andreev4be5de12012-03-02 15:45:41 +0200291 * @return array
Derek Allard2067d1a2008-11-13 22:59:24 +0000292 */
Andrey Andreev4be5de12012-03-02 15:45:41 +0200293 public function error()
Derek Allard2067d1a2008-11-13 22:59:24 +0000294 {
Andrey Andreev4be5de12012-03-02 15:45:41 +0200295 return array('code' => odbc_error($this->conn_id), 'message' => odbc_errormsg($this->conn_id));
Derek Allard2067d1a2008-11-13 22:59:24 +0000296 }
297
298 // --------------------------------------------------------------------
299
300 /**
Andrey Andreevb0478652012-07-18 15:34:46 +0300301 * Update statement
302 *
303 * Generates a platform-specific update string from the supplied data
304 *
305 * @param string the table name
306 * @param array the update data
307 * @return string
308 */
309 protected function _update($table, $values)
310 {
311 $this->qb_limit = FALSE;
312 $this->qb_orderby = array();
313 return parent::_update($table, $values);
314 }
315
316 // --------------------------------------------------------------------
317
318 /**
Derek Allard2067d1a2008-11-13 22:59:24 +0000319 * Truncate statement
320 *
321 * Generates a platform-specific truncate string from the supplied data
Andrey Andreeva6fe36e2012-04-05 16:00:32 +0300322 *
323 * If the database does not support the truncate() command,
324 * then this method maps to 'DELETE FROM table'
Derek Allard2067d1a2008-11-13 22:59:24 +0000325 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000326 * @param string the table name
327 * @return string
Barry Mienydd671972010-10-04 16:33:58 +0200328 */
Andrey Andreevb76029d2012-01-26 15:13:19 +0200329 protected function _truncate($table)
Derek Allard2067d1a2008-11-13 22:59:24 +0000330 {
Andrey Andreeva6fe36e2012-04-05 16:00:32 +0300331 return 'DELETE FROM '.$table;
Derek Allard2067d1a2008-11-13 22:59:24 +0000332 }
Barry Mienydd671972010-10-04 16:33:58 +0200333
Derek Allard2067d1a2008-11-13 22:59:24 +0000334 // --------------------------------------------------------------------
335
336 /**
Andrey Andreevb0478652012-07-18 15:34:46 +0300337 * Delete statement
338 *
339 * Generates a platform-specific delete string from the supplied data
340 *
341 * @param string the table name
342 * @return string
343 */
344 protected function _delete($table)
345 {
346 $this->qb_limit = FALSE;
347 return parent::_delete($table);
348 }
349
350 // --------------------------------------------------------------------
351
352 /**
Derek Allard2067d1a2008-11-13 22:59:24 +0000353 * Close DB Connection
354 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000355 * @return void
356 */
Andrey Andreev79922c02012-05-23 12:27:17 +0300357 protected function _close()
Derek Allard2067d1a2008-11-13 22:59:24 +0000358 {
Andrey Andreev79922c02012-05-23 12:27:17 +0300359 @odbc_close($this->conn_id);
Derek Allard2067d1a2008-11-13 22:59:24 +0000360 }
361
Derek Allard2067d1a2008-11-13 22:59:24 +0000362}
363
Derek Allard2067d1a2008-11-13 22:59:24 +0000364/* End of file odbc_driver.php */
Andrey Andreev79922c02012-05-23 12:27:17 +0300365/* Location: ./system/database/drivers/odbc/odbc_driver.php */