blob: 8f0a474b042c9ffc995a4dc036c0fbda09b324c4 [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
Derek Jonese4ed5832009-02-20 21:44:59 +000048 // clause and character used for LIKE escape sequences
Andrey Andreev5f356bf2012-03-20 14:32:27 +020049 protected $_like_escape_str = " {escape '%s'} ";
Andrey Andreevb76029d2012-01-26 15:13:19 +020050 protected $_like_escape_chr = '!';
Barry Mienydd671972010-10-04 16:33:58 +020051
Andrey Andreevb76029d2012-01-26 15:13:19 +020052 protected $_random_keyword;
Derek Allard2067d1a2008-11-13 22:59:24 +000053
Andrey Andreev5f356bf2012-03-20 14:32:27 +020054 public function __construct($params)
Derek Allard2067d1a2008-11-13 22:59:24 +000055 {
Timothy Warrena2097a02011-10-10 10:10:46 -040056 parent::__construct($params);
Barry Mienydd671972010-10-04 16:33:58 +020057
Derek Allard2067d1a2008-11-13 22:59:24 +000058 $this->_random_keyword = ' RND('.time().')'; // database specific random keyword
Andrey Andreev46f6dbc2012-02-13 01:39:53 +020059
60 // Legacy support for DSN in the hostname field
Andrey Andreeve4c30192012-06-04 15:08:24 +030061 if (empty($this->dsn))
Andrey Andreev46f6dbc2012-02-13 01:39:53 +020062 {
63 $this->dsn = $this->hostname;
64 }
Derek Allard2067d1a2008-11-13 22:59:24 +000065 }
66
67 /**
68 * Non-persistent database connection
69 *
Derek Allard2067d1a2008-11-13 22:59:24 +000070 * @return resource
Barry Mienydd671972010-10-04 16:33:58 +020071 */
Andrey Andreevb76029d2012-01-26 15:13:19 +020072 public function db_connect()
Derek Allard2067d1a2008-11-13 22:59:24 +000073 {
Andrey Andreev46f6dbc2012-02-13 01:39:53 +020074 return @odbc_connect($this->dsn, $this->username, $this->password);
Derek Allard2067d1a2008-11-13 22:59:24 +000075 }
Barry Mienydd671972010-10-04 16:33:58 +020076
Derek Allard2067d1a2008-11-13 22:59:24 +000077 // --------------------------------------------------------------------
78
79 /**
80 * Persistent database connection
81 *
Derek Allard2067d1a2008-11-13 22:59:24 +000082 * @return resource
Barry Mienydd671972010-10-04 16:33:58 +020083 */
Andrey Andreevb76029d2012-01-26 15:13:19 +020084 public function db_pconnect()
Derek Allard2067d1a2008-11-13 22:59:24 +000085 {
Andrey Andreev46f6dbc2012-02-13 01:39:53 +020086 return @odbc_pconnect($this->dsn, $this->username, $this->password);
Derek Allard2067d1a2008-11-13 22:59:24 +000087 }
Barry Mienydd671972010-10-04 16:33:58 +020088
Derek Allard2067d1a2008-11-13 22:59:24 +000089 // --------------------------------------------------------------------
90
91 /**
Derek Allard2067d1a2008-11-13 22:59:24 +000092 * Execute the query
93 *
Derek Allard2067d1a2008-11-13 22:59:24 +000094 * @param string an SQL query
95 * @return resource
Barry Mienydd671972010-10-04 16:33:58 +020096 */
Andrey Andreevb76029d2012-01-26 15:13:19 +020097 protected function _execute($sql)
Derek Allard2067d1a2008-11-13 22:59:24 +000098 {
Derek Allard2067d1a2008-11-13 22:59:24 +000099 return @odbc_exec($this->conn_id, $sql);
Derek Allard2067d1a2008-11-13 22:59:24 +0000100 }
101
102 // --------------------------------------------------------------------
103
104 /**
105 * Begin Transaction
106 *
Barry Mienydd671972010-10-04 16:33:58 +0200107 * @return bool
108 */
Andrey Andreevb76029d2012-01-26 15:13:19 +0200109 public function trans_begin($test_mode = FALSE)
Derek Allard2067d1a2008-11-13 22:59:24 +0000110 {
Derek Allard2067d1a2008-11-13 22:59:24 +0000111 // When transactions are nested we only begin/commit/rollback the outermost ones
Andrey Andreevb76029d2012-01-26 15:13:19 +0200112 if ( ! $this->trans_enabled OR $this->_trans_depth > 0)
Derek Allard2067d1a2008-11-13 22:59:24 +0000113 {
114 return TRUE;
115 }
116
117 // Reset the transaction failure flag.
118 // If the $test_mode flag is set to TRUE transactions will be rolled back
119 // even if the queries produce a successful result.
Andrey Andreevb76029d2012-01-26 15:13:19 +0200120 $this->_trans_failure = ($test_mode === TRUE);
Derek Allard2067d1a2008-11-13 22:59:24 +0000121
122 return odbc_autocommit($this->conn_id, FALSE);
123 }
124
125 // --------------------------------------------------------------------
126
127 /**
128 * Commit Transaction
129 *
Barry Mienydd671972010-10-04 16:33:58 +0200130 * @return bool
131 */
Andrey Andreevb76029d2012-01-26 15:13:19 +0200132 public function trans_commit()
Derek Allard2067d1a2008-11-13 22:59:24 +0000133 {
Derek Allard2067d1a2008-11-13 22:59:24 +0000134 // When transactions are nested we only begin/commit/rollback the outermost ones
Andrey Andreevb76029d2012-01-26 15:13:19 +0200135 if ( ! $this->trans_enabled OR $this->_trans_depth > 0)
Derek Allard2067d1a2008-11-13 22:59:24 +0000136 {
137 return TRUE;
138 }
139
140 $ret = odbc_commit($this->conn_id);
141 odbc_autocommit($this->conn_id, TRUE);
142 return $ret;
143 }
144
145 // --------------------------------------------------------------------
146
147 /**
148 * Rollback Transaction
149 *
Barry Mienydd671972010-10-04 16:33:58 +0200150 * @return bool
151 */
Andrey Andreevb76029d2012-01-26 15:13:19 +0200152 public function trans_rollback()
Derek Allard2067d1a2008-11-13 22:59:24 +0000153 {
Derek Allard2067d1a2008-11-13 22:59:24 +0000154 // When transactions are nested we only begin/commit/rollback the outermost ones
Andrey Andreevb76029d2012-01-26 15:13:19 +0200155 if ( ! $this->trans_enabled OR $this->_trans_depth > 0)
Derek Allard2067d1a2008-11-13 22:59:24 +0000156 {
157 return TRUE;
158 }
159
160 $ret = odbc_rollback($this->conn_id);
161 odbc_autocommit($this->conn_id, TRUE);
162 return $ret;
163 }
164
165 // --------------------------------------------------------------------
166
167 /**
168 * Escape String
169 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000170 * @param string
Derek Jonese4ed5832009-02-20 21:44:59 +0000171 * @param bool whether or not the string will be used in a LIKE condition
Derek Allard2067d1a2008-11-13 22:59:24 +0000172 * @return string
173 */
Andrey Andreevb76029d2012-01-26 15:13:19 +0200174 public function escape_str($str, $like = FALSE)
Derek Allard2067d1a2008-11-13 22:59:24 +0000175 {
Derek Jonese4ed5832009-02-20 21:44:59 +0000176 if (is_array($str))
177 {
Pascal Krietec3a4a8d2011-02-14 13:40:08 -0500178 foreach ($str as $key => $val)
Barry Mienydd671972010-10-04 16:33:58 +0200179 {
Derek Jonese4ed5832009-02-20 21:44:59 +0000180 $str[$key] = $this->escape_str($val, $like);
Barry Mienydd671972010-10-04 16:33:58 +0200181 }
182
183 return $str;
184 }
185
Greg Aker757dda62010-04-14 19:06:19 -0500186 $str = remove_invisible_characters($str);
Barry Mienydd671972010-10-04 16:33:58 +0200187
Derek Jonese4ed5832009-02-20 21:44:59 +0000188 // escape LIKE condition wildcards
189 if ($like === TRUE)
190 {
Andrey Andreev5e18e892012-03-09 14:25:00 +0200191 return str_replace(array($this->_like_escape_chr, '%', '_'),
192 array($this->_like_escape_chr.$this->_like_escape_chr, $this->_like_escape_chr.'%', $this->_like_escape_chr.'_'),
Andrey Andreevb76029d2012-01-26 15:13:19 +0200193 $str);
Derek Jonese4ed5832009-02-20 21:44:59 +0000194 }
Barry Mienydd671972010-10-04 16:33:58 +0200195
Derek Jonese4ed5832009-02-20 21:44:59 +0000196 return $str;
Derek Allard2067d1a2008-11-13 22:59:24 +0000197 }
Barry Mienydd671972010-10-04 16:33:58 +0200198
Derek Allard2067d1a2008-11-13 22:59:24 +0000199 // --------------------------------------------------------------------
200
201 /**
202 * Affected Rows
203 *
Andrey Andreevb76029d2012-01-26 15:13:19 +0200204 * @return int
Derek Allard2067d1a2008-11-13 22:59:24 +0000205 */
Andrey Andreevb76029d2012-01-26 15:13:19 +0200206 public function affected_rows()
Derek Allard2067d1a2008-11-13 22:59:24 +0000207 {
208 return @odbc_num_rows($this->conn_id);
209 }
Barry Mienydd671972010-10-04 16:33:58 +0200210
Derek Allard2067d1a2008-11-13 22:59:24 +0000211 // --------------------------------------------------------------------
212
213 /**
214 * Insert ID
215 *
Andrey Andreev8af76662012-03-05 14:33:41 +0200216 * @return bool
Derek Allard2067d1a2008-11-13 22:59:24 +0000217 */
Andrey Andreevb76029d2012-01-26 15:13:19 +0200218 public function insert_id()
Derek Allard2067d1a2008-11-13 22:59:24 +0000219 {
Andrey Andreev8af76662012-03-05 14:33:41 +0200220 return ($this->db->db_debug) ? $this->db->display_error('db_unsuported_feature') : FALSE;
Derek Allard2067d1a2008-11-13 22:59:24 +0000221 }
222
223 // --------------------------------------------------------------------
224
225 /**
Derek Allard2067d1a2008-11-13 22:59:24 +0000226 * Show table query
227 *
228 * Generates a platform-specific query string so that the table names can be fetched
229 *
Andrey Andreev5f356bf2012-03-20 14:32:27 +0200230 * @param bool
Derek Allard2067d1a2008-11-13 22:59:24 +0000231 * @return string
232 */
Andrey Andreevb76029d2012-01-26 15:13:19 +0200233 protected function _list_tables($prefix_limit = FALSE)
Derek Allard2067d1a2008-11-13 22:59:24 +0000234 {
Andrey Andreeve4c30192012-06-04 15:08:24 +0300235 $sql = 'SHOW TABLES FROM '.$this->database;
Derek Allard2067d1a2008-11-13 22:59:24 +0000236
Andrey Andreeve4c30192012-06-04 15:08:24 +0300237 if ($prefix_limit !== FALSE && $this->dbprefix !== '')
Derek Allard2067d1a2008-11-13 22:59:24 +0000238 {
Greg Aker0d424892010-01-26 02:14:44 +0000239 //$sql .= " LIKE '".$this->escape_like_str($this->dbprefix)."%' ".sprintf($this->_like_escape_str, $this->_like_escape_chr);
Derek Allard2067d1a2008-11-13 22:59:24 +0000240 return FALSE; // not currently supported
241 }
Barry Mienydd671972010-10-04 16:33:58 +0200242
Derek Allard2067d1a2008-11-13 22:59:24 +0000243 return $sql;
244 }
Barry Mienydd671972010-10-04 16:33:58 +0200245
Derek Allard2067d1a2008-11-13 22:59:24 +0000246 // --------------------------------------------------------------------
247
248 /**
249 * Show column query
250 *
251 * Generates a platform-specific query string so that the column names can be fetched
252 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000253 * @param string the table name
254 * @return string
255 */
Andrey Andreevb76029d2012-01-26 15:13:19 +0200256 protected function _list_columns($table = '')
Derek Allard2067d1a2008-11-13 22:59:24 +0000257 {
Andrey Andreevb76029d2012-01-26 15:13:19 +0200258 return 'SHOW COLUMNS FROM '.$table;
Derek Allard2067d1a2008-11-13 22:59:24 +0000259 }
260
261 // --------------------------------------------------------------------
262
263 /**
264 * Field data query
265 *
266 * Generates a platform-specific query so that the column data can be retrieved
267 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000268 * @param string the table name
Andrey Andreevb76029d2012-01-26 15:13:19 +0200269 * @return string
Derek Allard2067d1a2008-11-13 22:59:24 +0000270 */
Andrey Andreevb76029d2012-01-26 15:13:19 +0200271 protected function _field_data($table)
Derek Allard2067d1a2008-11-13 22:59:24 +0000272 {
Andrey Andreevb76029d2012-01-26 15:13:19 +0200273 return 'SELECT TOP 1 FROM '.$table;
Derek Allard2067d1a2008-11-13 22:59:24 +0000274 }
275
276 // --------------------------------------------------------------------
277
278 /**
Andrey Andreev4be5de12012-03-02 15:45:41 +0200279 * Error
Derek Allard2067d1a2008-11-13 22:59:24 +0000280 *
Andrey Andreev4be5de12012-03-02 15:45:41 +0200281 * Returns an array containing code and message of the last
282 * database error that has occured.
Derek Allard2067d1a2008-11-13 22:59:24 +0000283 *
Andrey Andreev4be5de12012-03-02 15:45:41 +0200284 * @return array
Derek Allard2067d1a2008-11-13 22:59:24 +0000285 */
Andrey Andreev4be5de12012-03-02 15:45:41 +0200286 public function error()
Derek Allard2067d1a2008-11-13 22:59:24 +0000287 {
Andrey Andreev4be5de12012-03-02 15:45:41 +0200288 return array('code' => odbc_error($this->conn_id), 'message' => odbc_errormsg($this->conn_id));
Derek Allard2067d1a2008-11-13 22:59:24 +0000289 }
290
291 // --------------------------------------------------------------------
292
293 /**
Derek Allard2067d1a2008-11-13 22:59:24 +0000294 * From Tables
295 *
296 * This function implicitly groups FROM tables so there is no confusion
297 * about operator precedence in harmony with SQL standards
298 *
Andrey Andreev5f356bf2012-03-20 14:32:27 +0200299 * @param array
Andrey Andreevb76029d2012-01-26 15:13:19 +0200300 * @return string
Derek Allard2067d1a2008-11-13 22:59:24 +0000301 */
Andrey Andreevb76029d2012-01-26 15:13:19 +0200302 protected function _from_tables($tables)
Derek Allard2067d1a2008-11-13 22:59:24 +0000303 {
Andrey Andreevc78e56a2012-06-08 02:12:07 +0300304 return is_array($tables) ? implode(', ', $tables) : $tables;
Derek Allard2067d1a2008-11-13 22:59:24 +0000305 }
306
307 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200308
Derek Allard2067d1a2008-11-13 22:59:24 +0000309 /**
Derek Allard2067d1a2008-11-13 22:59:24 +0000310 * Truncate statement
311 *
312 * Generates a platform-specific truncate string from the supplied data
Andrey Andreeva6fe36e2012-04-05 16:00:32 +0300313 *
314 * If the database does not support the truncate() command,
315 * then this method maps to 'DELETE FROM table'
Derek Allard2067d1a2008-11-13 22:59:24 +0000316 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000317 * @param string the table name
318 * @return string
Barry Mienydd671972010-10-04 16:33:58 +0200319 */
Andrey Andreevb76029d2012-01-26 15:13:19 +0200320 protected function _truncate($table)
Derek Allard2067d1a2008-11-13 22:59:24 +0000321 {
Andrey Andreeva6fe36e2012-04-05 16:00:32 +0300322 return 'DELETE FROM '.$table;
Derek Allard2067d1a2008-11-13 22:59:24 +0000323 }
Barry Mienydd671972010-10-04 16:33:58 +0200324
Derek Allard2067d1a2008-11-13 22:59:24 +0000325 // --------------------------------------------------------------------
326
327 /**
Derek Allard2067d1a2008-11-13 22:59:24 +0000328 * Close DB Connection
329 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000330 * @return void
331 */
Andrey Andreev79922c02012-05-23 12:27:17 +0300332 protected function _close()
Derek Allard2067d1a2008-11-13 22:59:24 +0000333 {
Andrey Andreev79922c02012-05-23 12:27:17 +0300334 @odbc_close($this->conn_id);
Derek Allard2067d1a2008-11-13 22:59:24 +0000335 }
336
Derek Allard2067d1a2008-11-13 22:59:24 +0000337}
338
Derek Allard2067d1a2008-11-13 22:59:24 +0000339/* End of file odbc_driver.php */
Andrey Andreev79922c02012-05-23 12:27:17 +0300340/* Location: ./system/database/drivers/odbc/odbc_driver.php */