blob: 37f7a28d3d71ed9ada8b85de79fe7f8e0cf6e057 [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 Andreev485a3482012-10-27 03:22:43 +030053 * @var string Database schema
54 */
55 public $schema = 'public';
56
57 /**
Andrey Andreev5fd3ae82012-10-24 14:55:35 +030058 * Constructor
59 *
60 * @param array $params
61 * @return void
62 */
Andrey Andreev5f356bf2012-03-20 14:32:27 +020063 public function __construct($params)
Derek Allard2067d1a2008-11-13 22:59:24 +000064 {
Timothy Warrena2097a02011-10-10 10:10:46 -040065 parent::__construct($params);
Barry Mienydd671972010-10-04 16:33:58 +020066
Derek Allard2067d1a2008-11-13 22:59:24 +000067 $this->_random_keyword = ' RND('.time().')'; // database specific random keyword
Andrey Andreev46f6dbc2012-02-13 01:39:53 +020068
69 // Legacy support for DSN in the hostname field
Andrey Andreeve4c30192012-06-04 15:08:24 +030070 if (empty($this->dsn))
Andrey Andreev46f6dbc2012-02-13 01:39:53 +020071 {
72 $this->dsn = $this->hostname;
73 }
Derek Allard2067d1a2008-11-13 22:59:24 +000074 }
75
Andrey Andreev5fd3ae82012-10-24 14:55:35 +030076 // --------------------------------------------------------------------
77
Derek Allard2067d1a2008-11-13 22:59:24 +000078 /**
79 * Non-persistent database connection
80 *
Derek Allard2067d1a2008-11-13 22:59:24 +000081 * @return resource
Barry Mienydd671972010-10-04 16:33:58 +020082 */
Andrey Andreevb76029d2012-01-26 15:13:19 +020083 public function db_connect()
Derek Allard2067d1a2008-11-13 22:59:24 +000084 {
Andrey Andreev46f6dbc2012-02-13 01:39:53 +020085 return @odbc_connect($this->dsn, $this->username, $this->password);
Derek Allard2067d1a2008-11-13 22:59:24 +000086 }
Barry Mienydd671972010-10-04 16:33:58 +020087
Derek Allard2067d1a2008-11-13 22:59:24 +000088 // --------------------------------------------------------------------
89
90 /**
91 * Persistent database connection
92 *
Derek Allard2067d1a2008-11-13 22:59:24 +000093 * @return resource
Barry Mienydd671972010-10-04 16:33:58 +020094 */
Andrey Andreevb76029d2012-01-26 15:13:19 +020095 public function db_pconnect()
Derek Allard2067d1a2008-11-13 22:59:24 +000096 {
Andrey Andreev46f6dbc2012-02-13 01:39:53 +020097 return @odbc_pconnect($this->dsn, $this->username, $this->password);
Derek Allard2067d1a2008-11-13 22:59:24 +000098 }
Barry Mienydd671972010-10-04 16:33:58 +020099
Derek Allard2067d1a2008-11-13 22:59:24 +0000100 // --------------------------------------------------------------------
101
102 /**
Derek Allard2067d1a2008-11-13 22:59:24 +0000103 * Execute the query
104 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000105 * @param string an SQL query
106 * @return resource
Barry Mienydd671972010-10-04 16:33:58 +0200107 */
Andrey Andreevb76029d2012-01-26 15:13:19 +0200108 protected function _execute($sql)
Derek Allard2067d1a2008-11-13 22:59:24 +0000109 {
Derek Allard2067d1a2008-11-13 22:59:24 +0000110 return @odbc_exec($this->conn_id, $sql);
Derek Allard2067d1a2008-11-13 22:59:24 +0000111 }
112
113 // --------------------------------------------------------------------
114
115 /**
116 * Begin Transaction
117 *
Andrey Andreev5fd3ae82012-10-24 14:55:35 +0300118 * @param bool $test_mode = FALSE
Barry Mienydd671972010-10-04 16:33:58 +0200119 * @return bool
120 */
Andrey Andreevb76029d2012-01-26 15:13:19 +0200121 public function trans_begin($test_mode = FALSE)
Derek Allard2067d1a2008-11-13 22:59:24 +0000122 {
Derek Allard2067d1a2008-11-13 22:59:24 +0000123 // When transactions are nested we only begin/commit/rollback the outermost ones
Andrey Andreevb76029d2012-01-26 15:13:19 +0200124 if ( ! $this->trans_enabled OR $this->_trans_depth > 0)
Derek Allard2067d1a2008-11-13 22:59:24 +0000125 {
126 return TRUE;
127 }
128
129 // Reset the transaction failure flag.
130 // If the $test_mode flag is set to TRUE transactions will be rolled back
131 // even if the queries produce a successful result.
Andrey Andreevb76029d2012-01-26 15:13:19 +0200132 $this->_trans_failure = ($test_mode === TRUE);
Derek Allard2067d1a2008-11-13 22:59:24 +0000133
134 return odbc_autocommit($this->conn_id, FALSE);
135 }
136
137 // --------------------------------------------------------------------
138
139 /**
140 * Commit Transaction
141 *
Barry Mienydd671972010-10-04 16:33:58 +0200142 * @return bool
143 */
Andrey Andreevb76029d2012-01-26 15:13:19 +0200144 public function trans_commit()
Derek Allard2067d1a2008-11-13 22:59:24 +0000145 {
Derek Allard2067d1a2008-11-13 22:59:24 +0000146 // When transactions are nested we only begin/commit/rollback the outermost ones
Andrey Andreevb76029d2012-01-26 15:13:19 +0200147 if ( ! $this->trans_enabled OR $this->_trans_depth > 0)
Derek Allard2067d1a2008-11-13 22:59:24 +0000148 {
149 return TRUE;
150 }
151
152 $ret = odbc_commit($this->conn_id);
153 odbc_autocommit($this->conn_id, TRUE);
154 return $ret;
155 }
156
157 // --------------------------------------------------------------------
158
159 /**
160 * Rollback Transaction
161 *
Barry Mienydd671972010-10-04 16:33:58 +0200162 * @return bool
163 */
Andrey Andreevb76029d2012-01-26 15:13:19 +0200164 public function trans_rollback()
Derek Allard2067d1a2008-11-13 22:59:24 +0000165 {
Derek Allard2067d1a2008-11-13 22:59:24 +0000166 // When transactions are nested we only begin/commit/rollback the outermost ones
Andrey Andreevb76029d2012-01-26 15:13:19 +0200167 if ( ! $this->trans_enabled OR $this->_trans_depth > 0)
Derek Allard2067d1a2008-11-13 22:59:24 +0000168 {
169 return TRUE;
170 }
171
172 $ret = odbc_rollback($this->conn_id);
173 odbc_autocommit($this->conn_id, TRUE);
174 return $ret;
175 }
176
177 // --------------------------------------------------------------------
178
179 /**
180 * Escape String
181 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000182 * @param string
Derek Jonese4ed5832009-02-20 21:44:59 +0000183 * @param bool whether or not the string will be used in a LIKE condition
Derek Allard2067d1a2008-11-13 22:59:24 +0000184 * @return string
185 */
Andrey Andreevb76029d2012-01-26 15:13:19 +0200186 public function escape_str($str, $like = FALSE)
Derek Allard2067d1a2008-11-13 22:59:24 +0000187 {
Derek Jonese4ed5832009-02-20 21:44:59 +0000188 if (is_array($str))
189 {
Pascal Krietec3a4a8d2011-02-14 13:40:08 -0500190 foreach ($str as $key => $val)
Barry Mienydd671972010-10-04 16:33:58 +0200191 {
Derek Jonese4ed5832009-02-20 21:44:59 +0000192 $str[$key] = $this->escape_str($val, $like);
Barry Mienydd671972010-10-04 16:33:58 +0200193 }
194
195 return $str;
196 }
197
Greg Aker757dda62010-04-14 19:06:19 -0500198 $str = remove_invisible_characters($str);
Barry Mienydd671972010-10-04 16:33:58 +0200199
Derek Jonese4ed5832009-02-20 21:44:59 +0000200 // escape LIKE condition wildcards
201 if ($like === TRUE)
202 {
Andrey Andreev5e18e892012-03-09 14:25:00 +0200203 return str_replace(array($this->_like_escape_chr, '%', '_'),
204 array($this->_like_escape_chr.$this->_like_escape_chr, $this->_like_escape_chr.'%', $this->_like_escape_chr.'_'),
Andrey Andreevb76029d2012-01-26 15:13:19 +0200205 $str);
Derek Jonese4ed5832009-02-20 21:44:59 +0000206 }
Barry Mienydd671972010-10-04 16:33:58 +0200207
Derek Jonese4ed5832009-02-20 21:44:59 +0000208 return $str;
Derek Allard2067d1a2008-11-13 22:59:24 +0000209 }
Barry Mienydd671972010-10-04 16:33:58 +0200210
Derek Allard2067d1a2008-11-13 22:59:24 +0000211 // --------------------------------------------------------------------
212
213 /**
214 * Affected Rows
215 *
Andrey Andreevb76029d2012-01-26 15:13:19 +0200216 * @return int
Derek Allard2067d1a2008-11-13 22:59:24 +0000217 */
Andrey Andreevb76029d2012-01-26 15:13:19 +0200218 public function affected_rows()
Derek Allard2067d1a2008-11-13 22:59:24 +0000219 {
220 return @odbc_num_rows($this->conn_id);
221 }
Barry Mienydd671972010-10-04 16:33:58 +0200222
Derek Allard2067d1a2008-11-13 22:59:24 +0000223 // --------------------------------------------------------------------
224
225 /**
226 * Insert ID
227 *
Andrey Andreev8af76662012-03-05 14:33:41 +0200228 * @return bool
Derek Allard2067d1a2008-11-13 22:59:24 +0000229 */
Andrey Andreevb76029d2012-01-26 15:13:19 +0200230 public function insert_id()
Derek Allard2067d1a2008-11-13 22:59:24 +0000231 {
Andrey Andreev8af76662012-03-05 14:33:41 +0200232 return ($this->db->db_debug) ? $this->db->display_error('db_unsuported_feature') : FALSE;
Derek Allard2067d1a2008-11-13 22:59:24 +0000233 }
234
235 // --------------------------------------------------------------------
236
237 /**
Derek Allard2067d1a2008-11-13 22:59:24 +0000238 * Show table query
239 *
240 * Generates a platform-specific query string so that the table names can be fetched
241 *
Andrey Andreev485a3482012-10-27 03:22:43 +0300242 * @param bool $prefix_limit = FALSE
Derek Allard2067d1a2008-11-13 22:59:24 +0000243 * @return string
244 */
Andrey Andreevb76029d2012-01-26 15:13:19 +0200245 protected function _list_tables($prefix_limit = FALSE)
Derek Allard2067d1a2008-11-13 22:59:24 +0000246 {
Andrey Andreev485a3482012-10-27 03:22:43 +0300247 $sql = "SELECT table_name FROM information_schema.tables WHERE table_schema = '".$this->schema."'";
Derek Allard2067d1a2008-11-13 22:59:24 +0000248
Andrey Andreeve4c30192012-06-04 15:08:24 +0300249 if ($prefix_limit !== FALSE && $this->dbprefix !== '')
Derek Allard2067d1a2008-11-13 22:59:24 +0000250 {
Andrey Andreev485a3482012-10-27 03:22:43 +0300251 return $sql." AND table_name LIKE '".$this->escape_like_str($this->dbprefix)."%' "
252 .sprintf($this->_like_escape_str, $this->_like_escape_chr);
Derek Allard2067d1a2008-11-13 22:59:24 +0000253 }
Barry Mienydd671972010-10-04 16:33:58 +0200254
Derek Allard2067d1a2008-11-13 22:59:24 +0000255 return $sql;
256 }
Barry Mienydd671972010-10-04 16:33:58 +0200257
Derek Allard2067d1a2008-11-13 22:59:24 +0000258 // --------------------------------------------------------------------
259
260 /**
261 * Show column query
262 *
263 * Generates a platform-specific query string so that the column names can be fetched
264 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000265 * @param string the table name
266 * @return string
267 */
Andrey Andreevb76029d2012-01-26 15:13:19 +0200268 protected function _list_columns($table = '')
Derek Allard2067d1a2008-11-13 22:59:24 +0000269 {
Andrey Andreevb76029d2012-01-26 15:13:19 +0200270 return 'SHOW COLUMNS FROM '.$table;
Derek Allard2067d1a2008-11-13 22:59:24 +0000271 }
272
273 // --------------------------------------------------------------------
274
275 /**
276 * Field data query
277 *
278 * Generates a platform-specific query so that the column data can be retrieved
279 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000280 * @param string the table name
Andrey Andreevb76029d2012-01-26 15:13:19 +0200281 * @return string
Derek Allard2067d1a2008-11-13 22:59:24 +0000282 */
Andrey Andreevb76029d2012-01-26 15:13:19 +0200283 protected function _field_data($table)
Derek Allard2067d1a2008-11-13 22:59:24 +0000284 {
Andrey Andreevb76029d2012-01-26 15:13:19 +0200285 return 'SELECT TOP 1 FROM '.$table;
Derek Allard2067d1a2008-11-13 22:59:24 +0000286 }
287
288 // --------------------------------------------------------------------
289
290 /**
Andrey Andreev4be5de12012-03-02 15:45:41 +0200291 * Error
Derek Allard2067d1a2008-11-13 22:59:24 +0000292 *
Andrey Andreev4be5de12012-03-02 15:45:41 +0200293 * Returns an array containing code and message of the last
294 * database error that has occured.
Derek Allard2067d1a2008-11-13 22:59:24 +0000295 *
Andrey Andreev4be5de12012-03-02 15:45:41 +0200296 * @return array
Derek Allard2067d1a2008-11-13 22:59:24 +0000297 */
Andrey Andreev4be5de12012-03-02 15:45:41 +0200298 public function error()
Derek Allard2067d1a2008-11-13 22:59:24 +0000299 {
Andrey Andreev4be5de12012-03-02 15:45:41 +0200300 return array('code' => odbc_error($this->conn_id), 'message' => odbc_errormsg($this->conn_id));
Derek Allard2067d1a2008-11-13 22:59:24 +0000301 }
302
303 // --------------------------------------------------------------------
304
305 /**
Andrey Andreevb0478652012-07-18 15:34:46 +0300306 * Update statement
307 *
308 * Generates a platform-specific update string from the supplied data
309 *
310 * @param string the table name
311 * @param array the update data
312 * @return string
313 */
314 protected function _update($table, $values)
315 {
316 $this->qb_limit = FALSE;
317 $this->qb_orderby = array();
318 return parent::_update($table, $values);
319 }
320
321 // --------------------------------------------------------------------
322
323 /**
Derek Allard2067d1a2008-11-13 22:59:24 +0000324 * Truncate statement
325 *
326 * Generates a platform-specific truncate string from the supplied data
Andrey Andreeva6fe36e2012-04-05 16:00:32 +0300327 *
328 * If the database does not support the truncate() command,
329 * then this method maps to 'DELETE FROM table'
Derek Allard2067d1a2008-11-13 22:59:24 +0000330 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000331 * @param string the table name
332 * @return string
Barry Mienydd671972010-10-04 16:33:58 +0200333 */
Andrey Andreevb76029d2012-01-26 15:13:19 +0200334 protected function _truncate($table)
Derek Allard2067d1a2008-11-13 22:59:24 +0000335 {
Andrey Andreeva6fe36e2012-04-05 16:00:32 +0300336 return 'DELETE FROM '.$table;
Derek Allard2067d1a2008-11-13 22:59:24 +0000337 }
Barry Mienydd671972010-10-04 16:33:58 +0200338
Derek Allard2067d1a2008-11-13 22:59:24 +0000339 // --------------------------------------------------------------------
340
341 /**
Andrey Andreevb0478652012-07-18 15:34:46 +0300342 * Delete statement
343 *
344 * Generates a platform-specific delete string from the supplied data
345 *
346 * @param string the table name
347 * @return string
348 */
349 protected function _delete($table)
350 {
351 $this->qb_limit = FALSE;
352 return parent::_delete($table);
353 }
354
355 // --------------------------------------------------------------------
356
357 /**
Derek Allard2067d1a2008-11-13 22:59:24 +0000358 * Close DB Connection
359 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000360 * @return void
361 */
Andrey Andreev79922c02012-05-23 12:27:17 +0300362 protected function _close()
Derek Allard2067d1a2008-11-13 22:59:24 +0000363 {
Andrey Andreev79922c02012-05-23 12:27:17 +0300364 @odbc_close($this->conn_id);
Derek Allard2067d1a2008-11-13 22:59:24 +0000365 }
366
Derek Allard2067d1a2008-11-13 22:59:24 +0000367}
368
Derek Allard2067d1a2008-11-13 22:59:24 +0000369/* End of file odbc_driver.php */
Andrey Andreev79922c02012-05-23 12:27:17 +0300370/* Location: ./system/database/drivers/odbc/odbc_driver.php */