blob: 19b7b744bf97e1c942917d04b8c5b034c4d8adca [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 Andreev125ef472016-01-11 12:33:00 +02009 * Copyright (c) 2014 - 2016, 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
Andrey Andreev1924e872016-01-11 12:55:34 +020031 * @copyright Copyright (c) 2008 - 2014, EllisLab, Inc. (https://ellislab.com/)
Andrey Andreev125ef472016-01-11 12:33:00 +020032 * @copyright Copyright (c) 2014 - 2016, British Columbia Institute of Technology (http://bcit.ca/)
Andrey Andreevbdb96ca2014-10-28 00:13:31 +020033 * @license http://opensource.org/licenses/MIT MIT License
Andrey Andreevbd202c92016-01-11 12:50:18 +020034 * @link https://codeigniter.com
Andrey Andreevbdb96ca2014-10-28 00:13:31 +020035 * @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
Andrey Andreevbd202c92016-01-11 12:50:18 +020051 * @link https://codeigniter.com/user_guide/database/
Derek Allard2067d1a2008-11-13 22:59:24 +000052 */
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 Andreev0ca9ae62016-01-06 14:51:27 +0200192 * Determines if a query is a "write" type.
193 *
194 * @param string An SQL query string
195 * @return bool
196 */
197 public function is_write_type($sql)
198 {
199 if (preg_match('#^(INSERT|UPDATE).*RETURNING\s.+(\,\s?.+)*$#i', $sql))
200 {
201 return FALSE;
202 }
203
204 return parent::is_write_type($sql);
205 }
206
207 // --------------------------------------------------------------------
208
209 /**
Andrey Andreev0b6a4922013-01-10 16:53:44 +0200210 * Platform-dependant string escape
Derek Allard2067d1a2008-11-13 22:59:24 +0000211 *
Andrey Andreev0b6a4922013-01-10 16:53:44 +0200212 * @param string
Derek Allard2067d1a2008-11-13 22:59:24 +0000213 * @return string
214 */
Andrey Andreev0b6a4922013-01-10 16:53:44 +0200215 protected function _escape_str($str)
Derek Allard2067d1a2008-11-13 22:59:24 +0000216 {
Andrey Andreev0b6a4922013-01-10 16:53:44 +0200217 return remove_invisible_characters($str);
Derek Allard2067d1a2008-11-13 22:59:24 +0000218 }
Barry Mienydd671972010-10-04 16:33:58 +0200219
Derek Allard2067d1a2008-11-13 22:59:24 +0000220 // --------------------------------------------------------------------
221
222 /**
223 * Affected Rows
224 *
Andrey Andreevb76029d2012-01-26 15:13:19 +0200225 * @return int
Derek Allard2067d1a2008-11-13 22:59:24 +0000226 */
Andrey Andreevb76029d2012-01-26 15:13:19 +0200227 public function affected_rows()
Derek Allard2067d1a2008-11-13 22:59:24 +0000228 {
Andrey Andreev2bbbd1a2014-05-09 10:24:14 +0300229 return odbc_num_rows($this->result_id);
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 * Insert ID
236 *
Andrey Andreev8af76662012-03-05 14:33:41 +0200237 * @return bool
Derek Allard2067d1a2008-11-13 22:59:24 +0000238 */
Andrey Andreevb76029d2012-01-26 15:13:19 +0200239 public function insert_id()
Derek Allard2067d1a2008-11-13 22:59:24 +0000240 {
Andrey Andreev8d3afde2012-11-06 12:53:47 +0200241 return ($this->db->db_debug) ? $this->db->display_error('db_unsupported_feature') : FALSE;
Derek Allard2067d1a2008-11-13 22:59:24 +0000242 }
243
244 // --------------------------------------------------------------------
245
246 /**
Derek Allard2067d1a2008-11-13 22:59:24 +0000247 * Show table query
248 *
249 * Generates a platform-specific query string so that the table names can be fetched
250 *
Andrey Andreeva24e52e2012-11-02 03:54:12 +0200251 * @param bool $prefix_limit
Derek Allard2067d1a2008-11-13 22:59:24 +0000252 * @return string
253 */
Andrey Andreevb76029d2012-01-26 15:13:19 +0200254 protected function _list_tables($prefix_limit = FALSE)
Derek Allard2067d1a2008-11-13 22:59:24 +0000255 {
Andrey Andreev485a3482012-10-27 03:22:43 +0300256 $sql = "SELECT table_name FROM information_schema.tables WHERE table_schema = '".$this->schema."'";
Derek Allard2067d1a2008-11-13 22:59:24 +0000257
Andrey Andreeve4c30192012-06-04 15:08:24 +0300258 if ($prefix_limit !== FALSE && $this->dbprefix !== '')
Derek Allard2067d1a2008-11-13 22:59:24 +0000259 {
Andrey Andreev485a3482012-10-27 03:22:43 +0300260 return $sql." AND table_name LIKE '".$this->escape_like_str($this->dbprefix)."%' "
261 .sprintf($this->_like_escape_str, $this->_like_escape_chr);
Derek Allard2067d1a2008-11-13 22:59:24 +0000262 }
Barry Mienydd671972010-10-04 16:33:58 +0200263
Derek Allard2067d1a2008-11-13 22:59:24 +0000264 return $sql;
265 }
Barry Mienydd671972010-10-04 16:33:58 +0200266
Derek Allard2067d1a2008-11-13 22:59:24 +0000267 // --------------------------------------------------------------------
268
269 /**
270 * Show column query
271 *
272 * Generates a platform-specific query string so that the column names can be fetched
273 *
Andrey Andreeva24e52e2012-11-02 03:54:12 +0200274 * @param string $table
Derek Allard2067d1a2008-11-13 22:59:24 +0000275 * @return string
276 */
Andrey Andreevb76029d2012-01-26 15:13:19 +0200277 protected function _list_columns($table = '')
Derek Allard2067d1a2008-11-13 22:59:24 +0000278 {
Andrey Andreevb76029d2012-01-26 15:13:19 +0200279 return 'SHOW COLUMNS FROM '.$table;
Derek Allard2067d1a2008-11-13 22:59:24 +0000280 }
281
282 // --------------------------------------------------------------------
283
284 /**
285 * Field data query
286 *
287 * Generates a platform-specific query so that the column data can be retrieved
288 *
Andrey Andreeva24e52e2012-11-02 03:54:12 +0200289 * @param string $table
Andrey Andreevb76029d2012-01-26 15:13:19 +0200290 * @return string
Derek Allard2067d1a2008-11-13 22:59:24 +0000291 */
Andrey Andreevb76029d2012-01-26 15:13:19 +0200292 protected function _field_data($table)
Derek Allard2067d1a2008-11-13 22:59:24 +0000293 {
Andrey Andreevb76029d2012-01-26 15:13:19 +0200294 return 'SELECT TOP 1 FROM '.$table;
Derek Allard2067d1a2008-11-13 22:59:24 +0000295 }
296
297 // --------------------------------------------------------------------
298
299 /**
Andrey Andreev4be5de12012-03-02 15:45:41 +0200300 * Error
Derek Allard2067d1a2008-11-13 22:59:24 +0000301 *
Andrey Andreev4be5de12012-03-02 15:45:41 +0200302 * Returns an array containing code and message of the last
303 * database error that has occured.
Derek Allard2067d1a2008-11-13 22:59:24 +0000304 *
Andrey Andreev4be5de12012-03-02 15:45:41 +0200305 * @return array
Derek Allard2067d1a2008-11-13 22:59:24 +0000306 */
Andrey Andreev4be5de12012-03-02 15:45:41 +0200307 public function error()
Derek Allard2067d1a2008-11-13 22:59:24 +0000308 {
Andrey Andreev4be5de12012-03-02 15:45:41 +0200309 return array('code' => odbc_error($this->conn_id), 'message' => odbc_errormsg($this->conn_id));
Derek Allard2067d1a2008-11-13 22:59:24 +0000310 }
311
312 // --------------------------------------------------------------------
313
314 /**
Andrey Andreevb0478652012-07-18 15:34:46 +0300315 * Update statement
316 *
317 * Generates a platform-specific update string from the supplied data
318 *
Andrey Andreeva24e52e2012-11-02 03:54:12 +0200319 * @param string $table
320 * @param array $values
Andrey Andreevb0478652012-07-18 15:34:46 +0300321 * @return string
Andrey Andreev838a9d62012-12-03 14:37:47 +0200322 */
Andrey Andreevb0478652012-07-18 15:34:46 +0300323 protected function _update($table, $values)
324 {
325 $this->qb_limit = FALSE;
326 $this->qb_orderby = array();
327 return parent::_update($table, $values);
328 }
329
330 // --------------------------------------------------------------------
331
332 /**
Derek Allard2067d1a2008-11-13 22:59:24 +0000333 * Truncate statement
334 *
335 * Generates a platform-specific truncate string from the supplied data
Andrey Andreeva6fe36e2012-04-05 16:00:32 +0300336 *
Andrey Andreeva24e52e2012-11-02 03:54:12 +0200337 * If the database does not support the TRUNCATE statement,
Andrey Andreeva6fe36e2012-04-05 16:00:32 +0300338 * then this method maps to 'DELETE FROM table'
Derek Allard2067d1a2008-11-13 22:59:24 +0000339 *
Andrey Andreeva24e52e2012-11-02 03:54:12 +0200340 * @param string $table
Derek Allard2067d1a2008-11-13 22:59:24 +0000341 * @return string
Barry Mienydd671972010-10-04 16:33:58 +0200342 */
Andrey Andreevb76029d2012-01-26 15:13:19 +0200343 protected function _truncate($table)
Derek Allard2067d1a2008-11-13 22:59:24 +0000344 {
Andrey Andreeva6fe36e2012-04-05 16:00:32 +0300345 return 'DELETE FROM '.$table;
Derek Allard2067d1a2008-11-13 22:59:24 +0000346 }
Barry Mienydd671972010-10-04 16:33:58 +0200347
Derek Allard2067d1a2008-11-13 22:59:24 +0000348 // --------------------------------------------------------------------
349
350 /**
Andrey Andreevb0478652012-07-18 15:34:46 +0300351 * Delete statement
352 *
353 * Generates a platform-specific delete string from the supplied data
354 *
Andrey Andreeva24e52e2012-11-02 03:54:12 +0200355 * @param string $table
Andrey Andreevb0478652012-07-18 15:34:46 +0300356 * @return string
357 */
358 protected function _delete($table)
359 {
360 $this->qb_limit = FALSE;
361 return parent::_delete($table);
362 }
363
364 // --------------------------------------------------------------------
365
366 /**
Derek Allard2067d1a2008-11-13 22:59:24 +0000367 * Close DB Connection
368 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000369 * @return void
370 */
Andrey Andreev79922c02012-05-23 12:27:17 +0300371 protected function _close()
Derek Allard2067d1a2008-11-13 22:59:24 +0000372 {
Andrey Andreev2bbbd1a2014-05-09 10:24:14 +0300373 odbc_close($this->conn_id);
Derek Allard2067d1a2008-11-13 22:59:24 +0000374 }
375
Derek Allard2067d1a2008-11-13 22:59:24 +0000376}