blob: f6ea412ad684eb860b3e36670657a1cbac198565 [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 /**
Andrey Andreev3e9d2b82012-10-27 14:28:51 +030053 * Database schema
54 *
55 * @var string
Andrey Andreev485a3482012-10-27 03:22:43 +030056 */
57 public $schema = 'public';
58
59 /**
Andrey Andreev5fd3ae82012-10-24 14:55:35 +030060 * Constructor
61 *
62 * @param array $params
63 * @return void
64 */
Andrey Andreev5f356bf2012-03-20 14:32:27 +020065 public function __construct($params)
Derek Allard2067d1a2008-11-13 22:59:24 +000066 {
Timothy Warrena2097a02011-10-10 10:10:46 -040067 parent::__construct($params);
Barry Mienydd671972010-10-04 16:33:58 +020068
Derek Allard2067d1a2008-11-13 22:59:24 +000069 $this->_random_keyword = ' RND('.time().')'; // database specific random keyword
Andrey Andreev46f6dbc2012-02-13 01:39:53 +020070
71 // Legacy support for DSN in the hostname field
Andrey Andreeve4c30192012-06-04 15:08:24 +030072 if (empty($this->dsn))
Andrey Andreev46f6dbc2012-02-13 01:39:53 +020073 {
74 $this->dsn = $this->hostname;
75 }
Derek Allard2067d1a2008-11-13 22:59:24 +000076 }
77
Andrey Andreev5fd3ae82012-10-24 14:55:35 +030078 // --------------------------------------------------------------------
79
Derek Allard2067d1a2008-11-13 22:59:24 +000080 /**
81 * Non-persistent database connection
82 *
Derek Allard2067d1a2008-11-13 22:59:24 +000083 * @return resource
Barry Mienydd671972010-10-04 16:33:58 +020084 */
Andrey Andreevb76029d2012-01-26 15:13:19 +020085 public function db_connect()
Derek Allard2067d1a2008-11-13 22:59:24 +000086 {
Andrey Andreev46f6dbc2012-02-13 01:39:53 +020087 return @odbc_connect($this->dsn, $this->username, $this->password);
Derek Allard2067d1a2008-11-13 22:59:24 +000088 }
Barry Mienydd671972010-10-04 16:33:58 +020089
Derek Allard2067d1a2008-11-13 22:59:24 +000090 // --------------------------------------------------------------------
91
92 /**
93 * Persistent database connection
94 *
Derek Allard2067d1a2008-11-13 22:59:24 +000095 * @return resource
Barry Mienydd671972010-10-04 16:33:58 +020096 */
Andrey Andreevb76029d2012-01-26 15:13:19 +020097 public function db_pconnect()
Derek Allard2067d1a2008-11-13 22:59:24 +000098 {
Andrey Andreev46f6dbc2012-02-13 01:39:53 +020099 return @odbc_pconnect($this->dsn, $this->username, $this->password);
Derek Allard2067d1a2008-11-13 22:59:24 +0000100 }
Barry Mienydd671972010-10-04 16:33:58 +0200101
Derek Allard2067d1a2008-11-13 22:59:24 +0000102 // --------------------------------------------------------------------
103
104 /**
Derek Allard2067d1a2008-11-13 22:59:24 +0000105 * Execute the query
106 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000107 * @param string an SQL query
108 * @return resource
Barry Mienydd671972010-10-04 16:33:58 +0200109 */
Andrey Andreevb76029d2012-01-26 15:13:19 +0200110 protected function _execute($sql)
Derek Allard2067d1a2008-11-13 22:59:24 +0000111 {
Derek Allard2067d1a2008-11-13 22:59:24 +0000112 return @odbc_exec($this->conn_id, $sql);
Derek Allard2067d1a2008-11-13 22:59:24 +0000113 }
114
115 // --------------------------------------------------------------------
116
117 /**
118 * Begin Transaction
119 *
Andrey Andreev5fd3ae82012-10-24 14:55:35 +0300120 * @param bool $test_mode = FALSE
Barry Mienydd671972010-10-04 16:33:58 +0200121 * @return bool
122 */
Andrey Andreevb76029d2012-01-26 15:13:19 +0200123 public function trans_begin($test_mode = FALSE)
Derek Allard2067d1a2008-11-13 22:59:24 +0000124 {
Derek Allard2067d1a2008-11-13 22:59:24 +0000125 // When transactions are nested we only begin/commit/rollback the outermost ones
Andrey Andreevb76029d2012-01-26 15:13:19 +0200126 if ( ! $this->trans_enabled OR $this->_trans_depth > 0)
Derek Allard2067d1a2008-11-13 22:59:24 +0000127 {
128 return TRUE;
129 }
130
131 // Reset the transaction failure flag.
132 // If the $test_mode flag is set to TRUE transactions will be rolled back
133 // even if the queries produce a successful result.
Andrey Andreevb76029d2012-01-26 15:13:19 +0200134 $this->_trans_failure = ($test_mode === TRUE);
Derek Allard2067d1a2008-11-13 22:59:24 +0000135
136 return odbc_autocommit($this->conn_id, FALSE);
137 }
138
139 // --------------------------------------------------------------------
140
141 /**
142 * Commit Transaction
143 *
Barry Mienydd671972010-10-04 16:33:58 +0200144 * @return bool
145 */
Andrey Andreevb76029d2012-01-26 15:13:19 +0200146 public function trans_commit()
Derek Allard2067d1a2008-11-13 22:59:24 +0000147 {
Derek Allard2067d1a2008-11-13 22:59:24 +0000148 // When transactions are nested we only begin/commit/rollback the outermost ones
Andrey Andreevb76029d2012-01-26 15:13:19 +0200149 if ( ! $this->trans_enabled OR $this->_trans_depth > 0)
Derek Allard2067d1a2008-11-13 22:59:24 +0000150 {
151 return TRUE;
152 }
153
154 $ret = odbc_commit($this->conn_id);
155 odbc_autocommit($this->conn_id, TRUE);
156 return $ret;
157 }
158
159 // --------------------------------------------------------------------
160
161 /**
162 * Rollback Transaction
163 *
Barry Mienydd671972010-10-04 16:33:58 +0200164 * @return bool
165 */
Andrey Andreevb76029d2012-01-26 15:13:19 +0200166 public function trans_rollback()
Derek Allard2067d1a2008-11-13 22:59:24 +0000167 {
Derek Allard2067d1a2008-11-13 22:59:24 +0000168 // When transactions are nested we only begin/commit/rollback the outermost ones
Andrey Andreevb76029d2012-01-26 15:13:19 +0200169 if ( ! $this->trans_enabled OR $this->_trans_depth > 0)
Derek Allard2067d1a2008-11-13 22:59:24 +0000170 {
171 return TRUE;
172 }
173
174 $ret = odbc_rollback($this->conn_id);
175 odbc_autocommit($this->conn_id, TRUE);
176 return $ret;
177 }
178
179 // --------------------------------------------------------------------
180
181 /**
182 * Escape String
183 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000184 * @param string
Derek Jonese4ed5832009-02-20 21:44:59 +0000185 * @param bool whether or not the string will be used in a LIKE condition
Derek Allard2067d1a2008-11-13 22:59:24 +0000186 * @return string
187 */
Andrey Andreevb76029d2012-01-26 15:13:19 +0200188 public function escape_str($str, $like = FALSE)
Derek Allard2067d1a2008-11-13 22:59:24 +0000189 {
Derek Jonese4ed5832009-02-20 21:44:59 +0000190 if (is_array($str))
191 {
Pascal Krietec3a4a8d2011-02-14 13:40:08 -0500192 foreach ($str as $key => $val)
Barry Mienydd671972010-10-04 16:33:58 +0200193 {
Derek Jonese4ed5832009-02-20 21:44:59 +0000194 $str[$key] = $this->escape_str($val, $like);
Barry Mienydd671972010-10-04 16:33:58 +0200195 }
196
197 return $str;
198 }
199
Greg Aker757dda62010-04-14 19:06:19 -0500200 $str = remove_invisible_characters($str);
Barry Mienydd671972010-10-04 16:33:58 +0200201
Derek Jonese4ed5832009-02-20 21:44:59 +0000202 // escape LIKE condition wildcards
203 if ($like === TRUE)
204 {
Andrey Andreev5e18e892012-03-09 14:25:00 +0200205 return str_replace(array($this->_like_escape_chr, '%', '_'),
206 array($this->_like_escape_chr.$this->_like_escape_chr, $this->_like_escape_chr.'%', $this->_like_escape_chr.'_'),
Andrey Andreevb76029d2012-01-26 15:13:19 +0200207 $str);
Derek Jonese4ed5832009-02-20 21:44:59 +0000208 }
Barry Mienydd671972010-10-04 16:33:58 +0200209
Derek Jonese4ed5832009-02-20 21:44:59 +0000210 return $str;
Derek Allard2067d1a2008-11-13 22:59:24 +0000211 }
Barry Mienydd671972010-10-04 16:33:58 +0200212
Derek Allard2067d1a2008-11-13 22:59:24 +0000213 // --------------------------------------------------------------------
214
215 /**
216 * Affected Rows
217 *
Andrey Andreevb76029d2012-01-26 15:13:19 +0200218 * @return int
Derek Allard2067d1a2008-11-13 22:59:24 +0000219 */
Andrey Andreevb76029d2012-01-26 15:13:19 +0200220 public function affected_rows()
Derek Allard2067d1a2008-11-13 22:59:24 +0000221 {
222 return @odbc_num_rows($this->conn_id);
223 }
Barry Mienydd671972010-10-04 16:33:58 +0200224
Derek Allard2067d1a2008-11-13 22:59:24 +0000225 // --------------------------------------------------------------------
226
227 /**
228 * Insert ID
229 *
Andrey Andreev8af76662012-03-05 14:33:41 +0200230 * @return bool
Derek Allard2067d1a2008-11-13 22:59:24 +0000231 */
Andrey Andreevb76029d2012-01-26 15:13:19 +0200232 public function insert_id()
Derek Allard2067d1a2008-11-13 22:59:24 +0000233 {
Andrey Andreev8af76662012-03-05 14:33:41 +0200234 return ($this->db->db_debug) ? $this->db->display_error('db_unsuported_feature') : FALSE;
Derek Allard2067d1a2008-11-13 22:59:24 +0000235 }
236
237 // --------------------------------------------------------------------
238
239 /**
Derek Allard2067d1a2008-11-13 22:59:24 +0000240 * Show table query
241 *
242 * Generates a platform-specific query string so that the table names can be fetched
243 *
Andrey Andreev485a3482012-10-27 03:22:43 +0300244 * @param bool $prefix_limit = FALSE
Derek Allard2067d1a2008-11-13 22:59:24 +0000245 * @return string
246 */
Andrey Andreevb76029d2012-01-26 15:13:19 +0200247 protected function _list_tables($prefix_limit = FALSE)
Derek Allard2067d1a2008-11-13 22:59:24 +0000248 {
Andrey Andreev485a3482012-10-27 03:22:43 +0300249 $sql = "SELECT table_name FROM information_schema.tables WHERE table_schema = '".$this->schema."'";
Derek Allard2067d1a2008-11-13 22:59:24 +0000250
Andrey Andreeve4c30192012-06-04 15:08:24 +0300251 if ($prefix_limit !== FALSE && $this->dbprefix !== '')
Derek Allard2067d1a2008-11-13 22:59:24 +0000252 {
Andrey Andreev485a3482012-10-27 03:22:43 +0300253 return $sql." AND table_name LIKE '".$this->escape_like_str($this->dbprefix)."%' "
254 .sprintf($this->_like_escape_str, $this->_like_escape_chr);
Derek Allard2067d1a2008-11-13 22:59:24 +0000255 }
Barry Mienydd671972010-10-04 16:33:58 +0200256
Derek Allard2067d1a2008-11-13 22:59:24 +0000257 return $sql;
258 }
Barry Mienydd671972010-10-04 16:33:58 +0200259
Derek Allard2067d1a2008-11-13 22:59:24 +0000260 // --------------------------------------------------------------------
261
262 /**
263 * Show column query
264 *
265 * Generates a platform-specific query string so that the column names can be fetched
266 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000267 * @param string the table name
268 * @return string
269 */
Andrey Andreevb76029d2012-01-26 15:13:19 +0200270 protected function _list_columns($table = '')
Derek Allard2067d1a2008-11-13 22:59:24 +0000271 {
Andrey Andreevb76029d2012-01-26 15:13:19 +0200272 return 'SHOW COLUMNS FROM '.$table;
Derek Allard2067d1a2008-11-13 22:59:24 +0000273 }
274
275 // --------------------------------------------------------------------
276
277 /**
278 * Field data query
279 *
280 * Generates a platform-specific query so that the column data can be retrieved
281 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000282 * @param string the table name
Andrey Andreevb76029d2012-01-26 15:13:19 +0200283 * @return string
Derek Allard2067d1a2008-11-13 22:59:24 +0000284 */
Andrey Andreevb76029d2012-01-26 15:13:19 +0200285 protected function _field_data($table)
Derek Allard2067d1a2008-11-13 22:59:24 +0000286 {
Andrey Andreevb76029d2012-01-26 15:13:19 +0200287 return 'SELECT TOP 1 FROM '.$table;
Derek Allard2067d1a2008-11-13 22:59:24 +0000288 }
289
290 // --------------------------------------------------------------------
291
292 /**
Andrey Andreev4be5de12012-03-02 15:45:41 +0200293 * Error
Derek Allard2067d1a2008-11-13 22:59:24 +0000294 *
Andrey Andreev4be5de12012-03-02 15:45:41 +0200295 * Returns an array containing code and message of the last
296 * database error that has occured.
Derek Allard2067d1a2008-11-13 22:59:24 +0000297 *
Andrey Andreev4be5de12012-03-02 15:45:41 +0200298 * @return array
Derek Allard2067d1a2008-11-13 22:59:24 +0000299 */
Andrey Andreev4be5de12012-03-02 15:45:41 +0200300 public function error()
Derek Allard2067d1a2008-11-13 22:59:24 +0000301 {
Andrey Andreev4be5de12012-03-02 15:45:41 +0200302 return array('code' => odbc_error($this->conn_id), 'message' => odbc_errormsg($this->conn_id));
Derek Allard2067d1a2008-11-13 22:59:24 +0000303 }
304
305 // --------------------------------------------------------------------
306
307 /**
Andrey Andreevb0478652012-07-18 15:34:46 +0300308 * Update statement
309 *
310 * Generates a platform-specific update string from the supplied data
311 *
312 * @param string the table name
313 * @param array the update data
314 * @return string
315 */
316 protected function _update($table, $values)
317 {
318 $this->qb_limit = FALSE;
319 $this->qb_orderby = array();
320 return parent::_update($table, $values);
321 }
322
323 // --------------------------------------------------------------------
324
325 /**
Derek Allard2067d1a2008-11-13 22:59:24 +0000326 * Truncate statement
327 *
328 * Generates a platform-specific truncate string from the supplied data
Andrey Andreeva6fe36e2012-04-05 16:00:32 +0300329 *
330 * If the database does not support the truncate() command,
331 * then this method maps to 'DELETE FROM table'
Derek Allard2067d1a2008-11-13 22:59:24 +0000332 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000333 * @param string the table name
334 * @return string
Barry Mienydd671972010-10-04 16:33:58 +0200335 */
Andrey Andreevb76029d2012-01-26 15:13:19 +0200336 protected function _truncate($table)
Derek Allard2067d1a2008-11-13 22:59:24 +0000337 {
Andrey Andreeva6fe36e2012-04-05 16:00:32 +0300338 return 'DELETE FROM '.$table;
Derek Allard2067d1a2008-11-13 22:59:24 +0000339 }
Barry Mienydd671972010-10-04 16:33:58 +0200340
Derek Allard2067d1a2008-11-13 22:59:24 +0000341 // --------------------------------------------------------------------
342
343 /**
Andrey Andreevb0478652012-07-18 15:34:46 +0300344 * Delete statement
345 *
346 * Generates a platform-specific delete string from the supplied data
347 *
348 * @param string the table name
349 * @return string
350 */
351 protected function _delete($table)
352 {
353 $this->qb_limit = FALSE;
354 return parent::_delete($table);
355 }
356
357 // --------------------------------------------------------------------
358
359 /**
Derek Allard2067d1a2008-11-13 22:59:24 +0000360 * Close DB Connection
361 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000362 * @return void
363 */
Andrey Andreev79922c02012-05-23 12:27:17 +0300364 protected function _close()
Derek Allard2067d1a2008-11-13 22:59:24 +0000365 {
Andrey Andreev79922c02012-05-23 12:27:17 +0300366 @odbc_close($this->conn_id);
Derek Allard2067d1a2008-11-13 22:59:24 +0000367 }
368
Derek Allard2067d1a2008-11-13 22:59:24 +0000369}
370
Derek Allard2067d1a2008-11-13 22:59:24 +0000371/* End of file odbc_driver.php */
Andrey Andreev79922c02012-05-23 12:27:17 +0300372/* Location: ./system/database/drivers/odbc/odbc_driver.php */