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