Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
Public Member Functions | Protected Member Functions | Protected Attributes
Zend_Db_Statement Class Reference
Inheritance diagram for Zend_Db_Statement:
Zend_Db_Statement_Interface Zend_Db_Statement_Db2 Zend_Db_Statement_Mysqli Zend_Db_Statement_Oracle Zend_Db_Statement_Pdo Zend_Db_Statement_Sqlsrv Mysql Zend_Db_Statement_Pdo_Ibm Zend_Db_Statement_Pdo_Oci

Public Member Functions

 __construct ($adapter, $sql)
 
 bindColumn ($column, &$param, $type=null)
 
 bindParam ($parameter, &$variable, $type=null, $length=null, $options=null)
 
 bindValue ($parameter, $value, $type=null)
 
 execute (array $params=null)
 
 fetchAll ($style=null, $col=null)
 
 fetchColumn ($col=0)
 
 fetchObject ($class='stdClass', array $config=array())
 
 getAttribute ($key)
 
 setAttribute ($key, $val)
 
 setFetchMode ($mode)
 
 _fetchBound ($row)
 
 getAdapter ()
 
 getDriverStatement ()
 
- Public Member Functions inherited from Zend_Db_Statement_Interface
 closeCursor ()
 
 columnCount ()
 
 errorCode ()
 
 errorInfo ()
 
 fetch ($style=null, $cursor=null, $offset=null)
 
 nextRowset ()
 
 rowCount ()
 

Protected Member Functions

 _prepare ($sql)
 
 _parseParameters ($sql)
 
 _stripQuoted ($sql)
 

Protected Attributes

 $_stmt = null
 
 $_adapter = null
 
 $_fetchMode = Zend_Db::FETCH_ASSOC
 
 $_attribute = array()
 
 $_bindColumn = array()
 
 $_bindParam = array()
 
 $_sqlSplit = array()
 
 $_sqlParam = array()
 
 $_queryId = null
 

Detailed Description

Definition at line 42 of file Statement.php.

Constructor & Destructor Documentation

◆ __construct()

__construct (   $adapter,
  $sql 
)

Constructor for a statement.

Parameters
Zend_Db_Adapter_Abstract$adapter
mixed$sqlEither a string or Zend_Db_Select.

Definition at line 108 of file Statement.php.

109  {
110  $this->_adapter = $adapter;
111  if ($sql instanceof Zend_Db_Select) {
112  $sql = $sql->assemble();
113  }
114  $this->_parseParameters($sql);
115  $this->_prepare($sql);
116 
117  $this->_queryId = $this->_adapter->getProfiler()->queryStart($sql);
118  }
$adapter
Definition: webapi_user.php:16
_parseParameters($sql)
Definition: Statement.php:135

Member Function Documentation

◆ _fetchBound()

_fetchBound (   $row)

Helper function to map retrieved row to bound column variables

Parameters
array$row
Returns
bool True

Definition at line 452 of file Statement.php.

453  {
454  foreach ($row as $key => $value) {
455  // bindColumn() takes 1-based integer positions
456  // but fetch() returns 0-based integer indexes
457  if (is_int($key)) {
458  $key++;
459  }
460  // set results only to variables that were bound previously
461  if (isset($this->_bindColumn[$key])) {
462  $this->_bindColumn[$key] = $value;
463  }
464  }
465  return true;
466  }
$value
Definition: gender.phtml:16

◆ _parseParameters()

_parseParameters (   $sql)
protected
Parameters
string$sql
Returns
void
See also
Zend_Db_Statement_Exception
Zend_Db_Statement_Exception

Definition at line 135 of file Statement.php.

136  {
137  $sql = $this->_stripQuoted($sql);
138 
139  // split into text and params
140  $this->_sqlSplit = preg_split('/(\?|\:[a-zA-Z0-9_]+)/',
141  $sql, -1, PREG_SPLIT_DELIM_CAPTURE|PREG_SPLIT_NO_EMPTY);
142 
143  // map params
144  $this->_sqlParam = array();
145  foreach ($this->_sqlSplit as $key => $val) {
146  if ($val == '?') {
147  if ($this->_adapter->supportsParameters('positional') === false) {
151  #require_once 'Zend/Db/Statement/Exception.php';
152  throw new Zend_Db_Statement_Exception("Invalid bind-variable position '$val'");
153  }
154  } else if ($val[0] == ':') {
155  if ($this->_adapter->supportsParameters('named') === false) {
159  #require_once 'Zend/Db/Statement/Exception.php';
160  throw new Zend_Db_Statement_Exception("Invalid bind-variable name '$val'");
161  }
162  }
163  $this->_sqlParam[] = $val;
164  }
165 
166  // set up for binding
167  $this->_bindParam = array();
168  }

