blob: fadcdd3c4b640a79f8852d6576fcffde27538e05 [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);
92 mysqli_next_result($this->conn_id);
93 return $result;
adminb0dd10f2006-08-25 17:25:49 +000094 }
95
96 // --------------------------------------------------------------------
97
98 /**
99 * Prep the query
100 *
101 * If needed, each database adapter can prep the query string
102 *
103 * @access private called by execute()
104 * @param string an SQL query
105 * @return string
106 */
adminb071bb52006-08-26 19:28:37 +0000107 function _prep_query($sql)
adminb0dd10f2006-08-25 17:25:49 +0000108 {
109 // "DELETE FROM TABLE" returns 0 affected rows This hack modifies
110 // the query so that it returns the number of affected rows
111 if ($this->delete_hack === TRUE)
112 {
113 if (preg_match('/^\s*DELETE\s+FROM\s+(\S+)\s*$/i', $sql))
114 {
115 $sql = preg_replace("/^\s*DELETE\s+FROM\s+(\S+)\s*$/", "DELETE FROM \\1 WHERE 1=1", $sql);
116 }
117 }
118
119 return $sql;
120 }
121
122 // --------------------------------------------------------------------
123
124 /**
125 * Escape String
126 *
127 * @access public
128 * @param string
129 * @return string
130 */
131 function escape_str($str)
132 {
adminb0dd10f2006-08-25 17:25:49 +0000133 return mysqli_real_escape_string($this->conn_id, $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 mysqli_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 @mysqli_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 @mysqli_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 mysqli_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 mysqli_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 $sql .= "LIMIT ".$limit;
386
387 if ($offset > 0)
388 {
389 $sql .= " OFFSET ".$offset;
390 }
391
392 return $sql;
393 }
394
395}
396
397
398
399/**
400 * MySQLi Result Class
401 *
402 * This class extends the parent result class: CI_DB_result
403 *
404 * @category Database
405 * @author Rick Ellis
406 * @link http://www.codeigniter.com/user_guide/libraries/database/
407 */
408class CI_DB_mysqli_result extends CI_DB_result {
409
410 /**
411 * Number of rows in the result set
412 *
413 * @access public
414 * @return integer
415 */
416 function num_rows()
417 {
418 return @mysqli_num_rows($this->result_id);
419 }
420
421 // --------------------------------------------------------------------
422
423 /**
424 * Number of fields in the result set
425 *
426 * @access public
427 * @return integer
428 */
429 function num_fields()
430 {
431 return @mysqli_num_fields($this->result_id);
432 }
433
434 // --------------------------------------------------------------------
435
436 /**
437 * Field data
438 *
439 * Generates an array of objects containing field meta-data
440 *
441 * @access public
442 * @return array
443 */
444 function field_data()
445 {
446 $retval = array();
447 while ($field = mysqli_fetch_field($this->result_id))
448 {
449 $F = new CI_DB_field();
450 $F->name = $field->name;
451 $F->type = $field->type;
452 $F->default = $field->def;
453 $F->max_length = $field->max_length;
454 $F->primary_key = 0;
455
456 $retval[] = $F;
457 }
458
459 return $retval;
460 }
461
462 // --------------------------------------------------------------------
463
464 /**
465 * Result - associative array
466 *
467 * Returns the result set as an array
468 *
469 * @access private
470 * @return array
471 */
472 function _fetch_assoc()
473 {
474 return mysqli_fetch_assoc($this->result_id);
475 }
476
477 // --------------------------------------------------------------------
478
479 /**
480 * Result - object
481 *
482 * Returns the result set as an object
483 *
484 * @access private
485 * @return object
486 */
487 function _fetch_object()
488 {
489 return mysqli_fetch_object($this->result_id);
490 }
491
492}
493
494?>