blob: 47d3a340b95e615544adf89c1dc631a3c7da75e0 [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
Greg Aker757dda62010-04-14 19:06:19 -0500235 $str = remove_invisible_characters($str);
Barry Mienydd671972010-10-04 16:33:58 +0200236
Derek Jonese4ed5832009-02-20 21:44:59 +0000237 // escape LIKE condition wildcards
238 if ($like === TRUE)
239 {
Andrey Andreev5e18e892012-03-09 14:25:00 +0200240 return str_replace(array($this->_like_escape_chr, '%', '_'),
241 array($this->_like_escape_chr.$this->_like_escape_chr, $this->_like_escape_chr.'%', $this->_like_escape_chr.'_'),
Andrey Andreevb76029d2012-01-26 15:13:19 +0200242 $str);
Derek Jonese4ed5832009-02-20 21:44:59 +0000243 }
Barry Mienydd671972010-10-04 16:33:58 +0200244
Derek Jonese4ed5832009-02-20 21:44:59 +0000245 return $str;
Derek Allard2067d1a2008-11-13 22:59:24 +0000246 }
Barry Mienydd671972010-10-04 16:33:58 +0200247
Derek Allard2067d1a2008-11-13 22:59:24 +0000248 // --------------------------------------------------------------------
249
250 /**
251 * Affected Rows
252 *
Andrey Andreevb76029d2012-01-26 15:13:19 +0200253 * @return int
Derek Allard2067d1a2008-11-13 22:59:24 +0000254 */
Andrey Andreevb76029d2012-01-26 15:13:19 +0200255 public function affected_rows()
Derek Allard2067d1a2008-11-13 22:59:24 +0000256 {
257 return @odbc_num_rows($this->conn_id);
258 }
Barry Mienydd671972010-10-04 16:33:58 +0200259
Derek Allard2067d1a2008-11-13 22:59:24 +0000260 // --------------------------------------------------------------------
261
262 /**
263 * Insert ID
264 *
Andrey Andreev8af76662012-03-05 14:33:41 +0200265 * @return bool
Derek Allard2067d1a2008-11-13 22:59:24 +0000266 */
Andrey Andreevb76029d2012-01-26 15:13:19 +0200267 public function insert_id()
Derek Allard2067d1a2008-11-13 22:59:24 +0000268 {
Andrey Andreev8af76662012-03-05 14:33:41 +0200269 return ($this->db->db_debug) ? $this->db->display_error('db_unsuported_feature') : FALSE;
Derek Allard2067d1a2008-11-13 22:59:24 +0000270 }
271
272 // --------------------------------------------------------------------
273
274 /**
275 * "Count All" query
276 *
277 * Generates a platform-specific query string that counts all records in
278 * the specified database
279 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000280 * @param string
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 count_all($table = '')
Derek Allard2067d1a2008-11-13 22:59:24 +0000284 {
285 if ($table == '')
Derek Allarde37ab382009-02-03 16:13:57 +0000286 {
287 return 0;
288 }
289
Andrey Andreev97715482012-03-09 14:21:54 +0200290 $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 +0000291 if ($query->num_rows() == 0)
Derek Allarde37ab382009-02-03 16:13:57 +0000292 {
293 return 0;
294 }
Derek Allard2067d1a2008-11-13 22:59:24 +0000295
Andrey Andreevb76029d2012-01-26 15:13:19 +0200296 $query = $query->row();
Greg Aker90248ab2011-08-20 14:23:14 -0500297 $this->_reset_select();
Andrey Andreevb76029d2012-01-26 15:13:19 +0200298 return (int) $query->numrows;
Derek Allard2067d1a2008-11-13 22:59:24 +0000299 }
300
301 // --------------------------------------------------------------------
302
303 /**
304 * Show table query
305 *
306 * Generates a platform-specific query string so that the table names can be fetched
307 *
308 * @access private
309 * @param boolean
310 * @return string
311 */
Andrey Andreevb76029d2012-01-26 15:13:19 +0200312 protected function _list_tables($prefix_limit = FALSE)
Derek Allard2067d1a2008-11-13 22:59:24 +0000313 {
Andrey Andreevb76029d2012-01-26 15:13:19 +0200314 $sql = 'SHOW TABLES FROM `'.$this->database.'`';
Derek Allard2067d1a2008-11-13 22:59:24 +0000315
Andrey Andreevb76029d2012-01-26 15:13:19 +0200316 if ($prefix_limit !== FALSE && $this->dbprefix != '')
Derek Allard2067d1a2008-11-13 22:59:24 +0000317 {
Greg Aker0d424892010-01-26 02:14:44 +0000318 //$sql .= " LIKE '".$this->escape_like_str($this->dbprefix)."%' ".sprintf($this->_like_escape_str, $this->_like_escape_chr);
Derek Allard2067d1a2008-11-13 22:59:24 +0000319 return FALSE; // not currently supported
320 }
Barry Mienydd671972010-10-04 16:33:58 +0200321
Derek Allard2067d1a2008-11-13 22:59:24 +0000322 return $sql;
323 }
Barry Mienydd671972010-10-04 16:33:58 +0200324
Derek Allard2067d1a2008-11-13 22:59:24 +0000325 // --------------------------------------------------------------------
326
327 /**
328 * Show column query
329 *
330 * Generates a platform-specific query string so that the column names can be fetched
331 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000332 * @param string the table name
333 * @return string
334 */
Andrey Andreevb76029d2012-01-26 15:13:19 +0200335 protected function _list_columns($table = '')
Derek Allard2067d1a2008-11-13 22:59:24 +0000336 {
Andrey Andreevb76029d2012-01-26 15:13:19 +0200337 return 'SHOW COLUMNS FROM '.$table;
Derek Allard2067d1a2008-11-13 22:59:24 +0000338 }
339
340 // --------------------------------------------------------------------
341
342 /**
343 * Field data query
344 *
345 * Generates a platform-specific query so that the column data can be retrieved
346 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000347 * @param string the table name
Andrey Andreevb76029d2012-01-26 15:13:19 +0200348 * @return string
Derek Allard2067d1a2008-11-13 22:59:24 +0000349 */
Andrey Andreevb76029d2012-01-26 15:13:19 +0200350 protected function _field_data($table)
Derek Allard2067d1a2008-11-13 22:59:24 +0000351 {
Andrey Andreevb76029d2012-01-26 15:13:19 +0200352 return 'SELECT TOP 1 FROM '.$table;
Derek Allard2067d1a2008-11-13 22:59:24 +0000353 }
354
355 // --------------------------------------------------------------------
356
357 /**
Andrey Andreev4be5de12012-03-02 15:45:41 +0200358 * Error
Derek Allard2067d1a2008-11-13 22:59:24 +0000359 *
Andrey Andreev4be5de12012-03-02 15:45:41 +0200360 * Returns an array containing code and message of the last
361 * database error that has occured.
Derek Allard2067d1a2008-11-13 22:59:24 +0000362 *
Andrey Andreev4be5de12012-03-02 15:45:41 +0200363 * @return array
Derek Allard2067d1a2008-11-13 22:59:24 +0000364 */
Andrey Andreev4be5de12012-03-02 15:45:41 +0200365 public function error()
Derek Allard2067d1a2008-11-13 22:59:24 +0000366 {
Andrey Andreev4be5de12012-03-02 15:45:41 +0200367 return array('code' => odbc_error($this->conn_id), 'message' => odbc_errormsg($this->conn_id));
Derek Allard2067d1a2008-11-13 22:59:24 +0000368 }
369
370 // --------------------------------------------------------------------
371
372 /**
373 * Escape the SQL Identifiers
374 *
375 * This function escapes column and table names
376 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000377 * @param string
378 * @return string
379 */
Andrey Andreevb76029d2012-01-26 15:13:19 +0200380 public function _escape_identifiers($item)
Derek Allard2067d1a2008-11-13 22:59:24 +0000381 {
382 if ($this->_escape_char == '')
383 {
384 return $item;
385 }
386
387 foreach ($this->_reserved_identifiers as $id)
388 {
389 if (strpos($item, '.'.$id) !== FALSE)
390 {
Andrey Andreevb76029d2012-01-26 15:13:19 +0200391 $item = str_replace('.', $this->_escape_char.'.', $item);
Barry Mienydd671972010-10-04 16:33:58 +0200392
Derek Allard2067d1a2008-11-13 22:59:24 +0000393 // remove duplicates if the user already included the escape
Andrey Andreevb76029d2012-01-26 15:13:19 +0200394 return preg_replace('/['.$this->_escape_char.']+/', $this->_escape_char, $this->_escape_char.$item);
Barry Mienydd671972010-10-04 16:33:58 +0200395 }
Derek Allard2067d1a2008-11-13 22:59:24 +0000396 }
Barry Mienydd671972010-10-04 16:33:58 +0200397
Derek Allard2067d1a2008-11-13 22:59:24 +0000398 if (strpos($item, '.') !== FALSE)
399 {
Andrey Andreevb76029d2012-01-26 15:13:19 +0200400 $item = str_replace('.', $this->_escape_char.'.'.$this->_escape_char, $item);
Derek Allard2067d1a2008-11-13 22:59:24 +0000401 }
Barry Mienydd671972010-10-04 16:33:58 +0200402
Derek Allard2067d1a2008-11-13 22:59:24 +0000403 // remove duplicates if the user already included the escape
Andrey Andreevb76029d2012-01-26 15:13:19 +0200404 return preg_replace('/['.$this->_escape_char.']+/', $this->_escape_char, $this->_escape_char.$item.$this->_escape_char);
Derek Allard2067d1a2008-11-13 22:59:24 +0000405 }
Barry Mienydd671972010-10-04 16:33:58 +0200406
Derek Allard2067d1a2008-11-13 22:59:24 +0000407 // --------------------------------------------------------------------
408
409 /**
410 * From Tables
411 *
412 * This function implicitly groups FROM tables so there is no confusion
413 * about operator precedence in harmony with SQL standards
414 *
Andrey Andreevb76029d2012-01-26 15:13:19 +0200415 * @param string the table name
416 * @return string
Derek Allard2067d1a2008-11-13 22:59:24 +0000417 */
Andrey Andreevb76029d2012-01-26 15:13:19 +0200418 protected function _from_tables($tables)
Derek Allard2067d1a2008-11-13 22:59:24 +0000419 {
420 if ( ! is_array($tables))
421 {
422 $tables = array($tables);
423 }
Barry Mienydd671972010-10-04 16:33:58 +0200424
Derek Allard2067d1a2008-11-13 22:59:24 +0000425 return '('.implode(', ', $tables).')';
426 }
427
428 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200429
Derek Allard2067d1a2008-11-13 22:59:24 +0000430 /**
431 * Insert statement
432 *
433 * Generates a platform-specific insert string from the supplied data
434 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000435 * @param string the table name
436 * @param array the insert keys
437 * @param array the insert values
438 * @return string
439 */
Andrey Andreevb76029d2012-01-26 15:13:19 +0200440 protected function _insert($table, $keys, $values)
Barry Mienydd671972010-10-04 16:33:58 +0200441 {
Andrey Andreevb76029d2012-01-26 15:13:19 +0200442 return 'INSERT INTO '.$table.' ('.implode(', ', $keys).') VALUES ('.implode(', ', $values).')';
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 * Update statement
449 *
450 * Generates a platform-specific update string from the supplied data
451 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000452 * @param string the table name
453 * @param array the update data
454 * @param array the where clause
455 * @param array the orderby clause
456 * @param array the limit clause
457 * @return string
458 */
Andrey Andreevb76029d2012-01-26 15:13:19 +0200459 protected function _update($table, $values, $where, $orderby = array(), $limit = FALSE)
Derek Allard2067d1a2008-11-13 22:59:24 +0000460 {
Pascal Krietec3a4a8d2011-02-14 13:40:08 -0500461 foreach ($values as $key => $val)
Derek Allard2067d1a2008-11-13 22:59:24 +0000462 {
Andrey Andreevb76029d2012-01-26 15:13:19 +0200463 $valstr[] = $key.' = '.$val;
Derek Allard2067d1a2008-11-13 22:59:24 +0000464 }
Barry Mienydd671972010-10-04 16:33:58 +0200465
Andrey Andreevb76029d2012-01-26 15:13:19 +0200466 return 'UPDATE '.$table.' SET '.implode(', ', $valstr)
467 .(($where != '' && count($where) > 0) ? ' WHERE '.implode(' ', $where) : '')
468 .(count($orderby) > 0 ? ' ORDER BY '.implode(', ', $orderby) : '')
469 .( ! $limit ? '' : ' LIMIT '.$limit);
Derek Allard2067d1a2008-11-13 22:59:24 +0000470 }
471
Barry Mienydd671972010-10-04 16:33:58 +0200472
Derek Allard2067d1a2008-11-13 22:59:24 +0000473 // --------------------------------------------------------------------
474
475 /**
476 * Truncate statement
477 *
478 * Generates a platform-specific truncate string from the supplied data
479 * If the database does not support the truncate() command
480 * This function maps to "DELETE FROM table"
481 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000482 * @param string the table name
483 * @return string
Barry Mienydd671972010-10-04 16:33:58 +0200484 */
Andrey Andreevb76029d2012-01-26 15:13:19 +0200485 protected function _truncate($table)
Derek Allard2067d1a2008-11-13 22:59:24 +0000486 {
487 return $this->_delete($table);
488 }
Barry Mienydd671972010-10-04 16:33:58 +0200489
Derek Allard2067d1a2008-11-13 22:59:24 +0000490 // --------------------------------------------------------------------
491
492 /**
493 * Delete statement
494 *
495 * Generates a platform-specific delete string from the supplied data
496 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000497 * @param string the table name
498 * @param array the where clause
499 * @param string the limit clause
500 * @return string
Barry Mienydd671972010-10-04 16:33:58 +0200501 */
Andrey Andreevb76029d2012-01-26 15:13:19 +0200502 protected function _delete($table, $where = array(), $like = array(), $limit = FALSE)
Derek Allard2067d1a2008-11-13 22:59:24 +0000503 {
504 $conditions = '';
505
506 if (count($where) > 0 OR count($like) > 0)
507 {
Andrey Andreevb76029d2012-01-26 15:13:19 +0200508 $conditions = "\nWHERE ".implode("\n", $this->ar_where);
Derek Allard2067d1a2008-11-13 22:59:24 +0000509
510 if (count($where) > 0 && count($like) > 0)
511 {
Andrey Andreevb76029d2012-01-26 15:13:19 +0200512 $conditions .= ' AND ';
Derek Allard2067d1a2008-11-13 22:59:24 +0000513 }
514 $conditions .= implode("\n", $like);
515 }
516
Andrey Andreevb76029d2012-01-26 15:13:19 +0200517 return 'DELETE FROM '.$table.$conditions.( ! $limit ? '' : ' LIMIT '.$limit);
Derek Allard2067d1a2008-11-13 22:59:24 +0000518 }
519
520 // --------------------------------------------------------------------
521
522 /**
523 * Limit string
524 *
525 * Generates a platform-specific LIMIT clause
526 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000527 * @param string the sql query string
Andrey Andreevb76029d2012-01-26 15:13:19 +0200528 * @param int the number of rows to limit the query to
529 * @param int the offset value
Derek Allard2067d1a2008-11-13 22:59:24 +0000530 * @return string
531 */
Andrey Andreevb76029d2012-01-26 15:13:19 +0200532 protected function _limit($sql, $limit, $offset)
Derek Allard2067d1a2008-11-13 22:59:24 +0000533 {
Andrey Andreevb76029d2012-01-26 15:13:19 +0200534 return $sql.($offset == 0 ? '' : $offset.', ').$limit;
Derek Allard2067d1a2008-11-13 22:59:24 +0000535 }
536
537 // --------------------------------------------------------------------
538
539 /**
540 * Close DB Connection
541 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000542 * @param resource
543 * @return void
544 */
Andrey Andreevb76029d2012-01-26 15:13:19 +0200545 protected function _close($conn_id)
Derek Allard2067d1a2008-11-13 22:59:24 +0000546 {
547 @odbc_close($conn_id);
548 }
549
Derek Allard2067d1a2008-11-13 22:59:24 +0000550}
551
Derek Allard2067d1a2008-11-13 22:59:24 +0000552/* End of file odbc_driver.php */
Andrey Andreevb76029d2012-01-26 15:13:19 +0200553/* Location: ./system/database/drivers/odbc/odbc_driver.php */