◆ _prepare()

_prepare (   $sql)
protected

Internal method called by abstract statment constructor to setup the driver level statement

Returns
void

Definition at line 126 of file Statement.php.

127  {
128  return;
129  }

◆ _stripQuoted()

_stripQuoted (   $sql)
protected

Remove parts of a SQL string that contain quoted strings of values or identifiers.

Parameters
string$sql
Returns
string

Definition at line 177 of file Statement.php.

178  {
179 
180  // get the character for value quoting
181  // this should be '
182  $q = $this->_adapter->quote('a');
183  $q = $q[0];
184  // get the value used as an escaped quote,
185  // e.g. \' or ''
186  $qe = $this->_adapter->quote($q);
187  $qe = substr($qe, 1, 2);
188  $qe = preg_quote($qe);
189  $escapeChar = substr($qe,0,1);
190  // remove 'foo\'bar'
191  if (!empty($q)) {
192  $escapeChar = preg_quote($escapeChar);
193  // this segfaults only after 65,000 characters instead of 9,000
194  $sql = preg_replace("/$q([^$q{$escapeChar}]*|($qe)*)*$q/s", '', $sql);
195  }
196 
197  // get a version of the SQL statement with all quoted
198  // values and delimited identifiers stripped out
199  // remove "foo\"bar"
200  $sql = preg_replace("/\"(\\\\\"|[^\"])*\"/Us", '', $sql);
201 
202  // get the character for delimited id quotes,
203  // this is usually " but in MySQL is `
204  $d = $this->_adapter->quoteIdentifier('a');
205  $d = $d[0];
206  // get the value used as an escaped delimited id quote,
207  // e.g. \" or "" or \`
208  $de = $this->_adapter->quoteIdentifier($d);
209  $de = substr($de, 1, 2);
210  $de = preg_quote($de);
211  // Note: $de and $d where never used..., now they are:
212  $sql = preg_replace("/$d($de|\\\\{2}|[^$d])*$d/Us", '', $sql);
213  return $sql;
214  }

◆ bindColumn()

bindColumn (   $column,
$param,
  $type = null 
)

Bind a column of the statement result set to a PHP variable.

Parameters
string$columnName the column in the result set, either by position or by name.
mixed$paramReference to the PHP variable containing the value.
mixed$typeOPTIONAL
Returns
bool

Implements Zend_Db_Statement_Interface.

Definition at line 225 of file Statement.php.

226  {
227  $this->_bindColumn[$column] =& $param;
228  return true;
229  }

◆ bindParam()

bindParam (   $parameter,
$variable,
  $type = null,
  $length = null,
  $options = null 
)

Binds a parameter to the specified variable name.

Parameters
mixed$parameterName the parameter, either integer or string.
mixed$variableReference to PHP variable containing the value.
mixed$typeOPTIONAL Datatype of SQL parameter.
mixed$lengthOPTIONAL Length of SQL parameter.
mixed$optionsOPTIONAL Other options.
Returns
bool
See also
Zend_Db_Statement_Exception
Zend_Db_Statement_Exception

Implements Zend_Db_Statement_Interface.

Definition at line 241 of file Statement.php.

242  {
243  if (!is_int($parameter) && !is_string($parameter)) {
247  #require_once 'Zend/Db/Statement/Exception.php';
248  throw new Zend_Db_Statement_Exception('Invalid bind-variable position');
249  }
250 
251  $position = null;
252  if (($intval = (int) $parameter) > 0 && $this->_adapter->supportsParameters('positional')) {
253  if ($intval >= 1 || $intval <= count($this->_sqlParam)) {
254  $position = $intval;
255  }
256  } else if ($this->_adapter->supportsParameters('named')) {
257  if ($parameter[0] != ':') {
258  $parameter = ':' . $parameter;
259  }
260  if (in_array($parameter, $this->_sqlParam) !== false) {
261  $position = $parameter;
262  }
263  }
264 
265  if ($position === null) {
269  #require_once 'Zend/Db/Statement/Exception.php';
270  throw new Zend_Db_Statement_Exception("Invalid bind-variable position '$parameter'");
271  }
272 
273  // Finally we are assured that $position is valid
274  $this->_bindParam[$position] =& $variable;
275  return $this->_bindParam($position, $variable, $type, $length, $options);
276  }
$variable
Definition: variable.php:7
$type
Definition: item.phtml:13

