Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
Sqlsrv.php
Go to the documentation of this file.
1 <?php
26 #require_once 'Zend/Db/Statement.php';
27 
38 {
39 
43  protected $_originalSQL;
44 
48  protected $_keys;
49 
53  protected $_executed = false;
54 
62  protected function _prepare($sql)
63  {
64  $connection = $this->_adapter->getConnection();
65 
66  $this->_stmt = sqlsrv_prepare($connection, $sql);
67 
68  if (!$this->_stmt) {
69  #require_once 'Zend/Db/Statement/Sqlsrv/Exception.php';
70  throw new Zend_Db_Statement_Sqlsrv_Exception(sqlsrv_errors());
71  }
72 
73  $this->_originalSQL = $sql;
74  }
75 
87  protected function _bindParam($parameter, &$variable, $type = null, $length = null, $options = null)
88  {
89  //Sql server doesn't support bind by name
90  return true;
91  }
92 
98  public function closeCursor()
99  {
100  if (!$this->_stmt) {
101  return false;
102  }
103 
104  sqlsrv_free_stmt($this->_stmt);
105  $this->_stmt = false;
106  return true;
107  }
108 
115  public function columnCount()
116  {
117  if ($this->_stmt && $this->_executed) {
118  return sqlsrv_num_fields($this->_stmt);
119  }
120 
121  return 0;
122  }
123 
124 
131  public function errorCode()
132  {
133  if (!$this->_stmt) {
134  return false;
135  }
136 
137  $error = sqlsrv_errors();
138  if (!$error) {
139  return false;
140  }
141 
142  return $error[0]['code'];
143  }
144 
145 
152  public function errorInfo()
153  {
154  if (!$this->_stmt) {
155  return false;
156  }
157 
158  $error = sqlsrv_errors();
159  if (!$error) {
160  return false;
161  }
162 
163  return array(
164  $error[0]['code'],
165  $error[0]['message'],
166  );
167  }
168 
169 
177  public function _execute(array $params = null)
178  {
179  $connection = $this->_adapter->getConnection();
180  if (!$this->_stmt) {
181  return false;
182  }
183 
184  if ($params !== null) {
185  if (!is_array($params)) {
186  $params = array($params);
187  }
188  $error = false;
189 
190  // make all params passed by reference
191  $params_ = array();
192  $temp = array();
193  $i = 1;
194  foreach ($params as $param) {
195  $temp[$i] = $param;
196  $params_[] = &$temp[$i];
197  $i++;
198  }
199  $params = $params_;
200  }
201 
202  $this->_stmt = sqlsrv_query($connection, $this->_originalSQL, $params);
203 
204  if (!$this->_stmt) {
205  #require_once 'Zend/Db/Statement/Sqlsrv/Exception.php';
206  throw new Zend_Db_Statement_Sqlsrv_Exception(sqlsrv_errors());
207  }
208 
209  $this->_executed = true;
210 
211  return (!$this->_stmt);
212  }
213 
223  public function fetch($style = null, $cursor = null, $offset = null)
224  {
225  if (!$this->_stmt) {
226  return false;
227  }
228 
229  if (null === $style) {
230  $style = $this->_fetchMode;
231  }
232 
233  $values = sqlsrv_fetch_array($this->_stmt, SQLSRV_FETCH_ASSOC);
234 
235  if (!$values && (null !== $error = sqlsrv_errors())) {
236  #require_once 'Zend/Db/Statement/Sqlsrv/Exception.php';
237  throw new Zend_Db_Statement_Sqlsrv_Exception($error);
238  }
239 
240  if (null === $values) {
241  return null;
242  }
243 
244  if (!$this->_keys) {
245  foreach ($values as $key => $value) {
246  $this->_keys[] = $this->_adapter->foldCase($key);
247  }
248  }
249 
250  $values = array_values($values);
251 
252  $row = false;
253  switch ($style) {
254  case Zend_Db::FETCH_NUM:
255  $row = $values;
256  break;
258  $row = array_combine($this->_keys, $values);
259  break;
260  case Zend_Db::FETCH_BOTH:
261  $assoc = array_combine($this->_keys, $values);
262  $row = array_merge($values, $assoc);
263  break;
264  case Zend_Db::FETCH_OBJ:
265  $row = (object) array_combine($this->_keys, $values);
266  break;
268  $assoc = array_combine($this->_keys, $values);
269  $row = array_merge($values, $assoc);
270  $row = $this->_fetchBound($row);
271  break;
272  default:
273  #require_once 'Zend/Db/Statement/Sqlsrv/Exception.php';
274  throw new Zend_Db_Statement_Sqlsrv_Exception("Invalid fetch mode '$style' specified");
275  break;
276  }
277 
278  return $row;
279  }
280 
288  public function fetchColumn($col = 0)
289  {
290  if (!$this->_stmt) {
291  return false;
292  }
293 
294  if (!sqlsrv_fetch($this->_stmt)) {
295  if (null !== $error = sqlsrv_errors()) {
296  #require_once 'Zend/Db/Statement/Sqlsrv/Exception.php';
297  throw new Zend_Db_Statement_Sqlsrv_Exception($error);
298  }
299 
300  // If no error, there is simply no record
301  return false;
302  }
303 
304  $data = sqlsrv_get_field($this->_stmt, $col); //0-based
305  if ($data === false) {
306  #require_once 'Zend/Db/Statement/Sqlsrv/Exception.php';
307  throw new Zend_Db_Statement_Sqlsrv_Exception(sqlsrv_errors());
308  }
309 
310  return $data;
311  }
312 
321  public function fetchObject($class = 'stdClass', array $config = array())
322  {
323  if (!$this->_stmt) {
324  return false;
325  }
326 
327  $obj = sqlsrv_fetch_object($this->_stmt);
328 
329  if ($error = sqlsrv_errors()) {
330  #require_once 'Zend/Db/Statement/Sqlsrv/Exception.php';
331  throw new Zend_Db_Statement_Sqlsrv_Exception($error);
332  }
333 
334  /* @todo XXX handle parameters */
335 
336  if (null === $obj) {
337  return false;
338  }
339 
340  return $obj;
341  }
342 
350  public function getColumnMeta($column)
351  {
352  $fields = sqlsrv_field_metadata($this->_stmt);
353 
354  if (!$fields) {
355  throw new Zend_Db_Statement_Sqlsrv_Exception('Column metadata can not be fetched');
356  }
357 
358  if (!isset($fields[$column])) {
359  throw new Zend_Db_Statement_Sqlsrv_Exception('Column index does not exist in statement');
360  }
361 
362  return $fields[$column];
363  }
364 
373  public function nextRowset()
374  {
375  if (sqlsrv_next_result($this->_stmt) === false) {
376  #require_once 'Zend/Db/Statement/Sqlsrv/Exception.php';
377  throw new Zend_Db_Statement_Sqlsrv_Exception(sqlsrv_errors());
378  }
379 
380  // reset column keys
381  $this->_keys = null;
382 
383  return true;
384  }
385 
394  public function rowCount()
395  {
396  if (!$this->_stmt) {
397  return false;
398  }
399 
400  if (!$this->_executed) {
401  return 0;
402  }
403 
404  $num_rows = sqlsrv_rows_affected($this->_stmt);
405 
406  // Strict check is necessary; 0 is a valid return value
407  if ($num_rows === false) {
408  #require_once 'Zend/Db/Statement/Sqlsrv/Exception.php';
409  throw new Zend_Db_Statement_Sqlsrv_Exception(sqlsrv_errors());
410  }
411 
412  return $num_rows;
413  }
414 
426  public function fetchAll($style = null, $col = null)
427  {
428  $data = parent::fetchAll($style, $col);
429  $results = array();
430  $remove = $this->_adapter->foldCase('ZEND_DB_ROWNUM');
431 
432  foreach ($data as $row) {
433  if (is_array($row) && array_key_exists($remove, $row)) {
434  unset($row[$remove]);
435  }
436  $results[] = $row;
437  }
438  return $results;
439  }
440 }
$results
Definition: popup.phtml:13
$config
Definition: fraud_order.php:17
const FETCH_BOUND
Definition: Db.php:144
const FETCH_ASSOC
Definition: Db.php:142
$fields
Definition: details.phtml:14
$values
Definition: options.phtml:88
fetch($style=null, $cursor=null, $offset=null)
Definition: Sqlsrv.php:223
_bindParam($parameter, &$variable, $type=null, $length=null, $options=null)
Definition: Sqlsrv.php:87
$variable
Definition: variable.php:7
$type
Definition: item.phtml:13
fetchAll($style=null, $col=null)
Definition: Sqlsrv.php:426
const FETCH_BOTH
Definition: Db.php:143
$_option $_optionId $class
Definition: date.phtml:13
$value
Definition: gender.phtml:16
const FETCH_NUM
Definition: Db.php:153
const FETCH_OBJ
Definition: Db.php:154
$connection
Definition: bulk.php:13
$params[\Magento\Store\Model\StoreManager::PARAM_RUN_CODE]
Definition: website.php:18
$i
Definition: gallery.phtml:31
_execute(array $params=null)
Definition: Sqlsrv.php:177
fetchObject($class='stdClass', array $config=array())
Definition: Sqlsrv.php:321