Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
Statement.php
Go to the documentation of this file.
1 <?php
26 #require_once 'Zend/Db.php';
27 
31 #require_once 'Zend/Db/Statement/Interface.php';
32 
43 {
44 
48  protected $_stmt = null;
49 
53  protected $_adapter = null;
54 
61 
67  protected $_attribute = array();
68 
74  protected $_bindColumn = array();
75 
81  protected $_bindParam = array();
82 
88  protected $_sqlSplit = array();
89 
95  protected $_sqlParam = array();
96 
100  protected $_queryId = null;
101 
108  public function __construct($adapter, $sql)
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  }
119 
126  protected function _prepare($sql)
127  {
128  return;
129  }
130 
135  protected function _parseParameters($sql)
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  }
169 
177  protected function _stripQuoted($sql)
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  }
215 
225  public function bindColumn($column, &$param, $type = null)
226  {
227  $this->_bindColumn[$column] =& $param;
228  return true;
229  }
230 
241  public function bindParam($parameter, &$variable, $type = null, $length = null, $options = null)
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  }
277 
286  public function bindValue($parameter, $value, $type = null)
287  {
288  return $this->bindParam($parameter, $value, $type);
289  }
290 
297  public function execute(array $params = null)
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  }
329 
337  public function fetchAll($style = null, $col = null)
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  }
354 
361  public function fetchColumn($col = 0)
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  }
371 
379  public function fetchObject($class = 'stdClass', array $config = array())
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  }
391 
398  public function getAttribute($key)
399  {
400  if (array_key_exists($key, $this->_attribute)) {
401  return $this->_attribute[$key];
402  }
403  }
404 
412  public function setAttribute($key, $val)
413  {
414  $this->_attribute[$key] = $val;
415  }
416 
424  public function setFetchMode($mode)
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  }
444 
452  public function _fetchBound($row)
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  }
467 
474  public function getAdapter()
475  {
476  return $this->_adapter;
477  }
478 
484  public function getDriverStatement()
485  {
486  return $this->_stmt;
487  }
488 }
fetchColumn($col=0)
Definition: Statement.php:361
$config
Definition: fraud_order.php:17
bindParam($parameter, &$variable, $type=null, $length=null, $options=null)
Definition: Statement.php:241
const FETCH_BOUND
Definition: Db.php:144
fetchObject($class='stdClass', array $config=array())
Definition: Statement.php:379
const FETCH_ASSOC
Definition: Db.php:142
const FETCH_COLUMN
Definition: Db.php:147
execute(array $params=null)
Definition: Statement.php:297
$adapter
Definition: webapi_user.php:16
$variable
Definition: variable.php:7
$type
Definition: item.phtml:13
const FETCH_BOTH
Definition: Db.php:143
fetchAll($style=null, $col=null)
Definition: Statement.php:337
fetch($style=null, $cursor=null, $offset=null)
bindColumn($column, &$param, $type=null)
Definition: Statement.php:225
$_option $_optionId $class
Definition: date.phtml:13
$value
Definition: gender.phtml:16
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
setAttribute($key, $val)
Definition: Statement.php:412
_parseParameters($sql)
Definition: Statement.php:135
bindValue($parameter, $value, $type=null)
Definition: Statement.php:286
$params[\Magento\Store\Model\StoreManager::PARAM_RUN_CODE]
Definition: website.php:18
__construct($adapter, $sql)
Definition: Statement.php:108