blob: 82e677a1a299e79c9e527b95cbd543ef8a90559b [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 */
86 function execute($sql)
87 {
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 }
117
118 // --------------------------------------------------------------------
119
120 /**
121 * Escape String
122 *
123 * @access public
124 * @param string
125 * @return string
126 */
127 function escape_str($str)
128 {
129 if (get_magic_quotes_gpc())
130 {
131 $str = stripslashes($str);
132 }
133 return mysql_real_escape_string($str);
134 }
135
136 // --------------------------------------------------------------------
137
138 /**
139 * Close DB Connection
140 *
141 * @access public
142 * @param resource
143 * @return void
144 */
145 function destroy($conn_id)
146 {
147 mysql_close($conn_id);
148 }
149
150 // --------------------------------------------------------------------
151
152 /**
153 * Affected Rows
154 *
155 * @access public
156 * @return integer
157 */
158 function affected_rows()
159 {
160 return @mysql_affected_rows($this->conn_id);
161 }
162
163 // --------------------------------------------------------------------
164
165 /**
166 * Insert ID
167 *
168 * @access public
169 * @return integer
170 */
171 function insert_id()
172 {
173 return @mysql_insert_id($this->conn_id);
174 }
175
176 // --------------------------------------------------------------------
177
178 /**
179 * "Count All" query
180 *
181 * Generates a platform-specific query string that counts all records in
182 * the specified database
183 *
184 * @access public
185 * @param string
186 * @return string
187 */
188 function count_all($table = '')
189 {
190 if ($table == '')
191 return '0';
192
193 $query = $this->query("SELECT COUNT(*) AS numrows FROM `".$this->dbprefix.$table."`");
194
195 if ($query->num_rows() == 0)
196 return '0';
197
198 $row = $query->row();
199 return $row->numrows;
200 }
201
202 // --------------------------------------------------------------------
203
204 /**
205 * The error message string
206 *
207 * @access public
208 * @return string
209 */
210 function error_message()
211 {
212 return mysql_error($this->conn_id);
213 }
214
215 // --------------------------------------------------------------------
216
217 /**
218 * The error message number
219 *
220 * @access public
221 * @return integer
222 */
223 function error_number()
224 {
225 return mysql_errno($this->conn_id);
226 }
227
228 // --------------------------------------------------------------------
229
230 /**
231 * Escape Table Name
232 *
233 * This function adds backticks if the table name has a period
234 * in it. Some DBs will get cranky unless periods are escaped
235 *
236 * @access public
237 * @param string the table name
238 * @return string
239 */
240 function escape_table($table)
241 {
242 if (stristr($table, '.'))
243 {
244 $table = preg_replace("/\./", "`.`", $table);
245 }
246
247 return $table;
248 }
249
250 // --------------------------------------------------------------------
251
252 /**
253 * Field data query
254 *
255 * Generates a platform-specific query so that the column data can be retrieved
256 *
257 * @access public
258 * @param string the table name
259 * @return object
260 */
261 function _field_data($table)
262 {
263 $sql = "SELECT * FROM ".$this->escape_table($table)." LIMIT 1";
264 $query = $this->query($sql);
265 return $query->field_data();
266 }
267
268 // --------------------------------------------------------------------
269
270 /**
271 * Insert statement
272 *
273 * Generates a platform-specific insert string from the supplied data
274 *
275 * @access public
276 * @param string the table name
277 * @param array the insert keys
278 * @param array the insert values
279 * @return string
280 */
281 function _insert($table, $keys, $values)
282 {
283 return "INSERT INTO ".$this->escape_table($table)." (".implode(', ', $keys).") VALUES (".implode(', ', $values).")";
284 }
285
286 // --------------------------------------------------------------------
287
288 /**
289 * Update statement
290 *
291 * Generates a platform-specific update string from the supplied data
292 *
293 * @access public
294 * @param string the table name
295 * @param array the update data
296 * @param array the where clause
297 * @return string
298 */
299 function _update($table, $values, $where)
300 {
301 foreach($values as $key => $val)
302 {
303 $valstr[] = $key." = ".$val;
304 }
305
306 return "UPDATE ".$this->escape_table($table)." SET ".implode(', ', $valstr)." WHERE ".implode(" ", $where);
307 }
308
309 // --------------------------------------------------------------------
310
311 /**
312 * Delete statement
313 *
314 * Generates a platform-specific delete string from the supplied data
315 *
316 * @access public
317 * @param string the table name
318 * @param array the where clause
319 * @return string
320 */
321 function _delete($table, $where)
322 {
323 return "DELETE FROM ".$this->escape_table($table)." WHERE ".implode(" ", $where);
324 }
325
326 // --------------------------------------------------------------------
327
328 /**
329 * Version number query string
330 *
331 * @access public
332 * @return string
333 */
334 function _version()
335 {
336 return "SELECT version() AS ver";
337 }
338
339 // --------------------------------------------------------------------
340
341 /**
342 * Show table query
343 *
344 * Generates a platform-specific query string so that the table names can be fetched
345 *
346 * @access public
347 * @return string
348 */
349 function _show_tables()
350 {
351 return "SHOW TABLES FROM `".$this->database."`";
352 }
353
354 // --------------------------------------------------------------------
355
356 /**
357 * Show columnn query
358 *
359 * Generates a platform-specific query string so that the column names can be fetched
360 *
361 * @access public
362 * @param string the table name
363 * @return string
364 */
365 function _show_columns($table = '')
366 {
367 return "SHOW COLUMNS FROM ".$this->escape_table($table);
368 }
369
370 // --------------------------------------------------------------------
371
372 /**
373 * Limit string
374 *
375 * Generates a platform-specific LIMIT clause
376 *
377 * @access public
378 * @param string the sql query string
379 * @param integer the number of rows to limit the query to
380 * @param integer the offset value
381 * @return string
382 */
383 function _limit($sql, $limit, $offset)
384 {
385 if ($offset == 0)
386 {
387 $offset = '';
388 }
389 else
390 {
391 $offset .= ", ";
392 }
393
394 return $sql."LIMIT ".$offset.$limit;
395 }
396
397}
398
399
400/**
401 * MySQL Result Class
402 *
403 * This class extends the parent result class: CI_DB_result
404 *
405 * @category Database
406 * @author Rick Ellis
407 * @link http://www.codeigniter.com/user_guide/libraries/database/
408 */
409class CI_DB_mysql_result extends CI_DB_result {
410
411 /**
412 * Number of rows in the result set
413 *
414 * @access public
415 * @return integer
416 */
417 function num_rows()
418 {
419 return @mysql_num_rows($this->result_id);
420 }
421
422 // --------------------------------------------------------------------
423
424 /**
425 * Number of fields in the result set
426 *
427 * @access public
428 * @return integer
429 */
430 function num_fields()
431 {
432 return @mysql_num_fields($this->result_id);
433 }
434
435 // --------------------------------------------------------------------
436
437 /**
438 * Field data
439 *
440 * Generates an array of objects containing field meta-data
441 *
442 * @access public
443 * @return array
444 */
445 function field_data()
446 {
447 $retval = array();
448 while ($field = mysql_fetch_field($this->result_id))
449 {
450 $F = new CI_DB_field();
451 $F->name = $field->name;
452 $F->type = $field->type;
453 $F->default = $field->def;
454 $F->max_length = $field->max_length;
455 $F->primary_key = $field->primary_key;
456
457 $retval[] = $F;
458 }
459
460 return $retval;
461 }
462
463 // --------------------------------------------------------------------
464
465 /**
466 * Result - associative array
467 *
468 * Returns the result set as an array
469 *
470 * @access private
471 * @return array
472 */
473 function _fetch_assoc()
474 {
475 return mysql_fetch_assoc($this->result_id);
476 }
477
478 // --------------------------------------------------------------------
479
480 /**
481 * Result - object
482 *
483 * Returns the result set as an object
484 *
485 * @access private
486 * @return object
487 */
488 function _fetch_object()
489 {
490 return mysql_fetch_object($this->result_id);
491 }
492
493}
494
495?>