◆ bindValue()

bindValue (   $parameter,
  $value,
  $type = null 
)

Binds a value to a parameter.

Parameters
mixed$parameterName the parameter, either integer or string.
mixed$valueScalar value to bind to the parameter.
mixed$typeOPTIONAL Datatype of the parameter.
Returns
bool

Implements Zend_Db_Statement_Interface.

Definition at line 286 of file Statement.php.

287  {
288  return $this->bindParam($parameter, $value, $type);
289  }
bindParam($parameter, &$variable, $type=null, $length=null, $options=null)
Definition: Statement.php:241
$type
Definition: item.phtml:13
$value
Definition: gender.phtml:16

◆ execute()

execute ( array  $params = null)

Executes a prepared statement.

Parameters
array$paramsOPTIONAL Values to bind to parameter placeholders.
Returns
bool

Implements Zend_Db_Statement_Interface.

Definition at line 297 of file Statement.php.

298  {
299  /*
300  * Simple case - no query profiler to manage.
301  */
302  if ($this->_queryId === null) {
303  return $this->_execute($params);
304  }
305 
306  /*
307  * Do the same thing, but with query profiler
308  * management before and after the execute.
309  */
310  $prof = $this->_adapter->getProfiler();
311  $qp = $prof->getQueryProfile($this->_queryId);
312  if ($qp->hasEnded()) {
313  $this->_queryId = $prof->queryClone($qp);
314  $qp = $prof->getQueryProfile($this->_queryId);
315  }
316  if ($params !== null) {
317  $qp->bindParams($params);
318  } else {
319  $qp->bindParams($this->_bindParam);
320  }
321  $qp->start($this->_queryId);
322 
323  $retval = $this->_execute($params);
324 
325  $prof->queryEnd($this->_queryId);
326 
327  return $retval;
328  }
$params[\Magento\Store\Model\StoreManager::PARAM_RUN_CODE]
Definition: website.php:18

◆ fetchAll()

fetchAll (   $style = null,
  $col = null 
)

Returns an array containing all of the result set rows.

Parameters
int$styleOPTIONAL Fetch mode.
int$colOPTIONAL Column number, if fetch mode is by column.
Returns
array Collection of rows, each in a format by the fetch mode.

Implements Zend_Db_Statement_Interface.

Definition at line 337 of file Statement.php.

338  {
339  $data = array();
340  if ($style === Zend_Db::FETCH_COLUMN && $col === null) {
341  $col = 0;
342  }
343  if ($col === null) {
344  while ($row = $this->fetch($style)) {
345  $data[] = $row;
346  }
347  } else {
348  while (false !== ($val = $this->fetchColumn($col))) {
349  $data[] = $val;
350  }
351  }
352  return $data;
353  }
fetchColumn($col=0)
Definition: Statement.php:361
const FETCH_COLUMN
Definition: Db.php:147
fetch($style=null, $cursor=null, $offset=null)

◆ fetchColumn()

fetchColumn (   $col = 0)

Returns a single column from the next row of a result set.

Parameters
int$colOPTIONAL Position of the column to fetch.
Returns
string One value from the next row of result set, or false.

Implements Zend_Db_Statement_Interface.

Definition at line 361 of file Statement.php.

362  {
363  $data = array();
364  $col = (int) $col;
365  $row = $this->fetch(Zend_Db::FETCH_NUM);
366  if (!is_array($row)) {
367  return false;
368  }
369  return $row[$col];
370  }
fetch($style=null, $cursor=null, $offset=null)
const FETCH_NUM
Definition: Db.php:153

◆ fetchObject()

fetchObject (   $class = 'stdClass',
array  $config = array() 
)

