Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
DbSchemaReader.php
Go to the documentation of this file.
1 <?php
8 
14 
19 {
23  const MYSQL_TABLE_TYPE = 'BASE TABLE';
24 
28  private $resourceConnection;
29 
33  private $definitionAggregator;
34 
41  public function __construct(
42  ResourceConnection $resourceConnection,
43  DefinitionAggregator $definitionAggregator
44  ) {
45  $this->resourceConnection = $resourceConnection;
46  $this->definitionAggregator = $definitionAggregator;
47  }
48 
53  {
54  $adapter = $this->resourceConnection->getConnection($resource);
55  $dbName = $this->resourceConnection->getSchemaName($resource);
56  $stmt = $adapter->select()
57  ->from(
58  ['i_tables' => 'information_schema.TABLES'],
59  [
60  'engine' => 'ENGINE',
61  'comment' => 'TABLE_COMMENT',
62  'collation' => 'TABLE_COLLATION'
63  ]
64  )
65  ->joinInner(
66  ['charset_applicability' => 'information_schema.COLLATION_CHARACTER_SET_APPLICABILITY'],
67  'i_tables.table_collation = charset_applicability.collation_name',
68  [
69  'charset' => 'charset_applicability.CHARACTER_SET_NAME'
70  ]
71  )
72  ->where('TABLE_SCHEMA = ?', $dbName)
73  ->where('TABLE_NAME = ?', $tableName);
74 
75  return $adapter->fetchRow($stmt);
76  }
77 
85  public function readColumns($tableName, $resource)
86  {
87  $columns = [];
88  $adapter = $this->resourceConnection->getConnection($resource);
89  $dbName = $this->resourceConnection->getSchemaName($resource);
90  $stmt = $adapter->select()
91  ->from(
92  'information_schema.COLUMNS',
93  [
94  'name' => 'COLUMN_NAME',
95  'default' => 'COLUMN_DEFAULT',
96  'type' => 'DATA_TYPE',
97  'nullable' => new Expression('IF(IS_NULLABLE="YES", true, false)'),
98  'definition' => 'COLUMN_TYPE',
99  'extra' => 'EXTRA',
100  'comment' => new Expression('IF(COLUMN_COMMENT="", NULL, COLUMN_COMMENT)')
101  ]
102  )
103  ->where('TABLE_SCHEMA = ?', $dbName)
104  ->where('TABLE_NAME = ?', $tableName)
105  ->order('ORDINAL_POSITION ASC');
106 
107  $columnsDefinition = $adapter->fetchAssoc($stmt);
108 
109  foreach ($columnsDefinition as $columnDefinition) {
110  $column = $this->definitionAggregator->fromDefinition($columnDefinition);
111  $columns[$column['name']] = $column;
112  }
113 
114  return $columns;
115  }
116 
124  public function readIndexes($tableName, $resource)
125  {
126  $indexes = [];
127  $adapter = $this->resourceConnection->getConnection($resource);
128  $condition = sprintf('`Non_unique` = 1');
129  $sql = sprintf('SHOW INDEXES FROM %s WHERE %s', $tableName, $condition);
130  $stmt = $adapter->query($sql);
131 
132  // Use FETCH_NUM so we are not dependent on the CASE attribute of the PDO connection
133  $indexesDefinition = $stmt->fetchAll(\Zend_Db::FETCH_ASSOC);
134 
135  foreach ($indexesDefinition as $indexDefinition) {
136  $indexDefinition['type'] = 'index';
137  $index = $this->definitionAggregator->fromDefinition($indexDefinition);
138 
139  if (!isset($indexes[$index['name']])) {
140  $indexes[$index['name']] = [];
141  }
142 
143  $indexes[$index['name']] = array_replace_recursive($indexes[$index['name']], $index);
144  }
145 
146  return $indexes;
147  }
148 
157  {
158  $createTableSql = $this->getCreateTableSql($tableName, $resource);
159  $createTableSql['type'] = 'reference';
160  return $this->definitionAggregator->fromDefinition($createTableSql);
161  }
162 
171  {
172  $adapter = $this->resourceConnection->getConnection($resource);
173  $sql = sprintf('SHOW CREATE TABLE %s', $tableName);
174  $stmt = $adapter->query($sql);
175  return $stmt->fetch(\Zend_Db::FETCH_ASSOC);
176  }
177 
185  {
186  $constraints = [];
187  $adapter = $this->resourceConnection->getConnection($resource);
188  $condition = sprintf('`Non_unique` = 0');
189  $sql = sprintf('SHOW INDEXES FROM %s WHERE %s', $tableName, $condition);
190  $stmt = $adapter->query($sql);
191 
192  // Use FETCH_NUM so we are not dependent on the CASE attribute of the PDO connection
193  $constraintsDefinition = $stmt->fetchAll(\Zend_Db::FETCH_ASSOC);
194 
195  foreach ($constraintsDefinition as $constraintDefinition) {
196  $constraintDefinition['type'] = Constraint::TYPE;
197  $constraint = $this->definitionAggregator->fromDefinition($constraintDefinition);
198 
199  if (!isset($constraints[$constraint['name']])) {
200  $constraints[$constraint['name']] = [];
201  }
202 
203  $constraints[$constraint['name']] = array_replace_recursive($constraints[$constraint['name']], $constraint);
204  }
205 
206  return $constraints;
207  }
208 
215  public function readTables($resource)
216  {
217  $adapter = $this->resourceConnection->getConnection($resource);
218  $dbName = $this->resourceConnection->getSchemaName($resource);
219  $stmt = $adapter->select()
220  ->from(
221  ['information_schema.TABLES'],
222  ['TABLE_NAME']
223  )
224  ->where('TABLE_SCHEMA = ?', $dbName)
225  ->where('TABLE_TYPE = ?', self::MYSQL_TABLE_TYPE);
226  return $adapter->fetchCol($stmt);
227  }
228 }
$tableName
Definition: trigger.php:13
__construct(ResourceConnection $resourceConnection, DefinitionAggregator $definitionAggregator)
const FETCH_ASSOC
Definition: Db.php:142
$resource
Definition: bulk.php:12
$adapter
Definition: webapi_user.php:16
$columns
Definition: default.phtml:15
$index
Definition: list.phtml:44