blob: 88df615e6db45354b5977ac5c177233c77c40caf [file] [log] [blame]
Andrey Andreevc5536aa2012-11-01 17:33:58 +02001<?php
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 */
Andrey Andreevc5536aa2012-11-01 17:33:58 +020027defined('BASEPATH') OR exit('No direct script access allowed');
Derek Allard2067d1a2008-11-13 22:59:24 +000028
Derek Allard2067d1a2008-11-13 22:59:24 +000029/**
30 * ODBC Database Adapter Class
31 *
32 * Note: _DB is an extender class that the app controller
Jamie Rumbelow7efad202012-02-19 12:37:00 +000033 * creates dynamically based on whether the query builder
Derek Allard2067d1a2008-11-13 22:59:24 +000034 * class is being used or not.
35 *
36 * @package CodeIgniter
37 * @subpackage Drivers
38 * @category Database
Derek Jonesf4a4bd82011-10-20 12:18:42 -050039 * @author EllisLab Dev Team
Derek Allard2067d1a2008-11-13 22:59:24 +000040 * @link http://codeigniter.com/user_guide/database/
41 */
42class CI_DB_odbc_driver extends CI_DB {
43
Andrey Andreeva24e52e2012-11-02 03:54:12 +020044 /**
45 * Database driver
46 *
47 * @var string
48 */
Andrey Andreevb76029d2012-01-26 15:13:19 +020049 public $dbdriver = 'odbc';
Barry Mienydd671972010-10-04 16:33:58 +020050
Andrey Andreev5fd3ae82012-10-24 14:55:35 +030051 /**
Andrey Andreev3e9d2b82012-10-27 14:28:51 +030052 * Database schema
53 *
54 * @var string
Andrey Andreev485a3482012-10-27 03:22:43 +030055 */
56 public $schema = 'public';
57
Andrey Andreeva24e52e2012-11-02 03:54:12 +020058 // --------------------------------------------------------------------
59
Andrey Andreev485a3482012-10-27 03:22:43 +030060 /**
Andrey Andreeva24e52e2012-11-02 03:54:12 +020061 * Identifier escape character
62 *
63 * Must be empty for ODBC.
64 *
65 * @var string
66 */
67 protected $_escape_char = '';
68
69 /**
70 * ESCAPE statement string
71 *
72 * @var string
73 */
74 protected $_like_escape_str = " {escape '%s'} ";
75
76 // --------------------------------------------------------------------
77
78 /**
79 * Class constructor
Andrey Andreev5fd3ae82012-10-24 14:55:35 +030080 *
81 * @param array $params
82 * @return void
83 */
Andrey Andreev5f356bf2012-03-20 14:32:27 +020084 public function __construct($params)
Derek Allard2067d1a2008-11-13 22:59:24 +000085 {
Timothy Warrena2097a02011-10-10 10:10:46 -040086 parent::__construct($params);
Barry Mienydd671972010-10-04 16:33:58 +020087
Derek Allard2067d1a2008-11-13 22:59:24 +000088 $this->_random_keyword = ' RND('.time().')'; // database specific random keyword
Andrey Andreev46f6dbc2012-02-13 01:39:53 +020089
90 // Legacy support for DSN in the hostname field
Andrey Andreeve4c30192012-06-04 15:08:24 +030091 if (empty($this->dsn))
Andrey Andreev46f6dbc2012-02-13 01:39:53 +020092 {
93 $this->dsn = $this->hostname;
94 }
Derek Allard2067d1a2008-11-13 22:59:24 +000095 }
96
Andrey Andreev5fd3ae82012-10-24 14:55:35 +030097 // --------------------------------------------------------------------
98
Derek Allard2067d1a2008-11-13 22:59:24 +000099 /**
100 * Non-persistent database connection
101 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000102 * @return resource
Barry Mienydd671972010-10-04 16:33:58 +0200103 */
Andrey Andreevb76029d2012-01-26 15:13:19 +0200104 public function db_connect()
Derek Allard2067d1a2008-11-13 22:59:24 +0000105 {
Andrey Andreev46f6dbc2012-02-13 01:39:53 +0200106 return @odbc_connect($this->dsn, $this->username, $this->password);
Derek Allard2067d1a2008-11-13 22:59:24 +0000107 }
Barry Mienydd671972010-10-04 16:33:58 +0200108
Derek Allard2067d1a2008-11-13 22:59:24 +0000109 // --------------------------------------------------------------------
110
111 /**
112 * Persistent database connection
113 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000114 * @return resource
Barry Mienydd671972010-10-04 16:33:58 +0200115 */
Andrey Andreevb76029d2012-01-26 15:13:19 +0200116 public function db_pconnect()
Derek Allard2067d1a2008-11-13 22:59:24 +0000117 {
Andrey Andreev46f6dbc2012-02-13 01:39:53 +0200118 return @odbc_pconnect($this->dsn, $this->username, $this->password);
Derek Allard2067d1a2008-11-13 22:59:24 +0000119 }
Barry Mienydd671972010-10-04 16:33:58 +0200120
Derek Allard2067d1a2008-11-13 22:59:24 +0000121 // --------------------------------------------------------------------
122
123 /**
Derek Allard2067d1a2008-11-13 22:59:24 +0000124 * Execute the query
125 *
Andrey Andreeva24e52e2012-11-02 03:54:12 +0200126 * @param string $sql an SQL query
Derek Allard2067d1a2008-11-13 22:59:24 +0000127 * @return resource
Barry Mienydd671972010-10-04 16:33:58 +0200128 */
Andrey Andreevb76029d2012-01-26 15:13:19 +0200129 protected function _execute($sql)
Derek Allard2067d1a2008-11-13 22:59:24 +0000130 {
Derek Allard2067d1a2008-11-13 22:59:24 +0000131 return @odbc_exec($this->conn_id, $sql);
Derek Allard2067d1a2008-11-13 22:59:24 +0000132 }
133
134 // --------------------------------------------------------------------
135
136 /**
137 * Begin Transaction
138 *
Andrey Andreeva24e52e2012-11-02 03:54:12 +0200139 * @param bool $test_mode
Barry Mienydd671972010-10-04 16:33:58 +0200140 * @return bool
141 */
Andrey Andreevb76029d2012-01-26 15:13:19 +0200142 public function trans_begin($test_mode = FALSE)
Derek Allard2067d1a2008-11-13 22:59:24 +0000143 {
Derek Allard2067d1a2008-11-13 22:59:24 +0000144 // When transactions are nested we only begin/commit/rollback the outermost ones
Andrey Andreevb76029d2012-01-26 15:13:19 +0200145 if ( ! $this->trans_enabled OR $this->_trans_depth > 0)
Derek Allard2067d1a2008-11-13 22:59:24 +0000146 {
147 return TRUE;
148 }
149
150 // Reset the transaction failure flag.
151 // If the $test_mode flag is set to TRUE transactions will be rolled back
152 // even if the queries produce a successful result.
Andrey Andreevb76029d2012-01-26 15:13:19 +0200153 $this->_trans_failure = ($test_mode === TRUE);
Derek Allard2067d1a2008-11-13 22:59:24 +0000154
155 return odbc_autocommit($this->conn_id, FALSE);
156 }
157
158 // --------------------------------------------------------------------
159
160 /**
161 * Commit Transaction
162 *
Barry Mienydd671972010-10-04 16:33:58 +0200163 * @return bool
164 */
Andrey Andreevb76029d2012-01-26 15:13:19 +0200165 public function trans_commit()
Derek Allard2067d1a2008-11-13 22:59:24 +0000166 {
Derek Allard2067d1a2008-11-13 22:59:24 +0000167 // When transactions are nested we only begin/commit/rollback the outermost ones
Andrey Andreevb76029d2012-01-26 15:13:19 +0200168 if ( ! $this->trans_enabled OR $this->_trans_depth > 0)
Derek Allard2067d1a2008-11-13 22:59:24 +0000169 {
170 return TRUE;
171 }
172
173 $ret = odbc_commit($this->conn_id);
174 odbc_autocommit($this->conn_id, TRUE);
175 return $ret;
176 }
177
178 // --------------------------------------------------------------------
179
180 /**
181 * Rollback Transaction
182 *
Barry Mienydd671972010-10-04 16:33:58 +0200183 * @return bool
184 */
Andrey Andreevb76029d2012-01-26 15:13:19 +0200185 public function trans_rollback()
Derek Allard2067d1a2008-11-13 22:59:24 +0000186 {
Derek Allard2067d1a2008-11-13 22:59:24 +0000187 // When transactions are nested we only begin/commit/rollback the outermost ones
Andrey Andreevb76029d2012-01-26 15:13:19 +0200188 if ( ! $this->trans_enabled OR $this->_trans_depth > 0)
Derek Allard2067d1a2008-11-13 22:59:24 +0000189 {
190 return TRUE;
191 }
192
193 $ret = odbc_rollback($this->conn_id);
194 odbc_autocommit($this->conn_id, TRUE);
195 return $ret;
196 }
197
198 // --------------------------------------------------------------------
199
200 /**
201 * Escape String
202 *
Andrey Andreeva24e52e2012-11-02 03:54:12 +0200203 * @param string $str
204 * @param bool $like Whether or not the string will be used in a LIKE condition
Derek Allard2067d1a2008-11-13 22:59:24 +0000205 * @return string
206 */
Andrey Andreevb76029d2012-01-26 15:13:19 +0200207 public function escape_str($str, $like = FALSE)
Derek Allard2067d1a2008-11-13 22:59:24 +0000208 {
Derek Jonese4ed5832009-02-20 21:44:59 +0000209 if (is_array($str))
210 {
Pascal Krietec3a4a8d2011-02-14 13:40:08 -0500211 foreach ($str as $key => $val)
Barry Mienydd671972010-10-04 16:33:58 +0200212 {
Derek Jonese4ed5832009-02-20 21:44:59 +0000213 $str[$key] = $this->escape_str($val, $like);
Barry Mienydd671972010-10-04 16:33:58 +0200214 }
215
216 return $str;
217 }
218
Greg Aker757dda62010-04-14 19:06:19 -0500219 $str = remove_invisible_characters($str);
Barry Mienydd671972010-10-04 16:33:58 +0200220
Derek Jonese4ed5832009-02-20 21:44:59 +0000221 // escape LIKE condition wildcards
222 if ($like === TRUE)
223 {
Andrey Andreev5e18e892012-03-09 14:25:00 +0200224 return str_replace(array($this->_like_escape_chr, '%', '_'),
225 array($this->_like_escape_chr.$this->_like_escape_chr, $this->_like_escape_chr.'%', $this->_like_escape_chr.'_'),
Andrey Andreevb76029d2012-01-26 15:13:19 +0200226 $str);
Derek Jonese4ed5832009-02-20 21:44:59 +0000227 }
Barry Mienydd671972010-10-04 16:33:58 +0200228
Derek Jonese4ed5832009-02-20 21:44:59 +0000229 return $str;
Derek Allard2067d1a2008-11-13 22:59:24 +0000230 }
Barry Mienydd671972010-10-04 16:33:58 +0200231
Derek Allard2067d1a2008-11-13 22:59:24 +0000232 // --------------------------------------------------------------------
233
234 /**
235 * Affected Rows
236 *
Andrey Andreevb76029d2012-01-26 15:13:19 +0200237 * @return int
Derek Allard2067d1a2008-11-13 22:59:24 +0000238 */
Andrey Andreevb76029d2012-01-26 15:13:19 +0200239 public function affected_rows()
Derek Allard2067d1a2008-11-13 22:59:24 +0000240 {
241 return @odbc_num_rows($this->conn_id);
242 }
Barry Mienydd671972010-10-04 16:33:58 +0200243
Derek Allard2067d1a2008-11-13 22:59:24 +0000244 // --------------------------------------------------------------------
245
246 /**
247 * Insert ID
248 *
Andrey Andreev8af76662012-03-05 14:33:41 +0200249 * @return bool
Derek Allard2067d1a2008-11-13 22:59:24 +0000250 */
Andrey Andreevb76029d2012-01-26 15:13:19 +0200251 public function insert_id()
Derek Allard2067d1a2008-11-13 22:59:24 +0000252 {
Andrey Andreev8af76662012-03-05 14:33:41 +0200253 return ($this->db->db_debug) ? $this->db->display_error('db_unsuported_feature') : FALSE;
Derek Allard2067d1a2008-11-13 22:59:24 +0000254 }
255
256 // --------------------------------------------------------------------
257
258 /**
Derek Allard2067d1a2008-11-13 22:59:24 +0000259 * Show table query
260 *
261 * Generates a platform-specific query string so that the table names can be fetched
262 *
Andrey Andreeva24e52e2012-11-02 03:54:12 +0200263 * @param bool $prefix_limit
Derek Allard2067d1a2008-11-13 22:59:24 +0000264 * @return string
265 */
Andrey Andreevb76029d2012-01-26 15:13:19 +0200266 protected function _list_tables($prefix_limit = FALSE)
Derek Allard2067d1a2008-11-13 22:59:24 +0000267 {
Andrey Andreev485a3482012-10-27 03:22:43 +0300268 $sql = "SELECT table_name FROM information_schema.tables WHERE table_schema = '".$this->schema."'";
Derek Allard2067d1a2008-11-13 22:59:24 +0000269
Andrey Andreeve4c30192012-06-04 15:08:24 +0300270 if ($prefix_limit !== FALSE && $this->dbprefix !== '')
Derek Allard2067d1a2008-11-13 22:59:24 +0000271 {
Andrey Andreev485a3482012-10-27 03:22:43 +0300272 return $sql." AND table_name LIKE '".$this->escape_like_str($this->dbprefix)."%' "
273 .sprintf($this->_like_escape_str, $this->_like_escape_chr);
Derek Allard2067d1a2008-11-13 22:59:24 +0000274 }
Barry Mienydd671972010-10-04 16:33:58 +0200275
Derek Allard2067d1a2008-11-13 22:59:24 +0000276 return $sql;
277 }
Barry Mienydd671972010-10-04 16:33:58 +0200278
Derek Allard2067d1a2008-11-13 22:59:24 +0000279 // --------------------------------------------------------------------
280
281 /**
282 * Show column query
283 *
284 * Generates a platform-specific query string so that the column names can be fetched
285 *
Andrey Andreeva24e52e2012-11-02 03:54:12 +0200286 * @param string $table
Derek Allard2067d1a2008-11-13 22:59:24 +0000287 * @return string
288 */
Andrey Andreevb76029d2012-01-26 15:13:19 +0200289 protected function _list_columns($table = '')
Derek Allard2067d1a2008-11-13 22:59:24 +0000290 {
Andrey Andreevb76029d2012-01-26 15:13:19 +0200291 return 'SHOW COLUMNS FROM '.$table;
Derek Allard2067d1a2008-11-13 22:59:24 +0000292 }
293
294 // --------------------------------------------------------------------
295
296 /**
297 * Field data query
298 *
299 * Generates a platform-specific query so that the column data can be retrieved
300 *
Andrey Andreeva24e52e2012-11-02 03:54:12 +0200301 * @param string $table
Andrey Andreevb76029d2012-01-26 15:13:19 +0200302 * @return string
Derek Allard2067d1a2008-11-13 22:59:24 +0000303 */
Andrey Andreevb76029d2012-01-26 15:13:19 +0200304 protected function _field_data($table)
Derek Allard2067d1a2008-11-13 22:59:24 +0000305 {
Andrey Andreevb76029d2012-01-26 15:13:19 +0200306 return 'SELECT TOP 1 FROM '.$table;
Derek Allard2067d1a2008-11-13 22:59:24 +0000307 }
308
309 // --------------------------------------------------------------------
310
311 /**
Andrey Andreev4be5de12012-03-02 15:45:41 +0200312 * Error
Derek Allard2067d1a2008-11-13 22:59:24 +0000313 *
Andrey Andreev4be5de12012-03-02 15:45:41 +0200314 * Returns an array containing code and message of the last
315 * database error that has occured.
Derek Allard2067d1a2008-11-13 22:59:24 +0000316 *
Andrey Andreev4be5de12012-03-02 15:45:41 +0200317 * @return array
Derek Allard2067d1a2008-11-13 22:59:24 +0000318 */
Andrey Andreev4be5de12012-03-02 15:45:41 +0200319 public function error()
Derek Allard2067d1a2008-11-13 22:59:24 +0000320 {
Andrey Andreev4be5de12012-03-02 15:45:41 +0200321 return array('code' => odbc_error($this->conn_id), 'message' => odbc_errormsg($this->conn_id));
Derek Allard2067d1a2008-11-13 22:59:24 +0000322 }
323
324 // --------------------------------------------------------------------
325
326 /**
Andrey Andreevb0478652012-07-18 15:34:46 +0300327 * Update statement
328 *
329 * Generates a platform-specific update string from the supplied data
330 *
Andrey Andreeva24e52e2012-11-02 03:54:12 +0200331 * @param string $table
332 * @param array $values
Andrey Andreevb0478652012-07-18 15:34:46 +0300333 * @return string
334 */
335 protected function _update($table, $values)
336 {
337 $this->qb_limit = FALSE;
338 $this->qb_orderby = array();
339 return parent::_update($table, $values);
340 }
341
342 // --------------------------------------------------------------------
343
344 /**
Derek Allard2067d1a2008-11-13 22:59:24 +0000345 * Truncate statement
346 *
347 * Generates a platform-specific truncate string from the supplied data
Andrey Andreeva6fe36e2012-04-05 16:00:32 +0300348 *
Andrey Andreeva24e52e2012-11-02 03:54:12 +0200349 * If the database does not support the TRUNCATE statement,
Andrey Andreeva6fe36e2012-04-05 16:00:32 +0300350 * then this method maps to 'DELETE FROM table'
Derek Allard2067d1a2008-11-13 22:59:24 +0000351 *
Andrey Andreeva24e52e2012-11-02 03:54:12 +0200352 * @param string $table
Derek Allard2067d1a2008-11-13 22:59:24 +0000353 * @return string
Barry Mienydd671972010-10-04 16:33:58 +0200354 */
Andrey Andreevb76029d2012-01-26 15:13:19 +0200355 protected function _truncate($table)
Derek Allard2067d1a2008-11-13 22:59:24 +0000356 {
Andrey Andreeva6fe36e2012-04-05 16:00:32 +0300357 return 'DELETE FROM '.$table;
Derek Allard2067d1a2008-11-13 22:59:24 +0000358 }
Barry Mienydd671972010-10-04 16:33:58 +0200359
Derek Allard2067d1a2008-11-13 22:59:24 +0000360 // --------------------------------------------------------------------
361
362 /**
Andrey Andreevb0478652012-07-18 15:34:46 +0300363 * Delete statement
364 *
365 * Generates a platform-specific delete string from the supplied data
366 *
Andrey Andreeva24e52e2012-11-02 03:54:12 +0200367 * @param string $table
Andrey Andreevb0478652012-07-18 15:34:46 +0300368 * @return string
369 */
370 protected function _delete($table)
371 {
372 $this->qb_limit = FALSE;
373 return parent::_delete($table);
374 }
375
376 // --------------------------------------------------------------------
377
378 /**
Derek Allard2067d1a2008-11-13 22:59:24 +0000379 * Close DB Connection
380 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000381 * @return void
382 */
Andrey Andreev79922c02012-05-23 12:27:17 +0300383 protected function _close()
Derek Allard2067d1a2008-11-13 22:59:24 +0000384 {
Andrey Andreev79922c02012-05-23 12:27:17 +0300385 @odbc_close($this->conn_id);
Derek Allard2067d1a2008-11-13 22:59:24 +0000386 }
387
Derek Allard2067d1a2008-11-13 22:59:24 +0000388}
389
Derek Allard2067d1a2008-11-13 22:59:24 +0000390/* End of file odbc_driver.php */
Andrey Andreev79922c02012-05-23 12:27:17 +0300391/* Location: ./system/database/drivers/odbc/odbc_driver.php */