PageRenderTime 47ms CodeModel.GetById 18ms RepoModel.GetById 0ms app.codeStats 1ms

/lib/Zend/Db/Statement.php

https://bitbucket.org/sunil_nextbits/magento2
PHP | 485 lines | 204 code | 45 blank | 236 comment | 37 complexity | b68e7161c42d65483d026380ac9233f3 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-2010 Zend Technologies USA Inc. (http://www.zend.com)
  19. * @license http://framework.zend.com/license/new-bsd New BSD License
  20. * @version $Id: Statement.php 20096 2010-01-06 02:05:09Z bkarwin $
  21. */
  22. /**
  23. * @see Zend_Db
  24. */
  25. #require_once 'Zend/Db.php';
  26. /**
  27. * @see Zend_Db_Statement_Interface
  28. */
  29. #require_once 'Zend/Db/Statement/Interface.php';
  30. /**
  31. * Abstract class to emulate a PDOStatement for native database adapters.
  32. *
  33. * @category Zend
  34. * @package Zend_Db
  35. * @subpackage Statement
  36. * @copyright Copyright (c) 2005-2010 Zend Technologies USA Inc. (http://www.zend.com)
  37. * @license http://framework.zend.com/license/new-bsd New BSD License
  38. */
  39. abstract class Zend_Db_Statement implements Zend_Db_Statement_Interface
  40. {
  41. /**
  42. * @var resource|object The driver level statement object/resource
  43. */
  44. protected $_stmt = null;
  45. /**
  46. * @var Zend_Db_Adapter_Abstract
  47. */
  48. protected $_adapter = null;
  49. /**
  50. * The current fetch mode.
  51. *
  52. * @var integer
  53. */
  54. protected $_fetchMode = Zend_Db::FETCH_ASSOC;
  55. /**
  56. * Attributes.
  57. *
  58. * @var array
  59. */
  60. protected $_attribute = array();
  61. /**
  62. * Column result bindings.
  63. *
  64. * @var array
  65. */
  66. protected $_bindColumn = array();
  67. /**
  68. * Query parameter bindings; covers bindParam() and bindValue().
  69. *
  70. * @var array
  71. */
  72. protected $_bindParam = array();
  73. /**
  74. * SQL string split into an array at placeholders.
  75. *
  76. * @var array
  77. */
  78. protected $_sqlSplit = array();
  79. /**
  80. * Parameter placeholders in the SQL string by position in the split array.
  81. *
  82. * @var array
  83. */
  84. protected $_sqlParam = array();
  85. /**
  86. * @var Zend_Db_Profiler_Query
  87. */
  88. protected $_queryId = null;
  89. /**
  90. * Constructor for a statement.
  91. *
  92. * @param Zend_Db_Adapter_Abstract $adapter
  93. * @param mixed $sql Either a string or Zend_Db_Select.
  94. */
  95. public function __construct($adapter, $sql)
  96. {
  97. $this->_adapter = $adapter;
  98. if ($sql instanceof Zend_Db_Select) {
  99. $sql = $sql->assemble();
  100. }
  101. $this->_parseParameters($sql);
  102. $this->_prepare($sql);
  103. $this->_queryId = $this->_adapter->getProfiler()->queryStart($sql);
  104. }
  105. /**
  106. * Internal method called by abstract statment constructor to setup
  107. * the driver level statement
  108. *
  109. * @return void
  110. */
  111. protected function _prepare($sql)
  112. {
  113. return;
  114. }
  115. /**
  116. * @param string $sql
  117. * @return void
  118. */
  119. protected function _parseParameters($sql)
  120. {
  121. $sql = $this->_stripQuoted($sql);
  122. // split into text and params
  123. $this->_sqlSplit = preg_split('/(\?|\:[a-zA-Z0-9_]+)/',
  124. $sql, -1, PREG_SPLIT_DELIM_CAPTURE|PREG_SPLIT_NO_EMPTY);
  125. // map params
  126. $this->_sqlParam = array();
  127. foreach ($this->_sqlSplit as $key => $val) {
  128. if ($val == '?') {
  129. if ($this->_adapter->supportsParameters('positional') === false) {
  130. /**
  131. * @see Zend_Db_Statement_Exception
  132. */
  133. #require_once 'Zend/Db/Statement/Exception.php';
  134. throw new Zend_Db_Statement_Exception("Invalid bind-variable position '$val'");
  135. }
  136. } else if ($val[0] == ':') {
  137. if ($this->_adapter->supportsParameters('named') === false) {
  138. /**
  139. * @see Zend_Db_Statement_Exception
  140. */
  141. #require_once 'Zend/Db/Statement/Exception.php';
  142. throw new Zend_Db_Statement_Exception("Invalid bind-variable name '$val'");
  143. }
  144. }
  145. $this->_sqlParam[] = $val;
  146. }
  147. // set up for binding
  148. $this->_bindParam = array();
  149. }
  150. /**
  151. * Remove parts of a SQL string that contain quoted strings
  152. * of values or identifiers.
  153. *
  154. * @param string $sql
  155. * @return string
  156. */
  157. protected function _stripQuoted($sql)
  158. {
  159. // get the character for delimited id quotes,
  160. // this is usually " but in MySQL is `
  161. $d = $this->_adapter->quoteIdentifier('a');
  162. $d = $d[0];
  163. // get the value used as an escaped delimited id quote,
  164. // e.g. \" or "" or \`
  165. $de = $this->_adapter->quoteIdentifier($d);
  166. $de = substr($de, 1, 2);
  167. $de = str_replace('\\', '\\\\', $de);
  168. // get the character for value quoting
  169. // this should be '
  170. $q = $this->_adapter->quote('a');
  171. $q = $q[0];
  172. // get the value used as an escaped quote,
  173. // e.g. \' or ''
  174. $qe = $this->_adapter->quote($q);
  175. $qe = substr($qe, 1, 2);
  176. $qe = str_replace('\\', '\\\\', $qe);
  177. // get a version of the SQL statement with all quoted
  178. // values and delimited identifiers stripped out
  179. // remove "foo\"bar"
  180. $sql = preg_replace("/$q($qe|\\\\{2}|[^$q])*$q/", '', $sql);
  181. // remove 'foo\'bar'
  182. if (!empty($q)) {
  183. $sql = preg_replace("/$q($qe|[^$q])*$q/", '', $sql);
  184. }
  185. return $sql;
  186. }
  187. /**
  188. * Bind a column of the statement result set to a PHP variable.
  189. *
  190. * @param string $column Name the column in the result set, either by
  191. * position or by name.
  192. * @param mixed $param Reference to the PHP variable containing the value.
  193. * @param mixed $type OPTIONAL
  194. * @return bool
  195. */
  196. public function bindColumn($column, &$param, $type = null)
  197. {
  198. $this->_bindColumn[$column] =& $param;
  199. return true;
  200. }
  201. /**
  202. * Binds a parameter to the specified variable name.
  203. *
  204. * @param mixed $parameter Name the parameter, either integer or string.
  205. * @param mixed $variable Reference to PHP variable containing the value.
  206. * @param mixed $type OPTIONAL Datatype of SQL parameter.
  207. * @param mixed $length OPTIONAL Length of SQL parameter.
  208. * @param mixed $options OPTIONAL Other options.
  209. * @return bool
  210. */
  211. public function bindParam($parameter, &$variable, $type = null, $length = null, $options = null)
  212. {
  213. if (!is_int($parameter) && !is_string($parameter)) {
  214. /**
  215. * @see Zend_Db_Statement_Exception
  216. */
  217. #require_once 'Zend/Db/Statement/Exception.php';
  218. throw new Zend_Db_Statement_Exception('Invalid bind-variable position');
  219. }
  220. $position = null;
  221. if (($intval = (int) $parameter) > 0 && $this->_adapter->supportsParameters('positional')) {
  222. if ($intval >= 1 || $intval <= count($this->_sqlParam)) {
  223. $position = $intval;
  224. }
  225. } else if ($this->_adapter->supportsParameters('named')) {
  226. if ($parameter[0] != ':') {
  227. $parameter = ':' . $parameter;
  228. }
  229. if (in_array($parameter, $this->_sqlParam) !== false) {
  230. $position = $parameter;
  231. }
  232. }
  233. if ($position === null) {
  234. /**
  235. * @see Zend_Db_Statement_Exception
  236. */
  237. #require_once 'Zend/Db/Statement/Exception.php';
  238. throw new Zend_Db_Statement_Exception("Invalid bind-variable position '$parameter'");
  239. }
  240. // Finally we are assured that $position is valid
  241. $this->_bindParam[$position] =& $variable;
  242. return $this->_bindParam($position, $variable, $type, $length, $options);
  243. }
  244. /**
  245. * Binds a value to a parameter.
  246. *
  247. * @param mixed $parameter Name the parameter, either integer or string.
  248. * @param mixed $value Scalar value to bind to the parameter.
  249. * @param mixed $type OPTIONAL Datatype of the parameter.
  250. * @return bool
  251. */
  252. public function bindValue($parameter, $value, $type = null)
  253. {
  254. return $this->bindParam($parameter, $value, $type);
  255. }
  256. /**
  257. * Executes a prepared statement.
  258. *
  259. * @param array $params OPTIONAL Values to bind to parameter placeholders.
  260. * @return bool
  261. */
  262. public function execute(array $params = null)
  263. {
  264. /*
  265. * Simple case - no query profiler to manage.
  266. */
  267. if ($this->_queryId === null) {
  268. return $this->_execute($params);
  269. }
  270. /*
  271. * Do the same thing, but with query profiler
  272. * management before and after the execute.
  273. */
  274. $prof = $this->_adapter->getProfiler();
  275. $qp = $prof->getQueryProfile($this->_queryId);
  276. if ($qp->hasEnded()) {
  277. $this->_queryId = $prof->queryClone($qp);
  278. $qp = $prof->getQueryProfile($this->_queryId);
  279. }
  280. if ($params !== null) {
  281. $qp->bindParams($params);
  282. } else {
  283. $qp->bindParams($this->_bindParam);
  284. }
  285. $qp->start($this->_queryId);
  286. $retval = $this->_execute($params);
  287. $prof->queryEnd($this->_queryId);
  288. return $retval;
  289. }
  290. /**
  291. * Returns an array containing all of the result set rows.
  292. *
  293. * @param int $style OPTIONAL Fetch mode.
  294. * @param int $col OPTIONAL Column number, if fetch mode is by column.
  295. * @return array Collection of rows, each in a format by the fetch mode.
  296. */
  297. public function fetchAll($style = null, $col = null)
  298. {
  299. $data = array();
  300. if ($style === Zend_Db::FETCH_COLUMN && $col === null) {
  301. $col = 0;
  302. }
  303. if ($col === null) {
  304. while ($row = $this->fetch($style)) {
  305. $data[] = $row;
  306. }
  307. } else {
  308. while (false !== ($val = $this->fetchColumn($col))) {
  309. $data[] = $val;
  310. }
  311. }
  312. return $data;
  313. }
  314. /**
  315. * Returns a single column from the next row of a result set.
  316. *
  317. * @param int $col OPTIONAL Position of the column to fetch.
  318. * @return string One value from the next row of result set, or false.
  319. */
  320. public function fetchColumn($col = 0)
  321. {
  322. $data = array();
  323. $col = (int) $col;
  324. $row = $this->fetch(Zend_Db::FETCH_NUM);
  325. if (!is_array($row)) {
  326. return false;
  327. }
  328. return $row[$col];
  329. }
  330. /**
  331. * Fetches the next row and returns it as an object.
  332. *
  333. * @param string $class OPTIONAL Name of the class to create.
  334. * @param array $config OPTIONAL Constructor arguments for the class.
  335. * @return mixed One object instance of the specified class, or false.
  336. */
  337. public function fetchObject($class = 'stdClass', array $config = array())
  338. {
  339. $obj = new $class($config);
  340. $row = $this->fetch(Zend_Db::FETCH_ASSOC);
  341. if (!is_array($row)) {
  342. return false;
  343. }
  344. foreach ($row as $key => $val) {
  345. $obj->$key = $val;
  346. }
  347. return $obj;
  348. }
  349. /**
  350. * Retrieve a statement attribute.
  351. *
  352. * @param string $key Attribute name.
  353. * @return mixed Attribute value.
  354. */
  355. public function getAttribute($key)
  356. {
  357. if (array_key_exists($key, $this->_attribute)) {
  358. return $this->_attribute[$key];
  359. }
  360. }
  361. /**
  362. * Set a statement attribute.
  363. *
  364. * @param string $key Attribute name.
  365. * @param mixed $val Attribute value.
  366. * @return bool
  367. */
  368. public function setAttribute($key, $val)
  369. {
  370. $this->_attribute[$key] = $val;
  371. }
  372. /**
  373. * Set the default fetch mode for this statement.
  374. *
  375. * @param int $mode The fetch mode.
  376. * @return bool
  377. * @throws Zend_Db_Statement_Exception
  378. */
  379. public function setFetchMode($mode)
  380. {
  381. switch ($mode) {
  382. case Zend_Db::FETCH_NUM:
  383. case Zend_Db::FETCH_ASSOC:
  384. case Zend_Db::FETCH_BOTH:
  385. case Zend_Db::FETCH_OBJ:
  386. $this->_fetchMode = $mode;
  387. break;
  388. case Zend_Db::FETCH_BOUND:
  389. default:
  390. $this->closeCursor();
  391. /**
  392. * @see Zend_Db_Statement_Exception
  393. */
  394. #require_once 'Zend/Db/Statement/Exception.php';
  395. throw new Zend_Db_Statement_Exception('invalid fetch mode');
  396. break;
  397. }
  398. }
  399. /**
  400. * Helper function to map retrieved row
  401. * to bound column variables
  402. *
  403. * @param array $row
  404. * @return bool True
  405. */
  406. public function _fetchBound($row)
  407. {
  408. foreach ($row as $key => $value) {
  409. // bindColumn() takes 1-based integer positions
  410. // but fetch() returns 0-based integer indexes
  411. if (is_int($key)) {
  412. $key++;
  413. }
  414. // set results only to variables that were bound previously
  415. if (isset($this->_bindColumn[$key])) {
  416. $this->_bindColumn[$key] = $value;
  417. }
  418. }
  419. return true;
  420. }
  421. /**
  422. * Gets the Zend_Db_Adapter_Abstract for this
  423. * particular Zend_Db_Statement object.
  424. *
  425. * @return Zend_Db_Adapter_Abstract
  426. */
  427. public function getAdapter()
  428. {
  429. return $this->_adapter;
  430. }
  431. /**
  432. * Gets the resource or object setup by the
  433. * _parse
  434. * @return unknown_type
  435. */
  436. public function getDriverStatement()
  437. {
  438. return $this->_stmt;
  439. }
  440. }