PageRenderTime 44ms CodeModel.GetById 19ms RepoModel.GetById 0ms app.codeStats 0ms

/ autowebsys/autowebsys/library/Zend/Db/Statement.php

http://autowebsys.googlecode.com/
PHP | 487 lines | 209 code | 45 blank | 233 comment | 36 complexity | 0bd9240dbb19bf2e9162fe56b5fdcfc6 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. do {
  222. if (($intval = (int) $parameter) > 0 && $this->_adapter->supportsParameters('positional')) {
  223. if ($intval >= 1 || $intval <= count($this->_sqlParam)) {
  224. $position = $intval;
  225. }
  226. } else if ($this->_adapter->supportsParameters('named')) {
  227. if ($parameter[0] != ':') {
  228. $parameter = ':' . $parameter;
  229. }
  230. if (in_array($parameter, $this->_sqlParam) !== false) {
  231. $position = $parameter;
  232. }
  233. }
  234. // if ($position === null) {
  235. // /**
  236. // * @see Zend_Db_Statement_Exception
  237. // */
  238. // require_once 'Zend/Db/Statement/Exception.php';
  239. // throw new Zend_Db_Statement_Exception("Invalid bind-variable position '$parameter'");
  240. // }
  241. } while($position !== null);
  242. // Finally we are assured that $position is valid
  243. $this->_bindParam[$position] =& $variable;
  244. return $this->_bindParam($position, $variable, $type, $length, $options);
  245. }
  246. /**
  247. * Binds a value to a parameter.
  248. *
  249. * @param mixed $parameter Name the parameter, either integer or string.
  250. * @param mixed $value Scalar value to bind to the parameter.
  251. * @param mixed $type OPTIONAL Datatype of the parameter.
  252. * @return bool
  253. */
  254. public function bindValue($parameter, $value, $type = null)
  255. {
  256. return $this->bindParam($parameter, $value, $type);
  257. }
  258. /**
  259. * Executes a prepared statement.
  260. *
  261. * @param array $params OPTIONAL Values to bind to parameter placeholders.
  262. * @return bool
  263. */
  264. public function execute(array $params = null)
  265. {
  266. /*
  267. * Simple case - no query profiler to manage.
  268. */
  269. if ($this->_queryId === null) {
  270. return $this->_execute($params);
  271. }
  272. /*
  273. * Do the same thing, but with query profiler
  274. * management before and after the execute.
  275. */
  276. $prof = $this->_adapter->getProfiler();
  277. $qp = $prof->getQueryProfile($this->_queryId);
  278. if ($qp->hasEnded()) {
  279. $this->_queryId = $prof->queryClone($qp);
  280. $qp = $prof->getQueryProfile($this->_queryId);
  281. }
  282. if ($params !== null) {
  283. $qp->bindParams($params);
  284. } else {
  285. $qp->bindParams($this->_bindParam);
  286. }
  287. $qp->start($this->_queryId);
  288. $retval = $this->_execute($params);
  289. $prof->queryEnd($this->_queryId);
  290. return $retval;
  291. }
  292. /**
  293. * Returns an array containing all of the result set rows.
  294. *
  295. * @param int $style OPTIONAL Fetch mode.
  296. * @param int $col OPTIONAL Column number, if fetch mode is by column.
  297. * @return array Collection of rows, each in a format by the fetch mode.
  298. */
  299. public function fetchAll($style = null, $col = null)
  300. {
  301. $data = array();
  302. if ($style === Zend_Db::FETCH_COLUMN && $col === null) {
  303. $col = 0;
  304. }
  305. if ($col === null) {
  306. while ($row = $this->fetch($style)) {
  307. $data[] = $row;
  308. }
  309. } else {
  310. while (false !== ($val = $this->fetchColumn($col))) {
  311. $data[] = $val;
  312. }
  313. }
  314. return $data;
  315. }
  316. /**
  317. * Returns a single column from the next row of a result set.
  318. *
  319. * @param int $col OPTIONAL Position of the column to fetch.
  320. * @return string One value from the next row of result set, or false.
  321. */
  322. public function fetchColumn($col = 0)
  323. {
  324. $data = array();
  325. $col = (int) $col;
  326. $row = $this->fetch(Zend_Db::FETCH_NUM);
  327. if (!is_array($row)) {
  328. return false;
  329. }
  330. return $row[$col];
  331. }
  332. /**
  333. * Fetches the next row and returns it as an object.
  334. *
  335. * @param string $class OPTIONAL Name of the class to create.
  336. * @param array $config OPTIONAL Constructor arguments for the class.
  337. * @return mixed One object instance of the specified class, or false.
  338. */
  339. public function fetchObject($class = 'stdClass', array $config = array())
  340. {
  341. $obj = new $class($config);
  342. $row = $this->fetch(Zend_Db::FETCH_ASSOC);
  343. if (!is_array($row)) {
  344. return false;
  345. }
  346. foreach ($row as $key => $val) {
  347. $obj->$key = $val;
  348. }
  349. return $obj;
  350. }
  351. /**
  352. * Retrieve a statement attribute.
  353. *
  354. * @param string $key Attribute name.
  355. * @return mixed Attribute value.
  356. */
  357. public function getAttribute($key)
  358. {
  359. if (array_key_exists($key, $this->_attribute)) {
  360. return $this->_attribute[$key];
  361. }
  362. }
  363. /**
  364. * Set a statement attribute.
  365. *
  366. * @param string $key Attribute name.
  367. * @param mixed $val Attribute value.
  368. * @return bool
  369. */
  370. public function setAttribute($key, $val)
  371. {
  372. $this->_attribute[$key] = $val;
  373. }
  374. /**
  375. * Set the default fetch mode for this statement.
  376. *
  377. * @param int $mode The fetch mode.
  378. * @return bool
  379. * @throws Zend_Db_Statement_Exception
  380. */
  381. public function setFetchMode($mode)
  382. {
  383. switch ($mode) {
  384. case Zend_Db::FETCH_NUM:
  385. case Zend_Db::FETCH_ASSOC:
  386. case Zend_Db::FETCH_BOTH:
  387. case Zend_Db::FETCH_OBJ:
  388. $this->_fetchMode = $mode;
  389. break;
  390. case Zend_Db::FETCH_BOUND:
  391. default:
  392. $this->closeCursor();
  393. /**
  394. * @see Zend_Db_Statement_Exception
  395. */
  396. require_once 'Zend/Db/Statement/Exception.php';
  397. throw new Zend_Db_Statement_Exception('invalid fetch mode');
  398. break;
  399. }
  400. }
  401. /**
  402. * Helper function to map retrieved row
  403. * to bound column variables
  404. *
  405. * @param array $row
  406. * @return bool True
  407. */
  408. public function _fetchBound($row)
  409. {
  410. foreach ($row as $key => $value) {
  411. // bindColumn() takes 1-based integer positions
  412. // but fetch() returns 0-based integer indexes
  413. if (is_int($key)) {
  414. $key++;
  415. }
  416. // set results only to variables that were bound previously
  417. if (isset($this->_bindColumn[$key])) {
  418. $this->_bindColumn[$key] = $value;
  419. }
  420. }
  421. return true;
  422. }
  423. /**
  424. * Gets the Zend_Db_Adapter_Abstract for this
  425. * particular Zend_Db_Statement object.
  426. *
  427. * @return Zend_Db_Adapter_Abstract
  428. */
  429. public function getAdapter()
  430. {
  431. return $this->_adapter;
  432. }
  433. /**
  434. * Gets the resource or object setup by the
  435. * _parse
  436. * @return unknown_type
  437. */
  438. public function getDriverStatement()
  439. {
  440. return $this->_stmt;
  441. }
  442. }