Mysql.php 8.7 KB

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