blob: 2e890c46b982c337381e72a24f665444196b47b1 [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 Andreevb76029d2012-01-26 15:13:19 +020044 public $dbdriver = 'odbc';
Barry Mienydd671972010-10-04 16:33:58 +020045
Derek Allard2067d1a2008-11-13 22:59:24 +000046 // the character used to excape - not necessary for ODBC
Andrey Andreevb76029d2012-01-26 15:13:19 +020047 protected $_escape_char = '';
Barry Mienydd671972010-10-04 16:33:58 +020048
Andrey Andreev5f356bf2012-03-20 14:32:27 +020049 protected $_like_escape_str = " {escape '%s'} ";
Barry Mienydd671972010-10-04 16:33:58 +020050
Andrey Andreevb76029d2012-01-26 15:13:19 +020051 protected $_random_keyword;
Derek Allard2067d1a2008-11-13 22:59:24 +000052
Andrey Andreev5fd3ae82012-10-24 14:55:35 +030053 /**
Andrey Andreev3e9d2b82012-10-27 14:28:51 +030054 * Database schema
55 *
56 * @var string
Andrey Andreev485a3482012-10-27 03:22:43 +030057 */
58 public $schema = 'public';
59
60 /**
Andrey Andreev5fd3ae82012-10-24 14:55:35 +030061 * Constructor
62 *
63 * @param array $params
64 * @return void
65 */
Andrey Andreev5f356bf2012-03-20 14:32:27 +020066 public function __construct($params)
Derek Allard2067d1a2008-11-13 22:59:24 +000067 {
Timothy Warrena2097a02011-10-10 10:10:46 -040068 parent::__construct($params);
Barry Mienydd671972010-10-04 16:33:58 +020069
Derek Allard2067d1a2008-11-13 22:59:24 +000070 $this->_random_keyword = ' RND('.time().')'; // database specific random keyword
Andrey Andreev46f6dbc2012-02-13 01:39:53 +020071
72 // Legacy support for DSN in the hostname field
Andrey Andreeve4c30192012-06-04 15:08:24 +030073 if (empty($this->dsn))
Andrey Andreev46f6dbc2012-02-13 01:39:53 +020074 {
75 $this->dsn = $this->hostname;
76 }
Derek Allard2067d1a2008-11-13 22:59:24 +000077 }
78
Andrey Andreev5fd3ae82012-10-24 14:55:35 +030079 // --------------------------------------------------------------------
80
Derek Allard2067d1a2008-11-13 22:59:24 +000081 /**
82 * Non-persistent database connection
83 *
Derek Allard2067d1a2008-11-13 22:59:24 +000084 * @return resource
Barry Mienydd671972010-10-04 16:33:58 +020085 */
Andrey Andreevb76029d2012-01-26 15:13:19 +020086 public function db_connect()
Derek Allard2067d1a2008-11-13 22:59:24 +000087 {
Andrey Andreev46f6dbc2012-02-13 01:39:53 +020088 return @odbc_connect($this->dsn, $this->username, $this->password);
Derek Allard2067d1a2008-11-13 22:59:24 +000089 }
Barry Mienydd671972010-10-04 16:33:58 +020090
Derek Allard2067d1a2008-11-13 22:59:24 +000091 // --------------------------------------------------------------------
92
93 /**
94 * Persistent database connection
95 *
Derek Allard2067d1a2008-11-13 22:59:24 +000096 * @return resource
Barry Mienydd671972010-10-04 16:33:58 +020097 */
Andrey Andreevb76029d2012-01-26 15:13:19 +020098 public function db_pconnect()
Derek Allard2067d1a2008-11-13 22:59:24 +000099 {
Andrey Andreev46f6dbc2012-02-13 01:39:53 +0200100 return @odbc_pconnect($this->dsn, $this->username, $this->password);
Derek Allard2067d1a2008-11-13 22:59:24 +0000101 }
Barry Mienydd671972010-10-04 16:33:58 +0200102
Derek Allard2067d1a2008-11-13 22:59:24 +0000103 // --------------------------------------------------------------------
104
105 /**
Derek Allard2067d1a2008-11-13 22:59:24 +0000106 * Execute the query
107 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000108 * @param string an SQL query
109 * @return resource
Barry Mienydd671972010-10-04 16:33:58 +0200110 */
Andrey Andreevb76029d2012-01-26 15:13:19 +0200111 protected function _execute($sql)
Derek Allard2067d1a2008-11-13 22:59:24 +0000112 {
Derek Allard2067d1a2008-11-13 22:59:24 +0000113 return @odbc_exec($this->conn_id, $sql);
Derek Allard2067d1a2008-11-13 22:59:24 +0000114 }
115
116 // --------------------------------------------------------------------
117
118 /**
119 * Begin Transaction
120 *
Andrey Andreev5fd3ae82012-10-24 14:55:35 +0300121 * @param bool $test_mode = FALSE
Barry Mienydd671972010-10-04 16:33:58 +0200122 * @return bool
123 */
Andrey Andreevb76029d2012-01-26 15:13:19 +0200124 public function trans_begin($test_mode = FALSE)
Derek Allard2067d1a2008-11-13 22:59:24 +0000125 {
Derek Allard2067d1a2008-11-13 22:59:24 +0000126 // When transactions are nested we only begin/commit/rollback the outermost ones
Andrey Andreevb76029d2012-01-26 15:13:19 +0200127 if ( ! $this->trans_enabled OR $this->_trans_depth > 0)
Derek Allard2067d1a2008-11-13 22:59:24 +0000128 {
129 return TRUE;
130 }
131
132 // Reset the transaction failure flag.
133 // If the $test_mode flag is set to TRUE transactions will be rolled back
134 // even if the queries produce a successful result.
Andrey Andreevb76029d2012-01-26 15:13:19 +0200135 $this->_trans_failure = ($test_mode === TRUE);
Derek Allard2067d1a2008-11-13 22:59:24 +0000136
137 return odbc_autocommit($this->conn_id, FALSE);
138 }
139
140 // --------------------------------------------------------------------
141
142 /**
143 * Commit Transaction
144 *
Barry Mienydd671972010-10-04 16:33:58 +0200145 * @return bool
146 */
Andrey Andreevb76029d2012-01-26 15:13:19 +0200147 public function trans_commit()
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 $ret = odbc_commit($this->conn_id);
156 odbc_autocommit($this->conn_id, TRUE);
157 return $ret;
158 }
159
160 // --------------------------------------------------------------------
161
162 /**
163 * Rollback Transaction
164 *
Barry Mienydd671972010-10-04 16:33:58 +0200165 * @return bool
166 */
Andrey Andreevb76029d2012-01-26 15:13:19 +0200167 public function trans_rollback()
Derek Allard2067d1a2008-11-13 22:59:24 +0000168 {
Derek Allard2067d1a2008-11-13 22:59:24 +0000169 // When transactions are nested we only begin/commit/rollback the outermost ones
Andrey Andreevb76029d2012-01-26 15:13:19 +0200170 if ( ! $this->trans_enabled OR $this->_trans_depth > 0)
Derek Allard2067d1a2008-11-13 22:59:24 +0000171 {
172 return TRUE;
173 }
174
175 $ret = odbc_rollback($this->conn_id);
176 odbc_autocommit($this->conn_id, TRUE);
177 return $ret;
178 }
179
180 // --------------------------------------------------------------------
181
182 /**
183 * Escape String
184 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000185 * @param string
Derek Jonese4ed5832009-02-20 21:44:59 +0000186 * @param bool whether or not the string will be used in a LIKE condition
Derek Allard2067d1a2008-11-13 22:59:24 +0000187 * @return string
188 */
Andrey Andreevb76029d2012-01-26 15:13:19 +0200189 public function escape_str($str, $like = FALSE)
Derek Allard2067d1a2008-11-13 22:59:24 +0000190 {
Derek Jonese4ed5832009-02-20 21:44:59 +0000191 if (is_array($str))
192 {
Pascal Krietec3a4a8d2011-02-14 13:40:08 -0500193 foreach ($str as $key => $val)
Barry Mienydd671972010-10-04 16:33:58 +0200194 {
Derek Jonese4ed5832009-02-20 21:44:59 +0000195 $str[$key] = $this->escape_str($val, $like);
Barry Mienydd671972010-10-04 16:33:58 +0200196 }
197
198 return $str;
199 }
200
Greg Aker757dda62010-04-14 19:06:19 -0500201 $str = remove_invisible_characters($str);
Barry Mienydd671972010-10-04 16:33:58 +0200202
Derek Jonese4ed5832009-02-20 21:44:59 +0000203 // escape LIKE condition wildcards
204 if ($like === TRUE)
205 {
Andrey Andreev5e18e892012-03-09 14:25:00 +0200206 return str_replace(array($this->_like_escape_chr, '%', '_'),
207 array($this->_like_escape_chr.$this->_like_escape_chr, $this->_like_escape_chr.'%', $this->_like_escape_chr.'_'),
Andrey Andreevb76029d2012-01-26 15:13:19 +0200208 $str);
Derek Jonese4ed5832009-02-20 21:44:59 +0000209 }
Barry Mienydd671972010-10-04 16:33:58 +0200210
Derek Jonese4ed5832009-02-20 21:44:59 +0000211 return $str;
Derek Allard2067d1a2008-11-13 22:59:24 +0000212 }
Barry Mienydd671972010-10-04 16:33:58 +0200213
Derek Allard2067d1a2008-11-13 22:59:24 +0000214 // --------------------------------------------------------------------
215
216 /**
217 * Affected Rows
218 *
Andrey Andreevb76029d2012-01-26 15:13:19 +0200219 * @return int
Derek Allard2067d1a2008-11-13 22:59:24 +0000220 */
Andrey Andreevb76029d2012-01-26 15:13:19 +0200221 public function affected_rows()
Derek Allard2067d1a2008-11-13 22:59:24 +0000222 {
223 return @odbc_num_rows($this->conn_id);
224 }
Barry Mienydd671972010-10-04 16:33:58 +0200225
Derek Allard2067d1a2008-11-13 22:59:24 +0000226 // --------------------------------------------------------------------
227
228 /**
229 * Insert ID
230 *
Andrey Andreev8af76662012-03-05 14:33:41 +0200231 * @return bool
Derek Allard2067d1a2008-11-13 22:59:24 +0000232 */
Andrey Andreevb76029d2012-01-26 15:13:19 +0200233 public function insert_id()
Derek Allard2067d1a2008-11-13 22:59:24 +0000234 {
Andrey Andreev8af76662012-03-05 14:33:41 +0200235 return ($this->db->db_debug) ? $this->db->display_error('db_unsuported_feature') : FALSE;
Derek Allard2067d1a2008-11-13 22:59:24 +0000236 }
237
238 // --------------------------------------------------------------------
239
240 /**
Derek Allard2067d1a2008-11-13 22:59:24 +0000241 * Show table query
242 *
243 * Generates a platform-specific query string so that the table names can be fetched
244 *
Andrey Andreev485a3482012-10-27 03:22:43 +0300245 * @param bool $prefix_limit = FALSE
Derek Allard2067d1a2008-11-13 22:59:24 +0000246 * @return string
247 */
Andrey Andreevb76029d2012-01-26 15:13:19 +0200248 protected function _list_tables($prefix_limit = FALSE)
Derek Allard2067d1a2008-11-13 22:59:24 +0000249 {
Andrey Andreev485a3482012-10-27 03:22:43 +0300250 $sql = "SELECT table_name FROM information_schema.tables WHERE table_schema = '".$this->schema."'";
Derek Allard2067d1a2008-11-13 22:59:24 +0000251
Andrey Andreeve4c30192012-06-04 15:08:24 +0300252 if ($prefix_limit !== FALSE && $this->dbprefix !== '')
Derek Allard2067d1a2008-11-13 22:59:24 +0000253 {
Andrey Andreev485a3482012-10-27 03:22:43 +0300254 return $sql." AND table_name LIKE '".$this->escape_like_str($this->dbprefix)."%' "
255 .sprintf($this->_like_escape_str, $this->_like_escape_chr);
Derek Allard2067d1a2008-11-13 22:59:24 +0000256 }
Barry Mienydd671972010-10-04 16:33:58 +0200257
Derek Allard2067d1a2008-11-13 22:59:24 +0000258 return $sql;
259 }
Barry Mienydd671972010-10-04 16:33:58 +0200260
Derek Allard2067d1a2008-11-13 22:59:24 +0000261 // --------------------------------------------------------------------
262
263 /**
264 * Show column query
265 *
266 * Generates a platform-specific query string so that the column names can be fetched
267 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000268 * @param string the table name
269 * @return string
270 */
Andrey Andreevb76029d2012-01-26 15:13:19 +0200271 protected function _list_columns($table = '')
Derek Allard2067d1a2008-11-13 22:59:24 +0000272 {
Andrey Andreevb76029d2012-01-26 15:13:19 +0200273 return 'SHOW COLUMNS FROM '.$table;
Derek Allard2067d1a2008-11-13 22:59:24 +0000274 }
275
276 // --------------------------------------------------------------------
277
278 /**
279 * Field data query
280 *
281 * Generates a platform-specific query so that the column data can be retrieved
282 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000283 * @param string the table name
Andrey Andreevb76029d2012-01-26 15:13:19 +0200284 * @return string
Derek Allard2067d1a2008-11-13 22:59:24 +0000285 */
Andrey Andreevb76029d2012-01-26 15:13:19 +0200286 protected function _field_data($table)
Derek Allard2067d1a2008-11-13 22:59:24 +0000287 {
Andrey Andreevb76029d2012-01-26 15:13:19 +0200288 return 'SELECT TOP 1 FROM '.$table;
Derek Allard2067d1a2008-11-13 22:59:24 +0000289 }
290
291 // --------------------------------------------------------------------
292
293 /**
Andrey Andreev4be5de12012-03-02 15:45:41 +0200294 * Error
Derek Allard2067d1a2008-11-13 22:59:24 +0000295 *
Andrey Andreev4be5de12012-03-02 15:45:41 +0200296 * Returns an array containing code and message of the last
297 * database error that has occured.
Derek Allard2067d1a2008-11-13 22:59:24 +0000298 *
Andrey Andreev4be5de12012-03-02 15:45:41 +0200299 * @return array
Derek Allard2067d1a2008-11-13 22:59:24 +0000300 */
Andrey Andreev4be5de12012-03-02 15:45:41 +0200301 public function error()
Derek Allard2067d1a2008-11-13 22:59:24 +0000302 {
Andrey Andreev4be5de12012-03-02 15:45:41 +0200303 return array('code' => odbc_error($this->conn_id), 'message' => odbc_errormsg($this->conn_id));
Derek Allard2067d1a2008-11-13 22:59:24 +0000304 }
305
306 // --------------------------------------------------------------------
307
308 /**
Andrey Andreevb0478652012-07-18 15:34:46 +0300309 * Update statement
310 *
311 * Generates a platform-specific update string from the supplied data
312 *
313 * @param string the table name
314 * @param array the update data
315 * @return string
316 */
317 protected function _update($table, $values)
318 {
319 $this->qb_limit = FALSE;
320 $this->qb_orderby = array();
321 return parent::_update($table, $values);
322 }
323
324 // --------------------------------------------------------------------
325
326 /**
Derek Allard2067d1a2008-11-13 22:59:24 +0000327 * Truncate statement
328 *
329 * Generates a platform-specific truncate string from the supplied data
Andrey Andreeva6fe36e2012-04-05 16:00:32 +0300330 *
331 * If the database does not support the truncate() command,
332 * then this method maps to 'DELETE FROM table'
Derek Allard2067d1a2008-11-13 22:59:24 +0000333 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000334 * @param string the table name
335 * @return string
Barry Mienydd671972010-10-04 16:33:58 +0200336 */
Andrey Andreevb76029d2012-01-26 15:13:19 +0200337 protected function _truncate($table)
Derek Allard2067d1a2008-11-13 22:59:24 +0000338 {
Andrey Andreeva6fe36e2012-04-05 16:00:32 +0300339 return 'DELETE FROM '.$table;
Derek Allard2067d1a2008-11-13 22:59:24 +0000340 }
Barry Mienydd671972010-10-04 16:33:58 +0200341
Derek Allard2067d1a2008-11-13 22:59:24 +0000342 // --------------------------------------------------------------------
343
344 /**
Andrey Andreevb0478652012-07-18 15:34:46 +0300345 * Delete statement
346 *
347 * Generates a platform-specific delete string from the supplied data
348 *
349 * @param string the table name
350 * @return string
351 */
352 protected function _delete($table)
353 {
354 $this->qb_limit = FALSE;
355 return parent::_delete($table);
356 }
357
358 // --------------------------------------------------------------------
359
360 /**
Derek Allard2067d1a2008-11-13 22:59:24 +0000361 * Close DB Connection
362 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000363 * @return void
364 */
Andrey Andreev79922c02012-05-23 12:27:17 +0300365 protected function _close()
Derek Allard2067d1a2008-11-13 22:59:24 +0000366 {
Andrey Andreev79922c02012-05-23 12:27:17 +0300367 @odbc_close($this->conn_id);
Derek Allard2067d1a2008-11-13 22:59:24 +0000368 }
369
Derek Allard2067d1a2008-11-13 22:59:24 +0000370}
371
Derek Allard2067d1a2008-11-13 22:59:24 +0000372/* End of file odbc_driver.php */
Andrey Andreev79922c02012-05-23 12:27:17 +0300373/* Location: ./system/database/drivers/odbc/odbc_driver.php */