Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
Db.php
Go to the documentation of this file.
1 <?php
7 
13 class Db
14 {
20  protected $connection;
21 
28  protected $_foreignKeys = [];
29 
35  protected $_resourceHelper;
36 
43  public function __construct(
44  \Magento\Backup\Model\ResourceModel\HelperFactory $resHelperFactory,
45  \Magento\Framework\App\ResourceConnection $resource
46  ) {
47  $this->_resourceHelper = $resHelperFactory->create();
48  $this->connection = $resource->getConnection('backup');
49  }
50 
56  public function clear()
57  {
58  $this->_foreignKeys = [];
59  }
60 
66  public function getTables()
67  {
68  return $this->connection->listTables();
69  }
70 
77  public function getTableDropSql($tableName)
78  {
79  return $this->_resourceHelper->getTableDropSql($tableName);
80  }
81 
89  public function getTableCreateSql($tableName, $withForeignKeys = false)
90  {
91  return $this->_resourceHelper->getTableCreateSql($tableName, $withForeignKeys = false);
92  }
93 
100  public function getTableForeignKeysSql($tableName = null)
101  {
102  $fkScript = '';
103  if (!$tableName) {
104  $tables = $this->getTables();
105  foreach ($tables as $table) {
106  $tableFkScript = $this->_resourceHelper->getTableForeignKeysSql($table);
107  if (!empty($tableFkScript)) {
108  $fkScript .= "\n" . $tableFkScript;
109  }
110  }
111  } else {
112  $fkScript = $this->getTableForeignKeysSql($tableName);
113  }
114  return $fkScript;
115  }
116 
125  public function getTableTriggersSql($tableName = null, $addDropIfExists = true)
126  {
127  $triggerScript = '';
128  if (!$tableName) {
129  $tables = $this->getTables();
130  foreach ($tables as $table) {
131  $tableTriggerScript = $this->_resourceHelper->getTableTriggersSql($table, $addDropIfExists);
132  if (!empty($tableTriggerScript)) {
133  $triggerScript .= "\n" . $tableTriggerScript;
134  }
135  }
136  } else {
137  $triggerScript = $this->getTableTriggersSql($tableName, $addDropIfExists);
138  }
139 
140  return $triggerScript;
141  }
142 
149  public function getTableStatus($tableName)
150  {
151  $row = $this->connection->showTableStatus($tableName);
152 
153  if ($row) {
154  $statusObject = new \Magento\Framework\DataObject();
155  foreach ($row as $field => $value) {
156  $statusObject->setData(strtolower($field), $value);
157  }
158 
159  $cntRow = $this->connection->fetchRow($this->connection->select()->from($tableName, 'COUNT(1) as rows'));
160  $statusObject->setRows($cntRow['rows']);
161 
162  return $statusObject;
163  }
164 
165  return false;
166  }
167 
176  public function getTableDataSql($tableName, $count = null, $offset = null)
177  {
178  return $this->_resourceHelper->getPartInsertSql($tableName, $count, $offset);
179  }
180 
188  public function getTableCreateScript($tableName, $addDropIfExists = false)
189  {
190  return $this->_resourceHelper->getTableCreateScript($tableName, $addDropIfExists);
191  }
192 
199  public function getTableHeader($tableName)
200  {
201  $quotedTableName = $this->connection->quoteIdentifier($tableName);
202  return "\n--\n" . "-- Table structure for table {$quotedTableName}\n" . "--\n\n";
203  }
204 
213  public function getTableDataDump($tableName, $step = false)
214  {
215  return $this->getTableDataSql($tableName);
216  }
217 
223  public function getHeader()
224  {
225  return $this->_resourceHelper->getHeader();
226  }
227 
233  public function getFooter()
234  {
235  return $this->_resourceHelper->getFooter();
236  }
237 
245  {
246  return $this->_resourceHelper->getTableDataBeforeSql($tableName);
247  }
248 
256  {
257  return $this->_resourceHelper->getTableDataAfterSql($tableName);
258  }
259 
265  public function beginTransaction()
266  {
267  $this->_resourceHelper->prepareTransactionIsolationLevel();
268  $this->connection->beginTransaction();
269  return $this;
270  }
271 
277  public function commitTransaction()
278  {
279  $this->connection->commit();
280  $this->_resourceHelper->restoreTransactionIsolationLevel();
281  return $this;
282  }
283 
289  public function rollBackTransaction()
290  {
291  $this->connection->rollBack();
292  $this->_resourceHelper->restoreTransactionIsolationLevel();
293  return $this;
294  }
295 
302  public function runCommand($command)
303  {
304  $this->connection->query($command);
305  return $this;
306  }
307 }
$tableName
Definition: trigger.php:13
getTableCreateSql($tableName, $withForeignKeys=false)
Definition: Db.php:89
$count
Definition: recent.phtml:13
getTableDataSql($tableName, $count=null, $offset=null)
Definition: Db.php:176
$resource
Definition: bulk.php:12
getTableForeignKeysSql($tableName=null)
Definition: Db.php:100
$value
Definition: gender.phtml:16
getTableDataDump($tableName, $step=false)
Definition: Db.php:213
__construct(\Magento\Backup\Model\ResourceModel\HelperFactory $resHelperFactory, \Magento\Framework\App\ResourceConnection $resource)
Definition: Db.php:43
$table
Definition: trigger.php:14
getTableCreateScript($tableName, $addDropIfExists=false)
Definition: Db.php:188
getTableTriggersSql($tableName=null, $addDropIfExists=true)
Definition: Db.php:125