blob: 409284b44e5d06a3cd024048547a7445eb66d99f [file] [log] [blame]
Andrey Andreevc5536aa2012-11-01 17:33:58 +02001<?php
Derek Allard2067d1a2008-11-13 22:59:24 +00002/**
3 * CodeIgniter
4 *
Andrey Andreevfe9309d2015-01-09 17:48:58 +02005 * An open source application development framework for PHP
Derek Allard2067d1a2008-11-13 22:59:24 +00006 *
Andrey Andreevbdb96ca2014-10-28 00:13:31 +02007 * This content is released under the MIT License (MIT)
Andrey Andreevb76029d2012-01-26 15:13:19 +02008 *
Andrey Andreevfe9309d2015-01-09 17:48:58 +02009 * Copyright (c) 2014 - 2015, British Columbia Institute of Technology
Andrey Andreevb76029d2012-01-26 15:13:19 +020010 *
Andrey Andreevbdb96ca2014-10-28 00:13:31 +020011 * Permission is hereby granted, free of charge, to any person obtaining a copy
12 * of this software and associated documentation files (the "Software"), to deal
13 * in the Software without restriction, including without limitation the rights
14 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
15 * copies of the Software, and to permit persons to whom the Software is
16 * furnished to do so, subject to the following conditions:
Derek Jonesf4a4bd82011-10-20 12:18:42 -050017 *
Andrey Andreevbdb96ca2014-10-28 00:13:31 +020018 * The above copyright notice and this permission notice shall be included in
19 * all copies or substantial portions of the Software.
20 *
21 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
22 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
23 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
24 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
25 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
26 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
27 * THE SOFTWARE.
28 *
29 * @package CodeIgniter
30 * @author EllisLab Dev Team
darwinel871754a2014-02-11 17:34:57 +010031 * @copyright Copyright (c) 2008 - 2014, EllisLab, Inc. (http://ellislab.com/)
Andrey Andreevfe9309d2015-01-09 17:48:58 +020032 * @copyright Copyright (c) 2014 - 2015, British Columbia Institute of Technology (http://bcit.ca/)
Andrey Andreevbdb96ca2014-10-28 00:13:31 +020033 * @license http://opensource.org/licenses/MIT MIT License
34 * @link http://codeigniter.com
35 * @since Version 1.3.0
Derek Allard2067d1a2008-11-13 22:59:24 +000036 * @filesource
37 */
Andrey Andreevc5536aa2012-11-01 17:33:58 +020038defined('BASEPATH') OR exit('No direct script access allowed');
Derek Allard2067d1a2008-11-13 22:59:24 +000039
Derek Allard2067d1a2008-11-13 22:59:24 +000040/**
41 * ODBC Database Adapter Class
42 *
43 * Note: _DB is an extender class that the app controller
Jamie Rumbelow7efad202012-02-19 12:37:00 +000044 * creates dynamically based on whether the query builder
Derek Allard2067d1a2008-11-13 22:59:24 +000045 * class is being used or not.
46 *
47 * @package CodeIgniter
48 * @subpackage Drivers
49 * @category Database
Derek Jonesf4a4bd82011-10-20 12:18:42 -050050 * @author EllisLab Dev Team
Derek Allard2067d1a2008-11-13 22:59:24 +000051 * @link http://codeigniter.com/user_guide/database/
52 */
53class CI_DB_odbc_driver extends CI_DB {
54
Andrey Andreeva24e52e2012-11-02 03:54:12 +020055 /**
56 * Database driver
57 *
58 * @var string
59 */
Andrey Andreevb76029d2012-01-26 15:13:19 +020060 public $dbdriver = 'odbc';
Barry Mienydd671972010-10-04 16:33:58 +020061
Andrey Andreev5fd3ae82012-10-24 14:55:35 +030062 /**
Andrey Andreev3e9d2b82012-10-27 14:28:51 +030063 * Database schema
64 *
65 * @var string
Andrey Andreev485a3482012-10-27 03:22:43 +030066 */
67 public $schema = 'public';
68
Andrey Andreeva24e52e2012-11-02 03:54:12 +020069 // --------------------------------------------------------------------
70
Andrey Andreev485a3482012-10-27 03:22:43 +030071 /**
Andrey Andreeva24e52e2012-11-02 03:54:12 +020072 * Identifier escape character
73 *
74 * Must be empty for ODBC.
75 *
76 * @var string
77 */
78 protected $_escape_char = '';
79
80 /**
81 * ESCAPE statement string
82 *
83 * @var string
84 */
85 protected $_like_escape_str = " {escape '%s'} ";
86
Andrey Andreev98e46cf2012-11-13 03:01:42 +020087 /**
88 * ORDER BY random keyword
89 *
90 * @var array
91 */
92 protected $_random_keyword = array('RND()', 'RND(%d)');
93
Andrey Andreeva24e52e2012-11-02 03:54:12 +020094 // --------------------------------------------------------------------
95
96 /**
97 * Class constructor
Andrey Andreev5fd3ae82012-10-24 14:55:35 +030098 *
99 * @param array $params
100 * @return void
101 */
Andrey Andreev5f356bf2012-03-20 14:32:27 +0200102 public function __construct($params)
Derek Allard2067d1a2008-11-13 22:59:24 +0000103 {
Timothy Warrena2097a02011-10-10 10:10:46 -0400104 parent::__construct($params);
Barry Mienydd671972010-10-04 16:33:58 +0200105
Andrey Andreev46f6dbc2012-02-13 01:39:53 +0200106 // Legacy support for DSN in the hostname field
Andrey Andreeve4c30192012-06-04 15:08:24 +0300107 if (empty($this->dsn))
Andrey Andreev46f6dbc2012-02-13 01:39:53 +0200108 {
109 $this->dsn = $this->hostname;
110 }
Derek Allard2067d1a2008-11-13 22:59:24 +0000111 }
112
Andrey Andreev5fd3ae82012-10-24 14:55:35 +0300113 // --------------------------------------------------------------------
114
Derek Allard2067d1a2008-11-13 22:59:24 +0000115 /**
116 * Non-persistent database connection
117 *
Andrey Andreev2e171022014-02-25 15:21:41 +0200118 * @param bool $persistent
Derek Allard2067d1a2008-11-13 22:59:24 +0000119 * @return resource
Barry Mienydd671972010-10-04 16:33:58 +0200120 */
Andrey Andreev2e171022014-02-25 15:21:41 +0200121 public function db_connect($persistent = FALSE)
Derek Allard2067d1a2008-11-13 22:59:24 +0000122 {
Andrey Andreev2e171022014-02-25 15:21:41 +0200123 return ($persistent === TRUE)
124 ? odbc_pconnect($this->dsn, $this->username, $this->password)
125 : odbc_connect($this->dsn, $this->username, $this->password);
Derek Allard2067d1a2008-11-13 22:59:24 +0000126 }
Barry Mienydd671972010-10-04 16:33:58 +0200127
Derek Allard2067d1a2008-11-13 22:59:24 +0000128 // --------------------------------------------------------------------
129
130 /**
Derek Allard2067d1a2008-11-13 22:59:24 +0000131 * Execute the query
132 *
Andrey Andreeva24e52e2012-11-02 03:54:12 +0200133 * @param string $sql an SQL query
Derek Allard2067d1a2008-11-13 22:59:24 +0000134 * @return resource
Barry Mienydd671972010-10-04 16:33:58 +0200135 */
Andrey Andreevb76029d2012-01-26 15:13:19 +0200136 protected function _execute($sql)
Derek Allard2067d1a2008-11-13 22:59:24 +0000137 {
Andrey Andreev2bbbd1a2014-05-09 10:24:14 +0300138 return odbc_exec($this->conn_id, $sql);
Derek Allard2067d1a2008-11-13 22:59:24 +0000139 }
140
141 // --------------------------------------------------------------------
142
143 /**
144 * Begin Transaction
145 *
Barry Mienydd671972010-10-04 16:33:58 +0200146 * @return bool
147 */
Andrey Andreeva7d4aba2015-10-19 14:39:44 +0300148 protected function _trans_begin()
Derek Allard2067d1a2008-11-13 22:59:24 +0000149 {
Derek Allard2067d1a2008-11-13 22:59:24 +0000150 return odbc_autocommit($this->conn_id, FALSE);
151 }
152
153 // --------------------------------------------------------------------
154
155 /**
156 * Commit Transaction
157 *
Barry Mienydd671972010-10-04 16:33:58 +0200158 * @return bool
159 */
Andrey Andreeva7d4aba2015-10-19 14:39:44 +0300160 protected function _trans_commit()
Derek Allard2067d1a2008-11-13 22:59:24 +0000161 {
Andrey Andreeva7d4aba2015-10-19 14:39:44 +0300162 if (odbc_commit($this->conn_id))
Derek Allard2067d1a2008-11-13 22:59:24 +0000163 {
Andrey Andreeva7d4aba2015-10-19 14:39:44 +0300164 odbc_autocommit($this->conn_id, TRUE);
Derek Allard2067d1a2008-11-13 22:59:24 +0000165 return TRUE;
166 }
167
Andrey Andreeva7d4aba2015-10-19 14:39:44 +0300168 return FALSE;
Derek Allard2067d1a2008-11-13 22:59:24 +0000169 }
170
171 // --------------------------------------------------------------------
172
173 /**
174 * Rollback Transaction
175 *
Barry Mienydd671972010-10-04 16:33:58 +0200176 * @return bool
177 */
Andrey Andreeva7d4aba2015-10-19 14:39:44 +0300178 protected function _trans_rollback()
Derek Allard2067d1a2008-11-13 22:59:24 +0000179 {
Andrey Andreeva7d4aba2015-10-19 14:39:44 +0300180 if (odbc_rollback($this->conn_id))
Derek Allard2067d1a2008-11-13 22:59:24 +0000181 {
Andrey Andreeva7d4aba2015-10-19 14:39:44 +0300182 odbc_autocommit($this->conn_id, TRUE);
Derek Allard2067d1a2008-11-13 22:59:24 +0000183 return TRUE;
184 }
185
Andrey Andreeva7d4aba2015-10-19 14:39:44 +0300186 return FALSE;
Derek Allard2067d1a2008-11-13 22:59:24 +0000187 }
188
189 // --------------------------------------------------------------------
190
191 /**
Andrey Andreev0b6a4922013-01-10 16:53:44 +0200192 * Platform-dependant string escape
Derek Allard2067d1a2008-11-13 22:59:24 +0000193 *
Andrey Andreev0b6a4922013-01-10 16:53:44 +0200194 * @param string
Derek Allard2067d1a2008-11-13 22:59:24 +0000195 * @return string
196 */
Andrey Andreev0b6a4922013-01-10 16:53:44 +0200197 protected function _escape_str($str)
Derek Allard2067d1a2008-11-13 22:59:24 +0000198 {
Andrey Andreev0b6a4922013-01-10 16:53:44 +0200199 return remove_invisible_characters($str);
Derek Allard2067d1a2008-11-13 22:59:24 +0000200 }
Barry Mienydd671972010-10-04 16:33:58 +0200201
Derek Allard2067d1a2008-11-13 22:59:24 +0000202 // --------------------------------------------------------------------
203
204 /**
205 * Affected Rows
206 *
Andrey Andreevb76029d2012-01-26 15:13:19 +0200207 * @return int
Derek Allard2067d1a2008-11-13 22:59:24 +0000208 */
Andrey Andreevb76029d2012-01-26 15:13:19 +0200209 public function affected_rows()
Derek Allard2067d1a2008-11-13 22:59:24 +0000210 {
Andrey Andreev2bbbd1a2014-05-09 10:24:14 +0300211 return odbc_num_rows($this->result_id);
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 * Insert ID
218 *
Andrey Andreev8af76662012-03-05 14:33:41 +0200219 * @return bool
Derek Allard2067d1a2008-11-13 22:59:24 +0000220 */
Andrey Andreevb76029d2012-01-26 15:13:19 +0200221 public function insert_id()
Derek Allard2067d1a2008-11-13 22:59:24 +0000222 {
Andrey Andreev8d3afde2012-11-06 12:53:47 +0200223 return ($this->db->db_debug) ? $this->db->display_error('db_unsupported_feature') : FALSE;
Derek Allard2067d1a2008-11-13 22:59:24 +0000224 }
225
226 // --------------------------------------------------------------------
227
228 /**
Derek Allard2067d1a2008-11-13 22:59:24 +0000229 * Show table query
230 *
231 * Generates a platform-specific query string so that the table names can be fetched
232 *
Andrey Andreeva24e52e2012-11-02 03:54:12 +0200233 * @param bool $prefix_limit
Derek Allard2067d1a2008-11-13 22:59:24 +0000234 * @return string
235 */
Andrey Andreevb76029d2012-01-26 15:13:19 +0200236 protected function _list_tables($prefix_limit = FALSE)
Derek Allard2067d1a2008-11-13 22:59:24 +0000237 {
Andrey Andreev485a3482012-10-27 03:22:43 +0300238 $sql = "SELECT table_name FROM information_schema.tables WHERE table_schema = '".$this->schema."'";
Derek Allard2067d1a2008-11-13 22:59:24 +0000239
Andrey Andreeve4c30192012-06-04 15:08:24 +0300240 if ($prefix_limit !== FALSE && $this->dbprefix !== '')
Derek Allard2067d1a2008-11-13 22:59:24 +0000241 {
Andrey Andreev485a3482012-10-27 03:22:43 +0300242 return $sql." AND table_name LIKE '".$this->escape_like_str($this->dbprefix)."%' "
243 .sprintf($this->_like_escape_str, $this->_like_escape_chr);
Derek Allard2067d1a2008-11-13 22:59:24 +0000244 }
Barry Mienydd671972010-10-04 16:33:58 +0200245
Derek Allard2067d1a2008-11-13 22:59:24 +0000246 return $sql;
247 }
Barry Mienydd671972010-10-04 16:33:58 +0200248
Derek Allard2067d1a2008-11-13 22:59:24 +0000249 // --------------------------------------------------------------------
250
251 /**
252 * Show column query
253 *
254 * Generates a platform-specific query string so that the column names can be fetched
255 *
Andrey Andreeva24e52e2012-11-02 03:54:12 +0200256 * @param string $table
Derek Allard2067d1a2008-11-13 22:59:24 +0000257 * @return string
258 */
Andrey Andreevb76029d2012-01-26 15:13:19 +0200259 protected function _list_columns($table = '')
Derek Allard2067d1a2008-11-13 22:59:24 +0000260 {
Andrey Andreevb76029d2012-01-26 15:13:19 +0200261 return 'SHOW COLUMNS FROM '.$table;
Derek Allard2067d1a2008-11-13 22:59:24 +0000262 }
263
264 // --------------------------------------------------------------------
265
266 /**
267 * Field data query
268 *
269 * Generates a platform-specific query so that the column data can be retrieved
270 *
Andrey Andreeva24e52e2012-11-02 03:54:12 +0200271 * @param string $table
Andrey Andreevb76029d2012-01-26 15:13:19 +0200272 * @return string
Derek Allard2067d1a2008-11-13 22:59:24 +0000273 */
Andrey Andreevb76029d2012-01-26 15:13:19 +0200274 protected function _field_data($table)
Derek Allard2067d1a2008-11-13 22:59:24 +0000275 {
Andrey Andreevb76029d2012-01-26 15:13:19 +0200276 return 'SELECT TOP 1 FROM '.$table;
Derek Allard2067d1a2008-11-13 22:59:24 +0000277 }
278
279 // --------------------------------------------------------------------
280
281 /**
Andrey Andreev4be5de12012-03-02 15:45:41 +0200282 * Error
Derek Allard2067d1a2008-11-13 22:59:24 +0000283 *
Andrey Andreev4be5de12012-03-02 15:45:41 +0200284 * Returns an array containing code and message of the last
285 * database error that has occured.
Derek Allard2067d1a2008-11-13 22:59:24 +0000286 *
Andrey Andreev4be5de12012-03-02 15:45:41 +0200287 * @return array
Derek Allard2067d1a2008-11-13 22:59:24 +0000288 */
Andrey Andreev4be5de12012-03-02 15:45:41 +0200289 public function error()
Derek Allard2067d1a2008-11-13 22:59:24 +0000290 {
Andrey Andreev4be5de12012-03-02 15:45:41 +0200291 return array('code' => odbc_error($this->conn_id), 'message' => odbc_errormsg($this->conn_id));
Derek Allard2067d1a2008-11-13 22:59:24 +0000292 }
293
294 // --------------------------------------------------------------------
295
296 /**
Andrey Andreevb0478652012-07-18 15:34:46 +0300297 * Update statement
298 *
299 * Generates a platform-specific update string from the supplied data
300 *
Andrey Andreeva24e52e2012-11-02 03:54:12 +0200301 * @param string $table
302 * @param array $values
Andrey Andreevb0478652012-07-18 15:34:46 +0300303 * @return string
Andrey Andreev838a9d62012-12-03 14:37:47 +0200304 */
Andrey Andreevb0478652012-07-18 15:34:46 +0300305 protected function _update($table, $values)
306 {
307 $this->qb_limit = FALSE;
308 $this->qb_orderby = array();
309 return parent::_update($table, $values);
310 }
311
312 // --------------------------------------------------------------------
313
314 /**
Derek Allard2067d1a2008-11-13 22:59:24 +0000315 * Truncate statement
316 *
317 * Generates a platform-specific truncate string from the supplied data
Andrey Andreeva6fe36e2012-04-05 16:00:32 +0300318 *
Andrey Andreeva24e52e2012-11-02 03:54:12 +0200319 * If the database does not support the TRUNCATE statement,
Andrey Andreeva6fe36e2012-04-05 16:00:32 +0300320 * then this method maps to 'DELETE FROM table'
Derek Allard2067d1a2008-11-13 22:59:24 +0000321 *
Andrey Andreeva24e52e2012-11-02 03:54:12 +0200322 * @param string $table
Derek Allard2067d1a2008-11-13 22:59:24 +0000323 * @return string
Barry Mienydd671972010-10-04 16:33:58 +0200324 */
Andrey Andreevb76029d2012-01-26 15:13:19 +0200325 protected function _truncate($table)
Derek Allard2067d1a2008-11-13 22:59:24 +0000326 {
Andrey Andreeva6fe36e2012-04-05 16:00:32 +0300327 return 'DELETE FROM '.$table;
Derek Allard2067d1a2008-11-13 22:59:24 +0000328 }
Barry Mienydd671972010-10-04 16:33:58 +0200329
Derek Allard2067d1a2008-11-13 22:59:24 +0000330 // --------------------------------------------------------------------
331
332 /**
Andrey Andreevb0478652012-07-18 15:34:46 +0300333 * Delete statement
334 *
335 * Generates a platform-specific delete string from the supplied data
336 *
Andrey Andreeva24e52e2012-11-02 03:54:12 +0200337 * @param string $table
Andrey Andreevb0478652012-07-18 15:34:46 +0300338 * @return string
339 */
340 protected function _delete($table)
341 {
342 $this->qb_limit = FALSE;
343 return parent::_delete($table);
344 }
345
346 // --------------------------------------------------------------------
347
348 /**
Derek Allard2067d1a2008-11-13 22:59:24 +0000349 * Close DB Connection
350 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000351 * @return void
352 */
Andrey Andreev79922c02012-05-23 12:27:17 +0300353 protected function _close()
Derek Allard2067d1a2008-11-13 22:59:24 +0000354 {
Andrey Andreev2bbbd1a2014-05-09 10:24:14 +0300355 odbc_close($this->conn_id);
Derek Allard2067d1a2008-11-13 22:59:24 +0000356 }
357
Derek Allard2067d1a2008-11-13 22:59:24 +0000358}