Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
Table.php
Go to the documentation of this file.
1 <?php
7 
9 
16 class Table
17 {
21  const TYPE_BOOLEAN = 'boolean';
22 
23  const TYPE_SMALLINT = 'smallint';
24 
25  const TYPE_INTEGER = 'integer';
26 
27  const TYPE_BIGINT = 'bigint';
28 
29  const TYPE_FLOAT = 'float';
30 
31  const TYPE_NUMERIC = 'numeric';
32 
33  const TYPE_DECIMAL = 'decimal';
34 
35  const TYPE_DATE = 'date';
36 
37  const TYPE_TIMESTAMP = 'timestamp';
38 
39  // Capable to support date-time from 1970 + auto-triggers in some RDBMS
40  const TYPE_DATETIME = 'datetime';
41 
42  // Capable to support long date-time before 1970
43  const TYPE_TEXT = 'text';
44 
45  // A real blob, stored as binary inside DB
46  const TYPE_BLOB = 'blob';
47 
48  // Used for back compatibility, when query param can't use statement options
49  const TYPE_VARBINARY = 'varbinary';
50 
54  const DEFAULT_TEXT_SIZE = 1024;
55 
56  const MAX_TEXT_SIZE = 2147483648;
57 
58  const MAX_VARBINARY_SIZE = 2147483648;
59 
63  const TIMESTAMP_INIT_UPDATE = 'TIMESTAMP_INIT_UPDATE';
64 
65  const TIMESTAMP_INIT = 'TIMESTAMP_INIT';
66 
67  const TIMESTAMP_UPDATE = 'TIMESTAMP_UPDATE';
68 
72  const ACTION_CASCADE = 'CASCADE';
73 
74  const ACTION_SET_NULL = 'SET NULL';
75 
76  const ACTION_NO_ACTION = 'NO ACTION';
77 
78  const ACTION_RESTRICT = 'RESTRICT';
79 
80  const ACTION_SET_DEFAULT = 'SET DEFAULT';
81 
87  const OPTION_DEFAULT = 'default';
88 
94  const OPTION_IDENTITY = 'identity';
95 
101  const OPTION_LENGTH = 'length';
102 
108  const OPTION_NULLABLE = 'nullable';
109 
115  const OPTION_PRECISION = 'precision';
116 
122  const OPTION_PRIMARY = 'primary';
123 
129  const OPTION_SCALE = 'scale';
130 
136  const OPTION_TYPE = 'type';
137 
143  const OPTION_UNSIGNED = 'unsigned';
144 
150  protected $_tableName;
151 
157  protected $_schemaName;
158 
164  protected $_tableComment;
165 
189  protected $_columns = [];
190 
212  protected $_indexes = [];
213 
233  protected $_foreignKeys = [];
234 
240  protected $_options = ['type' => 'INNODB', 'charset' => 'utf8', 'collate' => 'utf8_general_ci'];
241 
248  public function setName($name)
249  {
250  $this->_tableName = $name;
251  if ($this->_tableComment === null) {
252  $this->_tableComment = $name;
253  }
254  return $this;
255  }
256 
263  public function setSchema($name)
264  {
265  $this->_schemaName = $name;
266  return $this;
267  }
268 
275  public function setComment($comment)
276  {
277  $this->_tableComment = $comment;
278  return $this;
279  }
280 
287  public function getName()
288  {
289  if ($this->_tableName === null) {
290  throw new \Zend_Db_Exception('Table name is not defined');
291  }
292  return $this->_tableName;
293  }
294 
300  public function getSchema()
301  {
302  return $this->_schemaName;
303  }
304 
310  public function getComment()
311  {
312  return $this->_tableComment;
313  }
314 
339  public function addColumn($name, $type, $size = null, $options = [], $comment = null)
340  {
341  $position = count($this->_columns);
342  $default = false;
343  $nullable = true;
344  $length = null;
345  $scale = null;
346  $precision = null;
347  $unsigned = false;
348  $primary = false;
349  $primaryPosition = 0;
350  $identity = false;
351 
352  // Prepare different properties
353  switch ($type) {
354  case self::TYPE_BOOLEAN:
355  break;
356 
357  case self::TYPE_SMALLINT:
358  case self::TYPE_INTEGER:
359  case self::TYPE_BIGINT:
360  if (!empty($options['unsigned'])) {
361  $unsigned = true;
362  }
363 
364  break;
365 
366  case self::TYPE_FLOAT:
367  if (!empty($options['unsigned'])) {
368  $unsigned = true;
369  }
370  break;
371 
372  case self::TYPE_DECIMAL:
373  case self::TYPE_NUMERIC:
374  $match = [];
375  $scale = 0;
376  $precision = 10;
377  // parse size value
378  if (is_array($size)) {
379  if (count($size) == 2) {
380  $size = array_values($size);
381  $precision = $size[0];
382  $scale = $size[1];
383  }
384  } elseif (preg_match('#^(\d+),(\d+)$#', $size, $match)) {
385  $precision = $match[1];
386  $scale = $match[2];
387  }
388  // check options
389  if (isset($options['precision'])) {
390  $precision = $options['precision'];
391  }
392 
393  if (isset($options['scale'])) {
394  $scale = $options['scale'];
395  }
396 
397  if (!empty($options['unsigned'])) {
398  $unsigned = true;
399  }
400  break;
401  case self::TYPE_DATE:
402  case self::TYPE_DATETIME:
404  break;
405  case self::TYPE_TEXT:
406  case self::TYPE_BLOB:
408  $length = $size;
409  break;
410  default:
411  throw new \Zend_Db_Exception('Invalid column data type "' . $type . '"');
412  }
413 
414  if (array_key_exists('default', $options)) {
415  $default = $options['default'];
416  }
417  if (array_key_exists('nullable', $options)) {
418  $nullable = (bool)$options['nullable'];
419  }
420  if (!empty($options['primary'])) {
421  $primary = true;
422  if (isset($options['primary_position'])) {
423  $primaryPosition = (int)$options['primary_position'];
424  } else {
425  $primaryPosition = 0;
426  foreach ($this->_columns as $v) {
427  if ($v['PRIMARY']) {
428  $primaryPosition++;
429  }
430  }
431  }
432  }
433  if (!empty($options['identity']) || !empty($options['auto_increment'])) {
434  $identity = true;
435  }
436 
437  if ($comment === null) {
438  $comment = ucfirst($name);
439  }
440 
441  $upperName = strtoupper($name);
442  $this->_columns[$upperName] = [
443  'COLUMN_NAME' => $name,
444  'COLUMN_TYPE' => $type,
445  'COLUMN_POSITION' => $position,
446  'DATA_TYPE' => $type,
447  'DEFAULT' => $default,
448  'NULLABLE' => $nullable,
449  'LENGTH' => $length,
450  'SCALE' => $scale,
451  'PRECISION' => $precision,
452  'UNSIGNED' => $unsigned,
453  'PRIMARY' => $primary,
454  'PRIMARY_POSITION' => $primaryPosition,
455  'IDENTITY' => $identity,
456  'COMMENT' => $comment,
457  ];
458 
459  return $this;
460  }
461 
474  public function addForeignKey($fkName, $column, $refTable, $refColumn, $onDelete = null)
475  {
476  $upperName = strtoupper($fkName);
477 
478  // validate column name
479  if (!isset($this->_columns[strtoupper($column)])) {
480  throw new \Zend_Db_Exception('Undefined column "' . $column . '"');
481  }
482 
483  switch ($onDelete) {
488  break;
489  default:
490  $onDelete = self::ACTION_NO_ACTION;
491  }
492 
493  $this->_foreignKeys[$upperName] = [
494  'FK_NAME' => $fkName,
495  'COLUMN_NAME' => $column,
496  'REF_TABLE_NAME' => $refTable,
497  'REF_COLUMN_NAME' => $refColumn,
498  'ON_DELETE' => $onDelete
499  ];
500 
501  return $this;
502  }
503 
514  public function addIndex($indexName, $fields, $options = [])
515  {
517  $position = 0;
518  $columns = [];
519  if (!is_array($fields)) {
520  $fields = [$fields];
521  }
522 
523  foreach ($fields as $columnData) {
524  $columnSize = null;
525  $columnPos = $position;
526  if (is_string($columnData)) {
527  $columnName = $columnData;
528  } elseif (is_array($columnData)) {
529  if (!isset($columnData['name'])) {
530  throw new \Zend_Db_Exception('Invalid index column data');
531  }
532 
533  $columnName = $columnData['name'];
534  if (!empty($columnData['size'])) {
535  $columnSize = (int)$columnData['size'];
536  }
537  if (!empty($columnData['position'])) {
538  $columnPos = (int)$columnData['position'];
539  }
540  } else {
541  continue;
542  }
543 
544  $columns[strtoupper(
545  $columnName
546  )] = [
547  'NAME' => $columnName,
548  'SIZE' => $columnSize,
549  'POSITION' => $columnPos,
550  ];
551 
552  $position++;
553  }
554 
555  if (empty($columns)) {
556  throw new \Zend_Db_Exception('Columns for index are not defined');
557  }
558 
559  if (!empty($options['type'])) {
560  $idxType = $options['type'];
561  }
562 
563  $this->_indexes[strtoupper(
564  $indexName
565  )] = [
566  'INDEX_NAME' => $indexName,
567  'COLUMNS' => $this->_normalizeIndexColumnPosition($columns),
568  'TYPE' => $idxType,
569  ];
570 
571  return $this;
572  }
573 
581  public function getColumns($normalized = true)
582  {
583  if ($normalized) {
584  return $this->_normalizeColumnPosition($this->_columns);
585  }
586  return $this->_columns;
587  }
588 
596  public function setColumn($column)
597  {
598  $upperName = strtoupper($column['COLUMN_NAME']);
599  $this->_columns[$upperName] = $column;
600  return $this;
601  }
602 
609  public function getIndexes()
610  {
611  return $this->_indexes;
612  }
613 
620  public function getForeignKeys()
621  {
622  return $this->_foreignKeys;
623  }
624 
632  public function setOption($key, $value)
633  {
634  $this->_options[$key] = $value;
635  return $this;
636  }
637 
645  public function getOption($key)
646  {
647  if (!isset($this->_options[$key])) {
648  return null;
649  }
650  return $this->_options[$key];
651  }
652 
658  public function getOptions()
659  {
660  return $this->_options;
661  }
662 
670  protected function _sortIndexColumnPosition($a, $b)
671  {
672  return $a['POSITION'] - $b['POSITION'];
673  }
674 
682  protected function _sortColumnPosition($a, $b)
683  {
684  return $a['COLUMN_POSITION'] - $b['COLUMN_POSITION'];
685  }
686 
694  {
695  uasort($columns, [$this, '_sortIndexColumnPosition']);
696  $position = 0;
697  foreach (array_keys($columns) as $columnId) {
698  $columns[$columnId]['POSITION'] = $position;
699  $position++;
700  }
701  return $columns;
702  }
703 
710  protected function _normalizeColumnPosition($columns)
711  {
712  uasort($columns, [$this, '_sortColumnPosition']);
713  $position = 0;
714  foreach (array_keys($columns) as $columnId) {
715  $columns[$columnId]['COLUMN_POSITION'] = $position;
716  $position++;
717  }
718  return $columns;
719  }
720 }
elseif(isset( $params[ 'redirect_parent']))
Definition: iframe.phtml:17
$fields
Definition: details.phtml:14
addColumn($name, $type, $size=null, $options=[], $comment=null)
Definition: Table.php:339
_normalizeIndexColumnPosition($columns)
Definition: Table.php:693
$columns
Definition: default.phtml:15
$type
Definition: item.phtml:13
$value
Definition: gender.phtml:16
_normalizeColumnPosition($columns)
Definition: Table.php:710
addForeignKey($fkName, $column, $refTable, $refColumn, $onDelete=null)
Definition: Table.php:474
addIndex($indexName, $fields, $options=[])
Definition: Table.php:514
getColumns($normalized=true)
Definition: Table.php:581
if(!isset($_GET['name'])) $name
Definition: log.php:14