blob: a674c390e22e54dc3df67cad3cd6222d3a3f2fe0 [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
65 }
66
67 /**
68 * Non-persistent database connection
69 *
Derek Allard2067d1a2008-11-13 22:59:24 +000070 * @return resource
Barry Mienydd671972010-10-04 16:33:58 +020071 */
Andrey Andreevb76029d2012-01-26 15:13:19 +020072 public function db_connect()
Derek Allard2067d1a2008-11-13 22:59:24 +000073 {
74 return @odbc_connect($this->hostname, $this->username, $this->password);
75 }
Barry Mienydd671972010-10-04 16:33:58 +020076
Derek Allard2067d1a2008-11-13 22:59:24 +000077 // --------------------------------------------------------------------
78
79 /**
80 * Persistent database connection
81 *
Derek Allard2067d1a2008-11-13 22:59:24 +000082 * @return resource
Barry Mienydd671972010-10-04 16:33:58 +020083 */
Andrey Andreevb76029d2012-01-26 15:13:19 +020084 public function db_pconnect()
Derek Allard2067d1a2008-11-13 22:59:24 +000085 {
86 return @odbc_pconnect($this->hostname, $this->username, $this->password);
87 }
Barry Mienydd671972010-10-04 16:33:58 +020088
Derek Allard2067d1a2008-11-13 22:59:24 +000089 // --------------------------------------------------------------------
90
91 /**
Derek Jones87cbafc2009-02-27 16:29:59 +000092 * Reconnect
93 *
94 * Keep / reestablish the db connection if no queries have been
95 * sent for a length of time exceeding the server's idle timeout
96 *
Derek Jones87cbafc2009-02-27 16:29:59 +000097 * @return void
98 */
Andrey Andreevb76029d2012-01-26 15:13:19 +020099 public function reconnect()
Derek Jones87cbafc2009-02-27 16:29:59 +0000100 {
101 // not implemented in odbc
102 }
103
104 // --------------------------------------------------------------------
105
106 /**
Derek Allard2067d1a2008-11-13 22:59:24 +0000107 * Select the database
108 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000109 * @return resource
Barry Mienydd671972010-10-04 16:33:58 +0200110 */
Andrey Andreevb76029d2012-01-26 15:13:19 +0200111 public function db_select()
Derek Allard2067d1a2008-11-13 22:59:24 +0000112 {
113 // Not needed for ODBC
114 return TRUE;
115 }
116
117 // --------------------------------------------------------------------
118
119 /**
120 * Set client character set
121 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000122 * @param string
123 * @param string
Andrey Andreevb76029d2012-01-26 15:13:19 +0200124 * @return bool
Derek Allard2067d1a2008-11-13 22:59:24 +0000125 */
Andrey Andreevb76029d2012-01-26 15:13:19 +0200126 public function db_set_charset($charset, $collation)
Derek Allard2067d1a2008-11-13 22:59:24 +0000127 {
128 // @todo - add support if needed
129 return TRUE;
130 }
131
132 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200133
Derek Allard2067d1a2008-11-13 22:59:24 +0000134 /**
135 * Version number query string
136 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000137 * @return string
138 */
Andrey Andreevb76029d2012-01-26 15:13:19 +0200139 protected function _version()
Derek Allard2067d1a2008-11-13 22:59:24 +0000140 {
Andrey Andreevb76029d2012-01-26 15:13:19 +0200141 return 'SELECT version() AS ver';
Derek Allard2067d1a2008-11-13 22:59:24 +0000142 }
143
144 // --------------------------------------------------------------------
145
146 /**
147 * Execute the query
148 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000149 * @param string an SQL query
150 * @return resource
Barry Mienydd671972010-10-04 16:33:58 +0200151 */
Andrey Andreevb76029d2012-01-26 15:13:19 +0200152 protected function _execute($sql)
Derek Allard2067d1a2008-11-13 22:59:24 +0000153 {
Andrey Andreevb76029d2012-01-26 15:13:19 +0200154 return @odbc_exec($this->conn_id, $this->_prep_query($sql));
Derek Allard2067d1a2008-11-13 22:59:24 +0000155 }
Barry Mienydd671972010-10-04 16:33:58 +0200156
Derek Allard2067d1a2008-11-13 22:59:24 +0000157 // --------------------------------------------------------------------
158
159 /**
160 * Prep the query
161 *
162 * If needed, each database adapter can prep the query string
163 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000164 * @param string an SQL query
165 * @return string
Barry Mienydd671972010-10-04 16:33:58 +0200166 */
Andrey Andreevb76029d2012-01-26 15:13:19 +0200167 protected function _prep_query($sql)
Derek Allard2067d1a2008-11-13 22:59:24 +0000168 {
169 return $sql;
170 }
171
172 // --------------------------------------------------------------------
173
174 /**
175 * Begin Transaction
176 *
Barry Mienydd671972010-10-04 16:33:58 +0200177 * @return bool
178 */
Andrey Andreevb76029d2012-01-26 15:13:19 +0200179 public function trans_begin($test_mode = FALSE)
Derek Allard2067d1a2008-11-13 22:59:24 +0000180 {
Derek Allard2067d1a2008-11-13 22:59:24 +0000181 // When transactions are nested we only begin/commit/rollback the outermost ones
Andrey Andreevb76029d2012-01-26 15:13:19 +0200182 if ( ! $this->trans_enabled OR $this->_trans_depth > 0)
Derek Allard2067d1a2008-11-13 22:59:24 +0000183 {
184 return TRUE;
185 }
186
187 // Reset the transaction failure flag.
188 // If the $test_mode flag is set to TRUE transactions will be rolled back
189 // even if the queries produce a successful result.
Andrey Andreevb76029d2012-01-26 15:13:19 +0200190 $this->_trans_failure = ($test_mode === TRUE);
Derek Allard2067d1a2008-11-13 22:59:24 +0000191
192 return odbc_autocommit($this->conn_id, FALSE);
193 }
194
195 // --------------------------------------------------------------------
196
197 /**
198 * Commit Transaction
199 *
Barry Mienydd671972010-10-04 16:33:58 +0200200 * @return bool
201 */
Andrey Andreevb76029d2012-01-26 15:13:19 +0200202 public function trans_commit()
Derek Allard2067d1a2008-11-13 22:59:24 +0000203 {
Derek Allard2067d1a2008-11-13 22:59:24 +0000204 // When transactions are nested we only begin/commit/rollback the outermost ones
Andrey Andreevb76029d2012-01-26 15:13:19 +0200205 if ( ! $this->trans_enabled OR $this->_trans_depth > 0)
Derek Allard2067d1a2008-11-13 22:59:24 +0000206 {
207 return TRUE;
208 }
209
210 $ret = odbc_commit($this->conn_id);
211 odbc_autocommit($this->conn_id, TRUE);
212 return $ret;
213 }
214
215 // --------------------------------------------------------------------
216
217 /**
218 * Rollback Transaction
219 *
Barry Mienydd671972010-10-04 16:33:58 +0200220 * @return bool
221 */
Andrey Andreevb76029d2012-01-26 15:13:19 +0200222 public function trans_rollback()
Derek Allard2067d1a2008-11-13 22:59:24 +0000223 {
Derek Allard2067d1a2008-11-13 22:59:24 +0000224 // When transactions are nested we only begin/commit/rollback the outermost ones
Andrey Andreevb76029d2012-01-26 15:13:19 +0200225 if ( ! $this->trans_enabled OR $this->_trans_depth > 0)
Derek Allard2067d1a2008-11-13 22:59:24 +0000226 {
227 return TRUE;
228 }
229
230 $ret = odbc_rollback($this->conn_id);
231 odbc_autocommit($this->conn_id, TRUE);
232 return $ret;
233 }
234
235 // --------------------------------------------------------------------
236
237 /**
238 * Escape String
239 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000240 * @param string
Derek Jonese4ed5832009-02-20 21:44:59 +0000241 * @param bool whether or not the string will be used in a LIKE condition
Derek Allard2067d1a2008-11-13 22:59:24 +0000242 * @return string
243 */
Andrey Andreevb76029d2012-01-26 15:13:19 +0200244 public function escape_str($str, $like = FALSE)
Derek Allard2067d1a2008-11-13 22:59:24 +0000245 {
Derek Jonese4ed5832009-02-20 21:44:59 +0000246 if (is_array($str))
247 {
Pascal Krietec3a4a8d2011-02-14 13:40:08 -0500248 foreach ($str as $key => $val)
Barry Mienydd671972010-10-04 16:33:58 +0200249 {
Derek Jonese4ed5832009-02-20 21:44:59 +0000250 $str[$key] = $this->escape_str($val, $like);
Barry Mienydd671972010-10-04 16:33:58 +0200251 }
252
253 return $str;
254 }
255
Derek Allard2067d1a2008-11-13 22:59:24 +0000256 // ODBC doesn't require escaping
Greg Aker757dda62010-04-14 19:06:19 -0500257 $str = remove_invisible_characters($str);
Barry Mienydd671972010-10-04 16:33:58 +0200258
Derek Jonese4ed5832009-02-20 21:44:59 +0000259 // escape LIKE condition wildcards
260 if ($like === TRUE)
261 {
Andrey Andreevb76029d2012-01-26 15:13:19 +0200262 return str_replace(array('%', '_', $this->_like_escape_chr),
263 array($this->_like_escape_chr.'%', $this->_like_escape_chr.'_', $this->_like_escape_chr.$this->_like_escape_chr),
264 $str);
Derek Jonese4ed5832009-02-20 21:44:59 +0000265 }
Barry Mienydd671972010-10-04 16:33:58 +0200266
Derek Jonese4ed5832009-02-20 21:44:59 +0000267 return $str;
Derek Allard2067d1a2008-11-13 22:59:24 +0000268 }
Barry Mienydd671972010-10-04 16:33:58 +0200269
Derek Allard2067d1a2008-11-13 22:59:24 +0000270 // --------------------------------------------------------------------
271
272 /**
273 * Affected Rows
274 *
Andrey Andreevb76029d2012-01-26 15:13:19 +0200275 * @return int
Derek Allard2067d1a2008-11-13 22:59:24 +0000276 */
Andrey Andreevb76029d2012-01-26 15:13:19 +0200277 public function affected_rows()
Derek Allard2067d1a2008-11-13 22:59:24 +0000278 {
279 return @odbc_num_rows($this->conn_id);
280 }
Barry Mienydd671972010-10-04 16:33:58 +0200281
Derek Allard2067d1a2008-11-13 22:59:24 +0000282 // --------------------------------------------------------------------
283
284 /**
285 * Insert ID
286 *
Andrey Andreevb76029d2012-01-26 15:13:19 +0200287 * @return int
Derek Allard2067d1a2008-11-13 22:59:24 +0000288 */
Andrey Andreevb76029d2012-01-26 15:13:19 +0200289 public function insert_id()
Derek Allard2067d1a2008-11-13 22:59:24 +0000290 {
291 return @odbc_insert_id($this->conn_id);
292 }
293
294 // --------------------------------------------------------------------
295
296 /**
297 * "Count All" query
298 *
299 * Generates a platform-specific query string that counts all records in
300 * the specified database
301 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000302 * @param string
Andrey Andreevb76029d2012-01-26 15:13:19 +0200303 * @return int
Derek Allard2067d1a2008-11-13 22:59:24 +0000304 */
Andrey Andreevb76029d2012-01-26 15:13:19 +0200305 public function count_all($table = '')
Derek Allard2067d1a2008-11-13 22:59:24 +0000306 {
307 if ($table == '')
Derek Allarde37ab382009-02-03 16:13:57 +0000308 {
309 return 0;
310 }
311
Andrey Andreevb76029d2012-01-26 15:13:19 +0200312 $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 +0000313
Derek Allard2067d1a2008-11-13 22:59:24 +0000314 if ($query->num_rows() == 0)
Derek Allarde37ab382009-02-03 16:13:57 +0000315 {
316 return 0;
317 }
Derek Allard2067d1a2008-11-13 22:59:24 +0000318
Andrey Andreevb76029d2012-01-26 15:13:19 +0200319 $query = $query->row();
Greg Aker90248ab2011-08-20 14:23:14 -0500320 $this->_reset_select();
Andrey Andreevb76029d2012-01-26 15:13:19 +0200321 return (int) $query->numrows;
Derek Allard2067d1a2008-11-13 22:59:24 +0000322 }
323
324 // --------------------------------------------------------------------
325
326 /**
327 * Show table query
328 *
329 * Generates a platform-specific query string so that the table names can be fetched
330 *
331 * @access private
332 * @param boolean
333 * @return string
334 */
Andrey Andreevb76029d2012-01-26 15:13:19 +0200335 protected function _list_tables($prefix_limit = FALSE)
Derek Allard2067d1a2008-11-13 22:59:24 +0000336 {
Andrey Andreevb76029d2012-01-26 15:13:19 +0200337 $sql = 'SHOW TABLES FROM `'.$this->database.'`';
Derek Allard2067d1a2008-11-13 22:59:24 +0000338
Andrey Andreevb76029d2012-01-26 15:13:19 +0200339 if ($prefix_limit !== FALSE && $this->dbprefix != '')
Derek Allard2067d1a2008-11-13 22:59:24 +0000340 {
Greg Aker0d424892010-01-26 02:14:44 +0000341 //$sql .= " LIKE '".$this->escape_like_str($this->dbprefix)."%' ".sprintf($this->_like_escape_str, $this->_like_escape_chr);
Derek Allard2067d1a2008-11-13 22:59:24 +0000342 return FALSE; // not currently supported
343 }
Barry Mienydd671972010-10-04 16:33:58 +0200344
Derek Allard2067d1a2008-11-13 22:59:24 +0000345 return $sql;
346 }
Barry Mienydd671972010-10-04 16:33:58 +0200347
Derek Allard2067d1a2008-11-13 22:59:24 +0000348 // --------------------------------------------------------------------
349
350 /**
351 * Show column query
352 *
353 * Generates a platform-specific query string so that the column names can be fetched
354 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000355 * @param string the table name
356 * @return string
357 */
Andrey Andreevb76029d2012-01-26 15:13:19 +0200358 protected function _list_columns($table = '')
Derek Allard2067d1a2008-11-13 22:59:24 +0000359 {
Andrey Andreevb76029d2012-01-26 15:13:19 +0200360 return 'SHOW COLUMNS FROM '.$table;
Derek Allard2067d1a2008-11-13 22:59:24 +0000361 }
362
363 // --------------------------------------------------------------------
364
365 /**
366 * Field data query
367 *
368 * Generates a platform-specific query so that the column data can be retrieved
369 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000370 * @param string the table name
Andrey Andreevb76029d2012-01-26 15:13:19 +0200371 * @return string
Derek Allard2067d1a2008-11-13 22:59:24 +0000372 */
Andrey Andreevb76029d2012-01-26 15:13:19 +0200373 protected function _field_data($table)
Derek Allard2067d1a2008-11-13 22:59:24 +0000374 {
Andrey Andreevb76029d2012-01-26 15:13:19 +0200375 return 'SELECT TOP 1 FROM '.$table;
Derek Allard2067d1a2008-11-13 22:59:24 +0000376 }
377
378 // --------------------------------------------------------------------
379
380 /**
381 * The error message string
382 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000383 * @return string
384 */
Andrey Andreevb76029d2012-01-26 15:13:19 +0200385 protected function _error_message()
Derek Allard2067d1a2008-11-13 22:59:24 +0000386 {
387 return odbc_errormsg($this->conn_id);
388 }
Barry Mienydd671972010-10-04 16:33:58 +0200389
Derek Allard2067d1a2008-11-13 22:59:24 +0000390 // --------------------------------------------------------------------
391
392 /**
393 * The error message number
394 *
Andrey Andreevb76029d2012-01-26 15:13:19 +0200395 * @return int
Derek Allard2067d1a2008-11-13 22:59:24 +0000396 */
Andrey Andreevb76029d2012-01-26 15:13:19 +0200397 protected function _error_number()
Derek Allard2067d1a2008-11-13 22:59:24 +0000398 {
399 return odbc_error($this->conn_id);
400 }
401
402 // --------------------------------------------------------------------
403
404 /**
405 * Escape the SQL Identifiers
406 *
407 * This function escapes column and table names
408 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000409 * @param string
410 * @return string
411 */
Andrey Andreevb76029d2012-01-26 15:13:19 +0200412 public function _escape_identifiers($item)
Derek Allard2067d1a2008-11-13 22:59:24 +0000413 {
414 if ($this->_escape_char == '')
415 {
416 return $item;
417 }
418
419 foreach ($this->_reserved_identifiers as $id)
420 {
421 if (strpos($item, '.'.$id) !== FALSE)
422 {
Andrey Andreevb76029d2012-01-26 15:13:19 +0200423 $item = str_replace('.', $this->_escape_char.'.', $item);
Barry Mienydd671972010-10-04 16:33:58 +0200424
Derek Allard2067d1a2008-11-13 22:59:24 +0000425 // remove duplicates if the user already included the escape
Andrey Andreevb76029d2012-01-26 15:13:19 +0200426 return preg_replace('/['.$this->_escape_char.']+/', $this->_escape_char, $this->_escape_char.$item);
Barry Mienydd671972010-10-04 16:33:58 +0200427 }
Derek Allard2067d1a2008-11-13 22:59:24 +0000428 }
Barry Mienydd671972010-10-04 16:33:58 +0200429
Derek Allard2067d1a2008-11-13 22:59:24 +0000430 if (strpos($item, '.') !== FALSE)
431 {
Andrey Andreevb76029d2012-01-26 15:13:19 +0200432 $item = str_replace('.', $this->_escape_char.'.'.$this->_escape_char, $item);
Derek Allard2067d1a2008-11-13 22:59:24 +0000433 }
Barry Mienydd671972010-10-04 16:33:58 +0200434
Derek Allard2067d1a2008-11-13 22:59:24 +0000435 // remove duplicates if the user already included the escape
Andrey Andreevb76029d2012-01-26 15:13:19 +0200436 return preg_replace('/['.$this->_escape_char.']+/', $this->_escape_char, $this->_escape_char.$item.$this->_escape_char);
Derek Allard2067d1a2008-11-13 22:59:24 +0000437 }
Barry Mienydd671972010-10-04 16:33:58 +0200438
Derek Allard2067d1a2008-11-13 22:59:24 +0000439 // --------------------------------------------------------------------
440
441 /**
442 * From Tables
443 *
444 * This function implicitly groups FROM tables so there is no confusion
445 * about operator precedence in harmony with SQL standards
446 *
Andrey Andreevb76029d2012-01-26 15:13:19 +0200447 * @param string the table name
448 * @return string
Derek Allard2067d1a2008-11-13 22:59:24 +0000449 */
Andrey Andreevb76029d2012-01-26 15:13:19 +0200450 protected function _from_tables($tables)
Derek Allard2067d1a2008-11-13 22:59:24 +0000451 {
452 if ( ! is_array($tables))
453 {
454 $tables = array($tables);
455 }
Barry Mienydd671972010-10-04 16:33:58 +0200456
Derek Allard2067d1a2008-11-13 22:59:24 +0000457 return '('.implode(', ', $tables).')';
458 }
459
460 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200461
Derek Allard2067d1a2008-11-13 22:59:24 +0000462 /**
463 * Insert statement
464 *
465 * Generates a platform-specific insert string from the supplied data
466 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000467 * @param string the table name
468 * @param array the insert keys
469 * @param array the insert values
470 * @return string
471 */
Andrey Andreevb76029d2012-01-26 15:13:19 +0200472 protected function _insert($table, $keys, $values)
Barry Mienydd671972010-10-04 16:33:58 +0200473 {
Andrey Andreevb76029d2012-01-26 15:13:19 +0200474 return 'INSERT INTO '.$table.' ('.implode(', ', $keys).') VALUES ('.implode(', ', $values).')';
Derek Allard2067d1a2008-11-13 22:59:24 +0000475 }
Barry Mienydd671972010-10-04 16:33:58 +0200476
Derek Allard2067d1a2008-11-13 22:59:24 +0000477 // --------------------------------------------------------------------
478
479 /**
480 * Update statement
481 *
482 * Generates a platform-specific update string from the supplied data
483 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000484 * @param string the table name
485 * @param array the update data
486 * @param array the where clause
487 * @param array the orderby clause
488 * @param array the limit clause
489 * @return string
490 */
Andrey Andreevb76029d2012-01-26 15:13:19 +0200491 protected function _update($table, $values, $where, $orderby = array(), $limit = FALSE)
Derek Allard2067d1a2008-11-13 22:59:24 +0000492 {
Pascal Krietec3a4a8d2011-02-14 13:40:08 -0500493 foreach ($values as $key => $val)
Derek Allard2067d1a2008-11-13 22:59:24 +0000494 {
Andrey Andreevb76029d2012-01-26 15:13:19 +0200495 $valstr[] = $key.' = '.$val;
Derek Allard2067d1a2008-11-13 22:59:24 +0000496 }
Barry Mienydd671972010-10-04 16:33:58 +0200497
Andrey Andreevb76029d2012-01-26 15:13:19 +0200498 return 'UPDATE '.$table.' SET '.implode(', ', $valstr)
499 .(($where != '' && count($where) > 0) ? ' WHERE '.implode(' ', $where) : '')
500 .(count($orderby) > 0 ? ' ORDER BY '.implode(', ', $orderby) : '')
501 .( ! $limit ? '' : ' LIMIT '.$limit);
Derek Allard2067d1a2008-11-13 22:59:24 +0000502 }
503
Barry Mienydd671972010-10-04 16:33:58 +0200504
Derek Allard2067d1a2008-11-13 22:59:24 +0000505 // --------------------------------------------------------------------
506
507 /**
508 * Truncate statement
509 *
510 * Generates a platform-specific truncate string from the supplied data
511 * If the database does not support the truncate() command
512 * This function maps to "DELETE FROM table"
513 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000514 * @param string the table name
515 * @return string
Barry Mienydd671972010-10-04 16:33:58 +0200516 */
Andrey Andreevb76029d2012-01-26 15:13:19 +0200517 protected function _truncate($table)
Derek Allard2067d1a2008-11-13 22:59:24 +0000518 {
519 return $this->_delete($table);
520 }
Barry Mienydd671972010-10-04 16:33:58 +0200521
Derek Allard2067d1a2008-11-13 22:59:24 +0000522 // --------------------------------------------------------------------
523
524 /**
525 * Delete statement
526 *
527 * Generates a platform-specific delete string from the supplied data
528 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000529 * @param string the table name
530 * @param array the where clause
531 * @param string the limit clause
532 * @return string
Barry Mienydd671972010-10-04 16:33:58 +0200533 */
Andrey Andreevb76029d2012-01-26 15:13:19 +0200534 protected function _delete($table, $where = array(), $like = array(), $limit = FALSE)
Derek Allard2067d1a2008-11-13 22:59:24 +0000535 {
536 $conditions = '';
537
538 if (count($where) > 0 OR count($like) > 0)
539 {
Andrey Andreevb76029d2012-01-26 15:13:19 +0200540 $conditions = "\nWHERE ".implode("\n", $this->ar_where);
Derek Allard2067d1a2008-11-13 22:59:24 +0000541
542 if (count($where) > 0 && count($like) > 0)
543 {
Andrey Andreevb76029d2012-01-26 15:13:19 +0200544 $conditions .= ' AND ';
Derek Allard2067d1a2008-11-13 22:59:24 +0000545 }
546 $conditions .= implode("\n", $like);
547 }
548
Andrey Andreevb76029d2012-01-26 15:13:19 +0200549 return 'DELETE FROM '.$table.$conditions.( ! $limit ? '' : ' LIMIT '.$limit);
Derek Allard2067d1a2008-11-13 22:59:24 +0000550 }
551
552 // --------------------------------------------------------------------
553
554 /**
555 * Limit string
556 *
557 * Generates a platform-specific LIMIT clause
558 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000559 * @param string the sql query string
Andrey Andreevb76029d2012-01-26 15:13:19 +0200560 * @param int the number of rows to limit the query to
561 * @param int the offset value
Derek Allard2067d1a2008-11-13 22:59:24 +0000562 * @return string
563 */
Andrey Andreevb76029d2012-01-26 15:13:19 +0200564 protected function _limit($sql, $limit, $offset)
Derek Allard2067d1a2008-11-13 22:59:24 +0000565 {
Andrey Andreevb76029d2012-01-26 15:13:19 +0200566 return $sql.($offset == 0 ? '' : $offset.', ').$limit;
Derek Allard2067d1a2008-11-13 22:59:24 +0000567 }
568
569 // --------------------------------------------------------------------
570
571 /**
572 * Close DB Connection
573 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000574 * @param resource
575 * @return void
576 */
Andrey Andreevb76029d2012-01-26 15:13:19 +0200577 protected function _close($conn_id)
Derek Allard2067d1a2008-11-13 22:59:24 +0000578 {
579 @odbc_close($conn_id);
580 }
581
Derek Allard2067d1a2008-11-13 22:59:24 +0000582}
583
Derek Allard2067d1a2008-11-13 22:59:24 +0000584/* End of file odbc_driver.php */
Andrey Andreevb76029d2012-01-26 15:13:19 +0200585/* Location: ./system/database/drivers/odbc/odbc_driver.php */