Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
DbSchemaWriter.php
Go to the documentation of this file.
1 <?php
8 
18 
23 {
29  private $statementDirectives = [
30  self::ALTER_TYPE => 'ALTER TABLE %s %s',
31  self::CREATE_TYPE => 'CREATE TABLE %s %s',
32  self::DROP_TYPE => 'DROP TABLE %s'
33  ];
34 
40  private $tableOptions = [
41  'charset' => 'DEFAULT CHARSET',
42  'collation' => 'DEFAULT COLLATE',
43  'engine' => 'ENGINE',
44  'comment' => 'COMMENT'
45  ];
46 
50  private $resourceConnection;
51 
55  private $statementFactory;
56 
60  private $dryRunLogger;
61 
68  public function __construct(
69  ResourceConnection $resourceConnection,
70  StatementFactory $statementFactory,
71  DryRunLogger $dryRunLogger,
72  array $tableOptions = []
73  ) {
74  $this->resourceConnection = $resourceConnection;
75  $this->statementFactory = $statementFactory;
76  $this->dryRunLogger = $dryRunLogger;
77  $this->tableOptions = array_replace($this->tableOptions, $tableOptions);
78  }
79 
83  public function createTable($tableName, $resource, array $definition, array $options)
84  {
85  $sql = sprintf(
86  "(\n%s\n) ENGINE=%s DEFAULT CHARSET=%s DEFAULT COLLATE=%s %s",
87  implode(", \n", $definition),
88  $options['engine'],
89  $options['charset'],
90  $options['collation'],
91  isset($options['comment']) ? sprintf('COMMENT="%s"', $options['comment']) : ''
92  );
93 
94  return $this->statementFactory->create(
95  $tableName,
96  $tableName,
97  self::CREATE_TYPE,
98  $sql,
99  $resource
100  );
101  }
102 
108  public function dropTable($tableName, $resource)
109  {
110  return $this->statementFactory->create(
111  $tableName,
112  $tableName,
113  self::DROP_TYPE,
114  '',
115  $resource
116  );
117  }
118 
128  private function getDropElementSQL($type, $name)
129  {
130  switch ($type) {
132  return 'DROP PRIMARY KEY';
134  return sprintf('DROP KEY %s', $name);
135  case \Magento\Framework\Setup\Declaration\Schema\Dto\Index::TYPE:
136  return sprintf('DROP INDEX %s', $name);
137  case Reference::TYPE:
138  return sprintf('DROP FOREIGN KEY %s', $name);
139  default:
140  return sprintf('DROP COLUMN %s', $name);
141  }
142  }
143 
149  public function addElement($elementName, $resource, $tableName, $elementDefinition, $elementType)
150  {
151  $addElementSyntax = $elementType === Column::TYPE ? 'ADD COLUMN %s' : 'ADD %s';
152  $sql = sprintf(
153  $addElementSyntax,
154  $elementDefinition
155  );
156  return $this->statementFactory->create(
157  $elementName,
158  $tableName,
159  self::ALTER_TYPE,
160  $sql,
161  $resource,
162  $elementType
163  );
164  }
165 
169  public function modifyTableOption($tableName, $resource, $optionName, $optionValue)
170  {
171  return $this->statementFactory->create(
172  $tableName,
173  $tableName,
174  self::ALTER_TYPE,
175  sprintf("%s='%s'", $this->tableOptions[$optionName], $optionValue),
176  $resource
177  );
178  }
179 
185  public function modifyColumn($columnName, $resource, $tableName, $columnDefinition)
186  {
187  $sql = sprintf(
188  'MODIFY COLUMN %s',
189  $columnDefinition
190  );
191  return $this->statementFactory->create(
192  $columnName,
193  $tableName,
194  self::ALTER_TYPE,
195  $sql,
196  $resource
197  );
198  }
199 
204  {
205  $adapter = $this->resourceConnection->getConnection($resource);
206 
207  $sql = sprintf(
208  '%s',
209  $this->getDropElementSQL(
210  $type,
211  $adapter->quoteIdentifier($elementName)
212  )
213  );
214  return $this->statementFactory->create(
215  $elementName,
216  $tableName,
217  self::ALTER_TYPE,
218  $sql,
219  $resource,
220  $type
221  );
222  }
223 
228  {
229  $sql = 'AUTO_INCREMENT = 1';
230  return $this->statementFactory->create(
231  sprintf('RESET_AUTOINCREMENT_%s', $tableName),
232  $tableName,
233  self::ALTER_TYPE,
234  $sql,
235  $resource
236  );
237  }
238 
242  public function compile(StatementAggregator $statementAggregator, $dryRun)
243  {
244  foreach ($statementAggregator->getStatementsBank() as $statementBank) {
245  $statementsSql = [];
247  foreach ($statementBank as $statement) {
248  $statementsSql[] = $statement->getStatement();
249  }
250  $adapter = $this->resourceConnection->getConnection($statement->getResource());
251 
252  if ($dryRun) {
253  $this->dryRunLogger->log(
254  sprintf(
255  $this->statementDirectives[$statement->getType()],
256  $adapter->quoteIdentifier($statement->getTableName()),
257  implode(", ", $statementsSql)
258  )
259  );
260  } else {
261  $adapter->query(
262  sprintf(
263  $this->statementDirectives[$statement->getType()],
264  $adapter->quoteIdentifier($statement->getTableName()),
265  implode(", ", $statementsSql)
266  )
267  );
268  //Do post update, like SQL DML operations or etc...
269  foreach ($statement->getTriggers() as $trigger) {
271  }
272  }
273  }
274  }
275 }
$tableName
Definition: trigger.php:13
createTable($tableName, $resource, array $definition, array $options)
addElement($elementName, $resource, $tableName, $elementDefinition, $elementType)
__construct(ResourceConnection $resourceConnection, StatementFactory $statementFactory, DryRunLogger $dryRunLogger, array $tableOptions=[])
$resource
Definition: bulk.php:12
$adapter
Definition: webapi_user.php:16
$type
Definition: item.phtml:13
dropElement($resource, $elementName, $tableName, $type)
modifyColumn($columnName, $resource, $tableName, $columnDefinition)
$trigger
Definition: trigger.php:27
modifyTableOption($tableName, $resource, $optionName, $optionValue)
compile(StatementAggregator $statementAggregator, $dryRun)
$elementName
Definition: gallery.phtml:10
if(!isset($_GET['name'])) $name
Definition: log.php:14