PageRenderTime 34ms CodeModel.GetById 11ms RepoModel.GetById 0ms app.codeStats 0ms

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

https://bitbucket.org/selfeky/xclusivescardwebsite
PHP | 401 lines | 185 code | 42 blank | 174 comment | 21 complexity | 2e4b7731699fdc3c131f53e3ed1d7e07 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: Abstract.php 24593 2012-01-05 20:35:02Z matthew $
  21. */
  22. /**
  23. * @see Zend_Db_Adapter_Abstract
  24. */
  25. require_once 'Zend/Db/Adapter/Abstract.php';
  26. /**
  27. * @see Zend_Db_Statement_Pdo
  28. */
  29. require_once 'Zend/Db/Statement/Pdo.php';
  30. /**
  31. * Class for connecting to SQL databases and performing common operations using PDO.
  32. *
  33. * @category Zend
  34. * @package Zend_Db
  35. * @subpackage Adapter
  36. * @copyright Copyright (c) 2005-2012 Zend Technologies USA Inc. (http://www.zend.com)
  37. * @license http://framework.zend.com/license/new-bsd New BSD License
  38. */
  39. abstract class Zend_Db_Adapter_Pdo_Abstract extends Zend_Db_Adapter_Abstract
  40. {
  41. /**
  42. * Default class name for a DB statement.
  43. *
  44. * @var string
  45. */
  46. protected $_defaultStmtClass = 'Zend_Db_Statement_Pdo';
  47. /**
  48. * Creates a PDO DSN for the adapter from $this->_config settings.
  49. *
  50. * @return string
  51. */
  52. protected function _dsn()
  53. {
  54. // baseline of DSN parts
  55. $dsn = $this->_config;
  56. // don't pass the username, password, charset, persistent and driver_options in the DSN
  57. unset($dsn['username']);
  58. unset($dsn['password']);
  59. unset($dsn['options']);
  60. unset($dsn['charset']);
  61. unset($dsn['persistent']);
  62. unset($dsn['driver_options']);
  63. // use all remaining parts in the DSN
  64. foreach ($dsn as $key => $val) {
  65. $dsn[$key] = "$key=$val";
  66. }
  67. return $this->_pdoType . ':' . implode(';', $dsn);
  68. }
  69. /**
  70. * Creates a PDO object and connects to the database.
  71. *
  72. * @return void
  73. * @throws Zend_Db_Adapter_Exception
  74. */
  75. protected function _connect()
  76. {
  77. // if we already have a PDO object, no need to re-connect.
  78. if ($this->_connection) {
  79. return;
  80. }
  81. // get the dsn first, because some adapters alter the $_pdoType
  82. $dsn = $this->_dsn();
  83. // check for PDO extension
  84. if (!extension_loaded('pdo')) {
  85. /**
  86. * @see Zend_Db_Adapter_Exception
  87. */
  88. require_once 'Zend/Db/Adapter/Exception.php';
  89. throw new Zend_Db_Adapter_Exception('The PDO extension is required for this adapter but the extension is not loaded');
  90. }
  91. // check the PDO driver is available
  92. if (!in_array($this->_pdoType, PDO::getAvailableDrivers())) {
  93. /**
  94. * @see Zend_Db_Adapter_Exception
  95. */
  96. require_once 'Zend/Db/Adapter/Exception.php';
  97. throw new Zend_Db_Adapter_Exception('The ' . $this->_pdoType . ' driver is not currently installed');
  98. }
  99. // create PDO connection
  100. $q = $this->_profiler->queryStart('connect', Zend_Db_Profiler::CONNECT);
  101. // add the persistence flag if we find it in our config array
  102. if (isset($this->_config['persistent']) && ($this->_config['persistent'] == true)) {
  103. $this->_config['driver_options'][PDO::ATTR_PERSISTENT] = true;
  104. }
  105. try {
  106. $this->_connection = new PDO(
  107. $dsn,
  108. $this->_config['username'],
  109. $this->_config['password'],
  110. $this->_config['driver_options']
  111. );
  112. $this->_profiler->queryEnd($q);
  113. // set the PDO connection to perform case-folding on array keys, or not
  114. $this->_connection->setAttribute(PDO::ATTR_CASE, $this->_caseFolding);
  115. // always use exceptions.
  116. $this->_connection->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
  117. } catch (PDOException $e) {
  118. /**
  119. * @see Zend_Db_Adapter_Exception
  120. */
  121. require_once 'Zend/Db/Adapter/Exception.php';
  122. throw new Zend_Db_Adapter_Exception($e->getMessage(), $e->getCode(), $e);
  123. }
  124. }
  125. /**
  126. * Test if a connection is active
  127. *
  128. * @return boolean
  129. */
  130. public function isConnected()
  131. {
  132. return ((bool) ($this->_connection instanceof PDO));
  133. }
  134. /**
  135. * Force the connection to close.
  136. *
  137. * @return void
  138. */
  139. public function closeConnection()
  140. {
  141. $this->_connection = null;
  142. }
  143. /**
  144. * Prepares an SQL statement.
  145. *
  146. * @param string $sql The SQL statement with placeholders.
  147. * @param array $bind An array of data to bind to the placeholders.
  148. * @return PDOStatement
  149. */
  150. public function prepare($sql)
  151. {
  152. $this->_connect();
  153. $stmtClass = $this->_defaultStmtClass;
  154. if (!class_exists($stmtClass)) {
  155. require_once 'Zend/Loader.php';
  156. Zend_Loader::loadClass($stmtClass);
  157. }
  158. $stmt = new $stmtClass($this, $sql);
  159. $stmt->setFetchMode($this->_fetchMode);
  160. return $stmt;
  161. }
  162. /**
  163. * Gets the last ID generated automatically by an IDENTITY/AUTOINCREMENT column.
  164. *
  165. * As a convention, on RDBMS brands that support sequences
  166. * (e.g. Oracle, PostgreSQL, DB2), this method forms the name of a sequence
  167. * from the arguments and returns the last id generated by that sequence.
  168. * On RDBMS brands that support IDENTITY/AUTOINCREMENT columns, this method
  169. * returns the last value generated for such a column, and the table name
  170. * argument is disregarded.
  171. *
  172. * On RDBMS brands that don't support sequences, $tableName and $primaryKey
  173. * are ignored.
  174. *
  175. * @param string $tableName OPTIONAL Name of table.
  176. * @param string $primaryKey OPTIONAL Name of primary key column.
  177. * @return string
  178. */
  179. public function lastInsertId($tableName = null, $primaryKey = null)
  180. {
  181. $this->_connect();
  182. return $this->_connection->lastInsertId();
  183. }
  184. /**
  185. * Special handling for PDO query().
  186. * All bind parameter names must begin with ':'
  187. *
  188. * @param string|Zend_Db_Select $sql The SQL statement with placeholders.
  189. * @param array $bind An array of data to bind to the placeholders.
  190. * @return Zend_Db_Statement_Pdo
  191. * @throws Zend_Db_Adapter_Exception To re-throw PDOException.
  192. */
  193. public function query($sql, $bind = array())
  194. {
  195. if (empty($bind) && $sql instanceof Zend_Db_Select) {
  196. $bind = $sql->getBind();
  197. }
  198. if (is_array($bind)) {
  199. foreach ($bind as $name => $value) {
  200. if (!is_int($name) && !preg_match('/^:/', $name)) {
  201. $newName = ":$name";
  202. unset($bind[$name]);
  203. $bind[$newName] = $value;
  204. }
  205. }
  206. }
  207. try {
  208. return parent::query($sql, $bind);
  209. } catch (PDOException $e) {
  210. /**
  211. * @see Zend_Db_Statement_Exception
  212. */
  213. require_once 'Zend/Db/Statement/Exception.php';
  214. throw new Zend_Db_Statement_Exception($e->getMessage(), $e->getCode(), $e);
  215. }
  216. }
  217. /**
  218. * Executes an SQL statement and return the number of affected rows
  219. *
  220. * @param mixed $sql The SQL statement with placeholders.
  221. * May be a string or Zend_Db_Select.
  222. * @return integer Number of rows that were modified
  223. * or deleted by the SQL statement
  224. */
  225. public function exec($sql)
  226. {
  227. if ($sql instanceof Zend_Db_Select) {
  228. $sql = $sql->assemble();
  229. }
  230. try {
  231. $affected = $this->getConnection()->exec($sql);
  232. if ($affected === false) {
  233. $errorInfo = $this->getConnection()->errorInfo();
  234. /**
  235. * @see Zend_Db_Adapter_Exception
  236. */
  237. require_once 'Zend/Db/Adapter/Exception.php';
  238. throw new Zend_Db_Adapter_Exception($errorInfo[2]);
  239. }
  240. return $affected;
  241. } catch (PDOException $e) {
  242. /**
  243. * @see Zend_Db_Adapter_Exception
  244. */
  245. require_once 'Zend/Db/Adapter/Exception.php';
  246. throw new Zend_Db_Adapter_Exception($e->getMessage(), $e->getCode(), $e);
  247. }
  248. }
  249. /**
  250. * Quote a raw string.
  251. *
  252. * @param string $value Raw string
  253. * @return string Quoted string
  254. */
  255. protected function _quote($value)
  256. {
  257. if (is_int($value) || is_float($value)) {
  258. return $value;
  259. }
  260. $this->_connect();
  261. return $this->_connection->quote($value);
  262. }
  263. /**
  264. * Begin a transaction.
  265. */
  266. protected function _beginTransaction()
  267. {
  268. $this->_connect();
  269. $this->_connection->beginTransaction();
  270. }
  271. /**
  272. * Commit a transaction.
  273. */
  274. protected function _commit()
  275. {
  276. $this->_connect();
  277. $this->_connection->commit();
  278. }
  279. /**
  280. * Roll-back a transaction.
  281. */
  282. protected function _rollBack() {
  283. $this->_connect();
  284. $this->_connection->rollBack();
  285. }
  286. /**
  287. * Set the PDO fetch mode.
  288. *
  289. * @todo Support FETCH_CLASS and FETCH_INTO.
  290. *
  291. * @param int $mode A PDO fetch mode.
  292. * @return void
  293. * @throws Zend_Db_Adapter_Exception
  294. */
  295. public function setFetchMode($mode)
  296. {
  297. //check for PDO extension
  298. if (!extension_loaded('pdo')) {
  299. /**
  300. * @see Zend_Db_Adapter_Exception
  301. */
  302. require_once 'Zend/Db/Adapter/Exception.php';
  303. throw new Zend_Db_Adapter_Exception('The PDO extension is required for this adapter but the extension is not loaded');
  304. }
  305. switch ($mode) {
  306. case PDO::FETCH_LAZY:
  307. case PDO::FETCH_ASSOC:
  308. case PDO::FETCH_NUM:
  309. case PDO::FETCH_BOTH:
  310. case PDO::FETCH_NAMED:
  311. case PDO::FETCH_OBJ:
  312. $this->_fetchMode = $mode;
  313. break;
  314. default:
  315. /**
  316. * @see Zend_Db_Adapter_Exception
  317. */
  318. require_once 'Zend/Db/Adapter/Exception.php';
  319. throw new Zend_Db_Adapter_Exception("Invalid fetch mode '$mode' specified");
  320. break;
  321. }
  322. }
  323. /**
  324. * Check if the adapter supports real SQL parameters.
  325. *
  326. * @param string $type 'positional' or 'named'
  327. * @return bool
  328. */
  329. public function supportsParameters($type)
  330. {
  331. switch ($type) {
  332. case 'positional':
  333. case 'named':
  334. default:
  335. return true;
  336. }
  337. }
  338. /**
  339. * Retrieve server version in PHP style
  340. *
  341. * @return string
  342. */
  343. public function getServerVersion()
  344. {
  345. $this->_connect();
  346. try {
  347. $version = $this->_connection->getAttribute(PDO::ATTR_SERVER_VERSION);
  348. } catch (PDOException $e) {
  349. // In case of the driver doesn't support getting attributes
  350. return null;
  351. }
  352. $matches = null;
  353. if (preg_match('/((?:[0-9]{1,2}\.){1,3}[0-9]{1,2})/', $version, $matches)) {
  354. return $matches[1];
  355. } else {
  356. return null;
  357. }
  358. }
  359. }