PageRenderTime 44ms CodeModel.GetById 18ms RepoModel.GetById 1ms app.codeStats 0ms

/concrete/libraries/3rdparty/Zend/Db/Adapter/Pdo/Mysql.php

https://bitbucket.org/selfeky/xclusivescardwebsite
PHP | 270 lines | 137 code | 22 blank | 111 comment | 22 complexity | 4c699a91a8447b30981f5bfc74d67065 MD5 | raw file
  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: Mysql.php 24593 2012-01-05 20:35:02Z matthew $
  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. $initCommand = "SET NAMES '" . $this->_config['charset'] . "'";
  97. $this->_config['driver_options'][1002] = $initCommand; // 1002 = PDO::MYSQL_ATTR_INIT_COMMAND
  98. }
  99. parent::_connect();
  100. }
  101. /**
  102. * @return string
  103. */
  104. public function getQuoteIdentifierSymbol()
  105. {
  106. return "`";
  107. }
  108. /**
  109. * Returns a list of the tables in the database.
  110. *
  111. * @return array
  112. */
  113. public function listTables()
  114. {
  115. return $this->fetchCol('SHOW TABLES');
  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. if ($schemaName) {
  151. $sql = 'DESCRIBE ' . $this->quoteIdentifier("$schemaName.$tableName", true);
  152. } else {
  153. $sql = 'DESCRIBE ' . $this->quoteIdentifier($tableName, true);
  154. }
  155. $stmt = $this->query($sql);
  156. // Use FETCH_NUM so we are not dependent on the CASE attribute of the PDO connection
  157. $result = $stmt->fetchAll(Zend_Db::FETCH_NUM);
  158. $field = 0;
  159. $type = 1;
  160. $null = 2;
  161. $key = 3;
  162. $default = 4;
  163. $extra = 5;
  164. $desc = array();
  165. $i = 1;
  166. $p = 1;
  167. foreach ($result as $row) {
  168. list($length, $scale, $precision, $unsigned, $primary, $primaryPosition, $identity)
  169. = array(null, null, null, null, false, null, false);
  170. if (preg_match('/unsigned/', $row[$type])) {
  171. $unsigned = true;
  172. }
  173. if (preg_match('/^((?:var)?char)\((\d+)\)/', $row[$type], $matches)) {
  174. $row[$type] = $matches[1];
  175. $length = $matches[2];
  176. } else if (preg_match('/^decimal\((\d+),(\d+)\)/', $row[$type], $matches)) {
  177. $row[$type] = 'decimal';
  178. $precision = $matches[1];
  179. $scale = $matches[2];
  180. } else if (preg_match('/^float\((\d+),(\d+)\)/', $row[$type], $matches)) {
  181. $row[$type] = 'float';
  182. $precision = $matches[1];
  183. $scale = $matches[2];
  184. } else if (preg_match('/^((?:big|medium|small|tiny)?int)\((\d+)\)/', $row[$type], $matches)) {
  185. $row[$type] = $matches[1];
  186. // The optional argument of a MySQL int type is not precision
  187. // or length; it is only a hint for display width.
  188. }
  189. if (strtoupper($row[$key]) == 'PRI') {
  190. $primary = true;
  191. $primaryPosition = $p;
  192. if ($row[$extra] == 'auto_increment') {
  193. $identity = true;
  194. } else {
  195. $identity = false;
  196. }
  197. ++$p;
  198. }
  199. $desc[$this->foldCase($row[$field])] = array(
  200. 'SCHEMA_NAME' => null, // @todo
  201. 'TABLE_NAME' => $this->foldCase($tableName),
  202. 'COLUMN_NAME' => $this->foldCase($row[$field]),
  203. 'COLUMN_POSITION' => $i,
  204. 'DATA_TYPE' => $row[$type],
  205. 'DEFAULT' => $row[$default],
  206. 'NULLABLE' => (bool) ($row[$null] == 'YES'),
  207. 'LENGTH' => $length,
  208. 'SCALE' => $scale,
  209. 'PRECISION' => $precision,
  210. 'UNSIGNED' => $unsigned,
  211. 'PRIMARY' => $primary,
  212. 'PRIMARY_POSITION' => $primaryPosition,
  213. 'IDENTITY' => $identity
  214. );
  215. ++$i;
  216. }
  217. return $desc;
  218. }
  219. /**
  220. * Adds an adapter-specific LIMIT clause to the SELECT statement.
  221. *
  222. * @param string $sql
  223. * @param integer $count
  224. * @param integer $offset OPTIONAL
  225. * @throws Zend_Db_Adapter_Exception
  226. * @return string
  227. */
  228. public function limit($sql, $count, $offset = 0)
  229. {
  230. $count = intval($count);
  231. if ($count <= 0) {
  232. /** @see Zend_Db_Adapter_Exception */
  233. require_once 'Zend/Db/Adapter/Exception.php';
  234. throw new Zend_Db_Adapter_Exception("LIMIT argument count=$count is not valid");
  235. }
  236. $offset = intval($offset);
  237. if ($offset < 0) {
  238. /** @see Zend_Db_Adapter_Exception */
  239. require_once 'Zend/Db/Adapter/Exception.php';
  240. throw new Zend_Db_Adapter_Exception("LIMIT argument offset=$offset is not valid");
  241. }
  242. $sql .= " LIMIT $count";
  243. if ($offset > 0) {
  244. $sql .= " OFFSET $offset";
  245. }
  246. return $sql;
  247. }
  248. }