Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
Public Member Functions | Data Fields
DbSchemaReader Class Reference
Inheritance diagram for DbSchemaReader:
DbSchemaReaderInterface

Public Member Functions

 __construct (ResourceConnection $resourceConnection, DefinitionAggregator $definitionAggregator)
 
 getTableOptions ($tableName, $resource)
 
 readColumns ($tableName, $resource)
 
 readIndexes ($tableName, $resource)
 
 readReferences ($tableName, $resource)
 
 getCreateTableSql ($tableName, $resource)
 
 readConstraints ($tableName, $resource)
 
 readTables ($resource)
 

Data Fields

const MYSQL_TABLE_TYPE = 'BASE TABLE'
 

Detailed Description

Definition at line 18 of file DbSchemaReader.php.

Constructor & Destructor Documentation

◆ __construct()

__construct ( ResourceConnection  $resourceConnection,
DefinitionAggregator  $definitionAggregator 
)

Constructor.

Parameters
ResourceConnection$resourceConnection
DefinitionAggregator$definitionAggregator

Definition at line 41 of file DbSchemaReader.php.

44  {
45  $this->resourceConnection = $resourceConnection;
46  $this->definitionAggregator = $definitionAggregator;
47  }

Member Function Documentation

◆ getCreateTableSql()

getCreateTableSql (   $tableName,
  $resource 
)

Retrieve Create table SQL, from SHOW CREATE TABLE query.

Parameters
string$tableName
string$resource
Returns
array

Definition at line 170 of file DbSchemaReader.php.

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  }
$tableName
Definition: trigger.php:13
const FETCH_ASSOC
Definition: Db.php:142
$resource
Definition: bulk.php:12
$adapter
Definition: webapi_user.php:16

◆ getTableOptions()

getTableOptions (   $tableName,
  $resource 
)

Show table options like engine, partitioning, etc.

Parameters
string$tableName
string$resource
Returns
array

Implements DbSchemaReaderInterface.

Definition at line 52 of file DbSchemaReader.php.

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  }
$tableName
Definition: trigger.php:13
$resource
Definition: bulk.php:12
$adapter
Definition: webapi_user.php:16

◆ readColumns()

readColumns (   $tableName,
  $resource 
)

Prepare and fetch query: Describe {table_name}.

Parameters
string$tableName
string$resource
Returns
array

Implements DbSchemaReaderInterface.

Definition at line 85 of file DbSchemaReader.php.

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  }
$tableName
Definition: trigger.php:13
$resource
Definition: bulk.php:12
$adapter
Definition: webapi_user.php:16
$columns
Definition: default.phtml:15

◆ readConstraints()

readConstraints (   $tableName,
  $resource 
)

Reading DB constraints. Primary and unique constraints are always non_unique=0.

Read constraints from Magento tables.

Parameters
string$tableName
string$resource
Returns
array

Implements DbSchemaReaderInterface.

Definition at line 184 of file DbSchemaReader.php.

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  }
$tableName
Definition: trigger.php:13
const FETCH_ASSOC
Definition: Db.php:142
$resource
Definition: bulk.php:12
$adapter
Definition: webapi_user.php:16

◆ readIndexes()

readIndexes (   $tableName,
  $resource 
)

Fetch all indexes from table.

Parameters
string$tableName
string$resource
Returns
array

Implements DbSchemaReaderInterface.

Definition at line 124 of file DbSchemaReader.php.

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  }
$tableName
Definition: trigger.php:13
const FETCH_ASSOC
Definition: Db.php:142
$resource
Definition: bulk.php:12
$adapter
Definition: webapi_user.php:16
$index
Definition: list.phtml:44

◆ readReferences()

readReferences (   $tableName,
  $resource 
)

As MySQL has bug and do not show foreign keys during DESCRIBE and other directives required to take it from SHOW CREATE TABLE ... command

Read references (foreign keys) from Magento tables.

Parameters
string$tableName
string$resource
Returns
array

Implements DbSchemaReaderInterface.

Definition at line 156 of file DbSchemaReader.php.

157  {
158  $createTableSql = $this->getCreateTableSql($tableName, $resource);
159  $createTableSql['type'] = 'reference';
160  return $this->definitionAggregator->fromDefinition($createTableSql);
161  }
$tableName
Definition: trigger.php:13
$resource
Definition: bulk.php:12

◆ readTables()

readTables (   $resource)

Return names of all tables from shard.

Parameters
string$resourceShard name.
Returns
array

Implements DbSchemaReaderInterface.

Definition at line 215 of file DbSchemaReader.php.

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  }
$resource
Definition: bulk.php:12
$adapter
Definition: webapi_user.php:16

Field Documentation

◆ MYSQL_TABLE_TYPE

const MYSQL_TABLE_TYPE = 'BASE TABLE'

Table type in information_schema.TABLES which allows to identify only tables and ignore views

Definition at line 23 of file DbSchemaReader.php.


The documentation for this class was generated from the following file: