7
* This source file is subject to the new BSD license that is bundled
8
* with this package in the file LICENSE.txt.
9
* It is also available through the world-wide-web at this URL:
10
* http://framework.zend.com/license/new-bsd
11
* If you did not receive a copy of the license and are unable to
12
* obtain it through the world-wide-web, please send an email
13
* to license@zend.com so we can send you a copy immediately.
18
* @copyright Copyright (c) 2005-2008 Zend Technologies USA Inc. (http://www.zend.com)
19
* @license http://framework.zend.com/license/new-bsd New BSD License
20
* @version $Id: Mysqli.php 15599 2009-05-16 01:30:48Z yoshida@zend.co.jp $
25
* @see Zend_Db_Adapter_Abstract
27
require_once 'Zend/Db/Adapter/Abstract.php';
30
* @see Zend_Db_Profiler
32
require_once 'Zend/Db/Profiler.php';
37
require_once 'Zend/Db/Select.php';
40
* @see Zend_Db_Statement_Mysqli
42
require_once 'Zend/Db/Statement/Mysqli.php';
49
* @copyright Copyright (c) 2005-2008 Zend Technologies USA Inc. (http://www.zend.com)
50
* @license http://framework.zend.com/license/new-bsd New BSD License
52
class Zend_Db_Adapter_Mysqli extends Zend_Db_Adapter_Abstract
56
* Keys are UPPERCASE SQL datatypes or the constants
57
* Zend_Db::INT_TYPE, Zend_Db::BIGINT_TYPE, or Zend_Db::FLOAT_TYPE.
62
* 2 = float or decimal
64
* @var array Associative array of datatypes to values 0, 1, or 2.
66
protected $_numericDataTypes = array(
67
Zend_Db::INT_TYPE => Zend_Db::INT_TYPE,
68
Zend_Db::BIGINT_TYPE => Zend_Db::BIGINT_TYPE,
69
Zend_Db::FLOAT_TYPE => Zend_Db::FLOAT_TYPE,
70
'INT' => Zend_Db::INT_TYPE,
71
'INTEGER' => Zend_Db::INT_TYPE,
72
'MEDIUMINT' => Zend_Db::INT_TYPE,
73
'SMALLINT' => Zend_Db::INT_TYPE,
74
'TINYINT' => Zend_Db::INT_TYPE,
75
'BIGINT' => Zend_Db::BIGINT_TYPE,
76
'SERIAL' => Zend_Db::BIGINT_TYPE,
77
'DEC' => Zend_Db::FLOAT_TYPE,
78
'DECIMAL' => Zend_Db::FLOAT_TYPE,
79
'DOUBLE' => Zend_Db::FLOAT_TYPE,
80
'DOUBLE PRECISION' => Zend_Db::FLOAT_TYPE,
81
'FIXED' => Zend_Db::FLOAT_TYPE,
82
'FLOAT' => Zend_Db::FLOAT_TYPE
86
* @var Zend_Db_Statement_Mysqli
88
protected $_stmt = null;
91
* Default class name for a DB statement.
95
protected $_defaultStmtClass = 'Zend_Db_Statement_Mysqli';
100
* @param mixed $value Raw string
102
* @return string Quoted string
104
protected function _quote($value)
106
if (is_int($value) || is_float($value)) {
110
return "'" . $this->_connection->real_escape_string($value) . "'";
114
* Returns the symbol the adapter uses for delimiting identifiers.
118
public function getQuoteIdentifierSymbol()
124
* Returns a list of the tables in the database.
128
public function listTables()
131
// Use mysqli extension API, because SHOW doesn't work
132
// well as a prepared statement on MySQL 4.1.
133
$sql = 'SHOW TABLES';
134
if ($queryResult = $this->getConnection()->query($sql)) {
135
while ($row = $queryResult->fetch_row()) {
138
$queryResult->close();
141
* @see Zend_Db_Adapter_Mysqli_Exception
143
require_once 'Zend/Db/Adapter/Mysqli/Exception.php';
144
throw new Zend_Db_Adapter_Mysqli_Exception($this->getConnection()->error);
150
* Returns the column descriptions for a table.
152
* The return value is an associative array keyed by the column name,
153
* as returned by the RDBMS.
155
* The value of each array element is an associative array
156
* with the following keys:
158
* SCHEMA_NAME => string; name of database or schema
159
* TABLE_NAME => string;
160
* COLUMN_NAME => string; column name
161
* COLUMN_POSITION => number; ordinal position of column in table
162
* DATA_TYPE => string; SQL datatype name of column
163
* DEFAULT => string; default expression of column, null if none
164
* NULLABLE => boolean; true if column can have nulls
165
* LENGTH => number; length of CHAR/VARCHAR
166
* SCALE => number; scale of NUMERIC/DECIMAL
167
* PRECISION => number; precision of NUMERIC/DECIMAL
168
* UNSIGNED => boolean; unsigned property of an integer type
169
* PRIMARY => boolean; true if column is part of the primary key
170
* PRIMARY_POSITION => integer; position of column in primary key
171
* IDENTITY => integer; true if column is auto-generated with unique values
173
* @param string $tableName
174
* @param string $schemaName OPTIONAL
177
public function describeTable($tableName, $schemaName = null)
180
* @todo use INFORMATION_SCHEMA someday when
181
* MySQL's implementation isn't too slow.
185
$sql = 'DESCRIBE ' . $this->quoteIdentifier("$schemaName.$tableName", true);
187
$sql = 'DESCRIBE ' . $this->quoteIdentifier($tableName, true);
191
* Use mysqli extension API, because DESCRIBE doesn't work
192
* well as a prepared statement on MySQL 4.1.
194
if ($queryResult = $this->getConnection()->query($sql)) {
195
while ($row = $queryResult->fetch_assoc()) {
198
$queryResult->close();
201
* @see Zend_Db_Adapter_Mysqli_Exception
203
require_once 'Zend/Db/Adapter/Mysqli/Exception.php';
204
throw new Zend_Db_Adapter_Mysqli_Exception($this->getConnection()->error);
209
$row_defaults = array(
215
'PrimaryPosition' => null,
220
foreach ($result as $key => $row) {
221
$row = array_merge($row_defaults, $row);
222
if (preg_match('/unsigned/', $row['Type'])) {
223
$row['Unsigned'] = true;
225
if (preg_match('/^((?:var)?char)\((\d+)\)/', $row['Type'], $matches)) {
226
$row['Type'] = $matches[1];
227
$row['Length'] = $matches[2];
228
} else if (preg_match('/^decimal\((\d+),(\d+)\)/', $row['Type'], $matches)) {
229
$row['Type'] = 'decimal';
230
$row['Precision'] = $matches[1];
231
$row['Scale'] = $matches[2];
232
} else if (preg_match('/^float\((\d+),(\d+)\)/', $row['Type'], $matches)) {
233
$row['Type'] = 'float';
234
$row['Precision'] = $matches[1];
235
$row['Scale'] = $matches[2];
236
} else if (preg_match('/^((?:big|medium|small|tiny)?int)\((\d+)\)/', $row['Type'], $matches)) {
237
$row['Type'] = $matches[1];
239
* The optional argument of a MySQL int type is not precision
240
* or length; it is only a hint for display width.
243
if (strtoupper($row['Key']) == 'PRI') {
244
$row['Primary'] = true;
245
$row['PrimaryPosition'] = $p;
246
if ($row['Extra'] == 'auto_increment') {
247
$row['Identity'] = true;
249
$row['Identity'] = false;
253
$desc[$this->foldCase($row['Field'])] = array(
254
'SCHEMA_NAME' => null, // @todo
255
'TABLE_NAME' => $this->foldCase($tableName),
256
'COLUMN_NAME' => $this->foldCase($row['Field']),
257
'COLUMN_POSITION' => $i,
258
'DATA_TYPE' => $row['Type'],
259
'DEFAULT' => $row['Default'],
260
'NULLABLE' => (bool) ($row['Null'] == 'YES'),
261
'LENGTH' => $row['Length'],
262
'SCALE' => $row['Scale'],
263
'PRECISION' => $row['Precision'],
264
'UNSIGNED' => $row['Unsigned'],
265
'PRIMARY' => $row['Primary'],
266
'PRIMARY_POSITION' => $row['PrimaryPosition'],
267
'IDENTITY' => $row['Identity']
275
* Creates a connection to the database.
278
* @throws Zend_Db_Adapter_Mysqli_Exception
280
protected function _connect()
282
if ($this->_connection) {
286
if (!extension_loaded('mysqli')) {
288
* @see Zend_Db_Adapter_Mysqli_Exception
290
require_once 'Zend/Db/Adapter/Mysqli/Exception.php';
291
throw new Zend_Db_Adapter_Mysqli_Exception('The Mysqli extension is required for this adapter but the extension is not loaded');
294
if (isset($this->_config['port'])) {
295
$port = (integer) $this->_config['port'];
300
$this->_connection = mysqli_init();
302
if(!empty($this->_config['driver_options'])) {
303
foreach($this->_config['driver_options'] as $option=>$value) {
304
if(is_string($option)) {
305
// Suppress warnings here
306
// Ignore it if it's not a valid constant
307
$option = @constant(strtoupper($option));
311
mysqli_options($this->_connection, $option, $value);
315
// Suppress connection warnings here.
316
// Throw an exception instead.
317
$_isConnected = @mysqli_real_connect(
319
$this->_config['host'],
320
$this->_config['username'],
321
$this->_config['password'],
322
$this->_config['dbname'],
326
if ($_isConnected === false || mysqli_connect_errno()) {
328
$this->closeConnection();
330
* @see Zend_Db_Adapter_Mysqli_Exception
332
require_once 'Zend/Db/Adapter/Mysqli/Exception.php';
333
throw new Zend_Db_Adapter_Mysqli_Exception(mysqli_connect_error());
336
if (!empty($this->_config['charset'])) {
337
mysqli_set_charset($this->_connection, $this->_config['charset']);
342
* Test if a connection is active
346
public function isConnected()
348
return ((bool) ($this->_connection instanceof mysqli));
352
* Force the connection to close.
356
public function closeConnection()
358
if ($this->isConnected()) {
359
$this->_connection->close();
361
$this->_connection = null;
365
* Prepare a statement and return a PDOStatement-like object.
367
* @param string $sql SQL query
368
* @return Zend_Db_Statement_Mysqli
370
public function prepare($sql)
374
$this->_stmt->close();
376
$stmtClass = $this->_defaultStmtClass;
377
if (!class_exists($stmtClass)) {
378
require_once 'Zend/Loader.php';
379
Zend_Loader::loadClass($stmtClass);
381
$stmt = new $stmtClass($this, $sql);
382
if ($stmt === false) {
385
$stmt->setFetchMode($this->_fetchMode);
386
$this->_stmt = $stmt;
391
* Gets the last ID generated automatically by an IDENTITY/AUTOINCREMENT column.
393
* As a convention, on RDBMS brands that support sequences
394
* (e.g. Oracle, PostgreSQL, DB2), this method forms the name of a sequence
395
* from the arguments and returns the last id generated by that sequence.
396
* On RDBMS brands that support IDENTITY/AUTOINCREMENT columns, this method
397
* returns the last value generated for such a column, and the table name
398
* argument is disregarded.
400
* MySQL does not support sequences, so $tableName and $primaryKey are ignored.
402
* @param string $tableName OPTIONAL Name of table.
403
* @param string $primaryKey OPTIONAL Name of primary key column.
405
* @todo Return value should be int?
407
public function lastInsertId($tableName = null, $primaryKey = null)
409
$mysqli = $this->_connection;
410
return (string) $mysqli->insert_id;
414
* Begin a transaction.
418
protected function _beginTransaction()
421
$this->_connection->autocommit(false);
425
* Commit a transaction.
429
protected function _commit()
432
$this->_connection->commit();
433
$this->_connection->autocommit(true);
437
* Roll-back a transaction.
441
protected function _rollBack()
444
$this->_connection->rollback();
445
$this->_connection->autocommit(true);
449
* Set the fetch mode.
453
* @throws Zend_Db_Adapter_Mysqli_Exception
455
public function setFetchMode($mode)
458
case Zend_Db::FETCH_LAZY:
459
case Zend_Db::FETCH_ASSOC:
460
case Zend_Db::FETCH_NUM:
461
case Zend_Db::FETCH_BOTH:
462
case Zend_Db::FETCH_NAMED:
463
case Zend_Db::FETCH_OBJ:
464
$this->_fetchMode = $mode;
466
case Zend_Db::FETCH_BOUND: // bound to PHP variable
468
* @see Zend_Db_Adapter_Mysqli_Exception
470
require_once 'Zend/Db/Adapter/Mysqli/Exception.php';
471
throw new Zend_Db_Adapter_Mysqli_Exception('FETCH_BOUND is not supported yet');
475
* @see Zend_Db_Adapter_Mysqli_Exception
477
require_once 'Zend/Db/Adapter/Mysqli/Exception.php';
478
throw new Zend_Db_Adapter_Mysqli_Exception("Invalid fetch mode '$mode' specified");
483
* Adds an adapter-specific LIMIT clause to the SELECT statement.
487
* @param int $offset OPTIONAL
490
public function limit($sql, $count, $offset = 0)
492
$count = intval($count);
495
* @see Zend_Db_Adapter_Mysqli_Exception
497
require_once 'Zend/Db/Adapter/Mysqli/Exception.php';
498
throw new Zend_Db_Adapter_Mysqli_Exception("LIMIT argument count=$count is not valid");
501
$offset = intval($offset);
504
* @see Zend_Db_Adapter_Mysqli_Exception
506
require_once 'Zend/Db/Adapter/Mysqli/Exception.php';
507
throw new Zend_Db_Adapter_Mysqli_Exception("LIMIT argument offset=$offset is not valid");
510
$sql .= " LIMIT $count";
512
$sql .= " OFFSET $offset";
519
* Check if the adapter supports real SQL parameters.
521
* @param string $type 'positional' or 'named'
524
public function supportsParameters($type)
536
* Retrieve server version in PHP style
540
public function getServerVersion()
543
$version = $this->_connection->server_version;
544
$major = (int) ($version / 10000);
545
$minor = (int) ($version % 10000 / 100);
546
$revision = (int) ($version % 100);
547
return $major . '.' . $minor . '.' . $revision;