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