blob: c9aaa6e689a6f07346b6fdf116af7cb0595b5c90 [file] [log] [blame]
Andrey Andreevb76029d2012-01-26 15:13:19 +02001<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
Derek Allard2067d1a2008-11-13 22:59:24 +00002/**
3 * CodeIgniter
4 *
Greg Aker741de1c2010-11-10 14:52:57 -06005 * An open source application development framework for PHP 5.1.6 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 */
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
32 * creates dynamically based on whether the active record
33 * 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 Andreevb76029d2012-01-26 15:13:19 +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 Andreevb76029d2012-01-26 15:13:19 +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 Andreevb76029d2012-01-26 15:13:19 +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 Andreevb76029d2012-01-26 15:13:19 +020057 protected $_count_string = 'SELECT COUNT(*) AS ';
58 protected $_random_keyword;
Derek Allard2067d1a2008-11-13 22:59:24 +000059
Timothy Warrena2097a02011-10-10 10:10:46 -040060 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 Andreev46f6dbc2012-02-13 01:39:53 +020065
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 Andreevb76029d2012-01-26 15:13:19 +020078 public function db_connect()
Derek Allard2067d1a2008-11-13 22:59:24 +000079 {
Andrey Andreev46f6dbc2012-02-13 01:39:53 +020080 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 Andreevb76029d2012-01-26 15:13:19 +020090 public function db_pconnect()
Derek Allard2067d1a2008-11-13 22:59:24 +000091 {
Andrey Andreev46f6dbc2012-02-13 01:39:53 +020092 return @odbc_pconnect($this->dsn, $this->username, $this->password);
Derek Allard2067d1a2008-11-13 22:59:24 +000093 }
Barry Mienydd671972010-10-04 16:33:58 +020094
Derek Allard2067d1a2008-11-13 22:59:24 +000095 // --------------------------------------------------------------------
96
97 /**
Derek Jones87cbafc2009-02-27 16:29:59 +000098 * Reconnect
99 *
100 * Keep / reestablish the db connection if no queries have been
101 * sent for a length of time exceeding the server's idle timeout
102 *
Derek Jones87cbafc2009-02-27 16:29:59 +0000103 * @return void
104 */
Andrey Andreevb76029d2012-01-26 15:13:19 +0200105 public function reconnect()
Derek Jones87cbafc2009-02-27 16:29:59 +0000106 {
107 // not implemented in odbc
108 }
109
110 // --------------------------------------------------------------------
111
112 /**
Derek Allard2067d1a2008-11-13 22:59:24 +0000113 * Select the database
114 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000115 * @return resource
Barry Mienydd671972010-10-04 16:33:58 +0200116 */
Andrey Andreevb76029d2012-01-26 15:13:19 +0200117 public function db_select()
Derek Allard2067d1a2008-11-13 22:59:24 +0000118 {
119 // Not needed for ODBC
120 return TRUE;
121 }
122
123 // --------------------------------------------------------------------
124
125 /**
126 * Set client character set
127 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000128 * @param string
129 * @param string
Andrey Andreevb76029d2012-01-26 15:13:19 +0200130 * @return bool
Derek Allard2067d1a2008-11-13 22:59:24 +0000131 */
Andrey Andreevb76029d2012-01-26 15:13:19 +0200132 public function db_set_charset($charset, $collation)
Derek Allard2067d1a2008-11-13 22:59:24 +0000133 {
134 // @todo - add support if needed
135 return TRUE;
136 }
137
138 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200139
Derek Allard2067d1a2008-11-13 22:59:24 +0000140 /**
141 * Version number query string
142 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000143 * @return string
144 */
Andrey Andreevb76029d2012-01-26 15:13:19 +0200145 protected function _version()
Derek Allard2067d1a2008-11-13 22:59:24 +0000146 {
Andrey Andreevb76029d2012-01-26 15:13:19 +0200147 return 'SELECT version() AS ver';
Derek Allard2067d1a2008-11-13 22:59:24 +0000148 }
149
150 // --------------------------------------------------------------------
151
152 /**
153 * Execute the query
154 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000155 * @param string an SQL query
156 * @return resource
Barry Mienydd671972010-10-04 16:33:58 +0200157 */
Andrey Andreevb76029d2012-01-26 15:13:19 +0200158 protected function _execute($sql)
Derek Allard2067d1a2008-11-13 22:59:24 +0000159 {
Andrey Andreevb76029d2012-01-26 15:13:19 +0200160 return @odbc_exec($this->conn_id, $this->_prep_query($sql));
Derek Allard2067d1a2008-11-13 22:59:24 +0000161 }
Barry Mienydd671972010-10-04 16:33:58 +0200162
Derek Allard2067d1a2008-11-13 22:59:24 +0000163 // --------------------------------------------------------------------
164
165 /**
166 * Prep the query
167 *
168 * If needed, each database adapter can prep the query string
169 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000170 * @param string an SQL query
171 * @return string
Barry Mienydd671972010-10-04 16:33:58 +0200172 */
Andrey Andreevb76029d2012-01-26 15:13:19 +0200173 protected function _prep_query($sql)
Derek Allard2067d1a2008-11-13 22:59:24 +0000174 {
175 return $sql;
176 }
177
178 // --------------------------------------------------------------------
179
180 /**
181 * Begin Transaction
182 *
Barry Mienydd671972010-10-04 16:33:58 +0200183 * @return bool
184 */
Andrey Andreevb76029d2012-01-26 15:13:19 +0200185 public function trans_begin($test_mode = FALSE)
Derek Allard2067d1a2008-11-13 22:59:24 +0000186 {
Derek Allard2067d1a2008-11-13 22:59:24 +0000187 // When transactions are nested we only begin/commit/rollback the outermost ones
Andrey Andreevb76029d2012-01-26 15:13:19 +0200188 if ( ! $this->trans_enabled OR $this->_trans_depth > 0)
Derek Allard2067d1a2008-11-13 22:59:24 +0000189 {
190 return TRUE;
191 }
192
193 // Reset the transaction failure flag.
194 // If the $test_mode flag is set to TRUE transactions will be rolled back
195 // even if the queries produce a successful result.
Andrey Andreevb76029d2012-01-26 15:13:19 +0200196 $this->_trans_failure = ($test_mode === TRUE);
Derek Allard2067d1a2008-11-13 22:59:24 +0000197
198 return odbc_autocommit($this->conn_id, FALSE);
199 }
200
201 // --------------------------------------------------------------------
202
203 /**
204 * Commit Transaction
205 *
Barry Mienydd671972010-10-04 16:33:58 +0200206 * @return bool
207 */
Andrey Andreevb76029d2012-01-26 15:13:19 +0200208 public function trans_commit()
Derek Allard2067d1a2008-11-13 22:59:24 +0000209 {
Derek Allard2067d1a2008-11-13 22:59:24 +0000210 // When transactions are nested we only begin/commit/rollback the outermost ones
Andrey Andreevb76029d2012-01-26 15:13:19 +0200211 if ( ! $this->trans_enabled OR $this->_trans_depth > 0)
Derek Allard2067d1a2008-11-13 22:59:24 +0000212 {
213 return TRUE;
214 }
215
216 $ret = odbc_commit($this->conn_id);
217 odbc_autocommit($this->conn_id, TRUE);
218 return $ret;
219 }
220
221 // --------------------------------------------------------------------
222
223 /**
224 * Rollback Transaction
225 *
Barry Mienydd671972010-10-04 16:33:58 +0200226 * @return bool
227 */
Andrey Andreevb76029d2012-01-26 15:13:19 +0200228 public function trans_rollback()
Derek Allard2067d1a2008-11-13 22:59:24 +0000229 {
Derek Allard2067d1a2008-11-13 22:59:24 +0000230 // When transactions are nested we only begin/commit/rollback the outermost ones
Andrey Andreevb76029d2012-01-26 15:13:19 +0200231 if ( ! $this->trans_enabled OR $this->_trans_depth > 0)
Derek Allard2067d1a2008-11-13 22:59:24 +0000232 {
233 return TRUE;
234 }
235
236 $ret = odbc_rollback($this->conn_id);
237 odbc_autocommit($this->conn_id, TRUE);
238 return $ret;
239 }
240
241 // --------------------------------------------------------------------
242
243 /**
244 * Escape String
245 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000246 * @param string
Derek Jonese4ed5832009-02-20 21:44:59 +0000247 * @param bool whether or not the string will be used in a LIKE condition
Derek Allard2067d1a2008-11-13 22:59:24 +0000248 * @return string
249 */
Andrey Andreevb76029d2012-01-26 15:13:19 +0200250 public function escape_str($str, $like = FALSE)
Derek Allard2067d1a2008-11-13 22:59:24 +0000251 {
Derek Jonese4ed5832009-02-20 21:44:59 +0000252 if (is_array($str))
253 {
Pascal Krietec3a4a8d2011-02-14 13:40:08 -0500254 foreach ($str as $key => $val)
Barry Mienydd671972010-10-04 16:33:58 +0200255 {
Derek Jonese4ed5832009-02-20 21:44:59 +0000256 $str[$key] = $this->escape_str($val, $like);
Barry Mienydd671972010-10-04 16:33:58 +0200257 }
258
259 return $str;
260 }
261
Derek Allard2067d1a2008-11-13 22:59:24 +0000262 // ODBC doesn't require escaping
Greg Aker757dda62010-04-14 19:06:19 -0500263 $str = remove_invisible_characters($str);
Barry Mienydd671972010-10-04 16:33:58 +0200264
Derek Jonese4ed5832009-02-20 21:44:59 +0000265 // escape LIKE condition wildcards
266 if ($like === TRUE)
267 {
Andrey Andreevb76029d2012-01-26 15:13:19 +0200268 return str_replace(array('%', '_', $this->_like_escape_chr),
269 array($this->_like_escape_chr.'%', $this->_like_escape_chr.'_', $this->_like_escape_chr.$this->_like_escape_chr),
270 $str);
Derek Jonese4ed5832009-02-20 21:44:59 +0000271 }
Barry Mienydd671972010-10-04 16:33:58 +0200272
Derek Jonese4ed5832009-02-20 21:44:59 +0000273 return $str;
Derek Allard2067d1a2008-11-13 22:59:24 +0000274 }
Barry Mienydd671972010-10-04 16:33:58 +0200275
Derek Allard2067d1a2008-11-13 22:59:24 +0000276 // --------------------------------------------------------------------
277
278 /**
279 * Affected Rows
280 *
Andrey Andreevb76029d2012-01-26 15:13:19 +0200281 * @return int
Derek Allard2067d1a2008-11-13 22:59:24 +0000282 */
Andrey Andreevb76029d2012-01-26 15:13:19 +0200283 public function affected_rows()
Derek Allard2067d1a2008-11-13 22:59:24 +0000284 {
285 return @odbc_num_rows($this->conn_id);
286 }
Barry Mienydd671972010-10-04 16:33:58 +0200287
Derek Allard2067d1a2008-11-13 22:59:24 +0000288 // --------------------------------------------------------------------
289
290 /**
291 * Insert ID
292 *
Andrey Andreevb76029d2012-01-26 15:13:19 +0200293 * @return int
Derek Allard2067d1a2008-11-13 22:59:24 +0000294 */
Andrey Andreevb76029d2012-01-26 15:13:19 +0200295 public function insert_id()
Derek Allard2067d1a2008-11-13 22:59:24 +0000296 {
297 return @odbc_insert_id($this->conn_id);
298 }
299
300 // --------------------------------------------------------------------
301
302 /**
303 * "Count All" query
304 *
305 * Generates a platform-specific query string that counts all records in
306 * the specified database
307 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000308 * @param string
Andrey Andreevb76029d2012-01-26 15:13:19 +0200309 * @return int
Derek Allard2067d1a2008-11-13 22:59:24 +0000310 */
Andrey Andreevb76029d2012-01-26 15:13:19 +0200311 public function count_all($table = '')
Derek Allard2067d1a2008-11-13 22:59:24 +0000312 {
313 if ($table == '')
Derek Allarde37ab382009-02-03 16:13:57 +0000314 {
315 return 0;
316 }
317
Andrey Andreevb76029d2012-01-26 15:13:19 +0200318 $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 +0000319
Derek Allard2067d1a2008-11-13 22:59:24 +0000320 if ($query->num_rows() == 0)
Derek Allarde37ab382009-02-03 16:13:57 +0000321 {
322 return 0;
323 }
Derek Allard2067d1a2008-11-13 22:59:24 +0000324
Andrey Andreevb76029d2012-01-26 15:13:19 +0200325 $query = $query->row();
Greg Aker90248ab2011-08-20 14:23:14 -0500326 $this->_reset_select();
Andrey Andreevb76029d2012-01-26 15:13:19 +0200327 return (int) $query->numrows;
Derek Allard2067d1a2008-11-13 22:59:24 +0000328 }
329
330 // --------------------------------------------------------------------
331
332 /**
333 * Show table query
334 *
335 * Generates a platform-specific query string so that the table names can be fetched
336 *
337 * @access private
338 * @param boolean
339 * @return string
340 */
Andrey Andreevb76029d2012-01-26 15:13:19 +0200341 protected function _list_tables($prefix_limit = FALSE)
Derek Allard2067d1a2008-11-13 22:59:24 +0000342 {
Andrey Andreevb76029d2012-01-26 15:13:19 +0200343 $sql = 'SHOW TABLES FROM `'.$this->database.'`';
Derek Allard2067d1a2008-11-13 22:59:24 +0000344
Andrey Andreevb76029d2012-01-26 15:13:19 +0200345 if ($prefix_limit !== FALSE && $this->dbprefix != '')
Derek Allard2067d1a2008-11-13 22:59:24 +0000346 {
Greg Aker0d424892010-01-26 02:14:44 +0000347 //$sql .= " LIKE '".$this->escape_like_str($this->dbprefix)."%' ".sprintf($this->_like_escape_str, $this->_like_escape_chr);
Derek Allard2067d1a2008-11-13 22:59:24 +0000348 return FALSE; // not currently supported
349 }
Barry Mienydd671972010-10-04 16:33:58 +0200350
Derek Allard2067d1a2008-11-13 22:59:24 +0000351 return $sql;
352 }
Barry Mienydd671972010-10-04 16:33:58 +0200353
Derek Allard2067d1a2008-11-13 22:59:24 +0000354 // --------------------------------------------------------------------
355
356 /**
357 * Show column query
358 *
359 * Generates a platform-specific query string so that the column names can be fetched
360 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000361 * @param string the table name
362 * @return string
363 */
Andrey Andreevb76029d2012-01-26 15:13:19 +0200364 protected function _list_columns($table = '')
Derek Allard2067d1a2008-11-13 22:59:24 +0000365 {
Andrey Andreevb76029d2012-01-26 15:13:19 +0200366 return 'SHOW COLUMNS FROM '.$table;
Derek Allard2067d1a2008-11-13 22:59:24 +0000367 }
368
369 // --------------------------------------------------------------------
370
371 /**
372 * Field data query
373 *
374 * Generates a platform-specific query so that the column data can be retrieved
375 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000376 * @param string the table name
Andrey Andreevb76029d2012-01-26 15:13:19 +0200377 * @return string
Derek Allard2067d1a2008-11-13 22:59:24 +0000378 */
Andrey Andreevb76029d2012-01-26 15:13:19 +0200379 protected function _field_data($table)
Derek Allard2067d1a2008-11-13 22:59:24 +0000380 {
Andrey Andreevb76029d2012-01-26 15:13:19 +0200381 return 'SELECT TOP 1 FROM '.$table;
Derek Allard2067d1a2008-11-13 22:59:24 +0000382 }
383
384 // --------------------------------------------------------------------
385
386 /**
387 * The error message string
388 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000389 * @return string
390 */
Andrey Andreevb76029d2012-01-26 15:13:19 +0200391 protected function _error_message()
Derek Allard2067d1a2008-11-13 22:59:24 +0000392 {
393 return odbc_errormsg($this->conn_id);
394 }
Barry Mienydd671972010-10-04 16:33:58 +0200395
Derek Allard2067d1a2008-11-13 22:59:24 +0000396 // --------------------------------------------------------------------
397
398 /**
399 * The error message number
400 *
Andrey Andreevb76029d2012-01-26 15:13:19 +0200401 * @return int
Derek Allard2067d1a2008-11-13 22:59:24 +0000402 */
Andrey Andreevb76029d2012-01-26 15:13:19 +0200403 protected function _error_number()
Derek Allard2067d1a2008-11-13 22:59:24 +0000404 {
405 return odbc_error($this->conn_id);
406 }
407
408 // --------------------------------------------------------------------
409
410 /**
411 * Escape the SQL Identifiers
412 *
413 * This function escapes column and table names
414 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000415 * @param string
416 * @return string
417 */
Andrey Andreevb76029d2012-01-26 15:13:19 +0200418 public function _escape_identifiers($item)
Derek Allard2067d1a2008-11-13 22:59:24 +0000419 {
420 if ($this->_escape_char == '')
421 {
422 return $item;
423 }
424
425 foreach ($this->_reserved_identifiers as $id)
426 {
427 if (strpos($item, '.'.$id) !== FALSE)
428 {
Andrey Andreevb76029d2012-01-26 15:13:19 +0200429 $item = str_replace('.', $this->_escape_char.'.', $item);
Barry Mienydd671972010-10-04 16:33:58 +0200430
Derek Allard2067d1a2008-11-13 22:59:24 +0000431 // remove duplicates if the user already included the escape
Andrey Andreevb76029d2012-01-26 15:13:19 +0200432 return preg_replace('/['.$this->_escape_char.']+/', $this->_escape_char, $this->_escape_char.$item);
Barry Mienydd671972010-10-04 16:33:58 +0200433 }
Derek Allard2067d1a2008-11-13 22:59:24 +0000434 }
Barry Mienydd671972010-10-04 16:33:58 +0200435
Derek Allard2067d1a2008-11-13 22:59:24 +0000436 if (strpos($item, '.') !== FALSE)
437 {
Andrey Andreevb76029d2012-01-26 15:13:19 +0200438 $item = str_replace('.', $this->_escape_char.'.'.$this->_escape_char, $item);
Derek Allard2067d1a2008-11-13 22:59:24 +0000439 }
Barry Mienydd671972010-10-04 16:33:58 +0200440
Derek Allard2067d1a2008-11-13 22:59:24 +0000441 // remove duplicates if the user already included the escape
Andrey Andreevb76029d2012-01-26 15:13:19 +0200442 return preg_replace('/['.$this->_escape_char.']+/', $this->_escape_char, $this->_escape_char.$item.$this->_escape_char);
Derek Allard2067d1a2008-11-13 22:59:24 +0000443 }
Barry Mienydd671972010-10-04 16:33:58 +0200444
Derek Allard2067d1a2008-11-13 22:59:24 +0000445 // --------------------------------------------------------------------
446
447 /**
448 * From Tables
449 *
450 * This function implicitly groups FROM tables so there is no confusion
451 * about operator precedence in harmony with SQL standards
452 *
Andrey Andreevb76029d2012-01-26 15:13:19 +0200453 * @param string the table name
454 * @return string
Derek Allard2067d1a2008-11-13 22:59:24 +0000455 */
Andrey Andreevb76029d2012-01-26 15:13:19 +0200456 protected function _from_tables($tables)
Derek Allard2067d1a2008-11-13 22:59:24 +0000457 {
458 if ( ! is_array($tables))
459 {
460 $tables = array($tables);
461 }
Barry Mienydd671972010-10-04 16:33:58 +0200462
Derek Allard2067d1a2008-11-13 22:59:24 +0000463 return '('.implode(', ', $tables).')';
464 }
465
466 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200467
Derek Allard2067d1a2008-11-13 22:59:24 +0000468 /**
469 * Insert statement
470 *
471 * Generates a platform-specific insert string from the supplied data
472 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000473 * @param string the table name
474 * @param array the insert keys
475 * @param array the insert values
476 * @return string
477 */
Andrey Andreevb76029d2012-01-26 15:13:19 +0200478 protected function _insert($table, $keys, $values)
Barry Mienydd671972010-10-04 16:33:58 +0200479 {
Andrey Andreevb76029d2012-01-26 15:13:19 +0200480 return 'INSERT INTO '.$table.' ('.implode(', ', $keys).') VALUES ('.implode(', ', $values).')';
Derek Allard2067d1a2008-11-13 22:59:24 +0000481 }
Barry Mienydd671972010-10-04 16:33:58 +0200482
Derek Allard2067d1a2008-11-13 22:59:24 +0000483 // --------------------------------------------------------------------
484
485 /**
486 * Update statement
487 *
488 * Generates a platform-specific update string from the supplied data
489 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000490 * @param string the table name
491 * @param array the update data
492 * @param array the where clause
493 * @param array the orderby clause
494 * @param array the limit clause
495 * @return string
496 */
Andrey Andreevb76029d2012-01-26 15:13:19 +0200497 protected function _update($table, $values, $where, $orderby = array(), $limit = FALSE)
Derek Allard2067d1a2008-11-13 22:59:24 +0000498 {
Pascal Krietec3a4a8d2011-02-14 13:40:08 -0500499 foreach ($values as $key => $val)
Derek Allard2067d1a2008-11-13 22:59:24 +0000500 {
Andrey Andreevb76029d2012-01-26 15:13:19 +0200501 $valstr[] = $key.' = '.$val;
Derek Allard2067d1a2008-11-13 22:59:24 +0000502 }
Barry Mienydd671972010-10-04 16:33:58 +0200503
Andrey Andreevb76029d2012-01-26 15:13:19 +0200504 return 'UPDATE '.$table.' SET '.implode(', ', $valstr)
505 .(($where != '' && count($where) > 0) ? ' WHERE '.implode(' ', $where) : '')
506 .(count($orderby) > 0 ? ' ORDER BY '.implode(', ', $orderby) : '')
507 .( ! $limit ? '' : ' LIMIT '.$limit);
Derek Allard2067d1a2008-11-13 22:59:24 +0000508 }
509
Barry Mienydd671972010-10-04 16:33:58 +0200510
Derek Allard2067d1a2008-11-13 22:59:24 +0000511 // --------------------------------------------------------------------
512
513 /**
514 * Truncate statement
515 *
516 * Generates a platform-specific truncate string from the supplied data
517 * If the database does not support the truncate() command
518 * This function maps to "DELETE FROM table"
519 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000520 * @param string the table name
521 * @return string
Barry Mienydd671972010-10-04 16:33:58 +0200522 */
Andrey Andreevb76029d2012-01-26 15:13:19 +0200523 protected function _truncate($table)
Derek Allard2067d1a2008-11-13 22:59:24 +0000524 {
525 return $this->_delete($table);
526 }
Barry Mienydd671972010-10-04 16:33:58 +0200527
Derek Allard2067d1a2008-11-13 22:59:24 +0000528 // --------------------------------------------------------------------
529
530 /**
531 * Delete statement
532 *
533 * Generates a platform-specific delete string from the supplied data
534 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000535 * @param string the table name
536 * @param array the where clause
537 * @param string the limit clause
538 * @return string
Barry Mienydd671972010-10-04 16:33:58 +0200539 */
Andrey Andreevb76029d2012-01-26 15:13:19 +0200540 protected function _delete($table, $where = array(), $like = array(), $limit = FALSE)
Derek Allard2067d1a2008-11-13 22:59:24 +0000541 {
542 $conditions = '';
543
544 if (count($where) > 0 OR count($like) > 0)
545 {
Andrey Andreevb76029d2012-01-26 15:13:19 +0200546 $conditions = "\nWHERE ".implode("\n", $this->ar_where);
Derek Allard2067d1a2008-11-13 22:59:24 +0000547
548 if (count($where) > 0 && count($like) > 0)
549 {
Andrey Andreevb76029d2012-01-26 15:13:19 +0200550 $conditions .= ' AND ';
Derek Allard2067d1a2008-11-13 22:59:24 +0000551 }
552 $conditions .= implode("\n", $like);
553 }
554
Andrey Andreevb76029d2012-01-26 15:13:19 +0200555 return 'DELETE FROM '.$table.$conditions.( ! $limit ? '' : ' LIMIT '.$limit);
Derek Allard2067d1a2008-11-13 22:59:24 +0000556 }
557
558 // --------------------------------------------------------------------
559
560 /**
561 * Limit string
562 *
563 * Generates a platform-specific LIMIT clause
564 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000565 * @param string the sql query string
Andrey Andreevb76029d2012-01-26 15:13:19 +0200566 * @param int the number of rows to limit the query to
567 * @param int the offset value
Derek Allard2067d1a2008-11-13 22:59:24 +0000568 * @return string
569 */
Andrey Andreevb76029d2012-01-26 15:13:19 +0200570 protected function _limit($sql, $limit, $offset)
Derek Allard2067d1a2008-11-13 22:59:24 +0000571 {
Andrey Andreevb76029d2012-01-26 15:13:19 +0200572 return $sql.($offset == 0 ? '' : $offset.', ').$limit;
Derek Allard2067d1a2008-11-13 22:59:24 +0000573 }
574
575 // --------------------------------------------------------------------
576
577 /**
578 * Close DB Connection
579 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000580 * @param resource
581 * @return void
582 */
Andrey Andreevb76029d2012-01-26 15:13:19 +0200583 protected function _close($conn_id)
Derek Allard2067d1a2008-11-13 22:59:24 +0000584 {
585 @odbc_close($conn_id);
586 }
587
Derek Allard2067d1a2008-11-13 22:59:24 +0000588}
589
Derek Allard2067d1a2008-11-13 22:59:24 +0000590/* End of file odbc_driver.php */
Andrey Andreevb76029d2012-01-26 15:13:19 +0200591/* Location: ./system/database/drivers/odbc/odbc_driver.php */