1 <?php defined('SYSPATH') OR die('No direct access allowed.');
3 * MySQLi Database Driver
5 * $Id: Mysqli.php 4344 2009-05-11 16:41:39Z zombor $
9 * @copyright (c) 2007-2008 Kohana Team
10 * @license http://kohanaphp.com/license.html
12 class Database_Mysqli_Driver extends Database_Mysql_Driver {
14 // Database connection link
17 protected $statements = array();
20 * Sets the config for the class.
22 * @param array database configuration
24 public function __construct($config)
26 $this->db_config = $config;
28 Kohana::log('debug', 'MySQLi Database Driver Initialized');
32 * Closes the database connection.
34 public function __destruct()
36 is_object($this->link) and $this->link->close();
39 public function connect()
41 // Check if link already exists
42 if (is_object($this->link))
45 // Import the connect variables
46 extract($this->db_config['connection']);
48 // Build the connection info
49 $host = isset($host) ? $host : $socket;
51 // Make the connection and select the database
52 if ($this->link = new mysqli($host, $user, $pass, $database, $port))
54 if ($charset = $this->db_config['character_set'])
56 $this->set_charset($charset);
59 // Clear password after successful connect
60 $this->db_config['connection']['pass'] = NULL;
68 public function query($sql)
70 // Only cache if it's turned on, and only cache if it's not a write statement
71 if ($this->db_config['cache'] AND ! preg_match('#\b(?:INSERT|UPDATE|REPLACE|SET|DELETE|TRUNCATE)\b#i', $sql))
73 $hash = $this->query_hash($sql);
75 if ( ! isset($this->query_cache[$hash]))
77 // Set the cached object
78 $this->query_cache[$hash] = new Kohana_Mysqli_Result($this->link, $this->db_config['object'], $sql);
82 // Rewind cached result
83 $this->query_cache[$hash]->rewind();
86 // Return the cached query
87 return $this->query_cache[$hash];
90 return new Kohana_Mysqli_Result($this->link, $this->db_config['object'], $sql);
93 public function set_charset($charset)
95 if ($this->link->set_charset($charset) === FALSE)
96 throw new Kohana_Database_Exception('database.error', $this->show_error());
99 public function escape_str($str)
101 if (!$this->db_config['escape'])
104 is_object($this->link) or $this->connect();
106 return $this->link->real_escape_string($str);
109 public function show_error()
111 return $this->link->error;
114 } // End Database_Mysqli_Driver Class
119 class Kohana_Mysqli_Result extends Database_Result {
121 // Database connection
124 // Data fetching types
125 protected $fetch_type = 'mysqli_fetch_object';
126 protected $return_type = MYSQLI_ASSOC;
129 * Sets up the result variables.
131 * @param object database link
132 * @param boolean return objects or arrays
133 * @param string SQL query that was run
135 public function __construct($link, $object = TRUE, $sql)
139 if ( ! $this->link->multi_query($sql))
142 throw new Kohana_Database_Exception('database.error', $this->link->error.' - '.$sql);
146 $this->result = $this->link->store_result();
148 // If the query is an object, it was a SELECT, SHOW, DESCRIBE, EXPLAIN query
149 if (is_object($this->result))
151 $this->current_row = 0;
152 $this->total_rows = $this->result->num_rows;
153 $this->fetch_type = ($object === TRUE) ? 'fetch_object' : 'fetch_array';
155 elseif ($this->link->error)
158 throw new Kohana_Database_Exception('database.error', $this->link->error.' - '.$sql);
162 // Its an DELETE, INSERT, REPLACE, or UPDATE query
163 $this->insert_id = $this->link->insert_id;
164 $this->total_rows = $this->link->affected_rows;
169 $this->result($object);
176 * Magic __destruct function, frees the result.
178 public function __destruct()
180 if (is_object($this->result))
182 $this->result->free_result();
184 // this is kinda useless, but needs to be done to avoid the "Commands out of sync; you
185 // can't run this command now" error. Basically, we get all results after the first one
186 // (the one we actually need) and free them.
187 if (is_resource($this->link) AND $this->link->more_results())
191 if ($result = $this->link->store_result())
193 $result->free_result();
195 } while ($this->link->next_result());
200 public function result($object = TRUE, $type = MYSQLI_ASSOC)
202 $this->fetch_type = ((bool) $object) ? 'fetch_object' : 'fetch_array';
204 // This check has to be outside the previous statement, because we do not
205 // know the state of fetch_type when $object = NULL
206 // NOTE - The class set by $type must be defined before fetching the result,
207 // autoloading is disabled to save a lot of stupid overhead.
208 if ($this->fetch_type == 'fetch_object')
210 $this->return_type = (is_string($type) AND Kohana::auto_load($type)) ? $type : 'stdClass';
214 $this->return_type = $type;
220 public function as_array($object = NULL, $type = MYSQLI_ASSOC)
222 return $this->result_array($object, $type);
225 public function result_array($object = NULL, $type = MYSQLI_ASSOC)
229 if (is_string($object))
233 elseif (is_bool($object))
235 if ($object === TRUE)
237 $fetch = 'fetch_object';
239 // NOTE - The class set by $type must be defined before fetching the result,
240 // autoloading is disabled to save a lot of stupid overhead.
241 $type = (is_string($type) AND Kohana::auto_load($type)) ? $type : 'stdClass';
245 $fetch = 'fetch_array';
250 // Use the default config values
251 $fetch = $this->fetch_type;
253 if ($fetch == 'fetch_object')
255 $type = (is_string($type) AND Kohana::auto_load($type)) ? $type : 'stdClass';
259 if ($this->result->num_rows)
261 // Reset the pointer location to make sure things work properly
262 $this->result->data_seek(0);
264 while ($row = $this->result->$fetch($type))
270 return isset($rows) ? $rows : array();
273 public function list_fields()
275 $field_names = array();
276 while ($field = $this->result->fetch_field())
278 $field_names[] = $field->name;
284 public function seek($offset)
286 if ($this->offsetExists($offset) AND $this->result->data_seek($offset))
288 // Set the current row to the offset
289 $this->current_row = $offset;
297 public function offsetGet($offset)
299 if ( ! $this->seek($offset))
303 $fetch = $this->fetch_type;
304 return $this->result->$fetch($this->return_type);
307 } // End Mysqli_Result Class
310 * MySQLi Prepared Statement (experimental)
312 class Kohana_Mysqli_Statement {
314 protected $link = NULL;
316 protected $var_names = array();
317 protected $var_values = array();
319 public function __construct($sql, $link)
323 $this->stmt = $this->link->prepare($sql);
328 public function __destruct()
330 $this->stmt->close();
333 // Sets the bind parameters
334 public function bind_params($param_types, $params)
336 $this->var_names = array_keys($params);
337 $this->var_values = array_values($params);
338 call_user_func_array(array($this->stmt, 'bind_param'), array_merge($param_types, $var_names));
343 public function bind_result($params)
345 call_user_func_array(array($this->stmt, 'bind_result'), $params);
348 // Runs the statement
349 public function execute()
351 foreach ($this->var_names as $key => $name)
353 $$name = $this->var_values[$key];
355 $this->stmt->execute();