blob: 01515221b6c08e14e29c2a2d99af8bdf64bae05f [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 Andreev8af76662012-03-05 14:33:41 +0200266 * @return bool
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 {
Andrey Andreev8af76662012-03-05 14:33:41 +0200270 return ($this->db->db_debug) ? $this->db->display_error('db_unsuported_feature') : FALSE;
Derek Allard2067d1a2008-11-13 22:59:24 +0000271 }
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 Andreev97715482012-03-09 14:21:54 +0200291 $query = $this->query($this->_count_string.$this->protect_identifiers('numrows').' FROM '.$this->protect_identifiers($table, TRUE, NULL, FALSE));
Derek Allard2067d1a2008-11-13 22:59:24 +0000292 if ($query->num_rows() == 0)
Derek Allarde37ab382009-02-03 16:13:57 +0000293 {
294 return 0;
295 }
Derek Allard2067d1a2008-11-13 22:59:24 +0000296
Andrey Andreevb76029d2012-01-26 15:13:19 +0200297 $query = $query->row();
Greg Aker90248ab2011-08-20 14:23:14 -0500298 $this->_reset_select();
Andrey Andreevb76029d2012-01-26 15:13:19 +0200299 return (int) $query->numrows;
Derek Allard2067d1a2008-11-13 22:59:24 +0000300 }
301
302 // --------------------------------------------------------------------
303
304 /**
305 * Show table query
306 *
307 * Generates a platform-specific query string so that the table names can be fetched
308 *
309 * @access private
310 * @param boolean
311 * @return string
312 */
Andrey Andreevb76029d2012-01-26 15:13:19 +0200313 protected function _list_tables($prefix_limit = FALSE)
Derek Allard2067d1a2008-11-13 22:59:24 +0000314 {
Andrey Andreevb76029d2012-01-26 15:13:19 +0200315 $sql = 'SHOW TABLES FROM `'.$this->database.'`';
Derek Allard2067d1a2008-11-13 22:59:24 +0000316
Andrey Andreevb76029d2012-01-26 15:13:19 +0200317 if ($prefix_limit !== FALSE && $this->dbprefix != '')
Derek Allard2067d1a2008-11-13 22:59:24 +0000318 {
Greg Aker0d424892010-01-26 02:14:44 +0000319 //$sql .= " LIKE '".$this->escape_like_str($this->dbprefix)."%' ".sprintf($this->_like_escape_str, $this->_like_escape_chr);
Derek Allard2067d1a2008-11-13 22:59:24 +0000320 return FALSE; // not currently supported
321 }
Barry Mienydd671972010-10-04 16:33:58 +0200322
Derek Allard2067d1a2008-11-13 22:59:24 +0000323 return $sql;
324 }
Barry Mienydd671972010-10-04 16:33:58 +0200325
Derek Allard2067d1a2008-11-13 22:59:24 +0000326 // --------------------------------------------------------------------
327
328 /**
329 * Show column query
330 *
331 * Generates a platform-specific query string so that the column names can be fetched
332 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000333 * @param string the table name
334 * @return string
335 */
Andrey Andreevb76029d2012-01-26 15:13:19 +0200336 protected function _list_columns($table = '')
Derek Allard2067d1a2008-11-13 22:59:24 +0000337 {
Andrey Andreevb76029d2012-01-26 15:13:19 +0200338 return 'SHOW COLUMNS FROM '.$table;
Derek Allard2067d1a2008-11-13 22:59:24 +0000339 }
340
341 // --------------------------------------------------------------------
342
343 /**
344 * Field data query
345 *
346 * Generates a platform-specific query so that the column data can be retrieved
347 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000348 * @param string the table name
Andrey Andreevb76029d2012-01-26 15:13:19 +0200349 * @return string
Derek Allard2067d1a2008-11-13 22:59:24 +0000350 */
Andrey Andreevb76029d2012-01-26 15:13:19 +0200351 protected function _field_data($table)
Derek Allard2067d1a2008-11-13 22:59:24 +0000352 {
Andrey Andreevb76029d2012-01-26 15:13:19 +0200353 return 'SELECT TOP 1 FROM '.$table;
Derek Allard2067d1a2008-11-13 22:59:24 +0000354 }
355
356 // --------------------------------------------------------------------
357
358 /**
Andrey Andreev4be5de12012-03-02 15:45:41 +0200359 * Error
Derek Allard2067d1a2008-11-13 22:59:24 +0000360 *
Andrey Andreev4be5de12012-03-02 15:45:41 +0200361 * Returns an array containing code and message of the last
362 * database error that has occured.
Derek Allard2067d1a2008-11-13 22:59:24 +0000363 *
Andrey Andreev4be5de12012-03-02 15:45:41 +0200364 * @return array
Derek Allard2067d1a2008-11-13 22:59:24 +0000365 */
Andrey Andreev4be5de12012-03-02 15:45:41 +0200366 public function error()
Derek Allard2067d1a2008-11-13 22:59:24 +0000367 {
Andrey Andreev4be5de12012-03-02 15:45:41 +0200368 return array('code' => odbc_error($this->conn_id), 'message' => odbc_errormsg($this->conn_id));
Derek Allard2067d1a2008-11-13 22:59:24 +0000369 }
370
371 // --------------------------------------------------------------------
372
373 /**
374 * Escape the SQL Identifiers
375 *
376 * This function escapes column and table names
377 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000378 * @param string
379 * @return string
380 */
Andrey Andreevb76029d2012-01-26 15:13:19 +0200381 public function _escape_identifiers($item)
Derek Allard2067d1a2008-11-13 22:59:24 +0000382 {
383 if ($this->_escape_char == '')
384 {
385 return $item;
386 }
387
388 foreach ($this->_reserved_identifiers as $id)
389 {
390 if (strpos($item, '.'.$id) !== FALSE)
391 {
Andrey Andreevb76029d2012-01-26 15:13:19 +0200392 $item = str_replace('.', $this->_escape_char.'.', $item);
Barry Mienydd671972010-10-04 16:33:58 +0200393
Derek Allard2067d1a2008-11-13 22:59:24 +0000394 // remove duplicates if the user already included the escape
Andrey Andreevb76029d2012-01-26 15:13:19 +0200395 return preg_replace('/['.$this->_escape_char.']+/', $this->_escape_char, $this->_escape_char.$item);
Barry Mienydd671972010-10-04 16:33:58 +0200396 }
Derek Allard2067d1a2008-11-13 22:59:24 +0000397 }
Barry Mienydd671972010-10-04 16:33:58 +0200398
Derek Allard2067d1a2008-11-13 22:59:24 +0000399 if (strpos($item, '.') !== FALSE)
400 {
Andrey Andreevb76029d2012-01-26 15:13:19 +0200401 $item = str_replace('.', $this->_escape_char.'.'.$this->_escape_char, $item);
Derek Allard2067d1a2008-11-13 22:59:24 +0000402 }
Barry Mienydd671972010-10-04 16:33:58 +0200403
Derek Allard2067d1a2008-11-13 22:59:24 +0000404 // remove duplicates if the user already included the escape
Andrey Andreevb76029d2012-01-26 15:13:19 +0200405 return preg_replace('/['.$this->_escape_char.']+/', $this->_escape_char, $this->_escape_char.$item.$this->_escape_char);
Derek Allard2067d1a2008-11-13 22:59:24 +0000406 }
Barry Mienydd671972010-10-04 16:33:58 +0200407
Derek Allard2067d1a2008-11-13 22:59:24 +0000408 // --------------------------------------------------------------------
409
410 /**
411 * From Tables
412 *
413 * This function implicitly groups FROM tables so there is no confusion
414 * about operator precedence in harmony with SQL standards
415 *
Andrey Andreevb76029d2012-01-26 15:13:19 +0200416 * @param string the table name
417 * @return string
Derek Allard2067d1a2008-11-13 22:59:24 +0000418 */
Andrey Andreevb76029d2012-01-26 15:13:19 +0200419 protected function _from_tables($tables)
Derek Allard2067d1a2008-11-13 22:59:24 +0000420 {
421 if ( ! is_array($tables))
422 {
423 $tables = array($tables);
424 }
Barry Mienydd671972010-10-04 16:33:58 +0200425
Derek Allard2067d1a2008-11-13 22:59:24 +0000426 return '('.implode(', ', $tables).')';
427 }
428
429 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200430
Derek Allard2067d1a2008-11-13 22:59:24 +0000431 /**
432 * Insert statement
433 *
434 * Generates a platform-specific insert string from the supplied data
435 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000436 * @param string the table name
437 * @param array the insert keys
438 * @param array the insert values
439 * @return string
440 */
Andrey Andreevb76029d2012-01-26 15:13:19 +0200441 protected function _insert($table, $keys, $values)
Barry Mienydd671972010-10-04 16:33:58 +0200442 {
Andrey Andreevb76029d2012-01-26 15:13:19 +0200443 return 'INSERT INTO '.$table.' ('.implode(', ', $keys).') VALUES ('.implode(', ', $values).')';
Derek Allard2067d1a2008-11-13 22:59:24 +0000444 }
Barry Mienydd671972010-10-04 16:33:58 +0200445
Derek Allard2067d1a2008-11-13 22:59:24 +0000446 // --------------------------------------------------------------------
447
448 /**
449 * Update statement
450 *
451 * Generates a platform-specific update string from the supplied data
452 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000453 * @param string the table name
454 * @param array the update data
455 * @param array the where clause
456 * @param array the orderby clause
457 * @param array the limit clause
458 * @return string
459 */
Andrey Andreevb76029d2012-01-26 15:13:19 +0200460 protected function _update($table, $values, $where, $orderby = array(), $limit = FALSE)
Derek Allard2067d1a2008-11-13 22:59:24 +0000461 {
Pascal Krietec3a4a8d2011-02-14 13:40:08 -0500462 foreach ($values as $key => $val)
Derek Allard2067d1a2008-11-13 22:59:24 +0000463 {
Andrey Andreevb76029d2012-01-26 15:13:19 +0200464 $valstr[] = $key.' = '.$val;
Derek Allard2067d1a2008-11-13 22:59:24 +0000465 }
Barry Mienydd671972010-10-04 16:33:58 +0200466
Andrey Andreevb76029d2012-01-26 15:13:19 +0200467 return 'UPDATE '.$table.' SET '.implode(', ', $valstr)
468 .(($where != '' && count($where) > 0) ? ' WHERE '.implode(' ', $where) : '')
469 .(count($orderby) > 0 ? ' ORDER BY '.implode(', ', $orderby) : '')
470 .( ! $limit ? '' : ' LIMIT '.$limit);
Derek Allard2067d1a2008-11-13 22:59:24 +0000471 }
472
Barry Mienydd671972010-10-04 16:33:58 +0200473
Derek Allard2067d1a2008-11-13 22:59:24 +0000474 // --------------------------------------------------------------------
475
476 /**
477 * Truncate statement
478 *
479 * Generates a platform-specific truncate string from the supplied data
480 * If the database does not support the truncate() command
481 * This function maps to "DELETE FROM table"
482 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000483 * @param string the table name
484 * @return string
Barry Mienydd671972010-10-04 16:33:58 +0200485 */
Andrey Andreevb76029d2012-01-26 15:13:19 +0200486 protected function _truncate($table)
Derek Allard2067d1a2008-11-13 22:59:24 +0000487 {
488 return $this->_delete($table);
489 }
Barry Mienydd671972010-10-04 16:33:58 +0200490
Derek Allard2067d1a2008-11-13 22:59:24 +0000491 // --------------------------------------------------------------------
492
493 /**
494 * Delete statement
495 *
496 * Generates a platform-specific delete string from the supplied data
497 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000498 * @param string the table name
499 * @param array the where clause
500 * @param string the limit clause
501 * @return string
Barry Mienydd671972010-10-04 16:33:58 +0200502 */
Andrey Andreevb76029d2012-01-26 15:13:19 +0200503 protected function _delete($table, $where = array(), $like = array(), $limit = FALSE)
Derek Allard2067d1a2008-11-13 22:59:24 +0000504 {
505 $conditions = '';
506
507 if (count($where) > 0 OR count($like) > 0)
508 {
Andrey Andreevb76029d2012-01-26 15:13:19 +0200509 $conditions = "\nWHERE ".implode("\n", $this->ar_where);
Derek Allard2067d1a2008-11-13 22:59:24 +0000510
511 if (count($where) > 0 && count($like) > 0)
512 {
Andrey Andreevb76029d2012-01-26 15:13:19 +0200513 $conditions .= ' AND ';
Derek Allard2067d1a2008-11-13 22:59:24 +0000514 }
515 $conditions .= implode("\n", $like);
516 }
517
Andrey Andreevb76029d2012-01-26 15:13:19 +0200518 return 'DELETE FROM '.$table.$conditions.( ! $limit ? '' : ' LIMIT '.$limit);
Derek Allard2067d1a2008-11-13 22:59:24 +0000519 }
520
521 // --------------------------------------------------------------------
522
523 /**
524 * Limit string
525 *
526 * Generates a platform-specific LIMIT clause
527 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000528 * @param string the sql query string
Andrey Andreevb76029d2012-01-26 15:13:19 +0200529 * @param int the number of rows to limit the query to
530 * @param int the offset value
Derek Allard2067d1a2008-11-13 22:59:24 +0000531 * @return string
532 */
Andrey Andreevb76029d2012-01-26 15:13:19 +0200533 protected function _limit($sql, $limit, $offset)
Derek Allard2067d1a2008-11-13 22:59:24 +0000534 {
Andrey Andreevb76029d2012-01-26 15:13:19 +0200535 return $sql.($offset == 0 ? '' : $offset.', ').$limit;
Derek Allard2067d1a2008-11-13 22:59:24 +0000536 }
537
538 // --------------------------------------------------------------------
539
540 /**
541 * Close DB Connection
542 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000543 * @param resource
544 * @return void
545 */
Andrey Andreevb76029d2012-01-26 15:13:19 +0200546 protected function _close($conn_id)
Derek Allard2067d1a2008-11-13 22:59:24 +0000547 {
548 @odbc_close($conn_id);
549 }
550
Derek Allard2067d1a2008-11-13 22:59:24 +0000551}
552
Derek Allard2067d1a2008-11-13 22:59:24 +0000553/* End of file odbc_driver.php */
Andrey Andreevb76029d2012-01-26 15:13:19 +0200554/* Location: ./system/database/drivers/odbc/odbc_driver.php */