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