1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
|
<?php defined('SYSPATH') OR die('No direct access allowed.');
/**
* MySQLi Database Driver
*
* $Id: Mysqli.php 4344 2009-05-11 16:41:39Z zombor $
*
* @package Core
* @author Kohana Team
* @copyright (c) 2007-2008 Kohana Team
* @license http://kohanaphp.com/license.html
*/
class Database_Mysqli_Driver extends Database_Mysql_Driver {
// Database connection link
protected $link;
protected $db_config;
protected $statements = array();
/**
* Sets the config for the class.
*
* @param array database configuration
*/
public function __construct($config)
{
$this->db_config = $config;
Kohana::log('debug', 'MySQLi Database Driver Initialized');
}
/**
* Closes the database connection.
*/
public function __destruct()
{
is_object($this->link) and $this->link->close();
}
public function connect()
{
// Check if link already exists
if (is_object($this->link))
return $this->link;
// Import the connect variables
extract($this->db_config['connection']);
// Build the connection info
$host = isset($host) ? $host : $socket;
// Make the connection and select the database
if ($this->link = new mysqli($host, $user, $pass, $database, $port))
{
if ($charset = $this->db_config['character_set'])
{
$this->set_charset($charset);
}
// Clear password after successful connect
$this->db_config['connection']['pass'] = NULL;
return $this->link;
}
return FALSE;
}
public function query($sql)
{
// Only cache if it's turned on, and only cache if it's not a write statement
if ($this->db_config['cache'] AND ! preg_match('#\b(?:INSERT|UPDATE|REPLACE|SET|DELETE|TRUNCATE)\b#i', $sql))
{
$hash = $this->query_hash($sql);
if ( ! isset($this->query_cache[$hash]))
{
// Set the cached object
$this->query_cache[$hash] = new Kohana_Mysqli_Result($this->link, $this->db_config['object'], $sql);
}
else
{
// Rewind cached result
$this->query_cache[$hash]->rewind();
}
// Return the cached query
return $this->query_cache[$hash];
}
return new Kohana_Mysqli_Result($this->link, $this->db_config['object'], $sql);
}
public function set_charset($charset)
{
if ($this->link->set_charset($charset) === FALSE)
throw new Kohana_Database_Exception('database.error', $this->show_error());
}
public function escape_str($str)
{
if (!$this->db_config['escape'])
return $str;
is_object($this->link) or $this->connect();
return $this->link->real_escape_string($str);
}
public function show_error()
{
return $this->link->error;
}
} // End Database_Mysqli_Driver Class
/**
* MySQLi Result
*/
class Kohana_Mysqli_Result extends Database_Result {
// Database connection
protected $link;
// Data fetching types
protected $fetch_type = 'mysqli_fetch_object';
protected $return_type = MYSQLI_ASSOC;
/**
* Sets up the result variables.
*
* @param object database link
* @param boolean return objects or arrays
* @param string SQL query that was run
*/
public function __construct($link, $object = TRUE, $sql)
{
$this->link = $link;
if ( ! $this->link->multi_query($sql))
{
// SQL error
throw new Kohana_Database_Exception('database.error', $this->link->error.' - '.$sql);
}
else
{
$this->result = $this->link->store_result();
// If the query is an object, it was a SELECT, SHOW, DESCRIBE, EXPLAIN query
if (is_object($this->result))
{
$this->current_row = 0;
$this->total_rows = $this->result->num_rows;
$this->fetch_type = ($object === TRUE) ? 'fetch_object' : 'fetch_array';
}
elseif ($this->link->error)
{
// SQL error
throw new Kohana_Database_Exception('database.error', $this->link->error.' - '.$sql);
}
else
{
// Its an DELETE, INSERT, REPLACE, or UPDATE query
$this->insert_id = $this->link->insert_id;
$this->total_rows = $this->link->affected_rows;
}
}
// Set result type
$this->result($object);
// Store the SQL
$this->sql = $sql;
}
/**
* Magic __destruct function, frees the result.
*/
public function __destruct()
{
if (is_object($this->result))
{
$this->result->free_result();
// this is kinda useless, but needs to be done to avoid the "Commands out of sync; you
// can't run this command now" error. Basically, we get all results after the first one
// (the one we actually need) and free them.
if (is_resource($this->link) AND $this->link->more_results())
{
do
{
if ($result = $this->link->store_result())
{
$result->free_result();
}
} while ($this->link->next_result());
}
}
}
public function result($object = TRUE, $type = MYSQLI_ASSOC)
{
$this->fetch_type = ((bool) $object) ? 'fetch_object' : 'fetch_array';
// This check has to be outside the previous statement, because we do not
// know the state of fetch_type when $object = NULL
// NOTE - The class set by $type must be defined before fetching the result,
// autoloading is disabled to save a lot of stupid overhead.
if ($this->fetch_type == 'fetch_object')
{
$this->return_type = (is_string($type) AND Kohana::auto_load($type)) ? $type : 'stdClass';
}
else
{
$this->return_type = $type;
}
return $this;
}
public function as_array($object = NULL, $type = MYSQLI_ASSOC)
{
return $this->result_array($object, $type);
}
public function result_array($object = NULL, $type = MYSQLI_ASSOC)
{
$rows = array();
if (is_string($object))
{
$fetch = $object;
}
elseif (is_bool($object))
{
if ($object === TRUE)
{
$fetch = 'fetch_object';
// NOTE - The class set by $type must be defined before fetching the result,
// autoloading is disabled to save a lot of stupid overhead.
$type = (is_string($type) AND Kohana::auto_load($type)) ? $type : 'stdClass';
}
else
{
$fetch = 'fetch_array';
}
}
else
{
// Use the default config values
$fetch = $this->fetch_type;
if ($fetch == 'fetch_object')
{
$type = (is_string($type) AND Kohana::auto_load($type)) ? $type : 'stdClass';
}
}
if ($this->result->num_rows)
{
// Reset the pointer location to make sure things work properly
$this->result->data_seek(0);
while ($row = $this->result->$fetch($type))
{
$rows[] = $row;
}
}
return isset($rows) ? $rows : array();
}
public function list_fields()
{
$field_names = array();
while ($field = $this->result->fetch_field())
{
$field_names[] = $field->name;
}
return $field_names;
}
public function seek($offset)
{
if ($this->offsetExists($offset) AND $this->result->data_seek($offset))
{
// Set the current row to the offset
$this->current_row = $offset;
return TRUE;
}
return FALSE;
}
public function offsetGet($offset)
{
if ( ! $this->seek($offset))
return FALSE;
// Return the row
$fetch = $this->fetch_type;
return $this->result->$fetch($this->return_type);
}
} // End Mysqli_Result Class
/**
* MySQLi Prepared Statement (experimental)
*/
class Kohana_Mysqli_Statement {
protected $link = NULL;
protected $stmt;
protected $var_names = array();
protected $var_values = array();
public function __construct($sql, $link)
{
$this->link = $link;
$this->stmt = $this->link->prepare($sql);
return $this;
}
public function __destruct()
{
$this->stmt->close();
}
// Sets the bind parameters
public function bind_params($param_types, $params)
{
$this->var_names = array_keys($params);
$this->var_values = array_values($params);
call_user_func_array(array($this->stmt, 'bind_param'), array_merge($param_types, $var_names));
return $this;
}
public function bind_result($params)
{
call_user_func_array(array($this->stmt, 'bind_result'), $params);
}
// Runs the statement
public function execute()
{
foreach ($this->var_names as $key => $name)
{
$$name = $this->var_values[$key];
}
$this->stmt->execute();
return $this->stmt;
}
}
|