blob: e36f2d233452f1d1fac43b256327212ce7fe8d70 [file] [log] [blame]
Andrey Andreev5f356bf2012-03-20 14:32:27 +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 Andreev5f356bf2012-03-20 14:32:27 +02008 *
Derek Jonesf4a4bd82011-10-20 12:18:42 -05009 * Licensed under the Open Software License version 3.0
Andrey Andreev5f356bf2012-03-20 14:32:27 +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 Andreev5f356bf2012-03-20 14:32:27 +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 Andreev5f356bf2012-03-20 14:32:27 +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'} ";
50 protected $_like_escape_chr = '!';
Barry Mienydd671972010-10-04 16:33:58 +020051
Derek Allard2067d1a2008-11-13 22:59:24 +000052 /**
53 * The syntax to count rows is slightly different across different
54 * database engines, so this string appears in each driver and is
55 * used for the count_all() and count_all_results() functions.
56 */
Andrey Andreev5f356bf2012-03-20 14:32:27 +020057 protected $_count_string = 'SELECT COUNT(*) AS ';
58 protected $_random_keyword;
Derek Allard2067d1a2008-11-13 22:59:24 +000059
Andrey Andreev5f356bf2012-03-20 14:32:27 +020060 public function __construct($params)
Derek Allard2067d1a2008-11-13 22:59:24 +000061 {
Timothy Warrena2097a02011-10-10 10:10:46 -040062 parent::__construct($params);
Barry Mienydd671972010-10-04 16:33:58 +020063
Derek Allard2067d1a2008-11-13 22:59:24 +000064 $this->_random_keyword = ' RND('.time().')'; // database specific random keyword
Andrey Andreev9a1fc202012-03-26 12:38:34 +030065
66 // Legacy support for DSN in the hostname field
67 if ($this->dsn == '')
68 {
69 $this->dsn = $this->hostname;
70 }
Derek Allard2067d1a2008-11-13 22:59:24 +000071 }
72
73 /**
74 * Non-persistent database connection
75 *
Derek Allard2067d1a2008-11-13 22:59:24 +000076 * @return resource
Barry Mienydd671972010-10-04 16:33:58 +020077 */
Andrey Andreev5f356bf2012-03-20 14:32:27 +020078 public function db_connect()
Derek Allard2067d1a2008-11-13 22:59:24 +000079 {
Andrey Andreev9a1fc202012-03-26 12:38:34 +030080 return @odbc_connect($this->dsn, $this->username, $this->password);
Derek Allard2067d1a2008-11-13 22:59:24 +000081 }
Barry Mienydd671972010-10-04 16:33:58 +020082
Derek Allard2067d1a2008-11-13 22:59:24 +000083 // --------------------------------------------------------------------
84
85 /**
86 * Persistent database connection
87 *
Derek Allard2067d1a2008-11-13 22:59:24 +000088 * @return resource
Barry Mienydd671972010-10-04 16:33:58 +020089 */
Andrey Andreev5f356bf2012-03-20 14:32:27 +020090 public function db_pconnect()
Derek Allard2067d1a2008-11-13 22:59:24 +000091 {
Andrey Andreev9a1fc202012-03-26 12:38:34 +030092 return @odbc_pconnect($this->dsn, $this->username, $this->password);
Derek Allard2067d1a2008-11-13 22:59:24 +000093 }
94
95 // --------------------------------------------------------------------
96
97 /**
Derek Allard2067d1a2008-11-13 22:59:24 +000098 * Execute the query
99 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000100 * @param string an SQL query
101 * @return resource
Barry Mienydd671972010-10-04 16:33:58 +0200102 */
Andrey Andreev5f356bf2012-03-20 14:32:27 +0200103 protected function _execute($sql)
Derek Allard2067d1a2008-11-13 22:59:24 +0000104 {
Derek Allard2067d1a2008-11-13 22:59:24 +0000105 return @odbc_exec($this->conn_id, $sql);
106 }
Barry Mienydd671972010-10-04 16:33:58 +0200107
Derek Allard2067d1a2008-11-13 22:59:24 +0000108 // --------------------------------------------------------------------
109
110 /**
Derek Allard2067d1a2008-11-13 22:59:24 +0000111 * Begin Transaction
112 *
Barry Mienydd671972010-10-04 16:33:58 +0200113 * @return bool
114 */
Andrey Andreev5f356bf2012-03-20 14:32:27 +0200115 public function trans_begin($test_mode = FALSE)
Derek Allard2067d1a2008-11-13 22:59:24 +0000116 {
117 if ( ! $this->trans_enabled)
118 {
119 return TRUE;
120 }
Barry Mienydd671972010-10-04 16:33:58 +0200121
Derek Allard2067d1a2008-11-13 22:59:24 +0000122 // When transactions are nested we only begin/commit/rollback the outermost ones
123 if ($this->_trans_depth > 0)
124 {
125 return TRUE;
126 }
127
128 // Reset the transaction failure flag.
129 // If the $test_mode flag is set to TRUE transactions will be rolled back
130 // even if the queries produce a successful result.
131 $this->_trans_failure = ($test_mode === TRUE) ? TRUE : FALSE;
132
133 return odbc_autocommit($this->conn_id, FALSE);
134 }
135
136 // --------------------------------------------------------------------
137
138 /**
139 * Commit Transaction
140 *
Barry Mienydd671972010-10-04 16:33:58 +0200141 * @return bool
142 */
Andrey Andreev5f356bf2012-03-20 14:32:27 +0200143 public function trans_commit()
Derek Allard2067d1a2008-11-13 22:59:24 +0000144 {
145 if ( ! $this->trans_enabled)
146 {
147 return TRUE;
148 }
149
150 // When transactions are nested we only begin/commit/rollback the outermost ones
151 if ($this->_trans_depth > 0)
152 {
153 return TRUE;
154 }
155
156 $ret = odbc_commit($this->conn_id);
157 odbc_autocommit($this->conn_id, TRUE);
158 return $ret;
159 }
160
161 // --------------------------------------------------------------------
162
163 /**
164 * Rollback Transaction
165 *
Barry Mienydd671972010-10-04 16:33:58 +0200166 * @return bool
167 */
Andrey Andreev5f356bf2012-03-20 14:32:27 +0200168 public function trans_rollback()
Derek Allard2067d1a2008-11-13 22:59:24 +0000169 {
170 if ( ! $this->trans_enabled)
171 {
172 return TRUE;
173 }
174
175 // When transactions are nested we only begin/commit/rollback the outermost ones
176 if ($this->_trans_depth > 0)
177 {
178 return TRUE;
179 }
180
181 $ret = odbc_rollback($this->conn_id);
182 odbc_autocommit($this->conn_id, TRUE);
183 return $ret;
184 }
185
186 // --------------------------------------------------------------------
187
188 /**
189 * Escape String
190 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000191 * @param string
Derek Jonese4ed5832009-02-20 21:44:59 +0000192 * @param bool whether or not the string will be used in a LIKE condition
Derek Allard2067d1a2008-11-13 22:59:24 +0000193 * @return string
194 */
Andrey Andreev5f356bf2012-03-20 14:32:27 +0200195 public function escape_str($str, $like = FALSE)
Derek Allard2067d1a2008-11-13 22:59:24 +0000196 {
Derek Jonese4ed5832009-02-20 21:44:59 +0000197 if (is_array($str))
198 {
Pascal Krietec3a4a8d2011-02-14 13:40:08 -0500199 foreach ($str as $key => $val)
Barry Mienydd671972010-10-04 16:33:58 +0200200 {
Derek Jonese4ed5832009-02-20 21:44:59 +0000201 $str[$key] = $this->escape_str($val, $like);
Barry Mienydd671972010-10-04 16:33:58 +0200202 }
203
204 return $str;
205 }
206
Derek Allard2067d1a2008-11-13 22:59:24 +0000207 // ODBC doesn't require escaping
Greg Aker757dda62010-04-14 19:06:19 -0500208 $str = remove_invisible_characters($str);
Barry Mienydd671972010-10-04 16:33:58 +0200209
Derek Jonese4ed5832009-02-20 21:44:59 +0000210 // escape LIKE condition wildcards
211 if ($like === TRUE)
212 {
Andrey Andreev6ea625e2012-03-20 14:46:27 +0200213 return str_replace(array($this->_like_escape_chr, '%', '_'),
Andrey Andreevf1bda682012-03-20 14:35:06 +0200214 array($this->_like_escape_chr.$this->_like_escape_chr, $this->_like_escape_chr.'%', $this->_like_escape_chr.'_'),
215 $str);
Derek Jonese4ed5832009-02-20 21:44:59 +0000216 }
Barry Mienydd671972010-10-04 16:33:58 +0200217
Derek Jonese4ed5832009-02-20 21:44:59 +0000218 return $str;
Derek Allard2067d1a2008-11-13 22:59:24 +0000219 }
Barry Mienydd671972010-10-04 16:33:58 +0200220
Derek Allard2067d1a2008-11-13 22:59:24 +0000221 // --------------------------------------------------------------------
222
223 /**
224 * Affected Rows
225 *
Andrey Andreev5f356bf2012-03-20 14:32:27 +0200226 * @return int
Derek Allard2067d1a2008-11-13 22:59:24 +0000227 */
Andrey Andreev5f356bf2012-03-20 14:32:27 +0200228 public function affected_rows()
Derek Allard2067d1a2008-11-13 22:59:24 +0000229 {
230 return @odbc_num_rows($this->conn_id);
231 }
Barry Mienydd671972010-10-04 16:33:58 +0200232
Derek Allard2067d1a2008-11-13 22:59:24 +0000233 // --------------------------------------------------------------------
234
235 /**
236 * Insert ID
237 *
Andrey Andreev8af76662012-03-05 14:33:41 +0200238 * @return bool
Derek Allard2067d1a2008-11-13 22:59:24 +0000239 */
Andrey Andreev8af76662012-03-05 14:33:41 +0200240 public function insert_id()
Derek Allard2067d1a2008-11-13 22:59:24 +0000241 {
Andrey Andreev8af76662012-03-05 14:33:41 +0200242 return ($this->db->db_debug) ? $this->db->display_error('db_unsuported_feature') : FALSE;
Derek Allard2067d1a2008-11-13 22:59:24 +0000243 }
244
245 // --------------------------------------------------------------------
246
247 /**
248 * "Count All" query
249 *
250 * Generates a platform-specific query string that counts all records in
251 * the specified database
252 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000253 * @param string
254 * @return string
255 */
Andrey Andreev5f356bf2012-03-20 14:32:27 +0200256 public function count_all($table = '')
Derek Allard2067d1a2008-11-13 22:59:24 +0000257 {
258 if ($table == '')
Derek Allarde37ab382009-02-03 16:13:57 +0000259 {
260 return 0;
261 }
262
Andrey Andreev032e7ea2012-03-06 19:48:35 +0200263 $query = $this->query($this->_count_string . $this->protect_identifiers('numrows') . " FROM " . $this->protect_identifiers($table, TRUE, NULL, FALSE));
Derek Allarde37ab382009-02-03 16:13:57 +0000264
Derek Allard2067d1a2008-11-13 22:59:24 +0000265 if ($query->num_rows() == 0)
Derek Allarde37ab382009-02-03 16:13:57 +0000266 {
267 return 0;
268 }
Derek Allard2067d1a2008-11-13 22:59:24 +0000269
270 $row = $query->row();
Greg Aker90248ab2011-08-20 14:23:14 -0500271 $this->_reset_select();
Derek Allarde37ab382009-02-03 16:13:57 +0000272 return (int) $row->numrows;
Derek Allard2067d1a2008-11-13 22:59:24 +0000273 }
274
275 // --------------------------------------------------------------------
276
277 /**
278 * Show table query
279 *
280 * Generates a platform-specific query string so that the table names can be fetched
281 *
Andrey Andreev5f356bf2012-03-20 14:32:27 +0200282 * @param bool
Derek Allard2067d1a2008-11-13 22:59:24 +0000283 * @return string
284 */
Andrey Andreev5f356bf2012-03-20 14:32:27 +0200285 protected function _list_tables($prefix_limit = FALSE)
Derek Allard2067d1a2008-11-13 22:59:24 +0000286 {
287 $sql = "SHOW TABLES FROM `".$this->database."`";
288
289 if ($prefix_limit !== FALSE AND $this->dbprefix != '')
290 {
Greg Aker0d424892010-01-26 02:14:44 +0000291 //$sql .= " LIKE '".$this->escape_like_str($this->dbprefix)."%' ".sprintf($this->_like_escape_str, $this->_like_escape_chr);
Derek Allard2067d1a2008-11-13 22:59:24 +0000292 return FALSE; // not currently supported
293 }
Barry Mienydd671972010-10-04 16:33:58 +0200294
Derek Allard2067d1a2008-11-13 22:59:24 +0000295 return $sql;
296 }
Barry Mienydd671972010-10-04 16:33:58 +0200297
Derek Allard2067d1a2008-11-13 22:59:24 +0000298 // --------------------------------------------------------------------
299
300 /**
301 * Show column query
302 *
303 * Generates a platform-specific query string so that the column names can be fetched
304 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000305 * @param string the table name
306 * @return string
307 */
Andrey Andreev5f356bf2012-03-20 14:32:27 +0200308 protected function _list_columns($table = '')
Derek Allard2067d1a2008-11-13 22:59:24 +0000309 {
310 return "SHOW COLUMNS FROM ".$table;
311 }
312
313 // --------------------------------------------------------------------
314
315 /**
316 * Field data query
317 *
318 * Generates a platform-specific query so that the column data can be retrieved
319 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000320 * @param string the table name
Andrey Andreev5f356bf2012-03-20 14:32:27 +0200321 * @return string
Derek Allard2067d1a2008-11-13 22:59:24 +0000322 */
Andrey Andreev5f356bf2012-03-20 14:32:27 +0200323 protected function _field_data($table)
Derek Allard2067d1a2008-11-13 22:59:24 +0000324 {
325 return "SELECT TOP 1 FROM ".$table;
326 }
327
328 // --------------------------------------------------------------------
329
330 /**
Andrey Andreev4be5de12012-03-02 15:45:41 +0200331 * Error
Derek Allard2067d1a2008-11-13 22:59:24 +0000332 *
Andrey Andreev4be5de12012-03-02 15:45:41 +0200333 * Returns an array containing code and message of the last
334 * database error that has occured.
Derek Allard2067d1a2008-11-13 22:59:24 +0000335 *
Andrey Andreev4be5de12012-03-02 15:45:41 +0200336 * @return array
Derek Allard2067d1a2008-11-13 22:59:24 +0000337 */
Andrey Andreev4be5de12012-03-02 15:45:41 +0200338 public function error()
Derek Allard2067d1a2008-11-13 22:59:24 +0000339 {
Andrey Andreev4be5de12012-03-02 15:45:41 +0200340 return array('code' => odbc_error($this->conn_id), 'message' => odbc_errormsg($this->conn_id));
Derek Allard2067d1a2008-11-13 22:59:24 +0000341 }
342
343 // --------------------------------------------------------------------
344
345 /**
Derek Allard2067d1a2008-11-13 22:59:24 +0000346 * From Tables
347 *
348 * This function implicitly groups FROM tables so there is no confusion
349 * about operator precedence in harmony with SQL standards
350 *
Andrey Andreev5f356bf2012-03-20 14:32:27 +0200351 * @param array
352 * @return string
Derek Allard2067d1a2008-11-13 22:59:24 +0000353 */
Andrey Andreev5f356bf2012-03-20 14:32:27 +0200354 protected function _from_tables($tables)
Derek Allard2067d1a2008-11-13 22:59:24 +0000355 {
356 if ( ! is_array($tables))
357 {
358 $tables = array($tables);
359 }
Barry Mienydd671972010-10-04 16:33:58 +0200360
Derek Allard2067d1a2008-11-13 22:59:24 +0000361 return '('.implode(', ', $tables).')';
362 }
363
364 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200365
Derek Allard2067d1a2008-11-13 22:59:24 +0000366 /**
Derek Allard2067d1a2008-11-13 22:59:24 +0000367 * Truncate statement
368 *
369 * Generates a platform-specific truncate string from the supplied data
Derek Allard2067d1a2008-11-13 22:59:24 +0000370 *
Andrey Andreeva6fe36e2012-04-05 16:00:32 +0300371 * If the database does not support the truncate() command,
372 * then this method maps to 'DELETE FROM table'
Derek Allard2067d1a2008-11-13 22:59:24 +0000373 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000374 * @param string the table name
375 * @return string
Barry Mienydd671972010-10-04 16:33:58 +0200376 */
Andrey Andreev5f356bf2012-03-20 14:32:27 +0200377 protected function _truncate($table)
Derek Allard2067d1a2008-11-13 22:59:24 +0000378 {
Andrey Andreeva6fe36e2012-04-05 16:00:32 +0300379 return 'DELETE FROM '.$table;
Derek Allard2067d1a2008-11-13 22:59:24 +0000380 }
381
382 // --------------------------------------------------------------------
383
384 /**
385 * Limit string
386 *
387 * Generates a platform-specific LIMIT clause
388 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000389 * @param string the sql query string
Andrey Andreev5f356bf2012-03-20 14:32:27 +0200390 * @param int the number of rows to limit the query to
391 * @param int the offset value
Derek Allard2067d1a2008-11-13 22:59:24 +0000392 * @return string
393 */
Andrey Andreev5f356bf2012-03-20 14:32:27 +0200394 protected function _limit($sql, $limit, $offset)
Derek Allard2067d1a2008-11-13 22:59:24 +0000395 {
396 // Does ODBC doesn't use the LIMIT clause?
397 return $sql;
398 }
399
400 // --------------------------------------------------------------------
401
402 /**
403 * Close DB Connection
404 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000405 * @param resource
406 * @return void
407 */
Andrey Andreev5f356bf2012-03-20 14:32:27 +0200408 protected function _close($conn_id)
Derek Allard2067d1a2008-11-13 22:59:24 +0000409 {
410 @odbc_close($conn_id);
411 }
412
Derek Allard2067d1a2008-11-13 22:59:24 +0000413}
414
Derek Allard2067d1a2008-11-13 22:59:24 +0000415/* End of file odbc_driver.php */
Andrey Andreev063f5962012-02-27 12:20:52 +0200416/* Location: ./system/database/drivers/odbc/odbc_driver.php */