Db.php 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307
  1. <?php
  2. /**
  3. * Copyright © Magento, Inc. All rights reserved.
  4. * See COPYING.txt for license details.
  5. */
  6. namespace Magento\Backup\Model\ResourceModel;
  7. /**
  8. * Database backup resource model
  9. * @api
  10. * @since 100.0.2
  11. */
  12. class Db
  13. {
  14. /**
  15. * Database connection adapter
  16. *
  17. * @var \Magento\Framework\DB\Adapter\AdapterInterface
  18. */
  19. protected $connection;
  20. /**
  21. * Tables foreign key data array
  22. * [tbl_name] = array(create foreign key strings)
  23. *
  24. * @var array
  25. */
  26. protected $_foreignKeys = [];
  27. /**
  28. * Backup resource helper
  29. *
  30. * @var \Magento\Backup\Model\ResourceModel\Helper
  31. */
  32. protected $_resourceHelper;
  33. /**
  34. * Initialize Backup DB resource model
  35. *
  36. * @param \Magento\Backup\Model\ResourceModel\HelperFactory $resHelperFactory
  37. * @param \Magento\Framework\App\ResourceConnection $resource
  38. */
  39. public function __construct(
  40. \Magento\Backup\Model\ResourceModel\HelperFactory $resHelperFactory,
  41. \Magento\Framework\App\ResourceConnection $resource
  42. ) {
  43. $this->_resourceHelper = $resHelperFactory->create();
  44. $this->connection = $resource->getConnection('backup');
  45. }
  46. /**
  47. * Clear data
  48. *
  49. * @return void
  50. */
  51. public function clear()
  52. {
  53. $this->_foreignKeys = [];
  54. }
  55. /**
  56. * Retrieve table list
  57. *
  58. * @return array
  59. */
  60. public function getTables()
  61. {
  62. return $this->connection->listTables();
  63. }
  64. /**
  65. * Retrieve SQL fragment for drop table
  66. *
  67. * @param string $tableName
  68. * @return string
  69. */
  70. public function getTableDropSql($tableName)
  71. {
  72. return $this->_resourceHelper->getTableDropSql($tableName);
  73. }
  74. /**
  75. * Retrieve SQL fragment for create table
  76. *
  77. * @param string $tableName
  78. * @param bool $withForeignKeys
  79. * @return string
  80. */
  81. public function getTableCreateSql($tableName, $withForeignKeys = false)
  82. {
  83. return $this->_resourceHelper->getTableCreateSql($tableName, $withForeignKeys = false);
  84. }
  85. /**
  86. * Retrieve foreign keys for table(s)
  87. *
  88. * @param string|null $tableName
  89. * @return string
  90. */
  91. public function getTableForeignKeysSql($tableName = null)
  92. {
  93. $fkScript = '';
  94. if (!$tableName) {
  95. $tables = $this->getTables();
  96. foreach ($tables as $table) {
  97. $tableFkScript = $this->_resourceHelper->getTableForeignKeysSql($table);
  98. if (!empty($tableFkScript)) {
  99. $fkScript .= "\n" . $tableFkScript;
  100. }
  101. }
  102. } else {
  103. $fkScript = $this->getTableForeignKeysSql($tableName);
  104. }
  105. return $fkScript;
  106. }
  107. /**
  108. * Return triggers for table(s).
  109. *
  110. * @param string|null $tableName
  111. * @param bool $addDropIfExists
  112. * @return string
  113. * @since 100.2.3
  114. */
  115. public function getTableTriggersSql($tableName = null, $addDropIfExists = true)
  116. {
  117. $triggerScript = '';
  118. if (!$tableName) {
  119. $tables = $this->getTables();
  120. foreach ($tables as $table) {
  121. $tableTriggerScript = $this->_resourceHelper->getTableTriggersSql($table, $addDropIfExists);
  122. if (!empty($tableTriggerScript)) {
  123. $triggerScript .= "\n" . $tableTriggerScript;
  124. }
  125. }
  126. } else {
  127. $triggerScript = $this->getTableTriggersSql($tableName, $addDropIfExists);
  128. }
  129. return $triggerScript;
  130. }
  131. /**
  132. * Retrieve table status
  133. *
  134. * @param string $tableName
  135. * @return \Magento\Framework\DataObject|bool
  136. */
  137. public function getTableStatus($tableName)
  138. {
  139. $row = $this->connection->showTableStatus($tableName);
  140. if ($row) {
  141. $statusObject = new \Magento\Framework\DataObject();
  142. foreach ($row as $field => $value) {
  143. $statusObject->setData(strtolower($field), $value);
  144. }
  145. $cntRow = $this->connection->fetchRow($this->connection->select()->from($tableName, 'COUNT(1) as rows'));
  146. $statusObject->setRows($cntRow['rows']);
  147. return $statusObject;
  148. }
  149. return false;
  150. }
  151. /**
  152. * Retrieve table partial data SQL insert
  153. *
  154. * @param string $tableName
  155. * @param null|int $count
  156. * @param null|int $offset
  157. * @return string
  158. */
  159. public function getTableDataSql($tableName, $count = null, $offset = null)
  160. {
  161. return $this->_resourceHelper->getPartInsertSql($tableName, $count, $offset);
  162. }
  163. /**
  164. * Enter description here...
  165. *
  166. * @param string|array|\Zend_Db_Expr $tableName
  167. * @param bool $addDropIfExists
  168. * @return string
  169. */
  170. public function getTableCreateScript($tableName, $addDropIfExists = false)
  171. {
  172. return $this->_resourceHelper->getTableCreateScript($tableName, $addDropIfExists);
  173. }
  174. /**
  175. * Retrieve table header comment
  176. *
  177. * @param string $tableName
  178. * @return string
  179. */
  180. public function getTableHeader($tableName)
  181. {
  182. $quotedTableName = $this->connection->quoteIdentifier($tableName);
  183. return "\n--\n" . "-- Table structure for table {$quotedTableName}\n" . "--\n\n";
  184. }
  185. /**
  186. * Return table data dump
  187. *
  188. * @param string $tableName
  189. * @param bool $step
  190. * @return string
  191. * @SuppressWarnings(PHPMD.UnusedFormalParameter)
  192. */
  193. public function getTableDataDump($tableName, $step = false)
  194. {
  195. return $this->getTableDataSql($tableName);
  196. }
  197. /**
  198. * Returns SQL header data
  199. *
  200. * @return string
  201. */
  202. public function getHeader()
  203. {
  204. return $this->_resourceHelper->getHeader();
  205. }
  206. /**
  207. * Returns SQL footer data
  208. *
  209. * @return string
  210. */
  211. public function getFooter()
  212. {
  213. return $this->_resourceHelper->getFooter();
  214. }
  215. /**
  216. * Retrieve before insert data SQL fragment
  217. *
  218. * @param string $tableName
  219. * @return string
  220. */
  221. public function getTableDataBeforeSql($tableName)
  222. {
  223. return $this->_resourceHelper->getTableDataBeforeSql($tableName);
  224. }
  225. /**
  226. * Retrieve after insert data SQL fragment
  227. *
  228. * @param string $tableName
  229. * @return string
  230. */
  231. public function getTableDataAfterSql($tableName)
  232. {
  233. return $this->_resourceHelper->getTableDataAfterSql($tableName);
  234. }
  235. /**
  236. * Start transaction mode
  237. *
  238. * @return $this
  239. */
  240. public function beginTransaction()
  241. {
  242. $this->_resourceHelper->prepareTransactionIsolationLevel();
  243. $this->connection->beginTransaction();
  244. return $this;
  245. }
  246. /**
  247. * Commit transaction
  248. *
  249. * @return $this
  250. */
  251. public function commitTransaction()
  252. {
  253. $this->connection->commit();
  254. $this->_resourceHelper->restoreTransactionIsolationLevel();
  255. return $this;
  256. }
  257. /**
  258. * Rollback transaction
  259. *
  260. * @return $this
  261. */
  262. public function rollBackTransaction()
  263. {
  264. $this->connection->rollBack();
  265. $this->_resourceHelper->restoreTransactionIsolationLevel();
  266. return $this;
  267. }
  268. /**
  269. * Run sql code
  270. *
  271. * @param string $command
  272. * @return $this
  273. */
  274. public function runCommand($command)
  275. {
  276. $this->connection->query($command);
  277. return $this;
  278. }
  279. }