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