1 <?php
defined('SYSPATH') OR die('No direct access allowed.');
3 * MySQLi Database Driver
5 * $Id: Mysqli.php 3917 2009-01-21 03:06:22Z 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->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)\b#i', $sql))
73 $hash = $this->query_hash($sql);
75 if ( ! isset(self
::$query_cache[$hash]))
77 // Set the cached object
78 self
::$query_cache[$hash] = new Kohana_Mysqli_Result($this->link
, $this->db_config
['object'], $sql);
81 // Return the cached query
82 return self
::$query_cache[$hash];
85 return new Kohana_Mysqli_Result($this->link
, $this->db_config
['object'], $sql);
88 public function set_charset($charset)
90 if ($this->link
->set_charset($charset) === FALSE)
91 throw new Kohana_Database_Exception('database.error', $this->show_error());
94 public function stmt_prepare($sql = '')
96 is_object($this->link
) or $this->connect();
97 return new Kohana_Mysqli_Statement($sql, $this->link
);
100 public function escape_str($str)
102 if (!$this->db_config
['escape'])
105 is_object($this->link
) or $this->connect();
107 return $this->link
->real_escape_string($str);
110 public function show_error()
112 return $this->link
->error
;
115 public function field_data($table)
118 $query = $this->link
->query('SHOW COLUMNS FROM '.$this->escape_table($table));
120 if (is_object($query))
122 while ($row = $query->fetch_object())
131 } // End Database_Mysqli_Driver Class
136 class Kohana_Mysqli_Result
extends Database_Result
{
138 // Database connection
141 // Data fetching types
142 protected $fetch_type = 'mysqli_fetch_object';
143 protected $return_type = MYSQLI_ASSOC
;
146 * Sets up the result variables.
148 * @param object database link
149 * @param boolean return objects or arrays
150 * @param string SQL query that was run
152 public function __construct($link, $object = TRUE, $sql)
156 if ( ! $this->link
->multi_query($sql))
159 throw new Kohana_Database_Exception('database.error', $this->link
->error
.' - '.$sql);
163 $this->result
= $this->link
->store_result();
165 // If the query is an object, it was a SELECT, SHOW, DESCRIBE, EXPLAIN query
166 if (is_object($this->result
))
168 $this->current_row
= 0;
169 $this->total_rows
= $this->result
->num_rows
;
170 $this->fetch_type
= ($object === TRUE) ?
'fetch_object' : 'fetch_array';
172 elseif ($this->link
->error
)
175 throw new Kohana_Database_Exception('database.error', $this->link
->error
.' - '.$sql);
179 // Its an DELETE, INSERT, REPLACE, or UPDATE query
180 $this->insert_id
= $this->link
->insert_id
;
181 $this->total_rows
= $this->link
->affected_rows
;
186 $this->result($object);
193 * Magic __destruct function, frees the result.
195 public function __destruct()
197 if (is_object($this->result
))
199 $this->result
->free_result();
201 // this is kinda useless, but needs to be done to avoid the "Commands out of sync; you
202 // can't run this command now" error. Basically, we get all results after the first one
203 // (the one we actually need) and free them.
204 if (is_resource($this->link
) AND $this->link
->more_results())
208 if ($result = $this->link
->store_result())
210 $result->free_result();
212 } while ($this->link
->next_result());
217 public function result($object = TRUE, $type = MYSQLI_ASSOC
)
219 $this->fetch_type
= ((bool) $object) ?
'fetch_object' : 'fetch_array';
221 // This check has to be outside the previous statement, because we do not
222 // know the state of fetch_type when $object = NULL
223 // NOTE - The class set by $type must be defined before fetching the result,
224 // autoloading is disabled to save a lot of stupid overhead.
225 if ($this->fetch_type
== 'fetch_object')
227 $this->return_type
= (is_string($type) AND Kohana
::auto_load($type)) ?
$type : 'stdClass';
231 $this->return_type
= $type;
237 public function as_array($object = NULL, $type = MYSQLI_ASSOC
)
239 return $this->result_array($object, $type);
242 public function result_array($object = NULL, $type = MYSQLI_ASSOC
)
246 if (is_string($object))
250 elseif (is_bool($object))
252 if ($object === TRUE)
254 $fetch = 'fetch_object';
256 // NOTE - The class set by $type must be defined before fetching the result,
257 // autoloading is disabled to save a lot of stupid overhead.
258 $type = (is_string($type) AND Kohana
::auto_load($type)) ?
$type : 'stdClass';
262 $fetch = 'fetch_array';
267 // Use the default config values
268 $fetch = $this->fetch_type
;
270 if ($fetch == 'fetch_object')
272 $type = (is_string($type) AND Kohana
::auto_load($type)) ?
$type : 'stdClass';
276 if ($this->result
->num_rows
)
278 // Reset the pointer location to make sure things work properly
279 $this->result
->data_seek(0);
281 while ($row = $this->result
->$fetch($type))
287 return isset($rows) ?
$rows : array();
290 public function list_fields()
292 $field_names = array();
293 while ($field = $this->result
->fetch_field())
295 $field_names[] = $field->name
;
301 public function seek($offset)
303 if ( ! $this->offsetExists($offset))
306 $this->result
->data_seek($offset);
311 public function offsetGet($offset)
313 if ( ! $this->seek($offset))
317 $fetch = $this->fetch_type
;
318 return $this->result
->$fetch($this->return_type
);
321 } // End Mysqli_Result Class
324 * MySQLi Prepared Statement (experimental)
326 class Kohana_Mysqli_Statement
{
328 protected $link = NULL;
330 protected $var_names = array();
331 protected $var_values = array();
333 public function __construct($sql, $link)
337 $this->stmt
= $this->link
->prepare($sql);
342 public function __destruct()
344 $this->stmt
->close();
347 // Sets the bind parameters
348 public function bind_params($param_types, $params)
350 $this->var_names
= array_keys($params);
351 $this->var_values
= array_values($params);
352 call_user_func_array(array($this->stmt
, 'bind_param'), array_merge($param_types, $var_names));
357 public function bind_result($params)
359 call_user_func_array(array($this->stmt
, 'bind_result'), $params);
362 // Runs the statement
363 public function execute()
365 foreach ($this->var_names
as $key => $name)
367 $
$name = $this->var_values
[$key];
369 $this->stmt
->execute();