Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
Mysqli.php
Go to the documentation of this file.
1 <?php
27 #require_once 'Zend/Db/Adapter/Abstract.php';
28 
32 #require_once 'Zend/Db/Profiler.php';
33 
37 #require_once 'Zend/Db/Select.php';
38 
42 #require_once 'Zend/Db/Statement/Mysqli.php';
43 
44 
53 {
54 
66  protected $_numericDataTypes = array(
70  'INT' => Zend_Db::INT_TYPE,
71  'INTEGER' => Zend_Db::INT_TYPE,
72  'MEDIUMINT' => Zend_Db::INT_TYPE,
73  'SMALLINT' => Zend_Db::INT_TYPE,
74  'TINYINT' => Zend_Db::INT_TYPE,
75  'BIGINT' => Zend_Db::BIGINT_TYPE,
76  'SERIAL' => Zend_Db::BIGINT_TYPE,
77  'DEC' => Zend_Db::FLOAT_TYPE,
78  'DECIMAL' => Zend_Db::FLOAT_TYPE,
79  'DOUBLE' => Zend_Db::FLOAT_TYPE,
80  'DOUBLE PRECISION' => Zend_Db::FLOAT_TYPE,
81  'FIXED' => Zend_Db::FLOAT_TYPE,
82  'FLOAT' => Zend_Db::FLOAT_TYPE
83  );
84 
88  protected $_stmt = null;
89 
95  protected $_defaultStmtClass = 'Zend_Db_Statement_Mysqli';
96 
104  protected function _quote($value)
105  {
106  if (is_int($value) || is_float($value)) {
107  return $value;
108  }
109  $this->_connect();
110  return "'" . $this->_connection->real_escape_string($value) . "'";
111  }
112 
118  public function getQuoteIdentifierSymbol()
119  {
120  return "`";
121  }
122 
128  public function listTables()
129  {
130  $result = array();
131  // Use mysqli extension API, because SHOW doesn't work
132  // well as a prepared statement on MySQL 4.1.
133  $sql = 'SHOW TABLES';
134  if ($queryResult = $this->getConnection()->query($sql)) {
135  while ($row = $queryResult->fetch_row()) {
136  $result[] = $row[0];
137  }
138  $queryResult->close();
139  } else {
143  #require_once 'Zend/Db/Adapter/Mysqli/Exception.php';
144  throw new Zend_Db_Adapter_Mysqli_Exception($this->getConnection()->error);
145  }
146  return $result;
147  }
148 
177  public function describeTable($tableName, $schemaName = null)
178  {
184  if ($schemaName) {
185  $sql = 'DESCRIBE ' . $this->quoteIdentifier("$schemaName.$tableName", true);
186  } else {
187  $sql = 'DESCRIBE ' . $this->quoteIdentifier($tableName, true);
188  }
189 
194  if ($queryResult = $this->getConnection()->query($sql)) {
195  while ($row = $queryResult->fetch_assoc()) {
196  $result[] = $row;
197  }
198  $queryResult->close();
199  } else {
203  #require_once 'Zend/Db/Adapter/Mysqli/Exception.php';
204  throw new Zend_Db_Adapter_Mysqli_Exception($this->getConnection()->error);
205  }
206 
207  $desc = array();
208 
209  $row_defaults = array(
210  'Length' => null,
211  'Scale' => null,
212  'Precision' => null,
213  'Unsigned' => null,
214  'Primary' => false,
215  'PrimaryPosition' => null,
216  'Identity' => false
217  );
218  $i = 1;
219  $p = 1;
220  foreach ($result as $key => $row) {
221  $row = array_merge($row_defaults, $row);
222  if (preg_match('/unsigned/', $row['Type'])) {
223  $row['Unsigned'] = true;
224  }
225  if (preg_match('/^((?:var)?char)\((\d+)\)/', $row['Type'], $matches)) {
226  $row['Type'] = $matches[1];
227  $row['Length'] = $matches[2];
228  } else if (preg_match('/^decimal\((\d+),(\d+)\)/', $row['Type'], $matches)) {
229  $row['Type'] = 'decimal';
230  $row['Precision'] = $matches[1];
231  $row['Scale'] = $matches[2];
232  } else if (preg_match('/^float\((\d+),(\d+)\)/', $row['Type'], $matches)) {
233  $row['Type'] = 'float';
234  $row['Precision'] = $matches[1];
235  $row['Scale'] = $matches[2];
236  } else if (preg_match('/^((?:big|medium|small|tiny)?int)\((\d+)\)/', $row['Type'], $matches)) {
237  $row['Type'] = $matches[1];
242  }
243  if (strtoupper($row['Key']) == 'PRI') {
244  $row['Primary'] = true;
245  $row['PrimaryPosition'] = $p;
246  if ($row['Extra'] == 'auto_increment') {
247  $row['Identity'] = true;
248  } else {
249  $row['Identity'] = false;
250  }
251  ++$p;
252  }
253  $desc[$this->foldCase($row['Field'])] = array(
254  'SCHEMA_NAME' => null, // @todo
255  'TABLE_NAME' => $this->foldCase($tableName),
256  'COLUMN_NAME' => $this->foldCase($row['Field']),
257  'COLUMN_POSITION' => $i,
258  'DATA_TYPE' => $row['Type'],
259  'DEFAULT' => $row['Default'],
260  'NULLABLE' => (bool) ($row['Null'] == 'YES'),
261  'LENGTH' => $row['Length'],
262  'SCALE' => $row['Scale'],
263  'PRECISION' => $row['Precision'],
264  'UNSIGNED' => $row['Unsigned'],
265  'PRIMARY' => $row['Primary'],
266  'PRIMARY_POSITION' => $row['PrimaryPosition'],
267  'IDENTITY' => $row['Identity']
268  );
269  ++$i;
270  }
271  return $desc;
272  }
273 
280  protected function _connect()
281  {
282  if ($this->_connection) {
283  return;
284  }
285 
286  if (!extension_loaded('mysqli')) {
290  #require_once 'Zend/Db/Adapter/Mysqli/Exception.php';
291  throw new Zend_Db_Adapter_Mysqli_Exception('The Mysqli extension is required for this adapter but the extension is not loaded');
292  }
293 
294  if (isset($this->_config['port'])) {
295  $port = (integer) $this->_config['port'];
296  } else {
297  $port = null;
298  }
299 
300  if (isset($this->_config['socket'])) {
301  $socket = $this->_config['socket'];
302  } else {
303  $socket = null;
304  }
305 
306  $this->_connection = mysqli_init();
307 
308  if(!empty($this->_config['driver_options'])) {
309  foreach($this->_config['driver_options'] as $option=>$value) {
310  if(is_string($option)) {
311  // Suppress warnings here
312  // Ignore it if it's not a valid constant
313  $option = @constant(strtoupper($option));
314  if($option === null)
315  continue;
316  }
317  mysqli_options($this->_connection, $option, $value);
318  }
319  }
320 
321  // Suppress connection warnings here.
322  // Throw an exception instead.
323  $_isConnected = @mysqli_real_connect(
324  $this->_connection,
325  $this->_config['host'],
326  $this->_config['username'],
327  $this->_config['password'],
328  $this->_config['dbname'],
329  $port,
330  $socket
331  );
332 
333  if ($_isConnected === false || mysqli_connect_errno()) {
334 
335  $this->closeConnection();
339  #require_once 'Zend/Db/Adapter/Mysqli/Exception.php';
340  throw new Zend_Db_Adapter_Mysqli_Exception(mysqli_connect_error());
341  }
342 
343  if (!empty($this->_config['charset'])) {
344  mysqli_set_charset($this->_connection, $this->_config['charset']);
345  }
346  }
347 
353  public function isConnected()
354  {
355  return ((bool) ($this->_connection instanceof mysqli));
356  }
357 
363  public function closeConnection()
364  {
365  if ($this->isConnected()) {
366  $this->_connection->close();
367  }
368  $this->_connection = null;
369  }
370 
377  public function prepare($sql)
378  {
379  $this->_connect();
380  if ($this->_stmt) {
381  $this->_stmt->close();
382  }
383  $stmtClass = $this->_defaultStmtClass;
384  if (!class_exists($stmtClass)) {
385  #require_once 'Zend/Loader.php';
386  Zend_Loader::loadClass($stmtClass);
387  }
388  $stmt = new $stmtClass($this, $sql);
389  if ($stmt === false) {
390  return false;
391  }
392  $stmt->setFetchMode($this->_fetchMode);
393  $this->_stmt = $stmt;
394  return $stmt;
395  }
396 
414  public function lastInsertId($tableName = null, $primaryKey = null)
415  {
416  $mysqli = $this->_connection;
417  return (string) $mysqli->insert_id;
418  }
419 
425  protected function _beginTransaction()
426  {
427  $this->_connect();
428  $this->_connection->autocommit(false);
429  }
430 
436  protected function _commit()
437  {
438  $this->_connect();
439  $this->_connection->commit();
440  $this->_connection->autocommit(true);
441  }
442 
448  protected function _rollBack()
449  {
450  $this->_connect();
451  $this->_connection->rollback();
452  $this->_connection->autocommit(true);
453  }
454 
462  public function setFetchMode($mode)
463  {
464  switch ($mode) {
465  case Zend_Db::FETCH_LAZY:
467  case Zend_Db::FETCH_NUM:
468  case Zend_Db::FETCH_BOTH:
470  case Zend_Db::FETCH_OBJ:
471  $this->_fetchMode = $mode;
472  break;
473  case Zend_Db::FETCH_BOUND: // bound to PHP variable
477  #require_once 'Zend/Db/Adapter/Mysqli/Exception.php';
478  throw new Zend_Db_Adapter_Mysqli_Exception('FETCH_BOUND is not supported yet');
479  break;
480  default:
484  #require_once 'Zend/Db/Adapter/Mysqli/Exception.php';
485  throw new Zend_Db_Adapter_Mysqli_Exception("Invalid fetch mode '$mode' specified");
486  }
487  }
488 
497  public function limit($sql, $count, $offset = 0)
498  {
499  $count = intval($count);
500  if ($count <= 0) {
504  #require_once 'Zend/Db/Adapter/Mysqli/Exception.php';
505  throw new Zend_Db_Adapter_Mysqli_Exception("LIMIT argument count=$count is not valid");
506  }
507 
508  $offset = intval($offset);
509  if ($offset < 0) {
513  #require_once 'Zend/Db/Adapter/Mysqli/Exception.php';
514  throw new Zend_Db_Adapter_Mysqli_Exception("LIMIT argument offset=$offset is not valid");
515  }
516 
517  $sql .= " LIMIT $count";
518  if ($offset > 0) {
519  $sql .= " OFFSET $offset";
520  }
521 
522  return $sql;
523  }
524 
531  public function supportsParameters($type)
532  {
533  switch ($type) {
534  case 'positional':
535  return true;
536  case 'named':
537  default:
538  return false;
539  }
540  }
541 
547  public function getServerVersion()
548  {
549  $this->_connect();
550  $version = $this->_connection->server_version;
551  $major = (int) ($version / 10000);
552  $minor = (int) ($version % 10000 / 100);
553  $revision = (int) ($version % 100);
554  return $major . '.' . $minor . '.' . $revision;
555  }
556 }
describeTable($tableName, $schemaName=null)
Definition: Mysqli.php:177
$tableName
Definition: trigger.php:13
lastInsertId($tableName=null, $primaryKey=null)
Definition: Mysqli.php:414
const FETCH_LAZY
Definition: Db.php:151
limit($sql, $count, $offset=0)
Definition: Mysqli.php:497
static loadClass($class, $dirs=null)
Definition: Loader.php:52
const BIGINT_TYPE
Definition: Db.php:69
const INT_TYPE
Definition: Db.php:68
$count
Definition: recent.phtml:13
const FETCH_BOUND
Definition: Db.php:144
const FETCH_ASSOC
Definition: Db.php:142
const FLOAT_TYPE
Definition: Db.php:70
const FETCH_NAMED
Definition: Db.php:152
$type
Definition: item.phtml:13
supportsParameters($type)
Definition: Mysqli.php:531
const FETCH_BOTH
Definition: Db.php:143
$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
query($sql, $bind=array())
Definition: Abstract.php:457
quoteIdentifier($ident, $auto=false)
Definition: Abstract.php:959
$i
Definition: gallery.phtml:31