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-2010 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 20096 2010-01-06 02:05:09Z bkarwin $
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-2010 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: * Creates a PDO object and connects to the database.
81: *
82: * @return void
83: * @throws Zend_Db_Adapter_Exception
84: */
85: protected function _connect()
86: {
87: if ($this->_connection) {
88: return;
89: }
90:
91: if (!empty($this->_config['charset'])) {
92: $initCommand = "SET NAMES '" . $this->_config['charset'] . "'";
93: $this->_config['driver_options'][1002] = $initCommand; // 1002 = PDO::MYSQL_ATTR_INIT_COMMAND
94: }
95:
96: parent::_connect();
97: }
98:
99: /**
100: * @return string
101: */
102: public function getQuoteIdentifierSymbol()
103: {
104: return "`";
105: }
106:
107: /**
108: * Returns a list of the tables in the database.
109: *
110: * @return array
111: */
112: public function listTables()
113: {
114: return $this->fetchCol('SHOW TABLES');
115: }
116:
117: /**
118: * Returns the column descriptions for a table.
119: *
120: * The return value is an associative array keyed by the column name,
121: * as returned by the RDBMS.
122: *
123: * The value of each array element is an associative array
124: * with the following keys:
125: *
126: * SCHEMA_NAME => string; name of database or schema
127: * TABLE_NAME => string;
128: * COLUMN_NAME => string; column name
129: * COLUMN_POSITION => number; ordinal position of column in table
130: * DATA_TYPE => string; SQL datatype name of column
131: * DEFAULT => string; default expression of column, null if none
132: * NULLABLE => boolean; true if column can have nulls
133: * LENGTH => number; length of CHAR/VARCHAR
134: * SCALE => number; scale of NUMERIC/DECIMAL
135: * PRECISION => number; precision of NUMERIC/DECIMAL
136: * UNSIGNED => boolean; unsigned property of an integer type
137: * PRIMARY => boolean; true if column is part of the primary key
138: * PRIMARY_POSITION => integer; position of column in primary key
139: * IDENTITY => integer; true if column is auto-generated with unique values
140: *
141: * @param string $tableName
142: * @param string $schemaName OPTIONAL
143: * @return array
144: */
145: public function describeTable($tableName, $schemaName = null)
146: {
147: // @todo use INFORMATION_SCHEMA someday when MySQL's
148: // implementation has reasonably good performance and
149: // the version with this improvement is in wide use.
150:
151: if ($schemaName) {
152: $sql = 'DESCRIBE ' . $this->quoteIdentifier("$schemaName.$tableName", true);
153: } else {
154: $sql = 'DESCRIBE ' . $this->quoteIdentifier($tableName, true);
155: }
156: $stmt = $this->query($sql);
157:
158: // Use FETCH_NUM so we are not dependent on the CASE attribute of the PDO connection
159: $result = $stmt->fetchAll(Zend_Db::FETCH_NUM);
160:
161: $field = 0;
162: $type = 1;
163: $null = 2;
164: $key = 3;
165: $default = 4;
166: $extra = 5;
167:
168: $desc = array();
169: $i = 1;
170: $p = 1;
171: foreach ($result as $row) {
172: list($length, $scale, $precision, $unsigned, $primary, $primaryPosition, $identity)
173: = array(null, null, null, null, false, null, false);
174: if (preg_match('/unsigned/', $row[$type])) {
175: $unsigned = true;
176: }
177: if (preg_match('/^((?:var)?char)\((\d+)\)/', $row[$type], $matches)) {
178: $row[$type] = $matches[1];
179: $length = $matches[2];
180: } else if (preg_match('/^decimal\((\d+),(\d+)\)/', $row[$type], $matches)) {
181: $row[$type] = 'decimal';
182: $precision = $matches[1];
183: $scale = $matches[2];
184: } else if (preg_match('/^float\((\d+),(\d+)\)/', $row[$type], $matches)) {
185: $row[$type] = 'float';
186: $precision = $matches[1];
187: $scale = $matches[2];
188: } else if (preg_match('/^((?:big|medium|small|tiny)?int)\((\d+)\)/', $row[$type], $matches)) {
189: $row[$type] = $matches[1];
190: // The optional argument of a MySQL int type is not precision
191: // or length; it is only a hint for display width.
192: }
193: if (strtoupper($row[$key]) == 'PRI') {
194: $primary = true;
195: $primaryPosition = $p;
196: if ($row[$extra] == 'auto_increment') {
197: $identity = true;
198: } else {
199: $identity = false;
200: }
201: ++$p;
202: }
203: $desc[$this->foldCase($row[$field])] = array(
204: 'SCHEMA_NAME' => null, // @todo
205: 'TABLE_NAME' => $this->foldCase($tableName),
206: 'COLUMN_NAME' => $this->foldCase($row[$field]),
207: 'COLUMN_POSITION' => $i,
208: 'DATA_TYPE' => $row[$type],
209: 'DEFAULT' => $row[$default],
210: 'NULLABLE' => (bool) ($row[$null] == 'YES'),
211: 'LENGTH' => $length,
212: 'SCALE' => $scale,
213: 'PRECISION' => $precision,
214: 'UNSIGNED' => $unsigned,
215: 'PRIMARY' => $primary,
216: 'PRIMARY_POSITION' => $primaryPosition,
217: 'IDENTITY' => $identity
218: );
219: ++$i;
220: }
221: return $desc;
222: }
223:
224: /**
225: * Adds an adapter-specific LIMIT clause to the SELECT statement.
226: *
227: * @param string $sql
228: * @param integer $count
229: * @param integer $offset OPTIONAL
230: * @throws Zend_Db_Adapter_Exception
231: * @return string
232: */
233: public function limit($sql, $count, $offset = 0)
234: {
235: $count = intval($count);
236: if ($count <= 0) {
237: /** @see Zend_Db_Adapter_Exception */
238: require_once 'Zend/Db/Adapter/Exception.php';
239: throw new Zend_Db_Adapter_Exception("LIMIT argument count=$count is not valid");
240: }
241:
242: $offset = intval($offset);
243: if ($offset < 0) {
244: /** @see Zend_Db_Adapter_Exception */
245: require_once 'Zend/Db/Adapter/Exception.php';
246: throw new Zend_Db_Adapter_Exception("LIMIT argument offset=$offset is not valid");
247: }
248:
249: $sql .= " LIMIT $count";
250: if ($offset > 0) {
251: $sql .= " OFFSET $offset";
252: }
253:
254: return $sql;
255: }
256:
257: }
258: