blob: 69a6db0eb52de98b65003fd79882eb3d7ba94f5b [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 */
126 function trans_begin()
127 {
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
139 $this->simple_query('SET AUTOCOMMIT=0');
140 $this->simple_query('START TRANSACTION'); // can also be BEGIN or BEGIN WORK
141 return TRUE;
142 }
143
144 // --------------------------------------------------------------------
145
146 /**
147 * Commit Transaction
148 *
149 * @access public
150 * @return bool
151 */
152 function trans_commit()
153 {
154 if ( ! $this->trans_enabled)
155 {
156 return TRUE;
157 }
158
159 // When transactions are nested we only begin/commit/rollback the outermost ones
160 if ($this->_trans_depth > 0)
161 {
162 return TRUE;
163 }
164
165 $this->simple_query('COMMIT');
166 $this->simple_query('SET AUTOCOMMIT=1');
167 return TRUE;
168 }
169
170 // --------------------------------------------------------------------
171
172 /**
173 * Rollback Transaction
174 *
175 * @access public
176 * @return bool
177 */
178 function trans_rollback()
179 {
180 if ( ! $this->trans_enabled)
181 {
182 return TRUE;
183 }
184
185 // When transactions are nested we only begin/commit/rollback the outermost ones
186 if ($this->_trans_depth > 0)
187 {
188 return TRUE;
189 }
190
191 $this->simple_query('ROLLBACK');
192 $this->simple_query('SET AUTOCOMMIT=1');
193 return TRUE;
194 }
adminb0dd10f2006-08-25 17:25:49 +0000195
196 // --------------------------------------------------------------------
197
198 /**
199 * Escape String
200 *
201 * @access public
202 * @param string
203 * @return string
204 */
205 function escape_str($str)
206 {
adminb0dd10f2006-08-25 17:25:49 +0000207 return mysql_real_escape_string($str);
208 }
209
210 // --------------------------------------------------------------------
211
212 /**
213 * Close DB Connection
214 *
215 * @access public
216 * @param resource
217 * @return void
218 */
219 function destroy($conn_id)
220 {
221 mysql_close($conn_id);
222 }
223
224 // --------------------------------------------------------------------
225
226 /**
227 * Affected Rows
228 *
229 * @access public
230 * @return integer
231 */
232 function affected_rows()
233 {
234 return @mysql_affected_rows($this->conn_id);
235 }
236
237 // --------------------------------------------------------------------
238
239 /**
240 * Insert ID
241 *
242 * @access public
243 * @return integer
244 */
245 function insert_id()
246 {
247 return @mysql_insert_id($this->conn_id);
248 }
249
250 // --------------------------------------------------------------------
251
252 /**
253 * "Count All" query
254 *
255 * Generates a platform-specific query string that counts all records in
256 * the specified database
257 *
258 * @access public
259 * @param string
260 * @return string
261 */
262 function count_all($table = '')
263 {
264 if ($table == '')
265 return '0';
266
267 $query = $this->query("SELECT COUNT(*) AS numrows FROM `".$this->dbprefix.$table."`");
268
269 if ($query->num_rows() == 0)
270 return '0';
271
272 $row = $query->row();
273 return $row->numrows;
274 }
275
276 // --------------------------------------------------------------------
277
278 /**
279 * The error message string
280 *
281 * @access public
282 * @return string
283 */
284 function error_message()
285 {
286 return mysql_error($this->conn_id);
287 }
288
289 // --------------------------------------------------------------------
290
291 /**
292 * The error message number
293 *
294 * @access public
295 * @return integer
296 */
297 function error_number()
298 {
299 return mysql_errno($this->conn_id);
300 }
301
302 // --------------------------------------------------------------------
303
304 /**
305 * Escape Table Name
306 *
307 * This function adds backticks if the table name has a period
308 * in it. Some DBs will get cranky unless periods are escaped
309 *
310 * @access public
311 * @param string the table name
312 * @return string
313 */
314 function escape_table($table)
315 {
316 if (stristr($table, '.'))
317 {
318 $table = preg_replace("/\./", "`.`", $table);
319 }
320
321 return $table;
322 }
323
324 // --------------------------------------------------------------------
325
326 /**
327 * Field data query
328 *
329 * Generates a platform-specific query so that the column data can be retrieved
330 *
331 * @access public
332 * @param string the table name
333 * @return object
334 */
335 function _field_data($table)
336 {
337 $sql = "SELECT * FROM ".$this->escape_table($table)." LIMIT 1";
338 $query = $this->query($sql);
339 return $query->field_data();
340 }
341
342 // --------------------------------------------------------------------
343
344 /**
345 * Insert statement
346 *
347 * Generates a platform-specific insert string from the supplied data
348 *
349 * @access public
350 * @param string the table name
351 * @param array the insert keys
352 * @param array the insert values
353 * @return string
354 */
355 function _insert($table, $keys, $values)
356 {
357 return "INSERT INTO ".$this->escape_table($table)." (".implode(', ', $keys).") VALUES (".implode(', ', $values).")";
358 }
359
360 // --------------------------------------------------------------------
361
362 /**
363 * Update statement
364 *
365 * Generates a platform-specific update string from the supplied data
366 *
367 * @access public
368 * @param string the table name
369 * @param array the update data
370 * @param array the where clause
371 * @return string
372 */
373 function _update($table, $values, $where)
374 {
375 foreach($values as $key => $val)
376 {
377 $valstr[] = $key." = ".$val;
378 }
379
380 return "UPDATE ".$this->escape_table($table)." SET ".implode(', ', $valstr)." WHERE ".implode(" ", $where);
381 }
382
383 // --------------------------------------------------------------------
384
385 /**
386 * Delete statement
387 *
388 * Generates a platform-specific delete string from the supplied data
389 *
390 * @access public
391 * @param string the table name
392 * @param array the where clause
393 * @return string
394 */
395 function _delete($table, $where)
396 {
397 return "DELETE FROM ".$this->escape_table($table)." WHERE ".implode(" ", $where);
398 }
399
400 // --------------------------------------------------------------------
401
402 /**
403 * Version number query string
404 *
405 * @access public
406 * @return string
407 */
408 function _version()
409 {
410 return "SELECT version() AS ver";
411 }
412
413 // --------------------------------------------------------------------
414
415 /**
416 * Show table query
417 *
418 * Generates a platform-specific query string so that the table names can be fetched
419 *
420 * @access public
421 * @return string
422 */
423 function _show_tables()
424 {
425 return "SHOW TABLES FROM `".$this->database."`";
426 }
427
428 // --------------------------------------------------------------------
429
430 /**
431 * Show columnn query
432 *
433 * Generates a platform-specific query string so that the column names can be fetched
434 *
435 * @access public
436 * @param string the table name
437 * @return string
438 */
439 function _show_columns($table = '')
440 {
441 return "SHOW COLUMNS FROM ".$this->escape_table($table);
442 }
443
444 // --------------------------------------------------------------------
445
446 /**
447 * Limit string
448 *
449 * Generates a platform-specific LIMIT clause
450 *
451 * @access public
452 * @param string the sql query string
453 * @param integer the number of rows to limit the query to
454 * @param integer the offset value
455 * @return string
456 */
457 function _limit($sql, $limit, $offset)
458 {
459 if ($offset == 0)
460 {
461 $offset = '';
462 }
463 else
464 {
465 $offset .= ", ";
466 }
467
468 return $sql."LIMIT ".$offset.$limit;
469 }
470
471}
472
473
474/**
475 * MySQL Result Class
476 *
477 * This class extends the parent result class: CI_DB_result
478 *
479 * @category Database
480 * @author Rick Ellis
481 * @link http://www.codeigniter.com/user_guide/libraries/database/
482 */
483class CI_DB_mysql_result extends CI_DB_result {
484
485 /**
486 * Number of rows in the result set
487 *
488 * @access public
489 * @return integer
490 */
491 function num_rows()
492 {
493 return @mysql_num_rows($this->result_id);
494 }
495
496 // --------------------------------------------------------------------
497
498 /**
499 * Number of fields in the result set
500 *
501 * @access public
502 * @return integer
503 */
504 function num_fields()
505 {
506 return @mysql_num_fields($this->result_id);
507 }
508
509 // --------------------------------------------------------------------
510
511 /**
512 * Field data
513 *
514 * Generates an array of objects containing field meta-data
515 *
516 * @access public
517 * @return array
518 */
519 function field_data()
520 {
521 $retval = array();
522 while ($field = mysql_fetch_field($this->result_id))
523 {
admine348efb2006-09-20 21:13:26 +0000524 $F = new stdClass();
adminb0dd10f2006-08-25 17:25:49 +0000525 $F->name = $field->name;
526 $F->type = $field->type;
527 $F->default = $field->def;
528 $F->max_length = $field->max_length;
529 $F->primary_key = $field->primary_key;
530
531 $retval[] = $F;
532 }
533
534 return $retval;
535 }
536
537 // --------------------------------------------------------------------
538
539 /**
540 * Result - associative array
541 *
542 * Returns the result set as an array
543 *
544 * @access private
545 * @return array
546 */
547 function _fetch_assoc()
548 {
549 return mysql_fetch_assoc($this->result_id);
550 }
551
552 // --------------------------------------------------------------------
553
554 /**
555 * Result - object
556 *
557 * Returns the result set as an object
558 *
559 * @access private
560 * @return object
561 */
562 function _fetch_object()
563 {
564 return mysql_fetch_object($this->result_id);
565 }
566
567}
568
569?>