PageRenderTime 25ms CodeModel.GetById 20ms RepoModel.GetById 0ms app.codeStats 0ms

/libs/Zend/Db/Statement/Pdo.php

https://github.com/ntulip/piwik
PHP | 439 lines | 222 code | 26 blank | 191 comment | 18 complexity | c75a0eaa07570435f7356bcda276191d 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 Statement
  18. * @copyright Copyright (c) 2005-2009 Zend Technologies USA Inc. (http://www.zend.com)
  19. * @license http://framework.zend.com/license/new-bsd New BSD License
  20. * @version $Id: Pdo.php 18079 2009-09-11 17:46:45Z ralph $
  21. */
  22. /**
  23. * @see Zend_Db_Statement
  24. */
  25. require_once 'Zend/Db/Statement.php';
  26. /**
  27. * Proxy class to wrap a PDOStatement object.
  28. * Matches the interface of PDOStatement. All methods simply proxy to the
  29. * matching method in PDOStatement. PDOExceptions thrown by PDOStatement
  30. * are re-thrown as Zend_Db_Statement_Exception.
  31. *
  32. * @category Zend
  33. * @package Zend_Db
  34. * @subpackage Statement
  35. * @copyright Copyright (c) 2005-2009 Zend Technologies USA Inc. (http://www.zend.com)
  36. * @license http://framework.zend.com/license/new-bsd New BSD License
  37. */
  38. class Zend_Db_Statement_Pdo extends Zend_Db_Statement implements IteratorAggregate
  39. {
  40. /**
  41. * @var int
  42. */
  43. protected $_fetchMode = PDO::FETCH_ASSOC;
  44. /**
  45. * Prepare a string SQL statement and create a statement object.
  46. *
  47. * @param string $sql
  48. * @return void
  49. * @throws Zend_Db_Statement_Exception
  50. */
  51. protected function _prepare($sql)
  52. {
  53. try {
  54. $this->_stmt = $this->_adapter->getConnection()->prepare($sql);
  55. } catch (PDOException $e) {
  56. require_once 'Zend/Db/Statement/Exception.php';
  57. throw new Zend_Db_Statement_Exception($e->getMessage(), $e->getCode(), $e);
  58. }
  59. }
  60. /**
  61. * Bind a column of the statement result set to a PHP variable.
  62. *
  63. * @param string $column Name the column in the result set, either by
  64. * position or by name.
  65. * @param mixed $param Reference to the PHP variable containing the value.
  66. * @param mixed $type OPTIONAL
  67. * @return bool
  68. * @throws Zend_Db_Statement_Exception
  69. */
  70. public function bindColumn($column, &$param, $type = null)
  71. {
  72. try {
  73. if ($type === null) {
  74. return $this->_stmt->bindColumn($column, $param);
  75. } else {
  76. return $this->_stmt->bindColumn($column, $param, $type);
  77. }
  78. } catch (PDOException $e) {
  79. require_once 'Zend/Db/Statement/Exception.php';
  80. throw new Zend_Db_Statement_Exception($e->getMessage(), $e->getCode(), $e);
  81. }
  82. }
  83. /**
  84. * Binds a parameter to the specified variable name.
  85. *
  86. * @param mixed $parameter Name the parameter, either integer or string.
  87. * @param mixed $variable Reference to PHP variable containing the value.
  88. * @param mixed $type OPTIONAL Datatype of SQL parameter.
  89. * @param mixed $length OPTIONAL Length of SQL parameter.
  90. * @param mixed $options OPTIONAL Other options.
  91. * @return bool
  92. * @throws Zend_Db_Statement_Exception
  93. */
  94. protected function _bindParam($parameter, &$variable, $type = null, $length = null, $options = null)
  95. {
  96. try {
  97. if ($type === null) {
  98. if (is_bool($variable)) {
  99. $type = PDO::PARAM_BOOL;
  100. } elseif ($variable === null) {
  101. $type = PDO::PARAM_NULL;
  102. } elseif (is_integer($variable)) {
  103. $type = PDO::PARAM_INT;
  104. } else {
  105. $type = PDO::PARAM_STR;
  106. }
  107. }
  108. return $this->_stmt->bindParam($parameter, $variable, $type, $length, $options);
  109. } catch (PDOException $e) {
  110. require_once 'Zend/Db/Statement/Exception.php';
  111. throw new Zend_Db_Statement_Exception($e->getMessage(), $e->getCode(), $e);
  112. }
  113. }
  114. /**
  115. * Binds a value to a parameter.
  116. *
  117. * @param mixed $parameter Name the parameter, either integer or string.
  118. * @param mixed $value Scalar value to bind to the parameter.
  119. * @param mixed $type OPTIONAL Datatype of the parameter.
  120. * @return bool
  121. * @throws Zend_Db_Statement_Exception
  122. */
  123. public function bindValue($parameter, $value, $type = null)
  124. {
  125. if (is_string($parameter) && $parameter[0] != ':') {
  126. $parameter = ":$parameter";
  127. }
  128. $this->_bindParam[$parameter] = $value;
  129. try {
  130. if ($type === null) {
  131. return $this->_stmt->bindValue($parameter, $value);
  132. } else {
  133. return $this->_stmt->bindValue($parameter, $value, $type);
  134. }
  135. } catch (PDOException $e) {
  136. require_once 'Zend/Db/Statement/Exception.php';
  137. throw new Zend_Db_Statement_Exception($e->getMessage(), $e->getCode(), $e);
  138. }
  139. }
  140. /**
  141. * Closes the cursor, allowing the statement to be executed again.
  142. *
  143. * @return bool
  144. * @throws Zend_Db_Statement_Exception
  145. */
  146. public function closeCursor()
  147. {
  148. try {
  149. return $this->_stmt->closeCursor();
  150. } catch (PDOException $e) {
  151. require_once 'Zend/Db/Statement/Exception.php';
  152. throw new Zend_Db_Statement_Exception($e->getMessage(), $e->getCode(), $e);
  153. }
  154. }
  155. /**
  156. * Returns the number of columns in the result set.
  157. * Returns null if the statement has no result set metadata.
  158. *
  159. * @return int The number of columns.
  160. * @throws Zend_Db_Statement_Exception
  161. */
  162. public function columnCount()
  163. {
  164. try {
  165. return $this->_stmt->columnCount();
  166. } catch (PDOException $e) {
  167. require_once 'Zend/Db/Statement/Exception.php';
  168. throw new Zend_Db_Statement_Exception($e->getMessage(), $e->getCode(), $e);
  169. }
  170. }
  171. /**
  172. * Retrieves the error code, if any, associated with the last operation on
  173. * the statement handle.
  174. *
  175. * @return string error code.
  176. * @throws Zend_Db_Statement_Exception
  177. */
  178. public function errorCode()
  179. {
  180. try {
  181. return $this->_stmt->errorCode();
  182. } catch (PDOException $e) {
  183. require_once 'Zend/Db/Statement/Exception.php';
  184. throw new Zend_Db_Statement_Exception($e->getMessage(), $e->getCode(), $e);
  185. }
  186. }
  187. /**
  188. * Retrieves an array of error information, if any, associated with the
  189. * last operation on the statement handle.
  190. *
  191. * @return array
  192. * @throws Zend_Db_Statement_Exception
  193. */
  194. public function errorInfo()
  195. {
  196. try {
  197. return $this->_stmt->errorInfo();
  198. } catch (PDOException $e) {
  199. require_once 'Zend/Db/Statement/Exception.php';
  200. throw new Zend_Db_Statement_Exception($e->getMessage(), $e->getCode(), $e);
  201. }
  202. }
  203. /**
  204. * Executes a prepared statement.
  205. *
  206. * @param array $params OPTIONAL Values to bind to parameter placeholders.
  207. * @return bool
  208. * @throws Zend_Db_Statement_Exception
  209. */
  210. public function _execute(array $params = null)
  211. {
  212. try {
  213. if ($params !== null) {
  214. return $this->_stmt->execute($params);
  215. } else {
  216. return $this->_stmt->execute();
  217. }
  218. } catch (PDOException $e) {
  219. require_once 'Zend/Db/Statement/Exception.php';
  220. throw new Zend_Db_Statement_Exception($e->getMessage(), $e->getCode(), $e);
  221. }
  222. }
  223. /**
  224. * Fetches a row from the result set.
  225. *
  226. * @param int $style OPTIONAL Fetch mode for this fetch operation.
  227. * @param int $cursor OPTIONAL Absolute, relative, or other.
  228. * @param int $offset OPTIONAL Number for absolute or relative cursors.
  229. * @return mixed Array, object, or scalar depending on fetch mode.
  230. * @throws Zend_Db_Statement_Exception
  231. */
  232. public function fetch($style = null, $cursor = null, $offset = null)
  233. {
  234. if ($style === null) {
  235. $style = $this->_fetchMode;
  236. }
  237. try {
  238. return $this->_stmt->fetch($style, $cursor, $offset);
  239. } catch (PDOException $e) {
  240. require_once 'Zend/Db/Statement/Exception.php';
  241. throw new Zend_Db_Statement_Exception($e->getMessage(), $e->getCode(), $e);
  242. }
  243. }
  244. /**
  245. * Required by IteratorAggregate interface
  246. *
  247. * @return IteratorIterator
  248. */
  249. public function getIterator()
  250. {
  251. return new IteratorIterator($this->_stmt);
  252. }
  253. /**
  254. * Returns an array containing all of the result set rows.
  255. *
  256. * @param int $style OPTIONAL Fetch mode.
  257. * @param int $col OPTIONAL Column number, if fetch mode is by column.
  258. * @return array Collection of rows, each in a format by the fetch mode.
  259. * @throws Zend_Db_Statement_Exception
  260. */
  261. public function fetchAll($style = null, $col = null)
  262. {
  263. if ($style === null) {
  264. $style = $this->_fetchMode;
  265. }
  266. try {
  267. if ($style == PDO::FETCH_COLUMN) {
  268. if ($col === null) {
  269. $col = 0;
  270. }
  271. return $this->_stmt->fetchAll($style, $col);
  272. } else {
  273. return $this->_stmt->fetchAll($style);
  274. }
  275. } catch (PDOException $e) {
  276. require_once 'Zend/Db/Statement/Exception.php';
  277. throw new Zend_Db_Statement_Exception($e->getMessage(), $e->getCode(), $e);
  278. }
  279. }
  280. /**
  281. * Returns a single column from the next row of a result set.
  282. *
  283. * @param int $col OPTIONAL Position of the column to fetch.
  284. * @return string
  285. * @throws Zend_Db_Statement_Exception
  286. */
  287. public function fetchColumn($col = 0)
  288. {
  289. try {
  290. return $this->_stmt->fetchColumn($col);
  291. } catch (PDOException $e) {
  292. require_once 'Zend/Db/Statement/Exception.php';
  293. throw new Zend_Db_Statement_Exception($e->getMessage(), $e->getCode(), $e);
  294. }
  295. }
  296. /**
  297. * Fetches the next row and returns it as an object.
  298. *
  299. * @param string $class OPTIONAL Name of the class to create.
  300. * @param array $config OPTIONAL Constructor arguments for the class.
  301. * @return mixed One object instance of the specified class.
  302. * @throws Zend_Db_Statement_Exception
  303. */
  304. public function fetchObject($class = 'stdClass', array $config = array())
  305. {
  306. try {
  307. return $this->_stmt->fetchObject($class, $config);
  308. } catch (PDOException $e) {
  309. require_once 'Zend/Db/Statement/Exception.php';
  310. throw new Zend_Db_Statement_Exception($e->getMessage(), $e->getCode(), $e);
  311. }
  312. }
  313. /**
  314. * Retrieve a statement attribute.
  315. *
  316. * @param integer $key Attribute name.
  317. * @return mixed Attribute value.
  318. * @throws Zend_Db_Statement_Exception
  319. */
  320. public function getAttribute($key)
  321. {
  322. try {
  323. return $this->_stmt->getAttribute($key);
  324. } catch (PDOException $e) {
  325. require_once 'Zend/Db/Statement/Exception.php';
  326. throw new Zend_Db_Statement_Exception($e->getMessage(), $e->getCode(), $e);
  327. }
  328. }
  329. /**
  330. * Returns metadata for a column in a result set.
  331. *
  332. * @param int $column
  333. * @return mixed
  334. * @throws Zend_Db_Statement_Exception
  335. */
  336. public function getColumnMeta($column)
  337. {
  338. try {
  339. return $this->_stmt->getColumnMeta($column);
  340. } catch (PDOException $e) {
  341. require_once 'Zend/Db/Statement/Exception.php';
  342. throw new Zend_Db_Statement_Exception($e->getMessage(), $e->getCode(), $e);
  343. }
  344. }
  345. /**
  346. * Retrieves the next rowset (result set) for a SQL statement that has
  347. * multiple result sets. An example is a stored procedure that returns
  348. * the results of multiple queries.
  349. *
  350. * @return bool
  351. * @throws Zend_Db_Statement_Exception
  352. */
  353. public function nextRowset()
  354. {
  355. try {
  356. return $this->_stmt->nextRowset();
  357. } catch (PDOException $e) {
  358. require_once 'Zend/Db/Statement/Exception.php';
  359. throw new Zend_Db_Statement_Exception($e->getMessage(), $e->getCode(), $e);
  360. }
  361. }
  362. /**
  363. * Returns the number of rows affected by the execution of the
  364. * last INSERT, DELETE, or UPDATE statement executed by this
  365. * statement object.
  366. *
  367. * @return int The number of rows affected.
  368. * @throws Zend_Db_Statement_Exception
  369. */
  370. public function rowCount()
  371. {
  372. try {
  373. return $this->_stmt->rowCount();
  374. } catch (PDOException $e) {
  375. require_once 'Zend/Db/Statement/Exception.php';
  376. throw new Zend_Db_Statement_Exception($e->getMessage(), $e->getCode(), $e);
  377. }
  378. }
  379. /**
  380. * Set a statement attribute.
  381. *
  382. * @param string $key Attribute name.
  383. * @param mixed $val Attribute value.
  384. * @return bool
  385. * @throws Zend_Db_Statement_Exception
  386. */
  387. public function setAttribute($key, $val)
  388. {
  389. try {
  390. return $this->_stmt->setAttribute($key, $val);
  391. } catch (PDOException $e) {
  392. require_once 'Zend/Db/Statement/Exception.php';
  393. throw new Zend_Db_Statement_Exception($e->getMessage(), $e->getCode(), $e);
  394. }
  395. }
  396. /**
  397. * Set the default fetch mode for this statement.
  398. *
  399. * @param int $mode The fetch mode.
  400. * @return bool
  401. * @throws Zend_Db_Statement_Exception
  402. */
  403. public function setFetchMode($mode)
  404. {
  405. $this->_fetchMode = $mode;
  406. try {
  407. return $this->_stmt->setFetchMode($mode);
  408. } catch (PDOException $e) {
  409. require_once 'Zend/Db/Statement/Exception.php';
  410. throw new Zend_Db_Statement_Exception($e->getMessage(), $e->getCode(), $e);
  411. }
  412. }
  413. }