Pdo.php 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410
  1. <?php
  2. /**
  3. *
  4. * Cube Framework $Id$ YJ6QIPQLwvHGSqBT8khG6zOIOs40LWzpMnK9ALvjNQs=
  5. *
  6. * @link http://codecu.be/framework
  7. * @copyright Copyright (c) 2015 CodeCube SRL
  8. * @license http://codecu.be/framework/license Commercial License
  9. *
  10. * @version 1.5
  11. */
  12. /**
  13. * pdo statement class = proxy to \PDOStatement
  14. */
  15. namespace Cube\Db\Statement;
  16. use Cube\Exception;
  17. class Pdo extends AbstractStatement
  18. {
  19. /**
  20. *
  21. * statement resource
  22. *
  23. * @var \PDOStatement
  24. */
  25. protected $_stmt = null;
  26. /**
  27. *
  28. * fetch mode
  29. *
  30. * @var int
  31. */
  32. protected $_fetchMode = \PDO::FETCH_ASSOC;
  33. /**
  34. *
  35. * prepare a string SQL statement and create a statement object.
  36. *
  37. * @param string $sql
  38. *
  39. * @return void
  40. * @throws \Cube\Exception
  41. */
  42. protected function _prepare($sql)
  43. {
  44. try {
  45. $this->_stmt = $this->_adapter->getConnection()->prepare($sql);
  46. } catch (\PDOException $e) {
  47. throw new Exception($e->getMessage(), $e->getCode());
  48. }
  49. }
  50. /**
  51. *
  52. * bind a column to a php variable
  53. *
  54. * @param string $column name or position of the column in the result set
  55. * @param mixed $param reference to the php variable to which the column will be bound
  56. * @param int $type data type of the parameter
  57. *
  58. * @return bool
  59. * @throws \Cube\Exception
  60. */
  61. public function bindColumn($column, &$param, $type = null)
  62. {
  63. $this->_bindColumn[$column] = &$param;
  64. try {
  65. return $this->_stmt->bindColumn($column, $param, $type);
  66. } catch (\PDOException $e) {
  67. throw new Exception($e->getMessage(), $e->getCode());
  68. }
  69. }
  70. /**
  71. *
  72. * binds a parameter to the specified variable name
  73. *
  74. * @param int|string $parameter parameter identifier
  75. * @param mixed $variable name of the PHP variable to bind to the SQL statement parameter
  76. * @param int $type data type of the parameter
  77. * @param int $length length of the data type
  78. * @param mixed $options other driver options
  79. *
  80. * @throws \InvalidArgumentException
  81. * @throws \Cube\Exception
  82. * @return bool
  83. */
  84. public function bindParam($parameter, &$variable, $type = null, $length = null, $options = null)
  85. {
  86. if (!is_int($parameter) && !is_string($parameter)) {
  87. throw new \InvalidArgumentException('Invalid bind-variable position');
  88. }
  89. $this->_bindParam[$parameter] = &$variable;
  90. try {
  91. if ($type === null) {
  92. if (is_bool($variable)) {
  93. $type = \PDO::PARAM_BOOL;
  94. }
  95. elseif ($variable === null) {
  96. $type = \PDO::PARAM_NULL;
  97. }
  98. elseif (is_integer($variable)) {
  99. $type = \PDO::PARAM_INT;
  100. }
  101. else {
  102. $type = \PDO::PARAM_STR;
  103. }
  104. }
  105. return $this->_stmt->bindParam($parameter, $variable, $type, $length, $options);
  106. } catch (\PDOException $e) {
  107. throw new Exception($e->getMessage(), $e->getCode());
  108. }
  109. }
  110. /**
  111. *
  112. * binds a value to a parameter
  113. *
  114. * @param int|string $parameter parameter identifier
  115. * @param mixed $value the value to bind to the parameter
  116. * @param string $type data type of the parameter
  117. *
  118. * @return bool
  119. * @throws \Cube\Exception
  120. */
  121. public function bindValue($parameter, &$value, $type = null)
  122. {
  123. if (is_string($parameter) && $parameter[0] != ':') {
  124. $parameter = ":$parameter";
  125. }
  126. $this->_bindParam[$parameter] = $value;
  127. try {
  128. return $this->_stmt->bindValue($parameter, $value, $type);
  129. } catch (\PDOException $e) {
  130. throw new Exception($e->getMessage(), $e->getCode());
  131. }
  132. }
  133. /**
  134. *
  135. * closes the cursor, allowing the statement to be executed again
  136. *
  137. * @return bool
  138. * @throws \Cube\Exception
  139. */
  140. public function closeCursor()
  141. {
  142. try {
  143. return $this->_stmt->closeCursor();
  144. } catch (\PDOException $e) {
  145. throw new Exception($e->getMessage(), $e->getCode());
  146. }
  147. }
  148. /**
  149. *
  150. * returns the number of columns in the result set
  151. *
  152. * @return int
  153. * @throws \Cube\Exception
  154. */
  155. public function columnCount()
  156. {
  157. try {
  158. return $this->_stmt->columnCount();
  159. } catch (\PDOException $e) {
  160. throw new Exception($e->getMessage(), $e->getCode());
  161. }
  162. }
  163. /**
  164. *
  165. * fetch the error code associated with the last operation on the statement handle
  166. *
  167. * @return string
  168. * @throws \Cube\Exception
  169. */
  170. public function errorCode()
  171. {
  172. try {
  173. return $this->_stmt->errorCode();
  174. } catch (\PDOException $e) {
  175. throw new Exception($e->getMessage(), $e->getCode());
  176. }
  177. }
  178. /**
  179. *
  180. * fetch extended error information associated with the last operation on the statement handle
  181. *
  182. * @return array
  183. * @throws \Cube\Exception
  184. */
  185. public function errorInfo()
  186. {
  187. try {
  188. return $this->_stmt->errorCode();
  189. } catch (\PDOException $e) {
  190. throw new Exception($e->getMessage(), $e->getCode());
  191. }
  192. }
  193. /**
  194. *
  195. * execute a prepared statement
  196. *
  197. * @param array $params array of values with as many elements as there are bound parameters in the SQL statement being executed
  198. *
  199. * @return bool
  200. * @throws \Cube\Exception
  201. */
  202. public function execute(array $params = null)
  203. {
  204. try {
  205. return $this->_stmt->execute($params);
  206. } catch (\PDOException $e) {
  207. $errorMessage = $e->getMessage() . ' [Query]: ' . $this->_stmt->queryString;
  208. throw new Exception($errorMessage, $e->getCode());
  209. }
  210. }
  211. /**
  212. *
  213. * fetch the next row from the result set
  214. *
  215. * @param int $style fetch mode
  216. * @param int $cursor scrollable cursor
  217. * @param int $offset number of cursors
  218. *
  219. * @return mixed
  220. * @throws \Cube\Exception
  221. */
  222. public function fetch($style = null, $cursor = null, $offset = null)
  223. {
  224. if ($style === null) {
  225. $style = $this->_fetchMode;
  226. }
  227. try {
  228. return $this->_stmt->fetch($style, $cursor, $offset);
  229. } catch (\PDOException $e) {
  230. throw new Exception($e->getMessage(), $e->getCode());
  231. }
  232. }
  233. /**
  234. *
  235. * returns an array containing all of the result set rows
  236. *
  237. * @param int $style fetch mode
  238. * @param int $column column number, if fetch mode is by column
  239. *
  240. * @return array collection of rows, each in a format by fetch mode
  241. * @throws \Cube\Exception
  242. */
  243. public function fetchAll($style = null, $column = null)
  244. {
  245. if ($style === null) {
  246. $style = $this->_fetchMode;
  247. }
  248. try {
  249. if ($style === \PDO::FETCH_COLUMN) {
  250. return $this->_stmt->fetchAll($style, (int)$column);
  251. }
  252. else {
  253. return $this->_stmt->fetchAll($style);
  254. }
  255. } catch (\PDOException $e) {
  256. throw new Exception($e->getMessage(), $e->getCode());
  257. }
  258. }
  259. /**
  260. *
  261. * returns a single column from the next row of a result set
  262. *
  263. * @param int $column column number from the fetch row or the first column if no column is set
  264. *
  265. * @return string
  266. * @throws \Cube\Exception
  267. */
  268. public function fetchColumn($column = null)
  269. {
  270. try {
  271. return $this->_stmt->fetchColumn((int)$column);
  272. } catch (\PDOException $e) {
  273. throw new Exception($e->getMessage(), $e->getCode());
  274. }
  275. }
  276. /**
  277. *
  278. * fetches the next row and returns it as an object
  279. *
  280. * @param string $class name of the class to create
  281. * @param array $config constructor arguments to add to the class
  282. *
  283. * @return mixed one object instance of the specified class
  284. * @throws \Cube\Exception
  285. */
  286. public function fetchObject($class = 'stdClass', array $config = array())
  287. {
  288. try {
  289. return $this->_stmt->fetchObject($class, $config);
  290. } catch (\PDOException $e) {
  291. throw new Exception($e->getMessage(), $e->getCode());
  292. }
  293. }
  294. /**
  295. *
  296. * retrieve a statement attribute
  297. *
  298. * @param string $attribute the attribute name
  299. *
  300. * @return mixed
  301. * @throws \Cube\Exception
  302. */
  303. public function getAttribute($attribute)
  304. {
  305. try {
  306. return $this->_stmt->getAttribute($attribute);
  307. } catch (\PDOException $e) {
  308. throw new Exception($e->getMessage(), $e->getCode());
  309. }
  310. }
  311. /**
  312. *
  313. * advances to the next rowset in a multi-rowset statement handle
  314. *
  315. * @return bool
  316. * @throws \Cube\Exception
  317. */
  318. public function nextRowset()
  319. {
  320. try {
  321. return $this->_stmt->nextRowset();
  322. } catch (\PDOException $e) {
  323. throw new Exception($e->getMessage(), $e->getCode());
  324. }
  325. }
  326. /**
  327. *
  328. * returns the number of rows affected by the last SQL statement
  329. *
  330. * @return int the number of rows affected
  331. * @throws \Cube\Exception
  332. */
  333. public function rowCount()
  334. {
  335. try {
  336. return $this->_stmt->rowCount();
  337. } catch (\PDOException $e) {
  338. throw new Exception($e->getMessage(), $e->getCode());
  339. }
  340. }
  341. /**
  342. *
  343. * set a statement attribute
  344. *
  345. * @param string $key attribute name
  346. * @param mixed $value attribute value
  347. *
  348. * @return bool
  349. * @throws \Cube\Exception
  350. */
  351. public function setAttribute($key, $value)
  352. {
  353. $this->_attributes[$key] = $value;
  354. try {
  355. return $this->_stmt->setAttribute($key, $value);
  356. } catch (\PDOException $e) {
  357. throw new Exception($e->getMessage(), $e->getCode());
  358. }
  359. }
  360. /**
  361. *
  362. * set the default fetch mode for this statement.
  363. *
  364. * @param int $mode the fetch mode
  365. *
  366. * @return bool
  367. * @throws \Cube\Exception
  368. */
  369. public function setFetchMode($mode)
  370. {
  371. try {
  372. return $this->_stmt->setFetchMode($mode);
  373. } catch (\PDOException $e) {
  374. throw new Exception($e->getMessage(), $e->getCode());
  375. }
  376. }
  377. }