Ibm
6 years ago
Abstract.php
6 years ago
Ibm.php
6 years ago
Mssql.php
6 years ago
Mysql.php
6 years ago
Oci.php
6 years ago
Pgsql.php
6 years ago
Sqlite.php
5 years ago
Mysql.php
271 lines
| 1 | <?php |
| 2 | /** |
| 3 | * Zend Framework |
| 4 | * |
| 5 | * LICENSE |
| 6 | * |
| 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. |
| 14 | * |
| 15 | * @category Zend |
| 16 | * @package Zend_Db |
| 17 | * @subpackage Adapter |
| 18 | * @copyright Copyright (c) 2005-2011 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 23986 2011-05-03 20:10:42Z ralph $ |
| 21 | */ |
| 22 | |
| 23 | |
| 24 | /** |
| 25 | * @see Zend_Db_Adapter_Pdo_Abstract |
| 26 | */ |
| 27 | // require_once 'Zend/Db/Adapter/Pdo/Abstract.php'; |
| 28 | |
| 29 | |
| 30 | /** |
| 31 | * Class for connecting to MySQL databases and performing common operations. |
| 32 | * |
| 33 | * @category Zend |
| 34 | * @package Zend_Db |
| 35 | * @subpackage Adapter |
| 36 | * @copyright Copyright (c) 2005-2011 Zend Technologies USA Inc. (http://www.zend.com) |
| 37 | * @license http://framework.zend.com/license/new-bsd New BSD License |
| 38 | */ |
| 39 | class Zend_Db_Adapter_Pdo_Mysql extends Zend_Db_Adapter_Pdo_Abstract |
| 40 | { |
| 41 | |
| 42 | /** |
| 43 | * PDO type. |
| 44 | * |
| 45 | * @var string |
| 46 | */ |
| 47 | protected $_pdoType = 'mysql'; |
| 48 | |
| 49 | /** |
| 50 | * Keys are UPPERCASE SQL datatypes or the constants |
| 51 | * Zend_Db::INT_TYPE, Zend_Db::BIGINT_TYPE, or Zend_Db::FLOAT_TYPE. |
| 52 | * |
| 53 | * Values are: |
| 54 | * 0 = 32-bit integer |
| 55 | * 1 = 64-bit integer |
| 56 | * 2 = float or decimal |
| 57 | * |
| 58 | * @var array Associative array of datatypes to values 0, 1, or 2. |
| 59 | */ |
| 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 |
| 77 | ); |
| 78 | |
| 79 | /** |
| 80 | * Override _dsn() and ensure that charset is incorporated in mysql |
| 81 | * @see Zend_Db_Adapter_Pdo_Abstract::_dsn() |
| 82 | */ |
| 83 | protected function _dsn() |
| 84 | { |
| 85 | $dsn = parent::_dsn(); |
| 86 | if (isset($this->_config['charset'])) { |
| 87 | $dsn .= ';charset=' . $this->_config['charset']; |
| 88 | } |
| 89 | return $dsn; |
| 90 | } |
| 91 | |
| 92 | /** |
| 93 | * Creates a PDO object and connects to the database. |
| 94 | * |
| 95 | * @return void |
| 96 | * @throws Zend_Db_Adapter_Exception |
| 97 | */ |
| 98 | protected function _connect() |
| 99 | { |
| 100 | if ($this->_connection) { |
| 101 | return; |
| 102 | } |
| 103 | |
| 104 | if (!empty($this->_config['charset'])) { |
| 105 | $initCommand = "SET NAMES '" . $this->_config['charset'] . "'"; |
| 106 | $this->_config['driver_options'][1002] = $initCommand; // 1002 = PDO::MYSQL_ATTR_INIT_COMMAND |
| 107 | } |
| 108 | |
| 109 | parent::_connect(); |
| 110 | } |
| 111 | |
| 112 | /** |
| 113 | * @return string |
| 114 | */ |
| 115 | public function getQuoteIdentifierSymbol() |
| 116 | { |
| 117 | return "`"; |
| 118 | } |
| 119 | |
| 120 | /** |
| 121 | * Returns a list of the tables in the database. |
| 122 | * |
| 123 | * @return array |
| 124 | */ |
| 125 | public function listTables() |
| 126 | { |
| 127 | return $this->fetchCol('SHOW TABLES'); |
| 128 | } |
| 129 | |
| 130 | /** |
| 131 | * Returns the column descriptions for a table. |
| 132 | * |
| 133 | * The return value is an associative array keyed by the column name, |
| 134 | * as returned by the RDBMS. |
| 135 | * |
| 136 | * The value of each array element is an associative array |
| 137 | * with the following keys: |
| 138 | * |
| 139 | * SCHEMA_NAME => string; name of database or schema |
| 140 | * TABLE_NAME => string; |
| 141 | * COLUMN_NAME => string; column name |
| 142 | * COLUMN_POSITION => number; ordinal position of column in table |
| 143 | * DATA_TYPE => string; SQL datatype name of column |
| 144 | * DEFAULT => string; default expression of column, null if none |
| 145 | * NULLABLE => boolean; true if column can have nulls |
| 146 | * LENGTH => number; length of CHAR/VARCHAR |
| 147 | * SCALE => number; scale of NUMERIC/DECIMAL |
| 148 | * PRECISION => number; precision of NUMERIC/DECIMAL |
| 149 | * UNSIGNED => boolean; unsigned property of an integer type |
| 150 | * PRIMARY => boolean; true if column is part of the primary key |
| 151 | * PRIMARY_POSITION => integer; position of column in primary key |
| 152 | * IDENTITY => integer; true if column is auto-generated with unique values |
| 153 | * |
| 154 | * @param string $tableName |
| 155 | * @param string $schemaName OPTIONAL |
| 156 | * @return array |
| 157 | */ |
| 158 | public function describeTable($tableName, $schemaName = null) |
| 159 | { |
| 160 | // @todo use INFORMATION_SCHEMA someday when MySQL's |
| 161 | // implementation has reasonably good performance and |
| 162 | // the version with this improvement is in wide use. |
| 163 | |
| 164 | if ($schemaName) { |
| 165 | $sql = 'DESCRIBE ' . $this->quoteIdentifier("$schemaName.$tableName", true); |
| 166 | } else { |
| 167 | $sql = 'DESCRIBE ' . $this->quoteIdentifier($tableName, true); |
| 168 | } |
| 169 | $stmt = $this->query($sql); |
| 170 | |
| 171 | // Use FETCH_NUM so we are not dependent on the CASE attribute of the PDO connection |
| 172 | $result = $stmt->fetchAll(Zend_Db::FETCH_NUM); |
| 173 | |
| 174 | $field = 0; |
| 175 | $type = 1; |
| 176 | $null = 2; |
| 177 | $key = 3; |
| 178 | $default = 4; |
| 179 | $extra = 5; |
| 180 | |
| 181 | $desc = array(); |
| 182 | $i = 1; |
| 183 | $p = 1; |
| 184 | foreach ($result as $row) { |
| 185 | list($length, $scale, $precision, $unsigned, $primary, $primaryPosition, $identity) |
| 186 | = array(null, null, null, null, false, null, false); |
| 187 | if (preg_match('/unsigned/', $row[$type])) { |
| 188 | $unsigned = true; |
| 189 | } |
| 190 | if (preg_match('/^((?:var)?char)\((\d+)\)/', $row[$type], $matches)) { |
| 191 | $row[$type] = $matches[1]; |
| 192 | $length = $matches[2]; |
| 193 | } else if (preg_match('/^decimal\((\d+),(\d+)\)/', $row[$type], $matches)) { |
| 194 | $row[$type] = 'decimal'; |
| 195 | $precision = $matches[1]; |
| 196 | $scale = $matches[2]; |
| 197 | } else if (preg_match('/^float\((\d+),(\d+)\)/', $row[$type], $matches)) { |
| 198 | $row[$type] = 'float'; |
| 199 | $precision = $matches[1]; |
| 200 | $scale = $matches[2]; |
| 201 | } else if (preg_match('/^((?:big|medium|small|tiny)?int)\((\d+)\)/', $row[$type], $matches)) { |
| 202 | $row[$type] = $matches[1]; |
| 203 | // The optional argument of a MySQL int type is not precision |
| 204 | // or length; it is only a hint for display width. |
| 205 | } |
| 206 | if (strtoupper($row[$key]) == 'PRI') { |
| 207 | $primary = true; |
| 208 | $primaryPosition = $p; |
| 209 | if ($row[$extra] == 'auto_increment') { |
| 210 | $identity = true; |
| 211 | } else { |
| 212 | $identity = false; |
| 213 | } |
| 214 | ++$p; |
| 215 | } |
| 216 | $desc[$this->foldCase($row[$field])] = array( |
| 217 | 'SCHEMA_NAME' => null, // @todo |
| 218 | 'TABLE_NAME' => $this->foldCase($tableName), |
| 219 | 'COLUMN_NAME' => $this->foldCase($row[$field]), |
| 220 | 'COLUMN_POSITION' => $i, |
| 221 | 'DATA_TYPE' => $row[$type], |
| 222 | 'DEFAULT' => $row[$default], |
| 223 | 'NULLABLE' => (bool) ($row[$null] == 'YES'), |
| 224 | 'LENGTH' => $length, |
| 225 | 'SCALE' => $scale, |
| 226 | 'PRECISION' => $precision, |
| 227 | 'UNSIGNED' => $unsigned, |
| 228 | 'PRIMARY' => $primary, |
| 229 | 'PRIMARY_POSITION' => $primaryPosition, |
| 230 | 'IDENTITY' => $identity |
| 231 | ); |
| 232 | ++$i; |
| 233 | } |
| 234 | return $desc; |
| 235 | } |
| 236 | |
| 237 | /** |
| 238 | * Adds an adapter-specific LIMIT clause to the SELECT statement. |
| 239 | * |
| 240 | * @param string $sql |
| 241 | * @param integer $count |
| 242 | * @param integer $offset OPTIONAL |
| 243 | * @throws Zend_Db_Adapter_Exception |
| 244 | * @return string |
| 245 | */ |
| 246 | public function limit($sql, $count, $offset = 0) |
| 247 | { |
| 248 | $count = intval($count); |
| 249 | if ($count <= 0) { |
| 250 | /** @see Zend_Db_Adapter_Exception */ |
| 251 | // require_once 'Zend/Db/Adapter/Exception.php'; |
| 252 | throw new Zend_Db_Adapter_Exception("LIMIT argument count=$count is not valid"); |
| 253 | } |
| 254 | |
| 255 | $offset = intval($offset); |
| 256 | if ($offset < 0) { |
| 257 | /** @see Zend_Db_Adapter_Exception */ |
| 258 | // require_once 'Zend/Db/Adapter/Exception.php'; |
| 259 | throw new Zend_Db_Adapter_Exception("LIMIT argument offset=$offset is not valid"); |
| 260 | } |
| 261 | |
| 262 | $sql .= " LIMIT $count"; |
| 263 | if ($offset > 0) { |
| 264 | $sql .= " OFFSET $offset"; |
| 265 | } |
| 266 | |
| 267 | return $sql; |
| 268 | } |
| 269 | |
| 270 | } |
| 271 |