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