blob: 828ef006be35391d32220923a3eead26e0f299ab [file] [log] [blame]
Derek Jones37f4b9c2011-07-01 17:56:50 -05001<?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
8 *
9 * Licensed under the Open Software License version 3.0
10 *
11 * 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
21 * @copyright Copyright (c) 2008 - 2011, EllisLab, Inc. (http://ellislab.com/)
22 * @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
28// ------------------------------------------------------------------------
29
30/**
31 * MySQL Database Adapter Class
32 *
33 * Note: _DB is an extender class that the app controller
34 * creates dynamically based on whether the active record
35 * class is being used or not.
36 *
37 * @package CodeIgniter
38 * @subpackage Drivers
39 * @category Database
Derek Jonesf4a4bd82011-10-20 12:18:42 -050040 * @author EllisLab Dev Team
Derek Allard2067d1a2008-11-13 22:59:24 +000041 * @link http://codeigniter.com/user_guide/database/
42 */
43class CI_DB_mysql_driver extends CI_DB {
44
45 var $dbdriver = 'mysql';
46
47 // The character used for escaping
48 var $_escape_char = '`';
Derek Jonese4ed5832009-02-20 21:44:59 +000049
50 // clause and character used for LIKE escape sequences - not used in MySQL
51 var $_like_escape_str = '';
52 var $_like_escape_chr = '';
53
Derek Allard2067d1a2008-11-13 22:59:24 +000054 /**
55 * Whether to use the MySQL "delete hack" which allows the number
56 * of affected rows to be shown. Uses a preg_replace when enabled,
57 * adding a bit more processing to all queries.
Barry Mienydd671972010-10-04 16:33:58 +020058 */
Derek Allard2067d1a2008-11-13 22:59:24 +000059 var $delete_hack = TRUE;
Barry Mienydd671972010-10-04 16:33:58 +020060
Derek Allard2067d1a2008-11-13 22:59:24 +000061 /**
62 * The syntax to count rows is slightly different across different
63 * database engines, so this string appears in each driver and is
64 * used for the count_all() and count_all_results() functions.
65 */
66 var $_count_string = 'SELECT COUNT(*) AS ';
67 var $_random_keyword = ' RAND()'; // database specific random keyword
68
Derek Jones3b9f88d2011-05-20 10:25:13 -050069 // whether SET NAMES must be used to set the character set
70 var $use_set_names;
RH Beckercfdb2322011-10-03 17:28:32 -070071
Derek Allard2067d1a2008-11-13 22:59:24 +000072 /**
73 * Non-persistent database connection
74 *
75 * @access private called by the base class
76 * @return resource
Barry Mienydd671972010-10-04 16:33:58 +020077 */
Derek Allard2067d1a2008-11-13 22:59:24 +000078 function db_connect()
79 {
80 if ($this->port != '')
81 {
82 $this->hostname .= ':'.$this->port;
83 }
Barry Mienydd671972010-10-04 16:33:58 +020084
Derek Allard2067d1a2008-11-13 22:59:24 +000085 return @mysql_connect($this->hostname, $this->username, $this->password, TRUE);
86 }
Barry Mienydd671972010-10-04 16:33:58 +020087
Derek Allard2067d1a2008-11-13 22:59:24 +000088 // --------------------------------------------------------------------
89
90 /**
91 * Persistent database connection
92 *
93 * @access private called by the base class
94 * @return resource
Barry Mienydd671972010-10-04 16:33:58 +020095 */
Derek Allard2067d1a2008-11-13 22:59:24 +000096 function db_pconnect()
97 {
98 if ($this->port != '')
99 {
100 $this->hostname .= ':'.$this->port;
101 }
102
103 return @mysql_pconnect($this->hostname, $this->username, $this->password);
104 }
Barry Mienydd671972010-10-04 16:33:58 +0200105
Derek Allard2067d1a2008-11-13 22:59:24 +0000106 // --------------------------------------------------------------------
107
108 /**
Derek Jones87cbafc2009-02-27 16:29:59 +0000109 * Reconnect
110 *
111 * Keep / reestablish the db connection if no queries have been
112 * sent for a length of time exceeding the server's idle timeout
113 *
114 * @access public
115 * @return void
116 */
117 function reconnect()
118 {
119 if (mysql_ping($this->conn_id) === FALSE)
120 {
121 $this->conn_id = FALSE;
122 }
123 }
124
125 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200126
Derek Jones87cbafc2009-02-27 16:29:59 +0000127 /**
Derek Allard2067d1a2008-11-13 22:59:24 +0000128 * Select the database
129 *
130 * @access private called by the base class
131 * @return resource
Barry Mienydd671972010-10-04 16:33:58 +0200132 */
Derek Allard2067d1a2008-11-13 22:59:24 +0000133 function db_select()
134 {
135 return @mysql_select_db($this->database, $this->conn_id);
136 }
137
138 // --------------------------------------------------------------------
139
140 /**
141 * Set client character set
142 *
143 * @access public
144 * @param string
145 * @param string
146 * @return resource
147 */
148 function db_set_charset($charset, $collation)
149 {
RH Beckercfdb2322011-10-03 17:28:32 -0700150 return function_exists('mysql_set_charset')
151 ? @mysql_set_charset($charset, $this->conn_id)
152 : @mysql_query("SET NAMES '".$this->escape_str($charset)."' COLLATE '".$this->escape_str($collation)."'", $this->conn_id);
Derek Allard2067d1a2008-11-13 22:59:24 +0000153 }
154
155 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200156
Derek Allard2067d1a2008-11-13 22:59:24 +0000157 /**
158 * Version number query string
159 *
160 * @access public
161 * @return string
162 */
163 function _version()
164 {
165 return "SELECT version() AS ver";
166 }
167
168 // --------------------------------------------------------------------
169
170 /**
171 * Execute the query
172 *
173 * @access private called by the base class
174 * @param string an SQL query
175 * @return resource
Barry Mienydd671972010-10-04 16:33:58 +0200176 */
Derek Allard2067d1a2008-11-13 22:59:24 +0000177 function _execute($sql)
178 {
179 $sql = $this->_prep_query($sql);
180 return @mysql_query($sql, $this->conn_id);
181 }
Barry Mienydd671972010-10-04 16:33:58 +0200182
Derek Allard2067d1a2008-11-13 22:59:24 +0000183 // --------------------------------------------------------------------
184
185 /**
186 * Prep the query
187 *
188 * If needed, each database adapter can prep the query string
189 *
190 * @access private called by execute()
191 * @param string an SQL query
192 * @return string
Barry Mienydd671972010-10-04 16:33:58 +0200193 */
Derek Allard2067d1a2008-11-13 22:59:24 +0000194 function _prep_query($sql)
195 {
196 // "DELETE FROM TABLE" returns 0 affected rows This hack modifies
197 // the query so that it returns the number of affected rows
198 if ($this->delete_hack === TRUE)
199 {
200 if (preg_match('/^\s*DELETE\s+FROM\s+(\S+)\s*$/i', $sql))
201 {
202 $sql = preg_replace("/^\s*DELETE\s+FROM\s+(\S+)\s*$/", "DELETE FROM \\1 WHERE 1=1", $sql);
203 }
204 }
Barry Mienydd671972010-10-04 16:33:58 +0200205
Derek Allard2067d1a2008-11-13 22:59:24 +0000206 return $sql;
207 }
208
209 // --------------------------------------------------------------------
210
211 /**
212 * Begin Transaction
213 *
214 * @access public
Barry Mienydd671972010-10-04 16:33:58 +0200215 * @return bool
216 */
Derek Allard2067d1a2008-11-13 22:59:24 +0000217 function trans_begin($test_mode = FALSE)
218 {
219 if ( ! $this->trans_enabled)
220 {
221 return TRUE;
222 }
Barry Mienydd671972010-10-04 16:33:58 +0200223
Derek Allard2067d1a2008-11-13 22:59:24 +0000224 // When transactions are nested we only begin/commit/rollback the outermost ones
225 if ($this->_trans_depth > 0)
226 {
227 return TRUE;
228 }
229
230 // Reset the transaction failure flag.
231 // If the $test_mode flag is set to TRUE transactions will be rolled back
232 // even if the queries produce a successful result.
233 $this->_trans_failure = ($test_mode === TRUE) ? TRUE : FALSE;
Barry Mienydd671972010-10-04 16:33:58 +0200234
Derek Allard2067d1a2008-11-13 22:59:24 +0000235 $this->simple_query('SET AUTOCOMMIT=0');
236 $this->simple_query('START TRANSACTION'); // can also be BEGIN or BEGIN WORK
237 return TRUE;
238 }
239
240 // --------------------------------------------------------------------
241
242 /**
243 * Commit Transaction
244 *
245 * @access public
Barry Mienydd671972010-10-04 16:33:58 +0200246 * @return bool
247 */
Derek Allard2067d1a2008-11-13 22:59:24 +0000248 function trans_commit()
249 {
250 if ( ! $this->trans_enabled)
251 {
252 return TRUE;
253 }
254
255 // When transactions are nested we only begin/commit/rollback the outermost ones
256 if ($this->_trans_depth > 0)
257 {
258 return TRUE;
259 }
260
261 $this->simple_query('COMMIT');
262 $this->simple_query('SET AUTOCOMMIT=1');
263 return TRUE;
264 }
265
266 // --------------------------------------------------------------------
267
268 /**
269 * Rollback Transaction
270 *
271 * @access public
Barry Mienydd671972010-10-04 16:33:58 +0200272 * @return bool
273 */
Derek Allard2067d1a2008-11-13 22:59:24 +0000274 function trans_rollback()
275 {
276 if ( ! $this->trans_enabled)
277 {
278 return TRUE;
279 }
280
281 // When transactions are nested we only begin/commit/rollback the outermost ones
282 if ($this->_trans_depth > 0)
283 {
284 return TRUE;
285 }
286
287 $this->simple_query('ROLLBACK');
288 $this->simple_query('SET AUTOCOMMIT=1');
289 return TRUE;
290 }
Barry Mienydd671972010-10-04 16:33:58 +0200291
Derek Allard2067d1a2008-11-13 22:59:24 +0000292 // --------------------------------------------------------------------
293
294 /**
295 * Escape String
296 *
297 * @access public
298 * @param string
Derek Jonese4ed5832009-02-20 21:44:59 +0000299 * @param bool whether or not the string will be used in a LIKE condition
Derek Allard2067d1a2008-11-13 22:59:24 +0000300 * @return string
301 */
Barry Mienydd671972010-10-04 16:33:58 +0200302 function escape_str($str, $like = FALSE)
303 {
Derek Allard2067d1a2008-11-13 22:59:24 +0000304 if (is_array($str))
305 {
Pascal Krietec3a4a8d2011-02-14 13:40:08 -0500306 foreach ($str as $key => $val)
Derek Jones37f4b9c2011-07-01 17:56:50 -0500307 {
Derek Jonese4ed5832009-02-20 21:44:59 +0000308 $str[$key] = $this->escape_str($val, $like);
Derek Jones37f4b9c2011-07-01 17:56:50 -0500309 }
Barry Mienydd671972010-10-04 16:33:58 +0200310
Derek Jones37f4b9c2011-07-01 17:56:50 -0500311 return $str;
312 }
Derek Allard2067d1a2008-11-13 22:59:24 +0000313
314 if (function_exists('mysql_real_escape_string') AND is_resource($this->conn_id))
315 {
Derek Jonese4ed5832009-02-20 21:44:59 +0000316 $str = mysql_real_escape_string($str, $this->conn_id);
Derek Allard2067d1a2008-11-13 22:59:24 +0000317 }
318 elseif (function_exists('mysql_escape_string'))
319 {
Derek Jonese4ed5832009-02-20 21:44:59 +0000320 $str = mysql_escape_string($str);
Derek Allard2067d1a2008-11-13 22:59:24 +0000321 }
322 else
323 {
Derek Jonese4ed5832009-02-20 21:44:59 +0000324 $str = addslashes($str);
Derek Allard2067d1a2008-11-13 22:59:24 +0000325 }
Barry Mienydd671972010-10-04 16:33:58 +0200326
Derek Jonese4ed5832009-02-20 21:44:59 +0000327 // escape LIKE condition wildcards
328 if ($like === TRUE)
329 {
330 $str = str_replace(array('%', '_'), array('\\%', '\\_'), $str);
331 }
Barry Mienydd671972010-10-04 16:33:58 +0200332
Derek Jonese4ed5832009-02-20 21:44:59 +0000333 return $str;
Derek Allard2067d1a2008-11-13 22:59:24 +0000334 }
Barry Mienydd671972010-10-04 16:33:58 +0200335
Derek Allard2067d1a2008-11-13 22:59:24 +0000336 // --------------------------------------------------------------------
337
338 /**
339 * Affected Rows
340 *
341 * @access public
342 * @return integer
343 */
344 function affected_rows()
345 {
346 return @mysql_affected_rows($this->conn_id);
347 }
Barry Mienydd671972010-10-04 16:33:58 +0200348
Derek Allard2067d1a2008-11-13 22:59:24 +0000349 // --------------------------------------------------------------------
350
351 /**
352 * Insert ID
353 *
354 * @access public
355 * @return integer
356 */
357 function insert_id()
358 {
359 return @mysql_insert_id($this->conn_id);
360 }
361
362 // --------------------------------------------------------------------
363
364 /**
365 * "Count All" query
366 *
367 * Generates a platform-specific query string that counts all records in
368 * the specified database
369 *
370 * @access public
371 * @param string
372 * @return string
373 */
374 function count_all($table = '')
375 {
376 if ($table == '')
Derek Allarde37ab382009-02-03 16:13:57 +0000377 {
378 return 0;
379 }
Barry Mienydd671972010-10-04 16:33:58 +0200380
Derek Allarde37ab382009-02-03 16:13:57 +0000381 $query = $this->query($this->_count_string . $this->_protect_identifiers('numrows') . " FROM " . $this->_protect_identifiers($table, TRUE, NULL, FALSE));
382
Derek Allard2067d1a2008-11-13 22:59:24 +0000383 if ($query->num_rows() == 0)
Derek Allarde37ab382009-02-03 16:13:57 +0000384 {
385 return 0;
386 }
Derek Allard2067d1a2008-11-13 22:59:24 +0000387
388 $row = $query->row();
Greg Aker90248ab2011-08-20 14:23:14 -0500389 $this->_reset_select();
Derek Allarde37ab382009-02-03 16:13:57 +0000390 return (int) $row->numrows;
Derek Allard2067d1a2008-11-13 22:59:24 +0000391 }
392
393 // --------------------------------------------------------------------
394
395 /**
396 * List table query
397 *
398 * Generates a platform-specific query string so that the table names can be fetched
399 *
400 * @access private
401 * @param boolean
402 * @return string
403 */
404 function _list_tables($prefix_limit = FALSE)
405 {
Barry Mienydd671972010-10-04 16:33:58 +0200406 $sql = "SHOW TABLES FROM ".$this->_escape_char.$this->database.$this->_escape_char;
Derek Allard2067d1a2008-11-13 22:59:24 +0000407
408 if ($prefix_limit !== FALSE AND $this->dbprefix != '')
409 {
Derek Jones3c11b6f2009-02-20 22:36:27 +0000410 $sql .= " LIKE '".$this->escape_like_str($this->dbprefix)."%'";
Derek Allard2067d1a2008-11-13 22:59:24 +0000411 }
412
413 return $sql;
414 }
Barry Mienydd671972010-10-04 16:33:58 +0200415
Derek Allard2067d1a2008-11-13 22:59:24 +0000416 // --------------------------------------------------------------------
417
418 /**
419 * Show column query
420 *
421 * Generates a platform-specific query string so that the column names can be fetched
422 *
423 * @access public
424 * @param string the table name
425 * @return string
426 */
427 function _list_columns($table = '')
428 {
Greg Aker1edde302010-01-26 00:17:01 +0000429 return "SHOW COLUMNS FROM ".$this->_protect_identifiers($table, TRUE, NULL, FALSE);
Derek Allard2067d1a2008-11-13 22:59:24 +0000430 }
431
432 // --------------------------------------------------------------------
433
434 /**
435 * Field data query
436 *
437 * Generates a platform-specific query so that the column data can be retrieved
438 *
439 * @access public
440 * @param string the table name
441 * @return object
442 */
443 function _field_data($table)
444 {
danmontgomeryfc756452011-08-21 15:31:22 -0400445 return "DESCRIBE ".$table;
Derek Allard2067d1a2008-11-13 22:59:24 +0000446 }
447
448 // --------------------------------------------------------------------
449
450 /**
451 * The error message string
452 *
453 * @access private
454 * @return string
455 */
456 function _error_message()
457 {
458 return mysql_error($this->conn_id);
459 }
Barry Mienydd671972010-10-04 16:33:58 +0200460
Derek Allard2067d1a2008-11-13 22:59:24 +0000461 // --------------------------------------------------------------------
462
463 /**
464 * The error message number
465 *
466 * @access private
467 * @return integer
468 */
469 function _error_number()
470 {
471 return mysql_errno($this->conn_id);
472 }
473
474 // --------------------------------------------------------------------
475
476 /**
477 * Escape the SQL Identifiers
478 *
479 * This function escapes column and table names
480 *
481 * @access private
482 * @param string
483 * @return string
484 */
485 function _escape_identifiers($item)
486 {
487 if ($this->_escape_char == '')
488 {
489 return $item;
490 }
491
492 foreach ($this->_reserved_identifiers as $id)
493 {
494 if (strpos($item, '.'.$id) !== FALSE)
495 {
Barry Mienydd671972010-10-04 16:33:58 +0200496 $str = $this->_escape_char. str_replace('.', $this->_escape_char.'.', $item);
497
Derek Allard2067d1a2008-11-13 22:59:24 +0000498 // remove duplicates if the user already included the escape
499 return preg_replace('/['.$this->_escape_char.']+/', $this->_escape_char, $str);
Barry Mienydd671972010-10-04 16:33:58 +0200500 }
Derek Allard2067d1a2008-11-13 22:59:24 +0000501 }
Barry Mienydd671972010-10-04 16:33:58 +0200502
Derek Allard2067d1a2008-11-13 22:59:24 +0000503 if (strpos($item, '.') !== FALSE)
504 {
Barry Mienydd671972010-10-04 16:33:58 +0200505 $str = $this->_escape_char.str_replace('.', $this->_escape_char.'.'.$this->_escape_char, $item).$this->_escape_char;
Derek Allard2067d1a2008-11-13 22:59:24 +0000506 }
507 else
508 {
509 $str = $this->_escape_char.$item.$this->_escape_char;
510 }
Barry Mienydd671972010-10-04 16:33:58 +0200511
Derek Allard2067d1a2008-11-13 22:59:24 +0000512 // remove duplicates if the user already included the escape
513 return preg_replace('/['.$this->_escape_char.']+/', $this->_escape_char, $str);
514 }
Barry Mienydd671972010-10-04 16:33:58 +0200515
Derek Allard2067d1a2008-11-13 22:59:24 +0000516 // --------------------------------------------------------------------
517
518 /**
519 * From Tables
520 *
521 * This function implicitly groups FROM tables so there is no confusion
522 * about operator precedence in harmony with SQL standards
523 *
524 * @access public
525 * @param type
526 * @return type
527 */
528 function _from_tables($tables)
529 {
530 if ( ! is_array($tables))
531 {
532 $tables = array($tables);
533 }
Barry Mienydd671972010-10-04 16:33:58 +0200534
Derek Allard2067d1a2008-11-13 22:59:24 +0000535 return '('.implode(', ', $tables).')';
536 }
537
538 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200539
Derek Allard2067d1a2008-11-13 22:59:24 +0000540 /**
541 * Insert statement
542 *
543 * Generates a platform-specific insert string from the supplied data
544 *
545 * @access public
546 * @param string the table name
547 * @param array the insert keys
548 * @param array the insert values
549 * @return string
550 */
551 function _insert($table, $keys, $values)
Barry Mienydd671972010-10-04 16:33:58 +0200552 {
Phil Sturgeona12216b2011-02-09 16:09:31 +0000553 return "INSERT INTO ".$table." (".implode(', ', $keys).") VALUES (".implode(', ', $values).")";
Derek Allard2067d1a2008-11-13 22:59:24 +0000554 }
Barry Mienydd671972010-10-04 16:33:58 +0200555
Derek Allard2067d1a2008-11-13 22:59:24 +0000556 // --------------------------------------------------------------------
557
Barry Mienydd671972010-10-04 16:33:58 +0200558
Derek Jones20aa2bd2010-03-02 17:28:07 -0600559 /**
560 * Replace statement
561 *
562 * Generates a platform-specific replace string from the supplied data
563 *
564 * @access public
565 * @param string the table name
566 * @param array the insert keys
567 * @param array the insert values
568 * @return string
569 */
570 function _replace($table, $keys, $values)
Barry Mienydd671972010-10-04 16:33:58 +0200571 {
Phil Sturgeona12216b2011-02-09 16:09:31 +0000572 return "REPLACE INTO ".$table." (".implode(', ', $keys).") VALUES (".implode(', ', $values).")";
Derek Jones20aa2bd2010-03-02 17:28:07 -0600573 }
Barry Mienydd671972010-10-04 16:33:58 +0200574
Derek Jones20aa2bd2010-03-02 17:28:07 -0600575 // --------------------------------------------------------------------
576
577 /**
578 * Insert_batch statement
579 *
580 * Generates a platform-specific insert string from the supplied data
581 *
582 * @access public
583 * @param string the table name
584 * @param array the insert keys
585 * @param array the insert values
586 * @return string
587 */
588 function _insert_batch($table, $keys, $values)
Barry Mienydd671972010-10-04 16:33:58 +0200589 {
Phil Sturgeona12216b2011-02-09 16:09:31 +0000590 return "INSERT INTO ".$table." (".implode(', ', $keys).") VALUES ".implode(', ', $values);
Derek Jones20aa2bd2010-03-02 17:28:07 -0600591 }
Barry Mienydd671972010-10-04 16:33:58 +0200592
Derek Jones20aa2bd2010-03-02 17:28:07 -0600593 // --------------------------------------------------------------------
594
595
Derek Allard2067d1a2008-11-13 22:59:24 +0000596 /**
597 * Update statement
598 *
599 * Generates a platform-specific update string from the supplied data
600 *
601 * @access public
602 * @param string the table name
603 * @param array the update data
604 * @param array the where clause
605 * @param array the orderby clause
606 * @param array the limit clause
607 * @return string
608 */
609 function _update($table, $values, $where, $orderby = array(), $limit = FALSE)
610 {
Pascal Krietec3a4a8d2011-02-14 13:40:08 -0500611 foreach ($values as $key => $val)
Derek Allard2067d1a2008-11-13 22:59:24 +0000612 {
Phil Sturgeona12216b2011-02-09 16:09:31 +0000613 $valstr[] = $key . ' = ' . $val;
Derek Allard2067d1a2008-11-13 22:59:24 +0000614 }
Barry Mienydd671972010-10-04 16:33:58 +0200615
Derek Allard2067d1a2008-11-13 22:59:24 +0000616 $limit = ( ! $limit) ? '' : ' LIMIT '.$limit;
Barry Mienydd671972010-10-04 16:33:58 +0200617
Derek Allard2067d1a2008-11-13 22:59:24 +0000618 $orderby = (count($orderby) >= 1)?' ORDER BY '.implode(", ", $orderby):'';
Barry Mienydd671972010-10-04 16:33:58 +0200619
Derek Allard2067d1a2008-11-13 22:59:24 +0000620 $sql = "UPDATE ".$table." SET ".implode(', ', $valstr);
621
622 $sql .= ($where != '' AND count($where) >=1) ? " WHERE ".implode(" ", $where) : '';
623
624 $sql .= $orderby.$limit;
Barry Mienydd671972010-10-04 16:33:58 +0200625
Derek Allard2067d1a2008-11-13 22:59:24 +0000626 return $sql;
627 }
628
629 // --------------------------------------------------------------------
630
Derek Jones20aa2bd2010-03-02 17:28:07 -0600631
632 /**
633 * Update_Batch statement
634 *
635 * Generates a platform-specific batch update string from the supplied data
636 *
637 * @access public
638 * @param string the table name
639 * @param array the update data
640 * @param array the where clause
641 * @return string
642 */
643 function _update_batch($table, $values, $index, $where = NULL)
644 {
645 $ids = array();
646 $where = ($where != '' AND count($where) >=1) ? implode(" ", $where).' AND ' : '';
647
Pascal Krietec3a4a8d2011-02-14 13:40:08 -0500648 foreach ($values as $key => $val)
Derek Jones20aa2bd2010-03-02 17:28:07 -0600649 {
650 $ids[] = $val[$index];
Barry Mienydd671972010-10-04 16:33:58 +0200651
Pascal Krietec3a4a8d2011-02-14 13:40:08 -0500652 foreach (array_keys($val) as $field)
Barry Mienydd671972010-10-04 16:33:58 +0200653 {
Derek Jones20aa2bd2010-03-02 17:28:07 -0600654 if ($field != $index)
655 {
Derek Jones37f4b9c2011-07-01 17:56:50 -0500656 $final[$field][] = 'WHEN '.$index.' = '.$val[$index].' THEN '.$val[$field];
Derek Jones20aa2bd2010-03-02 17:28:07 -0600657 }
658 }
659 }
Barry Mienydd671972010-10-04 16:33:58 +0200660
Derek Jones20aa2bd2010-03-02 17:28:07 -0600661 $sql = "UPDATE ".$table." SET ";
662 $cases = '';
663
Pascal Krietec3a4a8d2011-02-14 13:40:08 -0500664 foreach ($final as $k => $v)
Derek Jones20aa2bd2010-03-02 17:28:07 -0600665 {
666 $cases .= $k.' = CASE '."\n";
667 foreach ($v as $row)
668 {
669 $cases .= $row."\n";
670 }
Barry Mienydd671972010-10-04 16:33:58 +0200671
Derek Jones20aa2bd2010-03-02 17:28:07 -0600672 $cases .= 'ELSE '.$k.' END, ';
673 }
Barry Mienydd671972010-10-04 16:33:58 +0200674
Derek Jones20aa2bd2010-03-02 17:28:07 -0600675 $sql .= substr($cases, 0, -2);
Barry Mienydd671972010-10-04 16:33:58 +0200676
Derek Jones20aa2bd2010-03-02 17:28:07 -0600677 $sql .= ' WHERE '.$where.$index.' IN ('.implode(',', $ids).')';
Barry Mienydd671972010-10-04 16:33:58 +0200678
Derek Jones20aa2bd2010-03-02 17:28:07 -0600679 return $sql;
680 }
681
682 // --------------------------------------------------------------------
683
684
Derek Allard2067d1a2008-11-13 22:59:24 +0000685 /**
686 * Truncate statement
687 *
688 * Generates a platform-specific truncate string from the supplied data
689 * If the database does not support the truncate() command
690 * This function maps to "DELETE FROM table"
691 *
692 * @access public
693 * @param string the table name
694 * @return string
Barry Mienydd671972010-10-04 16:33:58 +0200695 */
Derek Allard2067d1a2008-11-13 22:59:24 +0000696 function _truncate($table)
697 {
698 return "TRUNCATE ".$table;
699 }
Barry Mienydd671972010-10-04 16:33:58 +0200700
Derek Allard2067d1a2008-11-13 22:59:24 +0000701 // --------------------------------------------------------------------
702
703 /**
704 * Delete statement
705 *
706 * Generates a platform-specific delete string from the supplied data
707 *
708 * @access public
709 * @param string the table name
710 * @param array the where clause
711 * @param string the limit clause
712 * @return string
Barry Mienydd671972010-10-04 16:33:58 +0200713 */
Derek Allard2067d1a2008-11-13 22:59:24 +0000714 function _delete($table, $where = array(), $like = array(), $limit = FALSE)
715 {
716 $conditions = '';
717
718 if (count($where) > 0 OR count($like) > 0)
719 {
720 $conditions = "\nWHERE ";
721 $conditions .= implode("\n", $this->ar_where);
722
723 if (count($where) > 0 && count($like) > 0)
724 {
725 $conditions .= " AND ";
726 }
727 $conditions .= implode("\n", $like);
728 }
729
730 $limit = ( ! $limit) ? '' : ' LIMIT '.$limit;
Barry Mienydd671972010-10-04 16:33:58 +0200731
Derek Allard2067d1a2008-11-13 22:59:24 +0000732 return "DELETE FROM ".$table.$conditions.$limit;
733 }
734
735 // --------------------------------------------------------------------
736
737 /**
738 * Limit string
739 *
740 * Generates a platform-specific LIMIT clause
741 *
742 * @access public
743 * @param string the sql query string
744 * @param integer the number of rows to limit the query to
745 * @param integer the offset value
746 * @return string
747 */
748 function _limit($sql, $limit, $offset)
Barry Mienydd671972010-10-04 16:33:58 +0200749 {
Derek Allard2067d1a2008-11-13 22:59:24 +0000750 if ($offset == 0)
751 {
752 $offset = '';
753 }
754 else
755 {
756 $offset .= ", ";
757 }
Barry Mienydd671972010-10-04 16:33:58 +0200758
Derek Allard2067d1a2008-11-13 22:59:24 +0000759 return $sql."LIMIT ".$offset.$limit;
760 }
761
762 // --------------------------------------------------------------------
763
764 /**
765 * Close DB Connection
766 *
767 * @access public
768 * @param resource
769 * @return void
770 */
771 function _close($conn_id)
772 {
773 @mysql_close($conn_id);
774 }
Barry Mienydd671972010-10-04 16:33:58 +0200775
Derek Allard2067d1a2008-11-13 22:59:24 +0000776}
777
778
779/* End of file mysql_driver.php */
Derek Jonesa3ffbbb2008-05-11 18:18:29 +0000780/* Location: ./system/database/drivers/mysql/mysql_driver.php */