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: Mysql.php 12842 2008-11-25 22:07:48Z mikaelkael $
25
* @see Zend_Db_Adapter_Pdo_Abstract
27
require_once 'Zend/Db/Adapter/Pdo/Abstract.php';
31
* Class for connecting to MySQL databases and performing common operations.
36
* @copyright Copyright (c) 2005-2008 Zend Technologies USA Inc. (http://www.zend.com)
37
* @license http://framework.zend.com/license/new-bsd New BSD License
39
class Zend_Db_Adapter_Pdo_Mysql extends Zend_Db_Adapter_Pdo_Abstract
47
protected $_pdoType = 'mysql';
50
* Keys are UPPERCASE SQL datatypes or the constants
51
* Zend_Db::INT_TYPE, Zend_Db::BIGINT_TYPE, or Zend_Db::FLOAT_TYPE.
56
* 2 = float or decimal
58
* @var array Associative array of datatypes to values 0, 1, or 2.
60
protected $_numericDataTypes = array(
61
Zend_Db::INT_TYPE => Zend_Db::INT_TYPE,
62
Zend_Db::BIGINT_TYPE => Zend_Db::BIGINT_TYPE,
63
Zend_Db::FLOAT_TYPE => Zend_Db::FLOAT_TYPE,
64
'INT' => Zend_Db::INT_TYPE,
65
'INTEGER' => Zend_Db::INT_TYPE,
66
'MEDIUMINT' => Zend_Db::INT_TYPE,
67
'SMALLINT' => Zend_Db::INT_TYPE,
68
'TINYINT' => Zend_Db::INT_TYPE,
69
'BIGINT' => Zend_Db::BIGINT_TYPE,
70
'SERIAL' => Zend_Db::BIGINT_TYPE,
71
'DEC' => Zend_Db::FLOAT_TYPE,
72
'DECIMAL' => Zend_Db::FLOAT_TYPE,
73
'DOUBLE' => Zend_Db::FLOAT_TYPE,
74
'DOUBLE PRECISION' => Zend_Db::FLOAT_TYPE,
75
'FIXED' => Zend_Db::FLOAT_TYPE,
76
'FLOAT' => Zend_Db::FLOAT_TYPE
82
public function getQuoteIdentifierSymbol()
88
* Returns a list of the tables in the database.
92
public function listTables()
94
return $this->fetchCol('SHOW TABLES');
98
* Returns the column descriptions for a table.
100
* The return value is an associative array keyed by the column name,
101
* as returned by the RDBMS.
103
* The value of each array element is an associative array
104
* with the following keys:
106
* SCHEMA_NAME => string; name of database or schema
107
* TABLE_NAME => string;
108
* COLUMN_NAME => string; column name
109
* COLUMN_POSITION => number; ordinal position of column in table
110
* DATA_TYPE => string; SQL datatype name of column
111
* DEFAULT => string; default expression of column, null if none
112
* NULLABLE => boolean; true if column can have nulls
113
* LENGTH => number; length of CHAR/VARCHAR
114
* SCALE => number; scale of NUMERIC/DECIMAL
115
* PRECISION => number; precision of NUMERIC/DECIMAL
116
* UNSIGNED => boolean; unsigned property of an integer type
117
* PRIMARY => boolean; true if column is part of the primary key
118
* PRIMARY_POSITION => integer; position of column in primary key
119
* IDENTITY => integer; true if column is auto-generated with unique values
121
* @param string $tableName
122
* @param string $schemaName OPTIONAL
125
public function describeTable($tableName, $schemaName = null)
127
// @todo use INFORMATION_SCHEMA someday when MySQL's
128
// implementation has reasonably good performance and
129
// the version with this improvement is in wide use.
132
$sql = 'DESCRIBE ' . $this->quoteIdentifier("$schemaName.$tableName", true);
134
$sql = 'DESCRIBE ' . $this->quoteIdentifier($tableName, true);
136
$stmt = $this->query($sql);
138
// Use FETCH_NUM so we are not dependent on the CASE attribute of the PDO connection
139
$result = $stmt->fetchAll(Zend_Db::FETCH_NUM);
151
foreach ($result as $row) {
152
list($length, $scale, $precision, $unsigned, $primary, $primaryPosition, $identity)
153
= array(null, null, null, null, false, null, false);
154
if (preg_match('/unsigned/', $row[$type])) {
157
if (preg_match('/^((?:var)?char)\((\d+)\)/', $row[$type], $matches)) {
158
$row[$type] = $matches[1];
159
$length = $matches[2];
160
} else if (preg_match('/^decimal\((\d+),(\d+)\)/', $row[$type], $matches)) {
161
$row[$type] = 'decimal';
162
$precision = $matches[1];
163
$scale = $matches[2];
164
} else if (preg_match('/^float\((\d+),(\d+)\)/', $row[$type], $matches)) {
165
$row[$type] = 'float';
166
$precision = $matches[1];
167
$scale = $matches[2];
168
} else if (preg_match('/^((?:big|medium|small|tiny)?int)\((\d+)\)/', $row[$type], $matches)) {
169
$row[$type] = $matches[1];
170
// The optional argument of a MySQL int type is not precision
171
// or length; it is only a hint for display width.
173
if (strtoupper($row[$key]) == 'PRI') {
175
$primaryPosition = $p;
176
if ($row[$extra] == 'auto_increment') {
183
$desc[$this->foldCase($row[$field])] = array(
184
'SCHEMA_NAME' => null, // @todo
185
'TABLE_NAME' => $this->foldCase($tableName),
186
'COLUMN_NAME' => $this->foldCase($row[$field]),
187
'COLUMN_POSITION' => $i,
188
'DATA_TYPE' => $row[$type],
189
'DEFAULT' => $row[$default],
190
'NULLABLE' => (bool) ($row[$null] == 'YES'),
193
'PRECISION' => $precision,
194
'UNSIGNED' => $unsigned,
195
'PRIMARY' => $primary,
196
'PRIMARY_POSITION' => $primaryPosition,
197
'IDENTITY' => $identity
205
* Adds an adapter-specific LIMIT clause to the SELECT statement.
208
* @param integer $count
209
* @param integer $offset OPTIONAL
210
* @throws Zend_Db_Adapter_Exception
213
public function limit($sql, $count, $offset = 0)
215
$count = intval($count);
217
/** @see Zend_Db_Adapter_Exception */
218
require_once 'Zend/Db/Adapter/Exception.php';
219
throw new Zend_Db_Adapter_Exception("LIMIT argument count=$count is not valid");
222
$offset = intval($offset);
224
/** @see Zend_Db_Adapter_Exception */
225
require_once 'Zend/Db/Adapter/Exception.php';
226
throw new Zend_Db_Adapter_Exception("LIMIT argument offset=$offset is not valid");
229
$sql .= " LIMIT $count";
231
$sql .= " OFFSET $offset";