PageRenderTime 47ms CodeModel.GetById 18ms RepoModel.GetById 0ms app.codeStats 0ms

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

https://bitbucket.org/selfeky/xclusivescardwebsite
PHP | 425 lines | 232 code | 31 blank | 162 comment | 35 complexity | 16f425291b8045f61316a54e9d294041 MD5 | raw file
  1. <?php
  2. /**
  3. * LICENSE
  4. *
  5. * This source file is subject to the new BSD license that is bundled
  6. * with this package in the file LICENSE.
  7. *
  8. * If you did not receive a copy of the license and are unable to
  9. * obtain it through the world-wide-web, please send an email
  10. * to kontakt@beberlei.de so we can send you a copy immediately.
  11. *
  12. * @author Benjamin Eberlei (kontakt@beberlei.de)
  13. * @copyright Copyright (c) 2009 Benjamin Eberlei
  14. * @license New BSD License
  15. * @package Whitewashing
  16. * @subpackage Db
  17. */
  18. /**
  19. * @see Zend_Db_Adapter_Abstract
  20. */
  21. require_once "Zend/Db/Adapter/Abstract.php";
  22. /**
  23. * @see Whitewashing_Db_Statement_Mysql
  24. */
  25. require_once "Zend/Db/Statement/Mysql.php";
  26. class Zend_Db_Adapter_Mysql extends Zend_Db_Adapter_Abstract
  27. {
  28. /**
  29. * Keys are UPPERCASE SQL datatypes or the constants
  30. * Zend_Db::INT_TYPE, Zend_Db::BIGINT_TYPE, or Zend_Db::FLOAT_TYPE.
  31. *
  32. * Values are:
  33. * 0 = 32-bit integer
  34. * 1 = 64-bit integer
  35. * 2 = float or decimal
  36. *
  37. * @var array Associative array of datatypes to values 0, 1, or 2.
  38. */
  39. protected $_numericDataTypes = array(
  40. Zend_Db::INT_TYPE => Zend_Db::INT_TYPE,
  41. Zend_Db::BIGINT_TYPE => Zend_Db::BIGINT_TYPE,
  42. Zend_Db::FLOAT_TYPE => Zend_Db::FLOAT_TYPE,
  43. 'INT' => Zend_Db::INT_TYPE,
  44. 'INTEGER' => Zend_Db::INT_TYPE,
  45. 'MEDIUMINT' => Zend_Db::INT_TYPE,
  46. 'SMALLINT' => Zend_Db::INT_TYPE,
  47. 'TINYINT' => Zend_Db::INT_TYPE,
  48. 'BIGINT' => Zend_Db::BIGINT_TYPE,
  49. 'SERIAL' => Zend_Db::BIGINT_TYPE,
  50. 'DEC' => Zend_Db::FLOAT_TYPE,
  51. 'DECIMAL' => Zend_Db::FLOAT_TYPE,
  52. 'DOUBLE' => Zend_Db::FLOAT_TYPE,
  53. 'DOUBLE PRECISION' => Zend_Db::FLOAT_TYPE,
  54. 'FIXED' => Zend_Db::FLOAT_TYPE,
  55. 'FLOAT' => Zend_Db::FLOAT_TYPE
  56. );
  57. public function setConnectionResource($connection)
  58. {
  59. $this->_connection = $connection;
  60. }
  61. /**
  62. * Returns a list of the tables in the database.
  63. *
  64. * @return array
  65. */
  66. public function listTables()
  67. {
  68. $tables = array();
  69. $query = "SHOW TABLES FROM ".$this->quoteIdentifier($this->_config['dbname']);
  70. if($rs = mysql_query($query)) {
  71. while($row = mysql_fetch_row($rs)) {
  72. $tables[] = $row[0];
  73. }
  74. }
  75. return $tables;
  76. }
  77. /**
  78. * Returns the column descriptions for a table.
  79. *
  80. * The return value is an associative array keyed by the column name,
  81. * as returned by the RDBMS.
  82. *
  83. * The value of each array element is an associative array
  84. * with the following keys:
  85. *
  86. * SCHEMA_NAME => string; name of database or schema
  87. * TABLE_NAME => string;
  88. * COLUMN_NAME => string; column name
  89. * COLUMN_POSITION => number; ordinal position of column in table
  90. * DATA_TYPE => string; SQL datatype name of column
  91. * DEFAULT => string; default expression of column, null if none
  92. * NULLABLE => boolean; true if column can have nulls
  93. * LENGTH => number; length of CHAR/VARCHAR
  94. * SCALE => number; scale of NUMERIC/DECIMAL
  95. * PRECISION => number; precision of NUMERIC/DECIMAL
  96. * UNSIGNED => boolean; unsigned property of an integer type
  97. * PRIMARY => boolean; true if column is part of the primary key
  98. * PRIMARY_POSITION => integer; position of column in primary key
  99. *
  100. * @param string $tableName
  101. * @param string $schemaName OPTIONAL
  102. * @return array
  103. */
  104. public function describeTable($tableName, $schemaName = null)
  105. {
  106. /**
  107. * @todo use INFORMATION_SCHEMA someday when
  108. * MySQL's implementation isn't too slow.
  109. */
  110. if ($schemaName) {
  111. $sql = 'DESCRIBE ' . $this->quoteIdentifier("$schemaName.$tableName", true);
  112. } else {
  113. $sql = 'DESCRIBE ' . $this->quoteIdentifier($tableName, true);
  114. }
  115. /**
  116. * Use mysqli extension API, because DESCRIBE doesn't work
  117. * well as a prepared statement on MySQL 4.1.
  118. */
  119. if ($queryResult = mysql_query($sql)) {
  120. while ($row = mysql_fetch_assoc($queryResult)) {
  121. $result[] = $row;
  122. }
  123. mysql_free_result($queryResult);
  124. } else {
  125. /**
  126. * @see Zend_Db_Adapter_Mysqli_Exception
  127. */
  128. require_once 'Zend/Db/Adapter/Mysqli/Exception.php';
  129. throw new Zend_Db_Adapter_Mysqli_Exception($this->getConnection()->error);
  130. }
  131. $desc = array();
  132. $row_defaults = array(
  133. 'Length' => null,
  134. 'Scale' => null,
  135. 'Precision' => null,
  136. 'Unsigned' => null,
  137. 'Primary' => false,
  138. 'PrimaryPosition' => null,
  139. 'Identity' => false
  140. );
  141. $i = 1;
  142. $p = 1;
  143. foreach ($result as $key => $row) {
  144. $row = array_merge($row_defaults, $row);
  145. if (preg_match('/unsigned/', $row['Type'])) {
  146. $row['Unsigned'] = true;
  147. }
  148. if (preg_match('/^((?:var)?char)\((\d+)\)/', $row['Type'], $matches)) {
  149. $row['Type'] = $matches[1];
  150. $row['Length'] = $matches[2];
  151. } else if (preg_match('/^decimal\((\d+),(\d+)\)/', $row['Type'], $matches)) {
  152. $row['Type'] = 'decimal';
  153. $row['Precision'] = $matches[1];
  154. $row['Scale'] = $matches[2];
  155. } else if (preg_match('/^float\((\d+),(\d+)\)/', $row['Type'], $matches)) {
  156. $row['Type'] = 'float';
  157. $row['Precision'] = $matches[1];
  158. $row['Scale'] = $matches[2];
  159. } else if (preg_match('/^((?:big|medium|small|tiny)?int)\((\d+)\)/', $row['Type'], $matches)) {
  160. $row['Type'] = $matches[1];
  161. /**
  162. * The optional argument of a MySQL int type is not precision
  163. * or length; it is only a hint for display width.
  164. */
  165. }
  166. if (strtoupper($row['Key']) == 'PRI') {
  167. $row['Primary'] = true;
  168. $row['PrimaryPosition'] = $p;
  169. if ($row['Extra'] == 'auto_increment') {
  170. $row['Identity'] = true;
  171. } else {
  172. $row['Identity'] = false;
  173. }
  174. ++$p;
  175. }
  176. $desc[$this->foldCase($row['Field'])] = array(
  177. 'SCHEMA_NAME' => null, // @todo
  178. 'TABLE_NAME' => $this->foldCase($tableName),
  179. 'COLUMN_NAME' => $this->foldCase($row['Field']),
  180. 'COLUMN_POSITION' => $i,
  181. 'DATA_TYPE' => $row['Type'],
  182. 'DEFAULT' => $row['Default'],
  183. 'NULLABLE' => (bool) ($row['Null'] == 'YES'),
  184. 'LENGTH' => $row['Length'],
  185. 'SCALE' => $row['Scale'],
  186. 'PRECISION' => $row['Precision'],
  187. 'UNSIGNED' => $row['Unsigned'],
  188. 'PRIMARY' => $row['Primary'],
  189. 'PRIMARY_POSITION' => $row['PrimaryPosition'],
  190. 'IDENTITY' => $row['Identity']
  191. );
  192. ++$i;
  193. }
  194. return $desc;
  195. }
  196. /**
  197. * Creates a connection to the database.
  198. *
  199. * @return void
  200. */
  201. protected function _connect()
  202. {
  203. if(is_resource($this->_connection)) {
  204. return;
  205. }
  206. $this->_connection = @mysql_connect(
  207. $this->_config['host'],
  208. $this->_config['username'],
  209. $this->_config['password']
  210. );
  211. if($this->_connection == false) {
  212. $this->_throwException();
  213. }
  214. if(@mysql_select_db($this->_config['dbname'], $this->_connection) == false) {
  215. $this->_throwException();
  216. }
  217. if (!empty($this->_config['charset'])) {
  218. mysql_set_charset($this->_config['charset'], $this->_connection);
  219. }
  220. }
  221. private function _throwException($message=null)
  222. {
  223. require_once "Zend/Db/Adapter/Mysql/Exception.php";
  224. if($message === null) {
  225. $message = mysql_error($this->_connection)." (".mysql_errno($this->_connection).")";
  226. }
  227. throw new Zend_Db_Adapter_Mysql_Exception($message);
  228. }
  229. /**
  230. * Test if a connection is active
  231. *
  232. * @return boolean
  233. */
  234. public function isConnected()
  235. {
  236. return(is_resource($this->_connection));
  237. }
  238. /**
  239. * Force the connection to close.
  240. *
  241. * @return void
  242. */
  243. public function closeConnection()
  244. {
  245. if($this->isConnected()) {
  246. mysql_close($this->_connection);
  247. $this->_connection = null;
  248. }
  249. }
  250. /**
  251. * Prepare a statement and return a PDOStatement-like object.
  252. *
  253. * @param string|Zend_Db_Select $sql SQL query
  254. * @return Zend_Db_Statement|PDOStatement
  255. */
  256. public function prepare($sql)
  257. {
  258. return new Zend_Db_Statement_Mysql($this, $sql);
  259. }
  260. /**
  261. * Gets the last ID generated automatically by an IDENTITY/AUTOINCREMENT column.
  262. *
  263. * As a convention, on RDBMS brands that support sequences
  264. * (e.g. Oracle, PostgreSQL, DB2), this method forms the name of a sequence
  265. * from the arguments and returns the last id generated by that sequence.
  266. * On RDBMS brands that support IDENTITY/AUTOINCREMENT columns, this method
  267. * returns the last value generated for such a column, and the table name
  268. * argument is disregarded.
  269. *
  270. * @param string $tableName OPTIONAL Name of table.
  271. * @param string $primaryKey OPTIONAL Name of primary key column.
  272. * @return string
  273. */
  274. public function lastInsertId($tableName = null, $primaryKey = null)
  275. {
  276. return (string)mysql_insert_id($this->_connection);
  277. }
  278. /**
  279. * Begin a transaction.
  280. */
  281. protected function _beginTransaction()
  282. {
  283. $this->_connect();
  284. mysql_query("START TRANSACTION");
  285. }
  286. /**
  287. * Commit a transaction.
  288. */
  289. protected function _commit()
  290. {
  291. mysql_query("COMMIT");
  292. }
  293. /**
  294. * Roll-back a transaction.
  295. */
  296. protected function _rollBack()
  297. {
  298. mysql_query("ROLLBACK");
  299. }
  300. /**
  301. * Set the fetch mode.
  302. *
  303. * @param integer $mode
  304. * @return void
  305. * @throws Zend_Db_Adapter_Exception
  306. */
  307. public function setFetchMode($mode)
  308. {
  309. switch($mode) {
  310. case Zend_Db::FETCH_ASSOC:
  311. case Zend_Db::FETCH_NUM:
  312. case Zend_Db::FETCH_BOTH:
  313. case Zend_Db::FETCH_OBJ:
  314. case Zend_Db::FETCH_BOUND:
  315. $this->_fetchMode = $mode;
  316. break;
  317. case Zend_Db::FETCH_NAMED:
  318. case Zend_Db::FETCH_LAZY:
  319. default:
  320. $this->_throwException("Invalid fetch mode '".$mode."' specified");
  321. }
  322. }
  323. /**
  324. * Adds an adapter-specific LIMIT clause to the SELECT statement.
  325. *
  326. * @param mixed $sql
  327. * @param integer $count
  328. * @param integer $offset
  329. * @return string
  330. */
  331. public function limit($sql, $count, $offset = 0)
  332. {
  333. $count = intval($count);
  334. if ($count <= 0) {
  335. $this->_throwException("LIMIT argument count=$count is not valid");
  336. }
  337. $offset = intval($offset);
  338. if ($offset < 0) {
  339. $this->_throwException("LIMIT argument offset=$offset is not valid");
  340. }
  341. $sql .= " LIMIT $count";
  342. if ($offset > 0) {
  343. $sql .= " OFFSET $offset";
  344. }
  345. return $sql;
  346. }
  347. /**
  348. * Check if the adapter supports real SQL parameters.
  349. *
  350. * @param string $type 'positional' or 'named'
  351. * @return bool
  352. */
  353. public function supportsParameters($type)
  354. {
  355. if($type == 'positional') {
  356. return true;
  357. }
  358. return false;
  359. }
  360. /**
  361. * Retrieve server version in PHP style
  362. *
  363. * @return string
  364. */
  365. public function getServerVersion()
  366. {
  367. $this->_connect();
  368. return mysql_get_server_info($this->_connection);
  369. }
  370. /**
  371. * Returns the symbol the adapter uses for delimited identifiers.
  372. *
  373. * @return string
  374. */
  375. public function getQuoteIdentifierSymbol()
  376. {
  377. return '`';
  378. }
  379. /**
  380. * Quote a raw string.
  381. *
  382. * @param mixed $value Raw string
  383. *
  384. * @return string Quoted string
  385. */
  386. protected function _quote($value)
  387. {
  388. if (is_int($value) || is_float($value)) {
  389. return $value;
  390. }
  391. $this->_connect();
  392. return "'" . mysql_real_escape_string($value, $this->_connection) . "'";
  393. }
  394. }