blob: a01a9d68153aedc620fe25005cde3e56d4b94f83 [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
Andrey Andreev98e46cf2012-11-13 03:01:42 +020076 /**
77 * ORDER BY random keyword
78 *
79 * @var array
80 */
81 protected $_random_keyword = array('RND()', 'RND(%d)');
82
Andrey Andreeva24e52e2012-11-02 03:54:12 +020083 // --------------------------------------------------------------------
84
85 /**
86 * Class constructor
Andrey Andreev5fd3ae82012-10-24 14:55:35 +030087 *
88 * @param array $params
89 * @return void
90 */
Andrey Andreev5f356bf2012-03-20 14:32:27 +020091 public function __construct($params)
Derek Allard2067d1a2008-11-13 22:59:24 +000092 {
Timothy Warrena2097a02011-10-10 10:10:46 -040093 parent::__construct($params);
Barry Mienydd671972010-10-04 16:33:58 +020094
Andrey Andreev46f6dbc2012-02-13 01:39:53 +020095 // Legacy support for DSN in the hostname field
Andrey Andreeve4c30192012-06-04 15:08:24 +030096 if (empty($this->dsn))
Andrey Andreev46f6dbc2012-02-13 01:39:53 +020097 {
98 $this->dsn = $this->hostname;
99 }
Derek Allard2067d1a2008-11-13 22:59:24 +0000100 }
101
Andrey Andreev5fd3ae82012-10-24 14:55:35 +0300102 // --------------------------------------------------------------------
103
Derek Allard2067d1a2008-11-13 22:59:24 +0000104 /**
105 * Non-persistent database connection
106 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000107 * @return resource
Barry Mienydd671972010-10-04 16:33:58 +0200108 */
Andrey Andreevb76029d2012-01-26 15:13:19 +0200109 public function db_connect()
Derek Allard2067d1a2008-11-13 22:59:24 +0000110 {
Andrey Andreev46f6dbc2012-02-13 01:39:53 +0200111 return @odbc_connect($this->dsn, $this->username, $this->password);
Derek Allard2067d1a2008-11-13 22:59:24 +0000112 }
Barry Mienydd671972010-10-04 16:33:58 +0200113
Derek Allard2067d1a2008-11-13 22:59:24 +0000114 // --------------------------------------------------------------------
115
116 /**
117 * Persistent database connection
118 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000119 * @return resource
Barry Mienydd671972010-10-04 16:33:58 +0200120 */
Andrey Andreevb76029d2012-01-26 15:13:19 +0200121 public function db_pconnect()
Derek Allard2067d1a2008-11-13 22:59:24 +0000122 {
Andrey Andreev46f6dbc2012-02-13 01:39:53 +0200123 return @odbc_pconnect($this->dsn, $this->username, $this->password);
Derek Allard2067d1a2008-11-13 22:59:24 +0000124 }
Barry Mienydd671972010-10-04 16:33:58 +0200125
Derek Allard2067d1a2008-11-13 22:59:24 +0000126 // --------------------------------------------------------------------
127
128 /**
Derek Allard2067d1a2008-11-13 22:59:24 +0000129 * Execute the query
130 *
Andrey Andreeva24e52e2012-11-02 03:54:12 +0200131 * @param string $sql an SQL query
Derek Allard2067d1a2008-11-13 22:59:24 +0000132 * @return resource
Barry Mienydd671972010-10-04 16:33:58 +0200133 */
Andrey Andreevb76029d2012-01-26 15:13:19 +0200134 protected function _execute($sql)
Derek Allard2067d1a2008-11-13 22:59:24 +0000135 {
Derek Allard2067d1a2008-11-13 22:59:24 +0000136 return @odbc_exec($this->conn_id, $sql);
Derek Allard2067d1a2008-11-13 22:59:24 +0000137 }
138
139 // --------------------------------------------------------------------
140
141 /**
142 * Begin Transaction
143 *
Andrey Andreeva24e52e2012-11-02 03:54:12 +0200144 * @param bool $test_mode
Barry Mienydd671972010-10-04 16:33:58 +0200145 * @return bool
146 */
Andrey Andreevb76029d2012-01-26 15:13:19 +0200147 public function trans_begin($test_mode = FALSE)
Derek Allard2067d1a2008-11-13 22:59:24 +0000148 {
Derek Allard2067d1a2008-11-13 22:59:24 +0000149 // When transactions are nested we only begin/commit/rollback the outermost ones
Andrey Andreevb76029d2012-01-26 15:13:19 +0200150 if ( ! $this->trans_enabled OR $this->_trans_depth > 0)
Derek Allard2067d1a2008-11-13 22:59:24 +0000151 {
152 return TRUE;
153 }
154
155 // Reset the transaction failure flag.
156 // If the $test_mode flag is set to TRUE transactions will be rolled back
157 // even if the queries produce a successful result.
Andrey Andreevb76029d2012-01-26 15:13:19 +0200158 $this->_trans_failure = ($test_mode === TRUE);
Derek Allard2067d1a2008-11-13 22:59:24 +0000159
160 return odbc_autocommit($this->conn_id, FALSE);
161 }
162
163 // --------------------------------------------------------------------
164
165 /**
166 * Commit Transaction
167 *
Barry Mienydd671972010-10-04 16:33:58 +0200168 * @return bool
169 */
Andrey Andreevb76029d2012-01-26 15:13:19 +0200170 public function trans_commit()
Derek Allard2067d1a2008-11-13 22:59:24 +0000171 {
Derek Allard2067d1a2008-11-13 22:59:24 +0000172 // When transactions are nested we only begin/commit/rollback the outermost ones
Andrey Andreevb76029d2012-01-26 15:13:19 +0200173 if ( ! $this->trans_enabled OR $this->_trans_depth > 0)
Derek Allard2067d1a2008-11-13 22:59:24 +0000174 {
175 return TRUE;
176 }
177
178 $ret = odbc_commit($this->conn_id);
179 odbc_autocommit($this->conn_id, TRUE);
180 return $ret;
181 }
182
183 // --------------------------------------------------------------------
184
185 /**
186 * Rollback Transaction
187 *
Barry Mienydd671972010-10-04 16:33:58 +0200188 * @return bool
189 */
Andrey Andreevb76029d2012-01-26 15:13:19 +0200190 public function trans_rollback()
Derek Allard2067d1a2008-11-13 22:59:24 +0000191 {
Derek Allard2067d1a2008-11-13 22:59:24 +0000192 // When transactions are nested we only begin/commit/rollback the outermost ones
Andrey Andreevb76029d2012-01-26 15:13:19 +0200193 if ( ! $this->trans_enabled OR $this->_trans_depth > 0)
Derek Allard2067d1a2008-11-13 22:59:24 +0000194 {
195 return TRUE;
196 }
197
198 $ret = odbc_rollback($this->conn_id);
199 odbc_autocommit($this->conn_id, TRUE);
200 return $ret;
201 }
202
203 // --------------------------------------------------------------------
204
205 /**
206 * Escape String
207 *
Andrey Andreeva24e52e2012-11-02 03:54:12 +0200208 * @param string $str
209 * @param bool $like Whether or not the string will be used in a LIKE condition
Derek Allard2067d1a2008-11-13 22:59:24 +0000210 * @return string
211 */
Andrey Andreevb76029d2012-01-26 15:13:19 +0200212 public function escape_str($str, $like = FALSE)
Derek Allard2067d1a2008-11-13 22:59:24 +0000213 {
Derek Jonese4ed5832009-02-20 21:44:59 +0000214 if (is_array($str))
215 {
Pascal Krietec3a4a8d2011-02-14 13:40:08 -0500216 foreach ($str as $key => $val)
Barry Mienydd671972010-10-04 16:33:58 +0200217 {
Derek Jonese4ed5832009-02-20 21:44:59 +0000218 $str[$key] = $this->escape_str($val, $like);
Barry Mienydd671972010-10-04 16:33:58 +0200219 }
220
221 return $str;
222 }
223
Greg Aker757dda62010-04-14 19:06:19 -0500224 $str = remove_invisible_characters($str);
Barry Mienydd671972010-10-04 16:33:58 +0200225
Derek Jonese4ed5832009-02-20 21:44:59 +0000226 // escape LIKE condition wildcards
227 if ($like === TRUE)
228 {
Andrey Andreev5e18e892012-03-09 14:25:00 +0200229 return str_replace(array($this->_like_escape_chr, '%', '_'),
230 array($this->_like_escape_chr.$this->_like_escape_chr, $this->_like_escape_chr.'%', $this->_like_escape_chr.'_'),
Andrey Andreevb76029d2012-01-26 15:13:19 +0200231 $str);
Derek Jonese4ed5832009-02-20 21:44:59 +0000232 }
Barry Mienydd671972010-10-04 16:33:58 +0200233
Derek Jonese4ed5832009-02-20 21:44:59 +0000234 return $str;
Derek Allard2067d1a2008-11-13 22:59:24 +0000235 }
Barry Mienydd671972010-10-04 16:33:58 +0200236
Derek Allard2067d1a2008-11-13 22:59:24 +0000237 // --------------------------------------------------------------------
238
239 /**
240 * Affected Rows
241 *
Andrey Andreevb76029d2012-01-26 15:13:19 +0200242 * @return int
Derek Allard2067d1a2008-11-13 22:59:24 +0000243 */
Andrey Andreevb76029d2012-01-26 15:13:19 +0200244 public function affected_rows()
Derek Allard2067d1a2008-11-13 22:59:24 +0000245 {
246 return @odbc_num_rows($this->conn_id);
247 }
Barry Mienydd671972010-10-04 16:33:58 +0200248
Derek Allard2067d1a2008-11-13 22:59:24 +0000249 // --------------------------------------------------------------------
250
251 /**
252 * Insert ID
253 *
Andrey Andreev8af76662012-03-05 14:33:41 +0200254 * @return bool
Derek Allard2067d1a2008-11-13 22:59:24 +0000255 */
Andrey Andreevb76029d2012-01-26 15:13:19 +0200256 public function insert_id()
Derek Allard2067d1a2008-11-13 22:59:24 +0000257 {
Andrey Andreev8d3afde2012-11-06 12:53:47 +0200258 return ($this->db->db_debug) ? $this->db->display_error('db_unsupported_feature') : FALSE;
Derek Allard2067d1a2008-11-13 22:59:24 +0000259 }
260
261 // --------------------------------------------------------------------
262
263 /**
Derek Allard2067d1a2008-11-13 22:59:24 +0000264 * Show table query
265 *
266 * Generates a platform-specific query string so that the table names can be fetched
267 *
Andrey Andreeva24e52e2012-11-02 03:54:12 +0200268 * @param bool $prefix_limit
Derek Allard2067d1a2008-11-13 22:59:24 +0000269 * @return string
270 */
Andrey Andreevb76029d2012-01-26 15:13:19 +0200271 protected function _list_tables($prefix_limit = FALSE)
Derek Allard2067d1a2008-11-13 22:59:24 +0000272 {
Andrey Andreev485a3482012-10-27 03:22:43 +0300273 $sql = "SELECT table_name FROM information_schema.tables WHERE table_schema = '".$this->schema."'";
Derek Allard2067d1a2008-11-13 22:59:24 +0000274
Andrey Andreeve4c30192012-06-04 15:08:24 +0300275 if ($prefix_limit !== FALSE && $this->dbprefix !== '')
Derek Allard2067d1a2008-11-13 22:59:24 +0000276 {
Andrey Andreev485a3482012-10-27 03:22:43 +0300277 return $sql." AND table_name LIKE '".$this->escape_like_str($this->dbprefix)."%' "
278 .sprintf($this->_like_escape_str, $this->_like_escape_chr);
Derek Allard2067d1a2008-11-13 22:59:24 +0000279 }
Barry Mienydd671972010-10-04 16:33:58 +0200280
Derek Allard2067d1a2008-11-13 22:59:24 +0000281 return $sql;
282 }
Barry Mienydd671972010-10-04 16:33:58 +0200283
Derek Allard2067d1a2008-11-13 22:59:24 +0000284 // --------------------------------------------------------------------
285
286 /**
287 * Show column query
288 *
289 * Generates a platform-specific query string so that the column names can be fetched
290 *
Andrey Andreeva24e52e2012-11-02 03:54:12 +0200291 * @param string $table
Derek Allard2067d1a2008-11-13 22:59:24 +0000292 * @return string
293 */
Andrey Andreevb76029d2012-01-26 15:13:19 +0200294 protected function _list_columns($table = '')
Derek Allard2067d1a2008-11-13 22:59:24 +0000295 {
Andrey Andreevb76029d2012-01-26 15:13:19 +0200296 return 'SHOW COLUMNS FROM '.$table;
Derek Allard2067d1a2008-11-13 22:59:24 +0000297 }
298
299 // --------------------------------------------------------------------
300
301 /**
302 * Field data query
303 *
304 * Generates a platform-specific query so that the column data can be retrieved
305 *
Andrey Andreeva24e52e2012-11-02 03:54:12 +0200306 * @param string $table
Andrey Andreevb76029d2012-01-26 15:13:19 +0200307 * @return string
Derek Allard2067d1a2008-11-13 22:59:24 +0000308 */
Andrey Andreevb76029d2012-01-26 15:13:19 +0200309 protected function _field_data($table)
Derek Allard2067d1a2008-11-13 22:59:24 +0000310 {
Andrey Andreevb76029d2012-01-26 15:13:19 +0200311 return 'SELECT TOP 1 FROM '.$table;
Derek Allard2067d1a2008-11-13 22:59:24 +0000312 }
313
314 // --------------------------------------------------------------------
315
316 /**
Andrey Andreev4be5de12012-03-02 15:45:41 +0200317 * Error
Derek Allard2067d1a2008-11-13 22:59:24 +0000318 *
Andrey Andreev4be5de12012-03-02 15:45:41 +0200319 * Returns an array containing code and message of the last
320 * database error that has occured.
Derek Allard2067d1a2008-11-13 22:59:24 +0000321 *
Andrey Andreev4be5de12012-03-02 15:45:41 +0200322 * @return array
Derek Allard2067d1a2008-11-13 22:59:24 +0000323 */
Andrey Andreev4be5de12012-03-02 15:45:41 +0200324 public function error()
Derek Allard2067d1a2008-11-13 22:59:24 +0000325 {
Andrey Andreev4be5de12012-03-02 15:45:41 +0200326 return array('code' => odbc_error($this->conn_id), 'message' => odbc_errormsg($this->conn_id));
Derek Allard2067d1a2008-11-13 22:59:24 +0000327 }
328
329 // --------------------------------------------------------------------
330
331 /**
Andrey Andreevb0478652012-07-18 15:34:46 +0300332 * Update statement
333 *
334 * Generates a platform-specific update string from the supplied data
335 *
Andrey Andreeva24e52e2012-11-02 03:54:12 +0200336 * @param string $table
337 * @param array $values
Andrey Andreevb0478652012-07-18 15:34:46 +0300338 * @return string
339 */
340 protected function _update($table, $values)
341 {
342 $this->qb_limit = FALSE;
343 $this->qb_orderby = array();
344 return parent::_update($table, $values);
345 }
346
347 // --------------------------------------------------------------------
348
349 /**
Derek Allard2067d1a2008-11-13 22:59:24 +0000350 * Truncate statement
351 *
352 * Generates a platform-specific truncate string from the supplied data
Andrey Andreeva6fe36e2012-04-05 16:00:32 +0300353 *
Andrey Andreeva24e52e2012-11-02 03:54:12 +0200354 * If the database does not support the TRUNCATE statement,
Andrey Andreeva6fe36e2012-04-05 16:00:32 +0300355 * then this method maps to 'DELETE FROM table'
Derek Allard2067d1a2008-11-13 22:59:24 +0000356 *
Andrey Andreeva24e52e2012-11-02 03:54:12 +0200357 * @param string $table
Derek Allard2067d1a2008-11-13 22:59:24 +0000358 * @return string
Barry Mienydd671972010-10-04 16:33:58 +0200359 */
Andrey Andreevb76029d2012-01-26 15:13:19 +0200360 protected function _truncate($table)
Derek Allard2067d1a2008-11-13 22:59:24 +0000361 {
Andrey Andreeva6fe36e2012-04-05 16:00:32 +0300362 return 'DELETE FROM '.$table;
Derek Allard2067d1a2008-11-13 22:59:24 +0000363 }
Barry Mienydd671972010-10-04 16:33:58 +0200364
Derek Allard2067d1a2008-11-13 22:59:24 +0000365 // --------------------------------------------------------------------
366
367 /**
Andrey Andreevb0478652012-07-18 15:34:46 +0300368 * Delete statement
369 *
370 * Generates a platform-specific delete string from the supplied data
371 *
Andrey Andreeva24e52e2012-11-02 03:54:12 +0200372 * @param string $table
Andrey Andreevb0478652012-07-18 15:34:46 +0300373 * @return string
374 */
375 protected function _delete($table)
376 {
377 $this->qb_limit = FALSE;
378 return parent::_delete($table);
379 }
380
381 // --------------------------------------------------------------------
382
383 /**
Derek Allard2067d1a2008-11-13 22:59:24 +0000384 * Close DB Connection
385 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000386 * @return void
387 */
Andrey Andreev79922c02012-05-23 12:27:17 +0300388 protected function _close()
Derek Allard2067d1a2008-11-13 22:59:24 +0000389 {
Andrey Andreev79922c02012-05-23 12:27:17 +0300390 @odbc_close($this->conn_id);
Derek Allard2067d1a2008-11-13 22:59:24 +0000391 }
392
Derek Allard2067d1a2008-11-13 22:59:24 +0000393}
394
Derek Allard2067d1a2008-11-13 22:59:24 +0000395/* End of file odbc_driver.php */
Andrey Andreev79922c02012-05-23 12:27:17 +0300396/* Location: ./system/database/drivers/odbc/odbc_driver.php */