blob: 208f09c2535fc5fc88c730dff704d8e0a831f485 [file] [log] [blame]
adminb0dd10f2006-08-25 17:25:49 +00001<?php if (!defined('BASEPATH')) exit('No direct script access allowed');
2/**
3 * Code Igniter
4 *
5 * An open source application development framework for PHP 4.3.2 or newer
6 *
7 * @package CodeIgniter
8 * @author Rick Ellis
9 * @copyright Copyright (c) 2006, pMachine, Inc.
10 * @license http://www.codeignitor.com/user_guide/license.html
11 * @link http://www.codeigniter.com
12 * @since Version 1.0
13 * @filesource
14 */
15
16// ------------------------------------------------------------------------
17
18/**
19 * MySQL Database Adapter Class
20 *
21 * Note: _DB is an extender class that the app controller
22 * creates dynamically based on whether the active record
23 * class is being used or not.
24 *
25 * @package CodeIgniter
26 * @subpackage Drivers
27 * @category Database
28 * @author Rick Ellis
29 * @link http://www.codeigniter.com/user_guide/libraries/database/
30 */
31class CI_DB_mysql extends CI_DB {
32
33 /**
34 * Whether to use the MySQL "delete hack" which allows the number
35 * of affected rows to be shown. Uses a preg_replace when enabled,
36 * adding a bit more processing to all queries.
37 */
38 var $delete_hack = TRUE;
39
40 /**
41 * Non-persistent database connection
42 *
43 * @access private called by the base class
44 * @return resource
45 */
46 function db_connect()
47 {
48 return mysql_connect($this->hostname, $this->username, $this->password, TRUE);
49 }
50
51 // --------------------------------------------------------------------
52
53 /**
54 * Persistent database connection
55 *
56 * @access private called by the base class
57 * @return resource
58 */
59 function db_pconnect()
60 {
61 return mysql_pconnect($this->hostname, $this->username, $this->password);
62 }
63
64 // --------------------------------------------------------------------
65
66 /**
67 * Select the database
68 *
69 * @access private called by the base class
70 * @return resource
71 */
72 function db_select()
73 {
74 return @mysql_select_db($this->database, $this->conn_id);
75 }
76
77 // --------------------------------------------------------------------
78
79 /**
80 * Execute the query
81 *
82 * @access private called by the base class
83 * @param string an SQL query
84 * @return resource
85 */
adminf5ea6652006-09-23 19:58:47 +000086 function _execute($sql)
adminb0dd10f2006-08-25 17:25:49 +000087 {
88 $sql = $this->_prep_query($sql);
89 return @mysql_query($sql, $this->conn_id);
90 }
91
92 // --------------------------------------------------------------------
93
94 /**
95 * Prep the query
96 *
97 * If needed, each database adapter can prep the query string
98 *
99 * @access private called by execute()
100 * @param string an SQL query
101 * @return string
102 */
adminb071bb52006-08-26 19:28:37 +0000103 function _prep_query($sql)
adminb0dd10f2006-08-25 17:25:49 +0000104 {
105 // "DELETE FROM TABLE" returns 0 affected rows This hack modifies
106 // the query so that it returns the number of affected rows
107 if ($this->delete_hack === TRUE)
108 {
109 if (preg_match('/^\s*DELETE\s+FROM\s+(\S+)\s*$/i', $sql))
110 {
111 $sql = preg_replace("/^\s*DELETE\s+FROM\s+(\S+)\s*$/", "DELETE FROM \\1 WHERE 1=1", $sql);
112 }
113 }
114
115 return $sql;
116 }
adminf5ea6652006-09-23 19:58:47 +0000117
118 // --------------------------------------------------------------------
119
120 /**
121 * Begin Transaction
122 *
123 * @access public
124 * @return bool
125 */
admin8b180be2006-09-24 01:12:22 +0000126 function trans_begin($test_mode = FALSE)
adminf5ea6652006-09-23 19:58:47 +0000127 {
128 if ( ! $this->trans_enabled)
129 {
130 return TRUE;
131 }
132
133 // When transactions are nested we only begin/commit/rollback the outermost ones
134 if ($this->_trans_depth > 0)
135 {
136 return TRUE;
137 }
138
admin8b180be2006-09-24 01:12:22 +0000139 // Reset the transaction failure flag.
140 // If the $test_mode flag is set to TRUE transactions will be rolled back
141 // even if the queries produce a successful result.
142 $this->_trans_failure = ($test_mode === TRUE) ? TRUE : FALSE;
143
adminf5ea6652006-09-23 19:58:47 +0000144 $this->simple_query('SET AUTOCOMMIT=0');
145 $this->simple_query('START TRANSACTION'); // can also be BEGIN or BEGIN WORK
146 return TRUE;
147 }
148
149 // --------------------------------------------------------------------
150
151 /**
152 * Commit Transaction
153 *
154 * @access public
155 * @return bool
156 */
157 function trans_commit()
158 {
159 if ( ! $this->trans_enabled)
160 {
161 return TRUE;
162 }
163
164 // When transactions are nested we only begin/commit/rollback the outermost ones
165 if ($this->_trans_depth > 0)
166 {
167 return TRUE;
168 }
169
170 $this->simple_query('COMMIT');
171 $this->simple_query('SET AUTOCOMMIT=1');
172 return TRUE;
173 }
174
175 // --------------------------------------------------------------------
176
177 /**
178 * Rollback Transaction
179 *
180 * @access public
181 * @return bool
182 */
183 function trans_rollback()
184 {
185 if ( ! $this->trans_enabled)
186 {
187 return TRUE;
188 }
189
190 // When transactions are nested we only begin/commit/rollback the outermost ones
191 if ($this->_trans_depth > 0)
192 {
193 return TRUE;
194 }
195
196 $this->simple_query('ROLLBACK');
197 $this->simple_query('SET AUTOCOMMIT=1');
198 return TRUE;
199 }
adminb0dd10f2006-08-25 17:25:49 +0000200
201 // --------------------------------------------------------------------
202
203 /**
204 * Escape String
205 *
206 * @access public
207 * @param string
208 * @return string
209 */
210 function escape_str($str)
211 {
adminb0dd10f2006-08-25 17:25:49 +0000212 return mysql_real_escape_string($str);
213 }
214
215 // --------------------------------------------------------------------
216
217 /**
218 * Close DB Connection
219 *
220 * @access public
221 * @param resource
222 * @return void
223 */
224 function destroy($conn_id)
225 {
226 mysql_close($conn_id);
227 }
228
229 // --------------------------------------------------------------------
230
231 /**
232 * Affected Rows
233 *
234 * @access public
235 * @return integer
236 */
237 function affected_rows()
238 {
239 return @mysql_affected_rows($this->conn_id);
240 }
241
242 // --------------------------------------------------------------------
243
244 /**
245 * Insert ID
246 *
247 * @access public
248 * @return integer
249 */
250 function insert_id()
251 {
252 return @mysql_insert_id($this->conn_id);
253 }
254
255 // --------------------------------------------------------------------
256
257 /**
258 * "Count All" query
259 *
260 * Generates a platform-specific query string that counts all records in
261 * the specified database
262 *
263 * @access public
264 * @param string
265 * @return string
266 */
267 function count_all($table = '')
268 {
269 if ($table == '')
270 return '0';
271
272 $query = $this->query("SELECT COUNT(*) AS numrows FROM `".$this->dbprefix.$table."`");
273
274 if ($query->num_rows() == 0)
275 return '0';
276
277 $row = $query->row();
278 return $row->numrows;
279 }
280
281 // --------------------------------------------------------------------
282
283 /**
284 * The error message string
285 *
286 * @access public
287 * @return string
288 */
289 function error_message()
290 {
291 return mysql_error($this->conn_id);
292 }
293
294 // --------------------------------------------------------------------
295
296 /**
297 * The error message number
298 *
299 * @access public
300 * @return integer
301 */
302 function error_number()
303 {
304 return mysql_errno($this->conn_id);
305 }
306
307 // --------------------------------------------------------------------
308
309 /**
310 * Escape Table Name
311 *
312 * This function adds backticks if the table name has a period
313 * in it. Some DBs will get cranky unless periods are escaped
314 *
315 * @access public
316 * @param string the table name
317 * @return string
318 */
319 function escape_table($table)
320 {
321 if (stristr($table, '.'))
322 {
323 $table = preg_replace("/\./", "`.`", $table);
324 }
325
326 return $table;
327 }
328
329 // --------------------------------------------------------------------
330
331 /**
332 * Field data query
333 *
334 * Generates a platform-specific query so that the column data can be retrieved
335 *
336 * @access public
337 * @param string the table name
338 * @return object
339 */
340 function _field_data($table)
341 {
342 $sql = "SELECT * FROM ".$this->escape_table($table)." LIMIT 1";
343 $query = $this->query($sql);
344 return $query->field_data();
345 }
346
347 // --------------------------------------------------------------------
348
349 /**
350 * Insert statement
351 *
352 * Generates a platform-specific insert string from the supplied data
353 *
354 * @access public
355 * @param string the table name
356 * @param array the insert keys
357 * @param array the insert values
358 * @return string
359 */
360 function _insert($table, $keys, $values)
361 {
362 return "INSERT INTO ".$this->escape_table($table)." (".implode(', ', $keys).") VALUES (".implode(', ', $values).")";
363 }
364
365 // --------------------------------------------------------------------
366
367 /**
368 * Update statement
369 *
370 * Generates a platform-specific update string from the supplied data
371 *
372 * @access public
373 * @param string the table name
374 * @param array the update data
375 * @param array the where clause
376 * @return string
377 */
378 function _update($table, $values, $where)
379 {
380 foreach($values as $key => $val)
381 {
382 $valstr[] = $key." = ".$val;
383 }
384
385 return "UPDATE ".$this->escape_table($table)." SET ".implode(', ', $valstr)." WHERE ".implode(" ", $where);
386 }
387
388 // --------------------------------------------------------------------
389
390 /**
391 * Delete statement
392 *
393 * Generates a platform-specific delete string from the supplied data
394 *
395 * @access public
396 * @param string the table name
397 * @param array the where clause
398 * @return string
399 */
400 function _delete($table, $where)
401 {
402 return "DELETE FROM ".$this->escape_table($table)." WHERE ".implode(" ", $where);
403 }
404
405 // --------------------------------------------------------------------
406
407 /**
408 * Version number query string
409 *
410 * @access public
411 * @return string
412 */
413 function _version()
414 {
415 return "SELECT version() AS ver";
416 }
417
418 // --------------------------------------------------------------------
419
420 /**
421 * Show table query
422 *
423 * Generates a platform-specific query string so that the table names can be fetched
424 *
425 * @access public
426 * @return string
427 */
428 function _show_tables()
429 {
430 return "SHOW TABLES FROM `".$this->database."`";
431 }
432
433 // --------------------------------------------------------------------
434
435 /**
436 * Show columnn query
437 *
438 * Generates a platform-specific query string so that the column names can be fetched
439 *
440 * @access public
441 * @param string the table name
442 * @return string
443 */
444 function _show_columns($table = '')
445 {
446 return "SHOW COLUMNS FROM ".$this->escape_table($table);
447 }
448
449 // --------------------------------------------------------------------
450
451 /**
452 * Limit string
453 *
454 * Generates a platform-specific LIMIT clause
455 *
456 * @access public
457 * @param string the sql query string
458 * @param integer the number of rows to limit the query to
459 * @param integer the offset value
460 * @return string
461 */
462 function _limit($sql, $limit, $offset)
463 {
464 if ($offset == 0)
465 {
466 $offset = '';
467 }
468 else
469 {
470 $offset .= ", ";
471 }
472
473 return $sql."LIMIT ".$offset.$limit;
474 }
475
476}
477
478
479/**
480 * MySQL Result Class
481 *
482 * This class extends the parent result class: CI_DB_result
483 *
484 * @category Database
485 * @author Rick Ellis
486 * @link http://www.codeigniter.com/user_guide/libraries/database/
487 */
488class CI_DB_mysql_result extends CI_DB_result {
489
490 /**
491 * Number of rows in the result set
492 *
493 * @access public
494 * @return integer
495 */
496 function num_rows()
497 {
498 return @mysql_num_rows($this->result_id);
499 }
500
501 // --------------------------------------------------------------------
502
503 /**
504 * Number of fields in the result set
505 *
506 * @access public
507 * @return integer
508 */
509 function num_fields()
510 {
511 return @mysql_num_fields($this->result_id);
512 }
513
514 // --------------------------------------------------------------------
515
516 /**
517 * Field data
518 *
519 * Generates an array of objects containing field meta-data
520 *
521 * @access public
522 * @return array
523 */
524 function field_data()
525 {
526 $retval = array();
527 while ($field = mysql_fetch_field($this->result_id))
528 {
admine348efb2006-09-20 21:13:26 +0000529 $F = new stdClass();
adminb0dd10f2006-08-25 17:25:49 +0000530 $F->name = $field->name;
531 $F->type = $field->type;
532 $F->default = $field->def;
533 $F->max_length = $field->max_length;
534 $F->primary_key = $field->primary_key;
535
536 $retval[] = $F;
537 }
538
539 return $retval;
540 }
541
542 // --------------------------------------------------------------------
543
544 /**
545 * Result - associative array
546 *
547 * Returns the result set as an array
548 *
549 * @access private
550 * @return array
551 */
552 function _fetch_assoc()
553 {
554 return mysql_fetch_assoc($this->result_id);
555 }
556
557 // --------------------------------------------------------------------
558
559 /**
560 * Result - object
561 *
562 * Returns the result set as an object
563 *
564 * @access private
565 * @return object
566 */
567 function _fetch_object()
568 {
569 return mysql_fetch_object($this->result_id);
570 }
571
572}
573
574?>