blob: fbf6a4cb194414056c7f364ceb8dffe3418cf87e [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 Andreev5f356bf2012-03-20 14:32:27 +020052 public function __construct($params)
Derek Allard2067d1a2008-11-13 22:59:24 +000053 {
Timothy Warrena2097a02011-10-10 10:10:46 -040054 parent::__construct($params);
Barry Mienydd671972010-10-04 16:33:58 +020055
Derek Allard2067d1a2008-11-13 22:59:24 +000056 $this->_random_keyword = ' RND('.time().')'; // database specific random keyword
Andrey Andreev46f6dbc2012-02-13 01:39:53 +020057
58 // Legacy support for DSN in the hostname field
Andrey Andreeve4c30192012-06-04 15:08:24 +030059 if (empty($this->dsn))
Andrey Andreev46f6dbc2012-02-13 01:39:53 +020060 {
61 $this->dsn = $this->hostname;
62 }
Derek Allard2067d1a2008-11-13 22:59:24 +000063 }
64
65 /**
66 * Non-persistent database connection
67 *
Derek Allard2067d1a2008-11-13 22:59:24 +000068 * @return resource
Barry Mienydd671972010-10-04 16:33:58 +020069 */
Andrey Andreevb76029d2012-01-26 15:13:19 +020070 public function db_connect()
Derek Allard2067d1a2008-11-13 22:59:24 +000071 {
Andrey Andreev46f6dbc2012-02-13 01:39:53 +020072 return @odbc_connect($this->dsn, $this->username, $this->password);
Derek Allard2067d1a2008-11-13 22:59:24 +000073 }
Barry Mienydd671972010-10-04 16:33:58 +020074
Derek Allard2067d1a2008-11-13 22:59:24 +000075 // --------------------------------------------------------------------
76
77 /**
78 * Persistent database connection
79 *
Derek Allard2067d1a2008-11-13 22:59:24 +000080 * @return resource
Barry Mienydd671972010-10-04 16:33:58 +020081 */
Andrey Andreevb76029d2012-01-26 15:13:19 +020082 public function db_pconnect()
Derek Allard2067d1a2008-11-13 22:59:24 +000083 {
Andrey Andreev46f6dbc2012-02-13 01:39:53 +020084 return @odbc_pconnect($this->dsn, $this->username, $this->password);
Derek Allard2067d1a2008-11-13 22:59:24 +000085 }
Barry Mienydd671972010-10-04 16:33:58 +020086
Derek Allard2067d1a2008-11-13 22:59:24 +000087 // --------------------------------------------------------------------
88
89 /**
Derek Allard2067d1a2008-11-13 22:59:24 +000090 * Execute the query
91 *
Derek Allard2067d1a2008-11-13 22:59:24 +000092 * @param string an SQL query
93 * @return resource
Barry Mienydd671972010-10-04 16:33:58 +020094 */
Andrey Andreevb76029d2012-01-26 15:13:19 +020095 protected function _execute($sql)
Derek Allard2067d1a2008-11-13 22:59:24 +000096 {
Derek Allard2067d1a2008-11-13 22:59:24 +000097 return @odbc_exec($this->conn_id, $sql);
Derek Allard2067d1a2008-11-13 22:59:24 +000098 }
99
100 // --------------------------------------------------------------------
101
102 /**
103 * Begin Transaction
104 *
Barry Mienydd671972010-10-04 16:33:58 +0200105 * @return bool
106 */
Andrey Andreevb76029d2012-01-26 15:13:19 +0200107 public function trans_begin($test_mode = FALSE)
Derek Allard2067d1a2008-11-13 22:59:24 +0000108 {
Derek Allard2067d1a2008-11-13 22:59:24 +0000109 // When transactions are nested we only begin/commit/rollback the outermost ones
Andrey Andreevb76029d2012-01-26 15:13:19 +0200110 if ( ! $this->trans_enabled OR $this->_trans_depth > 0)
Derek Allard2067d1a2008-11-13 22:59:24 +0000111 {
112 return TRUE;
113 }
114
115 // Reset the transaction failure flag.
116 // If the $test_mode flag is set to TRUE transactions will be rolled back
117 // even if the queries produce a successful result.
Andrey Andreevb76029d2012-01-26 15:13:19 +0200118 $this->_trans_failure = ($test_mode === TRUE);
Derek Allard2067d1a2008-11-13 22:59:24 +0000119
120 return odbc_autocommit($this->conn_id, FALSE);
121 }
122
123 // --------------------------------------------------------------------
124
125 /**
126 * Commit Transaction
127 *
Barry Mienydd671972010-10-04 16:33:58 +0200128 * @return bool
129 */
Andrey Andreevb76029d2012-01-26 15:13:19 +0200130 public function trans_commit()
Derek Allard2067d1a2008-11-13 22:59:24 +0000131 {
Derek Allard2067d1a2008-11-13 22:59:24 +0000132 // When transactions are nested we only begin/commit/rollback the outermost ones
Andrey Andreevb76029d2012-01-26 15:13:19 +0200133 if ( ! $this->trans_enabled OR $this->_trans_depth > 0)
Derek Allard2067d1a2008-11-13 22:59:24 +0000134 {
135 return TRUE;
136 }
137
138 $ret = odbc_commit($this->conn_id);
139 odbc_autocommit($this->conn_id, TRUE);
140 return $ret;
141 }
142
143 // --------------------------------------------------------------------
144
145 /**
146 * Rollback Transaction
147 *
Barry Mienydd671972010-10-04 16:33:58 +0200148 * @return bool
149 */
Andrey Andreevb76029d2012-01-26 15:13:19 +0200150 public function trans_rollback()
Derek Allard2067d1a2008-11-13 22:59:24 +0000151 {
Derek Allard2067d1a2008-11-13 22:59:24 +0000152 // When transactions are nested we only begin/commit/rollback the outermost ones
Andrey Andreevb76029d2012-01-26 15:13:19 +0200153 if ( ! $this->trans_enabled OR $this->_trans_depth > 0)
Derek Allard2067d1a2008-11-13 22:59:24 +0000154 {
155 return TRUE;
156 }
157
158 $ret = odbc_rollback($this->conn_id);
159 odbc_autocommit($this->conn_id, TRUE);
160 return $ret;
161 }
162
163 // --------------------------------------------------------------------
164
165 /**
166 * Escape String
167 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000168 * @param string
Derek Jonese4ed5832009-02-20 21:44:59 +0000169 * @param bool whether or not the string will be used in a LIKE condition
Derek Allard2067d1a2008-11-13 22:59:24 +0000170 * @return string
171 */
Andrey Andreevb76029d2012-01-26 15:13:19 +0200172 public function escape_str($str, $like = FALSE)
Derek Allard2067d1a2008-11-13 22:59:24 +0000173 {
Derek Jonese4ed5832009-02-20 21:44:59 +0000174 if (is_array($str))
175 {
Pascal Krietec3a4a8d2011-02-14 13:40:08 -0500176 foreach ($str as $key => $val)
Barry Mienydd671972010-10-04 16:33:58 +0200177 {
Derek Jonese4ed5832009-02-20 21:44:59 +0000178 $str[$key] = $this->escape_str($val, $like);
Barry Mienydd671972010-10-04 16:33:58 +0200179 }
180
181 return $str;
182 }
183
Greg Aker757dda62010-04-14 19:06:19 -0500184 $str = remove_invisible_characters($str);
Barry Mienydd671972010-10-04 16:33:58 +0200185
Derek Jonese4ed5832009-02-20 21:44:59 +0000186 // escape LIKE condition wildcards
187 if ($like === TRUE)
188 {
Andrey Andreev5e18e892012-03-09 14:25:00 +0200189 return str_replace(array($this->_like_escape_chr, '%', '_'),
190 array($this->_like_escape_chr.$this->_like_escape_chr, $this->_like_escape_chr.'%', $this->_like_escape_chr.'_'),
Andrey Andreevb76029d2012-01-26 15:13:19 +0200191 $str);
Derek Jonese4ed5832009-02-20 21:44:59 +0000192 }
Barry Mienydd671972010-10-04 16:33:58 +0200193
Derek Jonese4ed5832009-02-20 21:44:59 +0000194 return $str;
Derek Allard2067d1a2008-11-13 22:59:24 +0000195 }
Barry Mienydd671972010-10-04 16:33:58 +0200196
Derek Allard2067d1a2008-11-13 22:59:24 +0000197 // --------------------------------------------------------------------
198
199 /**
200 * Affected Rows
201 *
Andrey Andreevb76029d2012-01-26 15:13:19 +0200202 * @return int
Derek Allard2067d1a2008-11-13 22:59:24 +0000203 */
Andrey Andreevb76029d2012-01-26 15:13:19 +0200204 public function affected_rows()
Derek Allard2067d1a2008-11-13 22:59:24 +0000205 {
206 return @odbc_num_rows($this->conn_id);
207 }
Barry Mienydd671972010-10-04 16:33:58 +0200208
Derek Allard2067d1a2008-11-13 22:59:24 +0000209 // --------------------------------------------------------------------
210
211 /**
212 * Insert ID
213 *
Andrey Andreev8af76662012-03-05 14:33:41 +0200214 * @return bool
Derek Allard2067d1a2008-11-13 22:59:24 +0000215 */
Andrey Andreevb76029d2012-01-26 15:13:19 +0200216 public function insert_id()
Derek Allard2067d1a2008-11-13 22:59:24 +0000217 {
Andrey Andreev8af76662012-03-05 14:33:41 +0200218 return ($this->db->db_debug) ? $this->db->display_error('db_unsuported_feature') : FALSE;
Derek Allard2067d1a2008-11-13 22:59:24 +0000219 }
220
221 // --------------------------------------------------------------------
222
223 /**
Derek Allard2067d1a2008-11-13 22:59:24 +0000224 * Show table query
225 *
226 * Generates a platform-specific query string so that the table names can be fetched
227 *
Andrey Andreev5f356bf2012-03-20 14:32:27 +0200228 * @param bool
Derek Allard2067d1a2008-11-13 22:59:24 +0000229 * @return string
230 */
Andrey Andreevb76029d2012-01-26 15:13:19 +0200231 protected function _list_tables($prefix_limit = FALSE)
Derek Allard2067d1a2008-11-13 22:59:24 +0000232 {
Andrey Andreeve4c30192012-06-04 15:08:24 +0300233 $sql = 'SHOW TABLES FROM '.$this->database;
Derek Allard2067d1a2008-11-13 22:59:24 +0000234
Andrey Andreeve4c30192012-06-04 15:08:24 +0300235 if ($prefix_limit !== FALSE && $this->dbprefix !== '')
Derek Allard2067d1a2008-11-13 22:59:24 +0000236 {
Greg Aker0d424892010-01-26 02:14:44 +0000237 //$sql .= " LIKE '".$this->escape_like_str($this->dbprefix)."%' ".sprintf($this->_like_escape_str, $this->_like_escape_chr);
Derek Allard2067d1a2008-11-13 22:59:24 +0000238 return FALSE; // not currently supported
239 }
Barry Mienydd671972010-10-04 16:33:58 +0200240
Derek Allard2067d1a2008-11-13 22:59:24 +0000241 return $sql;
242 }
Barry Mienydd671972010-10-04 16:33:58 +0200243
Derek Allard2067d1a2008-11-13 22:59:24 +0000244 // --------------------------------------------------------------------
245
246 /**
247 * Show column query
248 *
249 * Generates a platform-specific query string so that the column names can be fetched
250 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000251 * @param string the table name
252 * @return string
253 */
Andrey Andreevb76029d2012-01-26 15:13:19 +0200254 protected function _list_columns($table = '')
Derek Allard2067d1a2008-11-13 22:59:24 +0000255 {
Andrey Andreevb76029d2012-01-26 15:13:19 +0200256 return 'SHOW COLUMNS FROM '.$table;
Derek Allard2067d1a2008-11-13 22:59:24 +0000257 }
258
259 // --------------------------------------------------------------------
260
261 /**
262 * Field data query
263 *
264 * Generates a platform-specific query so that the column data can be retrieved
265 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000266 * @param string the table name
Andrey Andreevb76029d2012-01-26 15:13:19 +0200267 * @return string
Derek Allard2067d1a2008-11-13 22:59:24 +0000268 */
Andrey Andreevb76029d2012-01-26 15:13:19 +0200269 protected function _field_data($table)
Derek Allard2067d1a2008-11-13 22:59:24 +0000270 {
Andrey Andreevb76029d2012-01-26 15:13:19 +0200271 return 'SELECT TOP 1 FROM '.$table;
Derek Allard2067d1a2008-11-13 22:59:24 +0000272 }
273
274 // --------------------------------------------------------------------
275
276 /**
Andrey Andreev4be5de12012-03-02 15:45:41 +0200277 * Error
Derek Allard2067d1a2008-11-13 22:59:24 +0000278 *
Andrey Andreev4be5de12012-03-02 15:45:41 +0200279 * Returns an array containing code and message of the last
280 * database error that has occured.
Derek Allard2067d1a2008-11-13 22:59:24 +0000281 *
Andrey Andreev4be5de12012-03-02 15:45:41 +0200282 * @return array
Derek Allard2067d1a2008-11-13 22:59:24 +0000283 */
Andrey Andreev4be5de12012-03-02 15:45:41 +0200284 public function error()
Derek Allard2067d1a2008-11-13 22:59:24 +0000285 {
Andrey Andreev4be5de12012-03-02 15:45:41 +0200286 return array('code' => odbc_error($this->conn_id), 'message' => odbc_errormsg($this->conn_id));
Derek Allard2067d1a2008-11-13 22:59:24 +0000287 }
288
289 // --------------------------------------------------------------------
290
291 /**
Derek Allard2067d1a2008-11-13 22:59:24 +0000292 * From Tables
293 *
294 * This function implicitly groups FROM tables so there is no confusion
295 * about operator precedence in harmony with SQL standards
296 *
Andrey Andreev5f356bf2012-03-20 14:32:27 +0200297 * @param array
Andrey Andreevb76029d2012-01-26 15:13:19 +0200298 * @return string
Derek Allard2067d1a2008-11-13 22:59:24 +0000299 */
Andrey Andreevb76029d2012-01-26 15:13:19 +0200300 protected function _from_tables($tables)
Derek Allard2067d1a2008-11-13 22:59:24 +0000301 {
Andrey Andreevc78e56a2012-06-08 02:12:07 +0300302 return is_array($tables) ? implode(', ', $tables) : $tables;
Derek Allard2067d1a2008-11-13 22:59:24 +0000303 }
304
305 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200306
Derek Allard2067d1a2008-11-13 22:59:24 +0000307 /**
Derek Allard2067d1a2008-11-13 22:59:24 +0000308 * Truncate statement
309 *
310 * Generates a platform-specific truncate string from the supplied data
Andrey Andreeva6fe36e2012-04-05 16:00:32 +0300311 *
312 * If the database does not support the truncate() command,
313 * then this method maps to 'DELETE FROM table'
Derek Allard2067d1a2008-11-13 22:59:24 +0000314 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000315 * @param string the table name
316 * @return string
Barry Mienydd671972010-10-04 16:33:58 +0200317 */
Andrey Andreevb76029d2012-01-26 15:13:19 +0200318 protected function _truncate($table)
Derek Allard2067d1a2008-11-13 22:59:24 +0000319 {
Andrey Andreeva6fe36e2012-04-05 16:00:32 +0300320 return 'DELETE FROM '.$table;
Derek Allard2067d1a2008-11-13 22:59:24 +0000321 }
Barry Mienydd671972010-10-04 16:33:58 +0200322
Derek Allard2067d1a2008-11-13 22:59:24 +0000323 // --------------------------------------------------------------------
324
325 /**
Derek Allard2067d1a2008-11-13 22:59:24 +0000326 * Close DB Connection
327 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000328 * @return void
329 */
Andrey Andreev79922c02012-05-23 12:27:17 +0300330 protected function _close()
Derek Allard2067d1a2008-11-13 22:59:24 +0000331 {
Andrey Andreev79922c02012-05-23 12:27:17 +0300332 @odbc_close($this->conn_id);
Derek Allard2067d1a2008-11-13 22:59:24 +0000333 }
334
Derek Allard2067d1a2008-11-13 22:59:24 +0000335}
336
Derek Allard2067d1a2008-11-13 22:59:24 +0000337/* End of file odbc_driver.php */
Andrey Andreev79922c02012-05-23 12:27:17 +0300338/* Location: ./system/database/drivers/odbc/odbc_driver.php */