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.
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
25
require_once 'Zend/Db.php';
28
* @see Zend_Db_Statement_Interface
30
require_once 'Zend/Db/Statement/Interface.php';
33
* Abstract class to emulate a PDOStatement for native database adapters.
37
* @subpackage Statement
38
* @copyright Copyright (c) 2005-2008 Zend Technologies USA Inc. (http://www.zend.com)
39
* @license http://framework.zend.com/license/new-bsd New BSD License
41
abstract class Zend_Db_Statement implements Zend_Db_Statement_Interface
45
* @var Zend_Db_Adapter_Abstract
47
protected $_adapter = null;
50
* The current fetch mode.
54
protected $_fetchMode = Zend_Db::FETCH_ASSOC;
61
protected $_attribute = array();
64
* Column result bindings.
68
protected $_bindColumn = array();
71
* Query parameter bindings; covers bindParam() and bindValue().
75
protected $_bindParam = array();
78
* SQL string split into an array at placeholders.
82
protected $_sqlSplit = array();
85
* Parameter placeholders in the SQL string by position in the split array.
89
protected $_sqlParam = array();
92
* @var Zend_Db_Profiler_Query
94
protected $_queryId = null;
97
* Constructor for a statement.
99
* @param Zend_Db_Adapter_Abstract $adapter
100
* @param mixed $sql Either a string or Zend_Db_Select.
102
public function __construct($adapter, $sql)
104
$this->_adapter = $adapter;
105
if ($sql instanceof Zend_Db_Select) {
106
$sql = $sql->assemble();
108
$this->_parseParameters($sql);
109
$this->_prepare($sql);
111
$this->_queryId = $this->_adapter->getProfiler()->queryStart($sql);
118
protected function _parseParameters($sql)
120
$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);
127
$this->_sqlParam = array();
128
foreach ($this->_sqlSplit as $key => $val) {
130
if ($this->_adapter->supportsParameters('positional') === false) {
132
* @see Zend_Db_Statement_Exception
134
require_once 'Zend/Db/Statement/Exception.php';
135
throw new Zend_Db_Statement_Exception("Invalid bind-variable position '$val'");
137
} else if ($val[0] == ':') {
138
if ($this->_adapter->supportsParameters('named') === false) {
140
* @see Zend_Db_Statement_Exception
142
require_once 'Zend/Db/Statement/Exception.php';
143
throw new Zend_Db_Statement_Exception("Invalid bind-variable name '$val'");
146
$this->_sqlParam[] = $val;
149
// set up for binding
150
$this->_bindParam = array();
154
* Remove parts of a SQL string that contain quoted strings
155
* of values or identifiers.
160
protected function _stripQuoted($sql)
162
// get the character for delimited id quotes,
163
// this is usually " but in MySQL is `
164
$d = $this->_adapter->quoteIdentifier('a');
167
// get the value used as an escaped delimited id quote,
168
// e.g. \" or "" or \`
169
$de = $this->_adapter->quoteIdentifier($d);
170
$de = substr($de, 1, 2);
171
$de = str_replace('\\', '\\\\', $de);
173
// get the character for value quoting
175
$q = $this->_adapter->quote('a');
178
// get the value used as an escaped quote,
180
$qe = $this->_adapter->quote($q);
181
$qe = substr($qe, 1, 2);
182
$qe = str_replace('\\', '\\\\', $qe);
184
// get a version of the SQL statement with all quoted
185
// values and delimited identifiers stripped out
187
$sql = preg_replace("/$q($qe|\\\\{2}|[^$q])*$q/", '', $sql);
190
$sql = preg_replace("/$q($qe|[^$q])*$q/", '', $sql);
197
* Bind a column of the statement result set to a PHP variable.
199
* @param string $column Name the column in the result set, either by
200
* position or by name.
201
* @param mixed $param Reference to the PHP variable containing the value.
202
* @param mixed $type OPTIONAL
205
public function bindColumn($column, &$param, $type = null)
207
$this->_bindColumn[$column] =& $param;
212
* Binds a parameter to the specified variable name.
214
* @param mixed $parameter Name the parameter, either integer or string.
215
* @param mixed $variable Reference to PHP variable containing the value.
216
* @param mixed $type OPTIONAL Datatype of SQL parameter.
217
* @param mixed $length OPTIONAL Length of SQL parameter.
218
* @param mixed $options OPTIONAL Other options.
221
public function bindParam($parameter, &$variable, $type = null, $length = null, $options = null)
223
if (!is_int($parameter) && !is_string($parameter)) {
225
* @see Zend_Db_Statement_Exception
227
require_once 'Zend/Db/Statement/Exception.php';
228
throw new Zend_Db_Statement_Exception('Invalid bind-variable position');
232
if (($intval = (int) $parameter) > 0 && $this->_adapter->supportsParameters('positional')) {
233
if ($intval >= 1 || $intval <= count($this->_sqlParam)) {
236
} else if ($this->_adapter->supportsParameters('named')) {
237
if ($parameter[0] != ':') {
238
$parameter = ':' . $parameter;
240
if (in_array($parameter, $this->_sqlParam) !== false) {
241
$position = $parameter;
245
if ($position === null) {
247
* @see Zend_Db_Statement_Exception
249
require_once 'Zend/Db/Statement/Exception.php';
250
throw new Zend_Db_Statement_Exception("Invalid bind-variable position '$parameter'");
253
// Finally we are assured that $position is valid
254
$this->_bindParam[$position] =& $variable;
255
return $this->_bindParam($position, $variable, $type, $length, $options);
259
* Binds a value to a parameter.
261
* @param mixed $parameter Name the parameter, either integer or string.
262
* @param mixed $value Scalar value to bind to the parameter.
263
* @param mixed $type OPTIONAL Datatype of the parameter.
266
public function bindValue($parameter, $value, $type = null)
268
return $this->bindParam($parameter, $value, $type);
272
* Executes a prepared statement.
274
* @param array $params OPTIONAL Values to bind to parameter placeholders.
277
public function execute(array $params = null)
280
* Simple case - no query profiler to manage.
282
if ($this->_queryId === null) {
283
return $this->_execute($params);
287
* Do the same thing, but with query profiler
288
* management before and after the execute.
290
$prof = $this->_adapter->getProfiler();
291
$qp = $prof->getQueryProfile($this->_queryId);
292
if ($qp->hasEnded()) {
293
$this->_queryId = $prof->queryClone($qp);
294
$qp = $prof->getQueryProfile($this->_queryId);
296
if ($params !== null) {
297
$qp->bindParams($params);
299
$qp->bindParams($this->_bindParam);
301
$qp->start($this->_queryId);
303
$retval = $this->_execute($params);
305
$prof->queryEnd($this->_queryId);
311
* Returns an array containing all of the result set rows.
313
* @param int $style OPTIONAL Fetch mode.
314
* @param int $col OPTIONAL Column number, if fetch mode is by column.
315
* @return array Collection of rows, each in a format by the fetch mode.
317
public function fetchAll($style = null, $col = null)
320
if ($style === Zend_Db::FETCH_COLUMN && $col === null) {
324
while ($row = $this->fetch($style)) {
328
while ($val = $this->fetchColumn($col)) {
336
* Returns a single column from the next row of a result set.
338
* @param int $col OPTIONAL Position of the column to fetch.
339
* @return string One value from the next row of result set, or false.
341
public function fetchColumn($col = 0)
345
$row = $this->fetch(Zend_Db::FETCH_NUM);
346
if (!is_array($row)) {
353
* Fetches the next row and returns it as an object.
355
* @param string $class OPTIONAL Name of the class to create.
356
* @param array $config OPTIONAL Constructor arguments for the class.
357
* @return mixed One object instance of the specified class, or false.
359
public function fetchObject($class = 'stdClass', array $config = array())
361
$obj = new $class($config);
362
$row = $this->fetch(Zend_Db::FETCH_ASSOC);
363
if (!is_array($row)) {
366
foreach ($row as $key => $val) {
373
* Retrieve a statement attribute.
375
* @param string $key Attribute name.
376
* @return mixed Attribute value.
378
public function getAttribute($key)
380
if (array_key_exists($key, $this->_attribute)) {
381
return $this->_attribute[$key];
386
* Set a statement attribute.
388
* @param string $key Attribute name.
389
* @param mixed $val Attribute value.
392
public function setAttribute($key, $val)
394
$this->_attribute[$key] = $val;
398
* Set the default fetch mode for this statement.
400
* @param int $mode The fetch mode.
402
* @throws Zend_Db_Statement_Exception
404
public function setFetchMode($mode)
407
case Zend_Db::FETCH_NUM:
408
case Zend_Db::FETCH_ASSOC:
409
case Zend_Db::FETCH_BOTH:
410
case Zend_Db::FETCH_OBJ:
411
$this->_fetchMode = $mode;
413
case Zend_Db::FETCH_BOUND:
415
$this->closeCursor();
417
* @see Zend_Db_Statement_Exception
419
require_once 'Zend/Db/Statement/Exception.php';
420
throw new Zend_Db_Statement_Exception('invalid fetch mode');
426
* Helper function to map retrieved row
427
* to bound column variables
432
public function _fetchBound($row)
434
foreach ($row as $key => $value) {
435
// bindColumn() takes 1-based integer positions
436
// but fetch() returns 0-based integer indexes
440
// set results only to variables that were bound previously
441
if (isset($this->_bindColumn[$key])) {
442
$this->_bindColumn[$key] = $value;