Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
Mysql.php
Go to the documentation of this file.
1 <?php
27 #require_once 'Zend/Db/Adapter/Pdo/Abstract.php';
28 
29 
40 {
41 
47  protected $_pdoType = 'mysql';
48 
60  protected $_numericDataTypes = array(
64  'INT' => Zend_Db::INT_TYPE,
65  'INTEGER' => Zend_Db::INT_TYPE,
66  'MEDIUMINT' => Zend_Db::INT_TYPE,
67  'SMALLINT' => Zend_Db::INT_TYPE,
68  'TINYINT' => Zend_Db::INT_TYPE,
69  'BIGINT' => Zend_Db::BIGINT_TYPE,
70  'SERIAL' => Zend_Db::BIGINT_TYPE,
71  'DEC' => Zend_Db::FLOAT_TYPE,
72  'DECIMAL' => Zend_Db::FLOAT_TYPE,
73  'DOUBLE' => Zend_Db::FLOAT_TYPE,
74  'DOUBLE PRECISION' => Zend_Db::FLOAT_TYPE,
75  'FIXED' => Zend_Db::FLOAT_TYPE,
76  'FLOAT' => Zend_Db::FLOAT_TYPE
77  );
78 
83  protected function _dsn()
84  {
85  $dsn = parent::_dsn();
86  if (isset($this->_config['charset'])) {
87  $dsn .= ';charset=' . $this->_config['charset'];
88  }
89  return $dsn;
90  }
91 
98  protected function _connect()
99  {
100  if ($this->_connection) {
101  return;
102  }
103 
104  if (!empty($this->_config['charset'])
105  && version_compare(PHP_VERSION, '5.3.6', '<')
106  ) {
107  $initCommand = "SET NAMES '" . $this->_config['charset'] . "'";
108  $this->_config['driver_options'][1002] = $initCommand; // 1002 = PDO::MYSQL_ATTR_INIT_COMMAND
109  }
110 
111  parent::_connect();
112  }
113 
117  public function getQuoteIdentifierSymbol()
118  {
119  return "`";
120  }
121 
127  public function listTables()
128  {
129  return $this->fetchCol('SHOW TABLES');
130  }
131 
160  public function describeTable($tableName, $schemaName = null)
161  {
162  // @todo use INFORMATION_SCHEMA someday when MySQL's
163  // implementation has reasonably good performance and
164  // the version with this improvement is in wide use.
165 
166  if ($schemaName) {
167  $sql = 'DESCRIBE ' . $this->quoteIdentifier("$schemaName.$tableName", true);
168  } else {
169  $sql = 'DESCRIBE ' . $this->quoteIdentifier($tableName, true);
170  }
171  $stmt = $this->query($sql);
172 
173  // Use FETCH_NUM so we are not dependent on the CASE attribute of the PDO connection
174  $result = $stmt->fetchAll(Zend_Db::FETCH_NUM);
175 
176  $field = 0;
177  $type = 1;
178  $null = 2;
179  $key = 3;
180  $default = 4;
181  $extra = 5;
182 
183  $desc = array();
184  $i = 1;
185  $p = 1;
186  foreach ($result as $row) {
187  list($length, $scale, $precision, $unsigned, $primary, $primaryPosition, $identity)
188  = array(null, null, null, null, false, null, false);
189  if (preg_match('/unsigned/', $row[$type])) {
190  $unsigned = true;
191  }
192  if (preg_match('/^((?:var)?char)\((\d+)\)/', $row[$type], $matches)) {
193  $row[$type] = $matches[1];
194  $length = $matches[2];
195  } else if (preg_match('/^decimal\((\d+),(\d+)\)/', $row[$type], $matches)) {
196  $row[$type] = 'decimal';
197  $precision = $matches[1];
198  $scale = $matches[2];
199  } else if (preg_match('/^float\((\d+),(\d+)\)/', $row[$type], $matches)) {
200  $row[$type] = 'float';
201  $precision = $matches[1];
202  $scale = $matches[2];
203  } else if (preg_match('/^((?:big|medium|small|tiny)?int)\((\d+)\)/', $row[$type], $matches)) {
204  $row[$type] = $matches[1];
205  // The optional argument of a MySQL int type is not precision
206  // or length; it is only a hint for display width.
207  }
208  if (strtoupper($row[$key]) == 'PRI') {
209  $primary = true;
210  $primaryPosition = $p;
211  if ($row[$extra] == 'auto_increment') {
212  $identity = true;
213  } else {
214  $identity = false;
215  }
216  ++$p;
217  }
218  $desc[$this->foldCase($row[$field])] = array(
219  'SCHEMA_NAME' => null, // @todo
220  'TABLE_NAME' => $this->foldCase($tableName),
221  'COLUMN_NAME' => $this->foldCase($row[$field]),
222  'COLUMN_POSITION' => $i,
223  'DATA_TYPE' => $row[$type],
224  'DEFAULT' => $row[$default],
225  'NULLABLE' => (bool) ($row[$null] == 'YES'),
226  'LENGTH' => $length,
227  'SCALE' => $scale,
228  'PRECISION' => $precision,
229  'UNSIGNED' => $unsigned,
230  'PRIMARY' => $primary,
231  'PRIMARY_POSITION' => $primaryPosition,
232  'IDENTITY' => $identity
233  );
234  ++$i;
235  }
236  return $desc;
237  }
238 
248  public function limit($sql, $count, $offset = 0)
249  {
250  $count = intval($count);
251  if ($count <= 0) {
253  #require_once 'Zend/Db/Adapter/Exception.php';
254  throw new Zend_Db_Adapter_Exception("LIMIT argument count=$count is not valid");
255  }
256 
257  $offset = intval($offset);
258  if ($offset < 0) {
260  #require_once 'Zend/Db/Adapter/Exception.php';
261  throw new Zend_Db_Adapter_Exception("LIMIT argument offset=$offset is not valid");
262  }
263 
264  $sql .= " LIMIT $count";
265  if ($offset > 0) {
266  $sql .= " OFFSET $offset";
267  }
268 
269  return $sql;
270  }
271 
272 }
$tableName
Definition: trigger.php:13
fetchCol($sql, $bind=array())
Definition: Abstract.php:792
const BIGINT_TYPE
Definition: Db.php:69
const INT_TYPE
Definition: Db.php:68
$count
Definition: recent.phtml:13
const FLOAT_TYPE
Definition: Db.php:70
describeTable($tableName, $schemaName=null)
Definition: Mysql.php:160
$type
Definition: item.phtml:13
const FETCH_NUM
Definition: Db.php:153
limit($sql, $count, $offset=0)
Definition: Mysql.php:248
quoteIdentifier($ident, $auto=false)
Definition: Abstract.php:959
$i
Definition: gallery.phtml:31
query($sql, $bind=array())
Definition: Abstract.php:221
if( $_orders &&count( $_orders))( 'Orders') ?></caption >< thead >< tr >< th scopeforeach( $_orders as $_order)(__( 'Order #')) ?>" class $extra
Definition: history.phtml:32