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

/_Contrib/Zend-1.7.6/Db/Statement/Pdo.php

https://github.com/sitengine/sitengine
PHP | 433 lines | 218 code | 24 blank | 191 comment | 18 complexity | 35a18d1a615037ffc0a262682302c0ff 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-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
  39. {
  40. /**
  41. * The mysqli_stmt 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 (is_null($type)) {
  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 (is_null($variable)) {
  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 (is_null($type)) {
  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. * Returns an array containing all of the result set rows.
  251. *
  252. * @param int $style OPTIONAL Fetch mode.
  253. * @param int $col OPTIONAL Column number, if fetch mode is by column.
  254. * @return array Collection of rows, each in a format by the fetch mode.
  255. * @throws Zend_Db_Statement_Exception
  256. */
  257. public function fetchAll($style = null, $col = null)
  258. {
  259. if ($style === null) {
  260. $style = $this->_fetchMode;
  261. }
  262. try {
  263. if ($style == PDO::FETCH_COLUMN) {
  264. if ($col === null) {
  265. $col = 0;
  266. }
  267. return $this->_stmt->fetchAll($style, $col);
  268. } else {
  269. return $this->_stmt->fetchAll($style);
  270. }
  271. } catch (PDOException $e) {
  272. require_once 'Zend/Db/Statement/Exception.php';
  273. throw new Zend_Db_Statement_Exception($e->getMessage());
  274. }
  275. }
  276. /**
  277. * Returns a single column from the next row of a result set.
  278. *
  279. * @param int $col OPTIONAL Position of the column to fetch.
  280. * @return string
  281. * @throws Zend_Db_Statement_Exception
  282. */
  283. public function fetchColumn($col = 0)
  284. {
  285. try {
  286. return $this->_stmt->fetchColumn($col);
  287. } catch (PDOException $e) {
  288. require_once 'Zend/Db/Statement/Exception.php';
  289. throw new Zend_Db_Statement_Exception($e->getMessage());
  290. }
  291. }
  292. /**
  293. * Fetches the next row and returns it as an object.
  294. *
  295. * @param string $class OPTIONAL Name of the class to create.
  296. * @param array $config OPTIONAL Constructor arguments for the class.
  297. * @return mixed One object instance of the specified class.
  298. * @throws Zend_Db_Statement_Exception
  299. */
  300. public function fetchObject($class = 'stdClass', array $config = array())
  301. {
  302. try {
  303. return $this->_stmt->fetchObject($class, $config);
  304. } catch (PDOException $e) {
  305. require_once 'Zend/Db/Statement/Exception.php';
  306. throw new Zend_Db_Statement_Exception($e->getMessage());
  307. }
  308. }
  309. /**
  310. * Retrieve a statement attribute.
  311. *
  312. * @param integer $key Attribute name.
  313. * @return mixed Attribute value.
  314. * @throws Zend_Db_Statement_Exception
  315. */
  316. public function getAttribute($key)
  317. {
  318. try {
  319. return $this->_stmt->getAttribute($key);
  320. } catch (PDOException $e) {
  321. require_once 'Zend/Db/Statement/Exception.php';
  322. throw new Zend_Db_Statement_Exception($e->getMessage());
  323. }
  324. }
  325. /**
  326. * Returns metadata for a column in a result set.
  327. *
  328. * @param int $column
  329. * @return mixed
  330. * @throws Zend_Db_Statement_Exception
  331. */
  332. public function getColumnMeta($column)
  333. {
  334. try {
  335. return $this->_stmt->getColumnMeta($column);
  336. } catch (PDOException $e) {
  337. require_once 'Zend/Db/Statement/Exception.php';
  338. throw new Zend_Db_Statement_Exception($e->getMessage());
  339. }
  340. }
  341. /**
  342. * Retrieves the next rowset (result set) for a SQL statement that has
  343. * multiple result sets. An example is a stored procedure that returns
  344. * the results of multiple queries.
  345. *
  346. * @return bool
  347. * @throws Zend_Db_Statement_Exception
  348. */
  349. public function nextRowset()
  350. {
  351. try {
  352. return $this->_stmt->nextRowset();
  353. } catch (PDOException $e) {
  354. require_once 'Zend/Db/Statement/Exception.php';
  355. throw new Zend_Db_Statement_Exception($e->getMessage());
  356. }
  357. }
  358. /**
  359. * Returns the number of rows affected by the execution of the
  360. * last INSERT, DELETE, or UPDATE statement executed by this
  361. * statement object.
  362. *
  363. * @return int The number of rows affected.
  364. * @throws Zend_Db_Statement_Exception
  365. */
  366. public function rowCount()
  367. {
  368. try {
  369. return $this->_stmt->rowCount();
  370. } catch (PDOException $e) {
  371. require_once 'Zend/Db/Statement/Exception.php';
  372. throw new Zend_Db_Statement_Exception($e->getMessage());
  373. }
  374. }
  375. /**
  376. * Set a statement attribute.
  377. *
  378. * @param string $key Attribute name.
  379. * @param mixed $val Attribute value.
  380. * @return bool
  381. * @throws Zend_Db_Statement_Exception
  382. */
  383. public function setAttribute($key, $val)
  384. {
  385. try {
  386. return $this->_stmt->setAttribute($key, $val);
  387. } catch (PDOException $e) {
  388. require_once 'Zend/Db/Statement/Exception.php';
  389. throw new Zend_Db_Statement_Exception($e->getMessage());
  390. }
  391. }
  392. /**
  393. * Set the default fetch mode for this statement.
  394. *
  395. * @param int $mode The fetch mode.
  396. * @return bool
  397. * @throws Zend_Db_Statement_Exception
  398. */
  399. public function setFetchMode($mode)
  400. {
  401. $this->_fetchMode = $mode;
  402. try {
  403. return $this->_stmt->setFetchMode($mode);
  404. } catch (PDOException $e) {
  405. require_once 'Zend/Db/Statement/Exception.php';
  406. throw new Zend_Db_Statement_Exception($e->getMessage());
  407. }
  408. }
  409. }