Mysql.php 9.1 KB

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