Fetches the next row and returns it as an object.

Parameters
string$classOPTIONAL Name of the class to create.
array$configOPTIONAL Constructor arguments for the class.
Returns
mixed One object instance of the specified class, or false.

Implements Zend_Db_Statement_Interface.

Definition at line 379 of file Statement.php.

380  {
381  $obj = new $class($config);
382  $row = $this->fetch(Zend_Db::FETCH_ASSOC);
383  if (!is_array($row)) {
384  return false;
385  }
386  foreach ($row as $key => $val) {
387  $obj->$key = $val;
388  }
389  return $obj;
390  }
$config
Definition: fraud_order.php:17
const FETCH_ASSOC
Definition: Db.php:142
fetch($style=null, $cursor=null, $offset=null)
$_option $_optionId $class
Definition: date.phtml:13

◆ getAdapter()

getAdapter ( )

Gets the Zend_Db_Adapter_Abstract for this particular Zend_Db_Statement object.

Returns
Zend_Db_Adapter_Abstract

Definition at line 474 of file Statement.php.

475  {
476  return $this->_adapter;
477  }

◆ getAttribute()

getAttribute (   $key)

Retrieve a statement attribute.

Parameters
string$keyAttribute name.
Returns
mixed Attribute value.

Implements Zend_Db_Statement_Interface.

Definition at line 398 of file Statement.php.

399  {
400  if (array_key_exists($key, $this->_attribute)) {
401  return $this->_attribute[$key];
402  }
403  }

◆ getDriverStatement()

getDriverStatement ( )

Gets the resource or object setup by the _parse

Returns
unknown_type

Definition at line 484 of file Statement.php.

485  {
486  return $this->_stmt;
487  }

◆ setAttribute()

setAttribute (   $key,
  $val 
)

Set a statement attribute.

Parameters
string$keyAttribute name.
mixed$valAttribute value.
Returns
bool

Implements Zend_Db_Statement_Interface.

Definition at line 412 of file Statement.php.

413  {
414  $this->_attribute[$key] = $val;
415  }

◆ setFetchMode()

setFetchMode (   $mode)

Set the default fetch mode for this statement.

Parameters
int$modeThe fetch mode.
Returns
bool
Exceptions
Zend_Db_Statement_Exception
See also
Zend_Db_Statement_Exception

Implements Zend_Db_Statement_Interface.

Definition at line 424 of file Statement.php.

425  {
426  switch ($mode) {
427  case Zend_Db::FETCH_NUM:
429  case Zend_Db::FETCH_BOTH:
430  case Zend_Db::FETCH_OBJ:
431  $this->_fetchMode = $mode;
432  break;
434  default:
435  $this->closeCursor();
439  #require_once 'Zend/Db/Statement/Exception.php';
440  throw new Zend_Db_Statement_Exception('invalid fetch mode');
441  break;
442  }
443  }
const FETCH_BOUND
Definition: Db.php:144
const FETCH_ASSOC
Definition: Db.php:142
const FETCH_BOTH
Definition: Db.php:143
const FETCH_NUM
Definition: Db.php:153
if($exist=($block->getProductCollection() && $block->getProductCollection() ->getSize())) $mode
Definition: grid.phtml:15
const FETCH_OBJ
Definition: Db.php:154

Field Documentation

◆ $_adapter

$_adapter = null
protected

Definition at line 53 of file Statement.php.

◆ $_attribute

$_attribute = array()
protected

Definition at line 67 of file Statement.php.

◆ $_bindColumn

$_bindColumn = array()
protected

Definition at line 74 of file Statement.php.

◆ $_bindParam

$_bindParam = array()
protected

Definition at line 81 of file Statement.php.

◆ $_fetchMode

$_fetchMode = Zend_Db::FETCH_ASSOC
protected

Definition at line 60 of file Statement.php.

◆ $_queryId

$_queryId = null
protected

Definition at line 100 of file Statement.php.

◆ $_sqlParam

$_sqlParam = array()
protected

Definition at line 95 of file Statement.php.

◆ $_sqlSplit

$_sqlSplit = array()
protected

Definition at line 88 of file Statement.php.

◆ $_stmt

$_stmt = null
protected

Definition at line 48 of file Statement.php.


The documentation for this class was generated from the following file: