Pdo.php 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436
  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$
  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. try {
  129. if ($type === null) {
  130. return $this->_stmt->bindValue($parameter, $value);
  131. } else {
  132. return $this->_stmt->bindValue($parameter, $value, $type);
  133. }
  134. } catch (PDOException $e) {
  135. require_once 'Zend/Db/Statement/Exception.php';
  136. throw new Zend_Db_Statement_Exception($e->getMessage(), $e->getCode(), $e);
  137. }
  138. }
  139. /**
  140. * Closes the cursor, allowing the statement to be executed again.
  141. *
  142. * @return bool
  143. * @throws Zend_Db_Statement_Exception
  144. */
  145. public function closeCursor()
  146. {
  147. try {
  148. return $this->_stmt->closeCursor();
  149. } catch (PDOException $e) {
  150. require_once 'Zend/Db/Statement/Exception.php';
  151. throw new Zend_Db_Statement_Exception($e->getMessage(), $e->getCode(), $e);
  152. }
  153. }
  154. /**
  155. * Returns the number of columns in the result set.
  156. * Returns null if the statement has no result set metadata.
  157. *
  158. * @return int The number of columns.
  159. * @throws Zend_Db_Statement_Exception
  160. */
  161. public function columnCount()
  162. {
  163. try {
  164. return $this->_stmt->columnCount();
  165. } catch (PDOException $e) {
  166. require_once 'Zend/Db/Statement/Exception.php';
  167. throw new Zend_Db_Statement_Exception($e->getMessage(), $e->getCode(), $e);
  168. }
  169. }
  170. /**
  171. * Retrieves the error code, if any, associated with the last operation on
  172. * the statement handle.
  173. *
  174. * @return string error code.
  175. * @throws Zend_Db_Statement_Exception
  176. */
  177. public function errorCode()
  178. {
  179. try {
  180. return $this->_stmt->errorCode();
  181. } catch (PDOException $e) {
  182. require_once 'Zend/Db/Statement/Exception.php';
  183. throw new Zend_Db_Statement_Exception($e->getMessage(), $e->getCode(), $e);
  184. }
  185. }
  186. /**
  187. * Retrieves an array of error information, if any, associated with the
  188. * last operation on the statement handle.
  189. *
  190. * @return array
  191. * @throws Zend_Db_Statement_Exception
  192. */
  193. public function errorInfo()
  194. {
  195. try {
  196. return $this->_stmt->errorInfo();
  197. } catch (PDOException $e) {
  198. require_once 'Zend/Db/Statement/Exception.php';
  199. throw new Zend_Db_Statement_Exception($e->getMessage(), $e->getCode(), $e);
  200. }
  201. }
  202. /**
  203. * Executes a prepared statement.
  204. *
  205. * @param array $params OPTIONAL Values to bind to parameter placeholders.
  206. * @return bool
  207. * @throws Zend_Db_Statement_Exception
  208. */
  209. public function _execute(array $params = null)
  210. {
  211. try {
  212. if ($params !== null) {
  213. return $this->_stmt->execute($params);
  214. } else {
  215. return $this->_stmt->execute();
  216. }
  217. } catch (PDOException $e) {
  218. require_once 'Zend/Db/Statement/Exception.php';
  219. throw new Zend_Db_Statement_Exception($e->getMessage(), $e->getCode(), $e);
  220. }
  221. }
  222. /**
  223. * Fetches a row from the result set.
  224. *
  225. * @param int $style OPTIONAL Fetch mode for this fetch operation.
  226. * @param int $cursor OPTIONAL Absolute, relative, or other.
  227. * @param int $offset OPTIONAL Number for absolute or relative cursors.
  228. * @return mixed Array, object, or scalar depending on fetch mode.
  229. * @throws Zend_Db_Statement_Exception
  230. */
  231. public function fetch($style = null, $cursor = null, $offset = null)
  232. {
  233. if ($style === null) {
  234. $style = $this->_fetchMode;
  235. }
  236. try {
  237. return $this->_stmt->fetch($style, $cursor, $offset);
  238. } catch (PDOException $e) {
  239. require_once 'Zend/Db/Statement/Exception.php';
  240. throw new Zend_Db_Statement_Exception($e->getMessage(), $e->getCode(), $e);
  241. }
  242. }
  243. /**
  244. * Required by IteratorAggregate interface
  245. *
  246. * @return IteratorIterator
  247. */
  248. public function getIterator()
  249. {
  250. return new IteratorIterator($this->_stmt);
  251. }
  252. /**
  253. * Returns an array containing all of the result set rows.
  254. *
  255. * @param int $style OPTIONAL Fetch mode.
  256. * @param int $col OPTIONAL Column number, if fetch mode is by column.
  257. * @return array Collection of rows, each in a format by the fetch mode.
  258. * @throws Zend_Db_Statement_Exception
  259. */
  260. public function fetchAll($style = null, $col = null)
  261. {
  262. if ($style === null) {
  263. $style = $this->_fetchMode;
  264. }
  265. try {
  266. if ($style == PDO::FETCH_COLUMN) {
  267. if ($col === null) {
  268. $col = 0;
  269. }
  270. return $this->_stmt->fetchAll($style, $col);
  271. } else {
  272. return $this->_stmt->fetchAll($style);
  273. }
  274. } catch (PDOException $e) {
  275. require_once 'Zend/Db/Statement/Exception.php';
  276. throw new Zend_Db_Statement_Exception($e->getMessage(), $e->getCode(), $e);
  277. }
  278. }
  279. /**
  280. * Returns a single column from the next row of a result set.
  281. *
  282. * @param int $col OPTIONAL Position of the column to fetch.
  283. * @return string
  284. * @throws Zend_Db_Statement_Exception
  285. */
  286. public function fetchColumn($col = 0)
  287. {
  288. try {
  289. return $this->_stmt->fetchColumn($col);
  290. } catch (PDOException $e) {
  291. require_once 'Zend/Db/Statement/Exception.php';
  292. throw new Zend_Db_Statement_Exception($e->getMessage(), $e->getCode(), $e);
  293. }
  294. }
  295. /**
  296. * Fetches the next row and returns it as an object.
  297. *
  298. * @param string $class OPTIONAL Name of the class to create.
  299. * @param array $config OPTIONAL Constructor arguments for the class.
  300. * @return mixed One object instance of the specified class.
  301. * @throws Zend_Db_Statement_Exception
  302. */
  303. public function fetchObject($class = 'stdClass', array $config = array())
  304. {
  305. try {
  306. return $this->_stmt->fetchObject($class, $config);
  307. } catch (PDOException $e) {
  308. require_once 'Zend/Db/Statement/Exception.php';
  309. throw new Zend_Db_Statement_Exception($e->getMessage(), $e->getCode(), $e);
  310. }
  311. }
  312. /**
  313. * Retrieve a statement attribute.
  314. *
  315. * @param integer $key Attribute name.
  316. * @return mixed Attribute value.
  317. * @throws Zend_Db_Statement_Exception
  318. */
  319. public function getAttribute($key)
  320. {
  321. try {
  322. return $this->_stmt->getAttribute($key);
  323. } catch (PDOException $e) {
  324. require_once 'Zend/Db/Statement/Exception.php';
  325. throw new Zend_Db_Statement_Exception($e->getMessage(), $e->getCode(), $e);
  326. }
  327. }
  328. /**
  329. * Returns metadata for a column in a result set.
  330. *
  331. * @param int $column
  332. * @return mixed
  333. * @throws Zend_Db_Statement_Exception
  334. */
  335. public function getColumnMeta($column)
  336. {
  337. try {
  338. return $this->_stmt->getColumnMeta($column);
  339. } catch (PDOException $e) {
  340. require_once 'Zend/Db/Statement/Exception.php';
  341. throw new Zend_Db_Statement_Exception($e->getMessage(), $e->getCode(), $e);
  342. }
  343. }
  344. /**
  345. * Retrieves the next rowset (result set) for a SQL statement that has
  346. * multiple result sets. An example is a stored procedure that returns
  347. * the results of multiple queries.
  348. *
  349. * @return bool
  350. * @throws Zend_Db_Statement_Exception
  351. */
  352. public function nextRowset()
  353. {
  354. try {
  355. return $this->_stmt->nextRowset();
  356. } catch (PDOException $e) {
  357. require_once 'Zend/Db/Statement/Exception.php';
  358. throw new Zend_Db_Statement_Exception($e->getMessage(), $e->getCode(), $e);
  359. }
  360. }
  361. /**
  362. * Returns the number of rows affected by the execution of the
  363. * last INSERT, DELETE, or UPDATE statement executed by this
  364. * statement object.
  365. *
  366. * @return int The number of rows affected.
  367. * @throws Zend_Db_Statement_Exception
  368. */
  369. public function rowCount()
  370. {
  371. try {
  372. return $this->_stmt->rowCount();
  373. } catch (PDOException $e) {
  374. require_once 'Zend/Db/Statement/Exception.php';
  375. throw new Zend_Db_Statement_Exception($e->getMessage(), $e->getCode(), $e);
  376. }
  377. }
  378. /**
  379. * Set a statement attribute.
  380. *
  381. * @param string $key Attribute name.
  382. * @param mixed $val Attribute value.
  383. * @return bool
  384. * @throws Zend_Db_Statement_Exception
  385. */
  386. public function setAttribute($key, $val)
  387. {
  388. try {
  389. return $this->_stmt->setAttribute($key, $val);
  390. } catch (PDOException $e) {
  391. require_once 'Zend/Db/Statement/Exception.php';
  392. throw new Zend_Db_Statement_Exception($e->getMessage(), $e->getCode(), $e);
  393. }
  394. }
  395. /**
  396. * Set the default fetch mode for this statement.
  397. *
  398. * @param int $mode The fetch mode.
  399. * @return bool
  400. * @throws Zend_Db_Statement_Exception
  401. */
  402. public function setFetchMode($mode)
  403. {
  404. $this->_fetchMode = $mode;
  405. try {
  406. return $this->_stmt->setFetchMode($mode);
  407. } catch (PDOException $e) {
  408. require_once 'Zend/Db/Statement/Exception.php';
  409. throw new Zend_Db_Statement_Exception($e->getMessage(), $e->getCode(), $e);
  410. }
  411. }
  412. }