Pdo.php 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440
  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-2015 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_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-2015 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. $message = sprintf('%s, query was: %s', $e->getMessage(), $this->_stmt->queryString);
  221. throw new Zend_Db_Statement_Exception($message, (int) $e->getCode(), $e);
  222. }
  223. }
  224. /**
  225. * Fetches a row from the result set.
  226. *
  227. * @param int $style OPTIONAL Fetch mode for this fetch operation.
  228. * @param int $cursor OPTIONAL Absolute, relative, or other.
  229. * @param int $offset OPTIONAL Number for absolute or relative cursors.
  230. * @return mixed Array, object, or scalar depending on fetch mode.
  231. * @throws Zend_Db_Statement_Exception
  232. */
  233. public function fetch($style = null, $cursor = null, $offset = null)
  234. {
  235. if ($style === null) {
  236. $style = $this->_fetchMode;
  237. }
  238. try {
  239. return $this->_stmt->fetch($style, $cursor, $offset);
  240. } catch (PDOException $e) {
  241. #require_once 'Zend/Db/Statement/Exception.php';
  242. throw new Zend_Db_Statement_Exception($e->getMessage(), $e->getCode(), $e);
  243. }
  244. }
  245. /**
  246. * Required by IteratorAggregate interface
  247. *
  248. * @return IteratorIterator
  249. */
  250. public function getIterator()
  251. {
  252. return new IteratorIterator($this->_stmt);
  253. }
  254. /**
  255. * Returns an array containing all of the result set rows.
  256. *
  257. * @param int $style OPTIONAL Fetch mode.
  258. * @param int $col OPTIONAL Column number, if fetch mode is by column.
  259. * @return array Collection of rows, each in a format by the fetch mode.
  260. * @throws Zend_Db_Statement_Exception
  261. */
  262. public function fetchAll($style = null, $col = null)
  263. {
  264. if ($style === null) {
  265. $style = $this->_fetchMode;
  266. }
  267. try {
  268. if ($style == PDO::FETCH_COLUMN) {
  269. if ($col === null) {
  270. $col = 0;
  271. }
  272. return $this->_stmt->fetchAll($style, $col);
  273. } else {
  274. return $this->_stmt->fetchAll($style);
  275. }
  276. } catch (PDOException $e) {
  277. #require_once 'Zend/Db/Statement/Exception.php';
  278. throw new Zend_Db_Statement_Exception($e->getMessage(), $e->getCode(), $e);
  279. }
  280. }
  281. /**
  282. * Returns a single column from the next row of a result set.
  283. *
  284. * @param int $col OPTIONAL Position of the column to fetch.
  285. * @return string
  286. * @throws Zend_Db_Statement_Exception
  287. */
  288. public function fetchColumn($col = 0)
  289. {
  290. try {
  291. return $this->_stmt->fetchColumn($col);
  292. } catch (PDOException $e) {
  293. #require_once 'Zend/Db/Statement/Exception.php';
  294. throw new Zend_Db_Statement_Exception($e->getMessage(), $e->getCode(), $e);
  295. }
  296. }
  297. /**
  298. * Fetches the next row and returns it as an object.
  299. *
  300. * @param string $class OPTIONAL Name of the class to create.
  301. * @param array $config OPTIONAL Constructor arguments for the class.
  302. * @return mixed One object instance of the specified class.
  303. * @throws Zend_Db_Statement_Exception
  304. */
  305. public function fetchObject($class = 'stdClass', array $config = array())
  306. {
  307. try {
  308. return $this->_stmt->fetchObject($class, $config);
  309. } catch (PDOException $e) {
  310. #require_once 'Zend/Db/Statement/Exception.php';
  311. throw new Zend_Db_Statement_Exception($e->getMessage(), $e->getCode(), $e);
  312. }
  313. }
  314. /**
  315. * Retrieve a statement attribute.
  316. *
  317. * @param integer $key Attribute name.
  318. * @return mixed Attribute value.
  319. * @throws Zend_Db_Statement_Exception
  320. */
  321. public function getAttribute($key)
  322. {
  323. try {
  324. return $this->_stmt->getAttribute($key);
  325. } catch (PDOException $e) {
  326. #require_once 'Zend/Db/Statement/Exception.php';
  327. throw new Zend_Db_Statement_Exception($e->getMessage(), $e->getCode(), $e);
  328. }
  329. }
  330. /**
  331. * Returns metadata for a column in a result set.
  332. *
  333. * @param int $column
  334. * @return mixed
  335. * @throws Zend_Db_Statement_Exception
  336. */
  337. public function getColumnMeta($column)
  338. {
  339. try {
  340. return $this->_stmt->getColumnMeta($column);
  341. } catch (PDOException $e) {
  342. #require_once 'Zend/Db/Statement/Exception.php';
  343. throw new Zend_Db_Statement_Exception($e->getMessage(), $e->getCode(), $e);
  344. }
  345. }
  346. /**
  347. * Retrieves the next rowset (result set) for a SQL statement that has
  348. * multiple result sets. An example is a stored procedure that returns
  349. * the results of multiple queries.
  350. *
  351. * @return bool
  352. * @throws Zend_Db_Statement_Exception
  353. */
  354. public function nextRowset()
  355. {
  356. try {
  357. return $this->_stmt->nextRowset();
  358. } catch (PDOException $e) {
  359. #require_once 'Zend/Db/Statement/Exception.php';
  360. throw new Zend_Db_Statement_Exception($e->getMessage(), $e->getCode(), $e);
  361. }
  362. }
  363. /**
  364. * Returns the number of rows affected by the execution of the
  365. * last INSERT, DELETE, or UPDATE statement executed by this
  366. * statement object.
  367. *
  368. * @return int The number of rows affected.
  369. * @throws Zend_Db_Statement_Exception
  370. */
  371. public function rowCount()
  372. {
  373. try {
  374. return $this->_stmt->rowCount();
  375. } catch (PDOException $e) {
  376. #require_once 'Zend/Db/Statement/Exception.php';
  377. throw new Zend_Db_Statement_Exception($e->getMessage(), $e->getCode(), $e);
  378. }
  379. }
  380. /**
  381. * Set a statement attribute.
  382. *
  383. * @param string $key Attribute name.
  384. * @param mixed $val Attribute value.
  385. * @return bool
  386. * @throws Zend_Db_Statement_Exception
  387. */
  388. public function setAttribute($key, $val)
  389. {
  390. try {
  391. return $this->_stmt->setAttribute($key, $val);
  392. } catch (PDOException $e) {
  393. #require_once 'Zend/Db/Statement/Exception.php';
  394. throw new Zend_Db_Statement_Exception($e->getMessage(), $e->getCode(), $e);
  395. }
  396. }
  397. /**
  398. * Set the default fetch mode for this statement.
  399. *
  400. * @param int $mode The fetch mode.
  401. * @return bool
  402. * @throws Zend_Db_Statement_Exception
  403. */
  404. public function setFetchMode($mode)
  405. {
  406. $this->_fetchMode = $mode;
  407. try {
  408. return $this->_stmt->setFetchMode($mode);
  409. } catch (PDOException $e) {
  410. #require_once 'Zend/Db/Statement/Exception.php';
  411. throw new Zend_Db_Statement_Exception($e->getMessage(), $e->getCode(), $e);
  412. }
  413. }
  414. }