2
0

Pdo.php 14 KB

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