blob: a6e3dd7ebdcd16f259df16ec548a14a38557d5cb [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 /**
Derek Allard2067d1a2008-11-13 22:59:24 +0000126 * Execute the query
127 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000128 * @param string an SQL query
129 * @return resource
Barry Mienydd671972010-10-04 16:33:58 +0200130 */
Andrey Andreevb76029d2012-01-26 15:13:19 +0200131 protected function _execute($sql)
Derek Allard2067d1a2008-11-13 22:59:24 +0000132 {
Andrey Andreevb76029d2012-01-26 15:13:19 +0200133 return @odbc_exec($this->conn_id, $this->_prep_query($sql));
Derek Allard2067d1a2008-11-13 22:59:24 +0000134 }
Barry Mienydd671972010-10-04 16:33:58 +0200135
Derek Allard2067d1a2008-11-13 22:59:24 +0000136 // --------------------------------------------------------------------
137
138 /**
139 * Prep the query
140 *
141 * If needed, each database adapter can prep the query string
142 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000143 * @param string an SQL query
144 * @return string
Barry Mienydd671972010-10-04 16:33:58 +0200145 */
Andrey Andreevb76029d2012-01-26 15:13:19 +0200146 protected function _prep_query($sql)
Derek Allard2067d1a2008-11-13 22:59:24 +0000147 {
148 return $sql;
149 }
150
151 // --------------------------------------------------------------------
152
153 /**
154 * Begin Transaction
155 *
Barry Mienydd671972010-10-04 16:33:58 +0200156 * @return bool
157 */
Andrey Andreevb76029d2012-01-26 15:13:19 +0200158 public function trans_begin($test_mode = FALSE)
Derek Allard2067d1a2008-11-13 22:59:24 +0000159 {
Derek Allard2067d1a2008-11-13 22:59:24 +0000160 // When transactions are nested we only begin/commit/rollback the outermost ones
Andrey Andreevb76029d2012-01-26 15:13:19 +0200161 if ( ! $this->trans_enabled OR $this->_trans_depth > 0)
Derek Allard2067d1a2008-11-13 22:59:24 +0000162 {
163 return TRUE;
164 }
165
166 // Reset the transaction failure flag.
167 // If the $test_mode flag is set to TRUE transactions will be rolled back
168 // even if the queries produce a successful result.
Andrey Andreevb76029d2012-01-26 15:13:19 +0200169 $this->_trans_failure = ($test_mode === TRUE);
Derek Allard2067d1a2008-11-13 22:59:24 +0000170
171 return odbc_autocommit($this->conn_id, FALSE);
172 }
173
174 // --------------------------------------------------------------------
175
176 /**
177 * Commit Transaction
178 *
Barry Mienydd671972010-10-04 16:33:58 +0200179 * @return bool
180 */
Andrey Andreevb76029d2012-01-26 15:13:19 +0200181 public function trans_commit()
Derek Allard2067d1a2008-11-13 22:59:24 +0000182 {
Derek Allard2067d1a2008-11-13 22:59:24 +0000183 // When transactions are nested we only begin/commit/rollback the outermost ones
Andrey Andreevb76029d2012-01-26 15:13:19 +0200184 if ( ! $this->trans_enabled OR $this->_trans_depth > 0)
Derek Allard2067d1a2008-11-13 22:59:24 +0000185 {
186 return TRUE;
187 }
188
189 $ret = odbc_commit($this->conn_id);
190 odbc_autocommit($this->conn_id, TRUE);
191 return $ret;
192 }
193
194 // --------------------------------------------------------------------
195
196 /**
197 * Rollback Transaction
198 *
Barry Mienydd671972010-10-04 16:33:58 +0200199 * @return bool
200 */
Andrey Andreevb76029d2012-01-26 15:13:19 +0200201 public function trans_rollback()
Derek Allard2067d1a2008-11-13 22:59:24 +0000202 {
Derek Allard2067d1a2008-11-13 22:59:24 +0000203 // When transactions are nested we only begin/commit/rollback the outermost ones
Andrey Andreevb76029d2012-01-26 15:13:19 +0200204 if ( ! $this->trans_enabled OR $this->_trans_depth > 0)
Derek Allard2067d1a2008-11-13 22:59:24 +0000205 {
206 return TRUE;
207 }
208
209 $ret = odbc_rollback($this->conn_id);
210 odbc_autocommit($this->conn_id, TRUE);
211 return $ret;
212 }
213
214 // --------------------------------------------------------------------
215
216 /**
217 * Escape String
218 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000219 * @param string
Derek Jonese4ed5832009-02-20 21:44:59 +0000220 * @param bool whether or not the string will be used in a LIKE condition
Derek Allard2067d1a2008-11-13 22:59:24 +0000221 * @return string
222 */
Andrey Andreevb76029d2012-01-26 15:13:19 +0200223 public function escape_str($str, $like = FALSE)
Derek Allard2067d1a2008-11-13 22:59:24 +0000224 {
Derek Jonese4ed5832009-02-20 21:44:59 +0000225 if (is_array($str))
226 {
Pascal Krietec3a4a8d2011-02-14 13:40:08 -0500227 foreach ($str as $key => $val)
Barry Mienydd671972010-10-04 16:33:58 +0200228 {
Derek Jonese4ed5832009-02-20 21:44:59 +0000229 $str[$key] = $this->escape_str($val, $like);
Barry Mienydd671972010-10-04 16:33:58 +0200230 }
231
232 return $str;
233 }
234
Derek Allard2067d1a2008-11-13 22:59:24 +0000235 // ODBC doesn't require escaping
Greg Aker757dda62010-04-14 19:06:19 -0500236 $str = remove_invisible_characters($str);
Barry Mienydd671972010-10-04 16:33:58 +0200237
Derek Jonese4ed5832009-02-20 21:44:59 +0000238 // escape LIKE condition wildcards
239 if ($like === TRUE)
240 {
Andrey Andreevb76029d2012-01-26 15:13:19 +0200241 return str_replace(array('%', '_', $this->_like_escape_chr),
242 array($this->_like_escape_chr.'%', $this->_like_escape_chr.'_', $this->_like_escape_chr.$this->_like_escape_chr),
243 $str);
Derek Jonese4ed5832009-02-20 21:44:59 +0000244 }
Barry Mienydd671972010-10-04 16:33:58 +0200245
Derek Jonese4ed5832009-02-20 21:44:59 +0000246 return $str;
Derek Allard2067d1a2008-11-13 22:59:24 +0000247 }
Barry Mienydd671972010-10-04 16:33:58 +0200248
Derek Allard2067d1a2008-11-13 22:59:24 +0000249 // --------------------------------------------------------------------
250
251 /**
252 * Affected Rows
253 *
Andrey Andreevb76029d2012-01-26 15:13:19 +0200254 * @return int
Derek Allard2067d1a2008-11-13 22:59:24 +0000255 */
Andrey Andreevb76029d2012-01-26 15:13:19 +0200256 public function affected_rows()
Derek Allard2067d1a2008-11-13 22:59:24 +0000257 {
258 return @odbc_num_rows($this->conn_id);
259 }
Barry Mienydd671972010-10-04 16:33:58 +0200260
Derek Allard2067d1a2008-11-13 22:59:24 +0000261 // --------------------------------------------------------------------
262
263 /**
264 * Insert ID
265 *
Andrey Andreevb76029d2012-01-26 15:13:19 +0200266 * @return int
Derek Allard2067d1a2008-11-13 22:59:24 +0000267 */
Andrey Andreevb76029d2012-01-26 15:13:19 +0200268 public function insert_id()
Derek Allard2067d1a2008-11-13 22:59:24 +0000269 {
270 return @odbc_insert_id($this->conn_id);
271 }
272
273 // --------------------------------------------------------------------
274
275 /**
276 * "Count All" query
277 *
278 * Generates a platform-specific query string that counts all records in
279 * the specified database
280 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000281 * @param string
Andrey Andreevb76029d2012-01-26 15:13:19 +0200282 * @return int
Derek Allard2067d1a2008-11-13 22:59:24 +0000283 */
Andrey Andreevb76029d2012-01-26 15:13:19 +0200284 public function count_all($table = '')
Derek Allard2067d1a2008-11-13 22:59:24 +0000285 {
286 if ($table == '')
Derek Allarde37ab382009-02-03 16:13:57 +0000287 {
288 return 0;
289 }
290
Andrey Andreevb76029d2012-01-26 15:13:19 +0200291 $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 +0000292
Derek Allard2067d1a2008-11-13 22:59:24 +0000293 if ($query->num_rows() == 0)
Derek Allarde37ab382009-02-03 16:13:57 +0000294 {
295 return 0;
296 }
Derek Allard2067d1a2008-11-13 22:59:24 +0000297
Andrey Andreevb76029d2012-01-26 15:13:19 +0200298 $query = $query->row();
Greg Aker90248ab2011-08-20 14:23:14 -0500299 $this->_reset_select();
Andrey Andreevb76029d2012-01-26 15:13:19 +0200300 return (int) $query->numrows;
Derek Allard2067d1a2008-11-13 22:59:24 +0000301 }
302
303 // --------------------------------------------------------------------
304
305 /**
306 * Show table query
307 *
308 * Generates a platform-specific query string so that the table names can be fetched
309 *
310 * @access private
311 * @param boolean
312 * @return string
313 */
Andrey Andreevb76029d2012-01-26 15:13:19 +0200314 protected function _list_tables($prefix_limit = FALSE)
Derek Allard2067d1a2008-11-13 22:59:24 +0000315 {
Andrey Andreevb76029d2012-01-26 15:13:19 +0200316 $sql = 'SHOW TABLES FROM `'.$this->database.'`';
Derek Allard2067d1a2008-11-13 22:59:24 +0000317
Andrey Andreevb76029d2012-01-26 15:13:19 +0200318 if ($prefix_limit !== FALSE && $this->dbprefix != '')
Derek Allard2067d1a2008-11-13 22:59:24 +0000319 {
Greg Aker0d424892010-01-26 02:14:44 +0000320 //$sql .= " LIKE '".$this->escape_like_str($this->dbprefix)."%' ".sprintf($this->_like_escape_str, $this->_like_escape_chr);
Derek Allard2067d1a2008-11-13 22:59:24 +0000321 return FALSE; // not currently supported
322 }
Barry Mienydd671972010-10-04 16:33:58 +0200323
Derek Allard2067d1a2008-11-13 22:59:24 +0000324 return $sql;
325 }
Barry Mienydd671972010-10-04 16:33:58 +0200326
Derek Allard2067d1a2008-11-13 22:59:24 +0000327 // --------------------------------------------------------------------
328
329 /**
330 * Show column query
331 *
332 * Generates a platform-specific query string so that the column names can be fetched
333 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000334 * @param string the table name
335 * @return string
336 */
Andrey Andreevb76029d2012-01-26 15:13:19 +0200337 protected function _list_columns($table = '')
Derek Allard2067d1a2008-11-13 22:59:24 +0000338 {
Andrey Andreevb76029d2012-01-26 15:13:19 +0200339 return 'SHOW COLUMNS FROM '.$table;
Derek Allard2067d1a2008-11-13 22:59:24 +0000340 }
341
342 // --------------------------------------------------------------------
343
344 /**
345 * Field data query
346 *
347 * Generates a platform-specific query so that the column data can be retrieved
348 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000349 * @param string the table name
Andrey Andreevb76029d2012-01-26 15:13:19 +0200350 * @return string
Derek Allard2067d1a2008-11-13 22:59:24 +0000351 */
Andrey Andreevb76029d2012-01-26 15:13:19 +0200352 protected function _field_data($table)
Derek Allard2067d1a2008-11-13 22:59:24 +0000353 {
Andrey Andreevb76029d2012-01-26 15:13:19 +0200354 return 'SELECT TOP 1 FROM '.$table;
Derek Allard2067d1a2008-11-13 22:59:24 +0000355 }
356
357 // --------------------------------------------------------------------
358
359 /**
Andrey Andreev4be5de12012-03-02 15:45:41 +0200360 * Error
Derek Allard2067d1a2008-11-13 22:59:24 +0000361 *
Andrey Andreev4be5de12012-03-02 15:45:41 +0200362 * Returns an array containing code and message of the last
363 * database error that has occured.
Derek Allard2067d1a2008-11-13 22:59:24 +0000364 *
Andrey Andreev4be5de12012-03-02 15:45:41 +0200365 * @return array
Derek Allard2067d1a2008-11-13 22:59:24 +0000366 */
Andrey Andreev4be5de12012-03-02 15:45:41 +0200367 public function error()
Derek Allard2067d1a2008-11-13 22:59:24 +0000368 {
Andrey Andreev4be5de12012-03-02 15:45:41 +0200369 return array('code' => odbc_error($this->conn_id), 'message' => odbc_errormsg($this->conn_id));
Derek Allard2067d1a2008-11-13 22:59:24 +0000370 }
371
372 // --------------------------------------------------------------------
373
374 /**
375 * Escape the SQL Identifiers
376 *
377 * This function escapes column and table names
378 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000379 * @param string
380 * @return string
381 */
Andrey Andreevb76029d2012-01-26 15:13:19 +0200382 public function _escape_identifiers($item)
Derek Allard2067d1a2008-11-13 22:59:24 +0000383 {
384 if ($this->_escape_char == '')
385 {
386 return $item;
387 }
388
389 foreach ($this->_reserved_identifiers as $id)
390 {
391 if (strpos($item, '.'.$id) !== FALSE)
392 {
Andrey Andreevb76029d2012-01-26 15:13:19 +0200393 $item = str_replace('.', $this->_escape_char.'.', $item);
Barry Mienydd671972010-10-04 16:33:58 +0200394
Derek Allard2067d1a2008-11-13 22:59:24 +0000395 // remove duplicates if the user already included the escape
Andrey Andreevb76029d2012-01-26 15:13:19 +0200396 return preg_replace('/['.$this->_escape_char.']+/', $this->_escape_char, $this->_escape_char.$item);
Barry Mienydd671972010-10-04 16:33:58 +0200397 }
Derek Allard2067d1a2008-11-13 22:59:24 +0000398 }
Barry Mienydd671972010-10-04 16:33:58 +0200399
Derek Allard2067d1a2008-11-13 22:59:24 +0000400 if (strpos($item, '.') !== FALSE)
401 {
Andrey Andreevb76029d2012-01-26 15:13:19 +0200402 $item = str_replace('.', $this->_escape_char.'.'.$this->_escape_char, $item);
Derek Allard2067d1a2008-11-13 22:59:24 +0000403 }
Barry Mienydd671972010-10-04 16:33:58 +0200404
Derek Allard2067d1a2008-11-13 22:59:24 +0000405 // remove duplicates if the user already included the escape
Andrey Andreevb76029d2012-01-26 15:13:19 +0200406 return preg_replace('/['.$this->_escape_char.']+/', $this->_escape_char, $this->_escape_char.$item.$this->_escape_char);
Derek Allard2067d1a2008-11-13 22:59:24 +0000407 }
Barry Mienydd671972010-10-04 16:33:58 +0200408
Derek Allard2067d1a2008-11-13 22:59:24 +0000409 // --------------------------------------------------------------------
410
411 /**
412 * From Tables
413 *
414 * This function implicitly groups FROM tables so there is no confusion
415 * about operator precedence in harmony with SQL standards
416 *
Andrey Andreevb76029d2012-01-26 15:13:19 +0200417 * @param string the table name
418 * @return string
Derek Allard2067d1a2008-11-13 22:59:24 +0000419 */
Andrey Andreevb76029d2012-01-26 15:13:19 +0200420 protected function _from_tables($tables)
Derek Allard2067d1a2008-11-13 22:59:24 +0000421 {
422 if ( ! is_array($tables))
423 {
424 $tables = array($tables);
425 }
Barry Mienydd671972010-10-04 16:33:58 +0200426
Derek Allard2067d1a2008-11-13 22:59:24 +0000427 return '('.implode(', ', $tables).')';
428 }
429
430 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200431
Derek Allard2067d1a2008-11-13 22:59:24 +0000432 /**
433 * Insert statement
434 *
435 * Generates a platform-specific insert string from the supplied data
436 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000437 * @param string the table name
438 * @param array the insert keys
439 * @param array the insert values
440 * @return string
441 */
Andrey Andreevb76029d2012-01-26 15:13:19 +0200442 protected function _insert($table, $keys, $values)
Barry Mienydd671972010-10-04 16:33:58 +0200443 {
Andrey Andreevb76029d2012-01-26 15:13:19 +0200444 return 'INSERT INTO '.$table.' ('.implode(', ', $keys).') VALUES ('.implode(', ', $values).')';
Derek Allard2067d1a2008-11-13 22:59:24 +0000445 }
Barry Mienydd671972010-10-04 16:33:58 +0200446
Derek Allard2067d1a2008-11-13 22:59:24 +0000447 // --------------------------------------------------------------------
448
449 /**
450 * Update statement
451 *
452 * Generates a platform-specific update string from the supplied data
453 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000454 * @param string the table name
455 * @param array the update data
456 * @param array the where clause
457 * @param array the orderby clause
458 * @param array the limit clause
459 * @return string
460 */
Andrey Andreevb76029d2012-01-26 15:13:19 +0200461 protected function _update($table, $values, $where, $orderby = array(), $limit = FALSE)
Derek Allard2067d1a2008-11-13 22:59:24 +0000462 {
Pascal Krietec3a4a8d2011-02-14 13:40:08 -0500463 foreach ($values as $key => $val)
Derek Allard2067d1a2008-11-13 22:59:24 +0000464 {
Andrey Andreevb76029d2012-01-26 15:13:19 +0200465 $valstr[] = $key.' = '.$val;
Derek Allard2067d1a2008-11-13 22:59:24 +0000466 }
Barry Mienydd671972010-10-04 16:33:58 +0200467
Andrey Andreevb76029d2012-01-26 15:13:19 +0200468 return 'UPDATE '.$table.' SET '.implode(', ', $valstr)
469 .(($where != '' && count($where) > 0) ? ' WHERE '.implode(' ', $where) : '')
470 .(count($orderby) > 0 ? ' ORDER BY '.implode(', ', $orderby) : '')
471 .( ! $limit ? '' : ' LIMIT '.$limit);
Derek Allard2067d1a2008-11-13 22:59:24 +0000472 }
473
Barry Mienydd671972010-10-04 16:33:58 +0200474
Derek Allard2067d1a2008-11-13 22:59:24 +0000475 // --------------------------------------------------------------------
476
477 /**
478 * Truncate statement
479 *
480 * Generates a platform-specific truncate string from the supplied data
481 * If the database does not support the truncate() command
482 * This function maps to "DELETE FROM table"
483 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000484 * @param string the table name
485 * @return string
Barry Mienydd671972010-10-04 16:33:58 +0200486 */
Andrey Andreevb76029d2012-01-26 15:13:19 +0200487 protected function _truncate($table)
Derek Allard2067d1a2008-11-13 22:59:24 +0000488 {
489 return $this->_delete($table);
490 }
Barry Mienydd671972010-10-04 16:33:58 +0200491
Derek Allard2067d1a2008-11-13 22:59:24 +0000492 // --------------------------------------------------------------------
493
494 /**
495 * Delete statement
496 *
497 * Generates a platform-specific delete string from the supplied data
498 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000499 * @param string the table name
500 * @param array the where clause
501 * @param string the limit clause
502 * @return string
Barry Mienydd671972010-10-04 16:33:58 +0200503 */
Andrey Andreevb76029d2012-01-26 15:13:19 +0200504 protected function _delete($table, $where = array(), $like = array(), $limit = FALSE)
Derek Allard2067d1a2008-11-13 22:59:24 +0000505 {
506 $conditions = '';
507
508 if (count($where) > 0 OR count($like) > 0)
509 {
Andrey Andreevb76029d2012-01-26 15:13:19 +0200510 $conditions = "\nWHERE ".implode("\n", $this->ar_where);
Derek Allard2067d1a2008-11-13 22:59:24 +0000511
512 if (count($where) > 0 && count($like) > 0)
513 {
Andrey Andreevb76029d2012-01-26 15:13:19 +0200514 $conditions .= ' AND ';
Derek Allard2067d1a2008-11-13 22:59:24 +0000515 }
516 $conditions .= implode("\n", $like);
517 }
518
Andrey Andreevb76029d2012-01-26 15:13:19 +0200519 return 'DELETE FROM '.$table.$conditions.( ! $limit ? '' : ' LIMIT '.$limit);
Derek Allard2067d1a2008-11-13 22:59:24 +0000520 }
521
522 // --------------------------------------------------------------------
523
524 /**
525 * Limit string
526 *
527 * Generates a platform-specific LIMIT clause
528 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000529 * @param string the sql query string
Andrey Andreevb76029d2012-01-26 15:13:19 +0200530 * @param int the number of rows to limit the query to
531 * @param int the offset value
Derek Allard2067d1a2008-11-13 22:59:24 +0000532 * @return string
533 */
Andrey Andreevb76029d2012-01-26 15:13:19 +0200534 protected function _limit($sql, $limit, $offset)
Derek Allard2067d1a2008-11-13 22:59:24 +0000535 {
Andrey Andreevb76029d2012-01-26 15:13:19 +0200536 return $sql.($offset == 0 ? '' : $offset.', ').$limit;
Derek Allard2067d1a2008-11-13 22:59:24 +0000537 }
538
539 // --------------------------------------------------------------------
540
541 /**
542 * Close DB Connection
543 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000544 * @param resource
545 * @return void
546 */
Andrey Andreevb76029d2012-01-26 15:13:19 +0200547 protected function _close($conn_id)
Derek Allard2067d1a2008-11-13 22:59:24 +0000548 {
549 @odbc_close($conn_id);
550 }
551
Derek Allard2067d1a2008-11-13 22:59:24 +0000552}
553
Derek Allard2067d1a2008-11-13 22:59:24 +0000554/* End of file odbc_driver.php */
Andrey Andreevb76029d2012-01-26 15:13:19 +0200555/* Location: ./system/database/drivers/odbc/odbc_driver.php */