PageRenderTime 45ms CodeModel.GetById 16ms RepoModel.GetById 0ms app.codeStats 1ms

/app/code/core/Mage/Backup/Model/Resource/Helper/Mysql4.php

https://gitlab.com/LisovyiEvhenii/ismextensions
PHP | 322 lines | 179 code | 30 blank | 113 comment | 19 complexity | 4d24628fa1b2890c91d8048cd65fd7da MD5 | raw file
  1. <?php
  2. /**
  3. * Magento
  4. *
  5. * NOTICE OF LICENSE
  6. *
  7. * This source file is subject to the Open Software License (OSL 3.0)
  8. * that is bundled with this package in the file LICENSE.txt.
  9. * It is also available through the world-wide-web at this URL:
  10. * http://opensource.org/licenses/osl-3.0.php
  11. * If you did not receive a copy of the license and are unable to
  12. * obtain it through the world-wide-web, please send an email
  13. * to license@magento.com so we can send you a copy immediately.
  14. *
  15. * DISCLAIMER
  16. *
  17. * Do not edit or add to this file if you wish to upgrade Magento to newer
  18. * versions in the future. If you wish to customize Magento for your
  19. * needs please refer to http://www.magento.com for more information.
  20. *
  21. * @category Mage
  22. * @package Mage_Backup
  23. * @copyright Copyright (c) 2006-2016 X.commerce, Inc. and affiliates (http://www.magento.com)
  24. * @license http://opensource.org/licenses/osl-3.0.php Open Software License (OSL 3.0)
  25. */
  26. class Mage_Backup_Model_Resource_Helper_Mysql4 extends Mage_Core_Model_Resource_Helper_Mysql4
  27. {
  28. /**
  29. * Tables foreign key data array
  30. * [tbl_name] = array(create foreign key strings)
  31. *
  32. * @var array
  33. */
  34. protected $_foreignKeys = array();
  35. /**
  36. * Retrieve SQL fragment for drop table
  37. *
  38. * @param string $tableName
  39. * @return string
  40. */
  41. public function getTableDropSql($tableName)
  42. {
  43. $quotedTableName = $this->_getReadAdapter()->quoteIdentifier($tableName);
  44. return sprintf('DROP TABLE IF EXISTS %s;', $quotedTableName);
  45. }
  46. /**
  47. * Retrieve foreign keys for table(s)
  48. *
  49. * @param string|null $tableName
  50. * @return string|false
  51. */
  52. public function getTableForeignKeysSql($tableName = null)
  53. {
  54. $sql = false;
  55. if ($tableName === null) {
  56. $sql = '';
  57. foreach ($this->_foreignKeys as $table => $foreignKeys) {
  58. $sql .= $this->_buildForeignKeysAlterTableSql($table, $foreignKeys);
  59. }
  60. } else if (isset($this->_foreignKeys[$tableName])) {
  61. $foreignKeys = $this->_foreignKeys[$tableName];
  62. $sql = $this->_buildForeignKeysAlterTableSql($tableName, $foreignKeys);
  63. }
  64. return $sql;
  65. }
  66. /**
  67. * Build sql that will add foreign keys to it
  68. *
  69. * @param string $tableName
  70. * @param array $foreignKeys
  71. * @return string
  72. */
  73. protected function _buildForeignKeysAlterTableSql($tableName, $foreignKeys)
  74. {
  75. if (!is_array($foreignKeys) || empty($foreignKeys)) {
  76. return '';
  77. }
  78. return sprintf("ALTER TABLE %s\n %s;\n",
  79. $this->_getReadAdapter()->quoteIdentifier($tableName),
  80. join(",\n ", $foreignKeys)
  81. );
  82. }
  83. /**
  84. * Get create script for table
  85. *
  86. * @param string $tableName
  87. * @param boolean $addDropIfExists
  88. * @return string
  89. */
  90. public function getTableCreateScript($tableName, $addDropIfExists = false)
  91. {
  92. $script = '';
  93. $quotedTableName = $this->_getReadAdapter()->quoteIdentifier($tableName);
  94. if ($addDropIfExists) {
  95. $script .= 'DROP TABLE IF EXISTS ' . $quotedTableName .";\n";
  96. }
  97. //TODO fix me
  98. $sql = 'SHOW CREATE TABLE ' . $quotedTableName;
  99. $data = $this->_getReadAdapter()->fetchRow($sql);
  100. $script .= isset($data['Create Table']) ? $data['Create Table'].";\n" : '';
  101. return $script;
  102. }
  103. /**
  104. * Retrieve SQL fragment for create table
  105. *
  106. * @param string $tableName
  107. * @param bool $withForeignKeys
  108. * @return string
  109. */
  110. public function getTableCreateSql($tableName, $withForeignKeys = false)
  111. {
  112. $adapter = $this->_getReadAdapter();
  113. $quotedTableName = $adapter->quoteIdentifier($tableName);
  114. $query = 'SHOW CREATE TABLE ' . $quotedTableName;
  115. $row = $adapter->fetchRow($query);
  116. if (!$row || !isset($row['Table']) || !isset($row['Create Table'])) {
  117. return false;
  118. }
  119. $regExp = '/,\s+CONSTRAINT `([^`]*)` FOREIGN KEY \(`([^`]*)`\) '
  120. . 'REFERENCES `([^`]*)` \(`([^`]*)`\)'
  121. . '( ON DELETE (RESTRICT|CASCADE|SET NULL|NO ACTION))?'
  122. . '( ON UPDATE (RESTRICT|CASCADE|SET NULL|NO ACTION))?/';
  123. $matches = array();
  124. preg_match_all($regExp, $row['Create Table'], $matches, PREG_SET_ORDER);
  125. if (is_array($matches)) {
  126. foreach ($matches as $match) {
  127. $this->_foreignKeys[$tableName][] = sprintf('ADD CONSTRAINT %s FOREIGN KEY (%s) REFERENCES %s (%s)%s%s',
  128. $adapter->quoteIdentifier($match[1]),
  129. $adapter->quoteIdentifier($match[2]),
  130. $adapter->quoteIdentifier($match[3]),
  131. $adapter->quoteIdentifier($match[4]),
  132. isset($match[5]) ? $match[5] : '',
  133. isset($match[7]) ? $match[7] : ''
  134. );
  135. }
  136. }
  137. if ($withForeignKeys) {
  138. $sql = $row['Create Table'];
  139. } else {
  140. $sql = preg_replace($regExp, '', $row['Create Table']);
  141. }
  142. return $sql . ';';
  143. }
  144. /**
  145. * Returns SQL header data, move from original resource model
  146. *
  147. * @return string
  148. */
  149. public function getHeader()
  150. {
  151. $dbConfig = $this->_getReadAdapter()->getConfig();
  152. $versionRow = $this->_getReadAdapter()->fetchRow('SHOW VARIABLES LIKE \'version\'');
  153. $hostName = !empty($dbConfig['unix_socket']) ? $dbConfig['unix_socket']
  154. : (!empty($dbConfig['host']) ? $dbConfig['host'] : 'localhost');
  155. $header = "-- Magento DB backup\n"
  156. . "--\n"
  157. . "-- Host: {$hostName} Database: {$dbConfig['dbname']}\n"
  158. . "-- ------------------------------------------------------\n"
  159. . "-- Server version: {$versionRow['Value']}\n\n"
  160. . "/*!40101 SET @OLD_CHARACTER_SET_CLIENT=@@CHARACTER_SET_CLIENT */;\n"
  161. . "/*!40101 SET @OLD_CHARACTER_SET_RESULTS=@@CHARACTER_SET_RESULTS */;\n"
  162. . "/*!40101 SET @OLD_COLLATION_CONNECTION=@@COLLATION_CONNECTION */;\n"
  163. . "/*!40101 SET NAMES utf8 */;\n"
  164. . "/*!40014 SET @OLD_UNIQUE_CHECKS=@@UNIQUE_CHECKS, UNIQUE_CHECKS=0 */;\n"
  165. . "/*!40014 SET @OLD_FOREIGN_KEY_CHECKS=@@FOREIGN_KEY_CHECKS, FOREIGN_KEY_CHECKS=0 */;\n"
  166. . "/*!40101 SET @OLD_SQL_MODE=@@SQL_MODE, SQL_MODE='NO_AUTO_VALUE_ON_ZERO' */;\n"
  167. . "/*!40111 SET @OLD_SQL_NOTES=@@SQL_NOTES, SQL_NOTES=0 */;\n";
  168. return $header;
  169. }
  170. /**
  171. * Returns SQL footer data, move from original resource model
  172. *
  173. * @return string
  174. */
  175. public function getFooter()
  176. {
  177. $footer = "\n/*!40101 SET SQL_MODE=@OLD_SQL_MODE */;\n"
  178. . "/*!40014 SET FOREIGN_KEY_CHECKS=@OLD_FOREIGN_KEY_CHECKS */; \n"
  179. . "/*!40014 SET UNIQUE_CHECKS=@OLD_UNIQUE_CHECKS */;\n"
  180. . "/*!40101 SET CHARACTER_SET_CLIENT=@OLD_CHARACTER_SET_CLIENT */;\n"
  181. . "/*!40101 SET CHARACTER_SET_RESULTS=@OLD_CHARACTER_SET_RESULTS */;\n"
  182. . "/*!40101 SET COLLATION_CONNECTION=@OLD_COLLATION_CONNECTION */;\n"
  183. . "/*!40111 SET SQL_NOTES=@OLD_SQL_NOTES */;\n"
  184. . "\n-- Dump completed on " . Mage::getSingleton('core/date')->gmtDate() . " GMT";
  185. return $footer;
  186. }
  187. /**
  188. * Retrieve before insert data SQL fragment
  189. *
  190. * @param string $tableName
  191. * @return string
  192. */
  193. public function getTableDataBeforeSql($tableName)
  194. {
  195. $quotedTableName = $this->_getReadAdapter()->quoteIdentifier($tableName);
  196. return "\n--\n"
  197. . "-- Dumping data for table {$quotedTableName}\n"
  198. . "--\n\n"
  199. . "LOCK TABLES {$quotedTableName} WRITE;\n"
  200. . "/*!40000 ALTER TABLE {$quotedTableName} DISABLE KEYS */;\n";
  201. }
  202. /**
  203. * Retrieve after insert data SQL fragment
  204. *
  205. * @param string $tableName
  206. * @return string
  207. */
  208. public function getTableDataAfterSql($tableName)
  209. {
  210. $quotedTableName = $this->_getReadAdapter()->quoteIdentifier($tableName);
  211. return "/*!40000 ALTER TABLE {$quotedTableName} ENABLE KEYS */;\n"
  212. . "UNLOCK TABLES;\n";
  213. }
  214. /**
  215. * Return table part data SQL insert
  216. *
  217. * @param string $tableName
  218. * @param int $count
  219. * @param int $offset
  220. * @return string
  221. */
  222. public function getPartInsertSql($tableName, $count = null, $offset = null)
  223. {
  224. $sql = null;
  225. $adapter = $this->_getWriteAdapter();
  226. $select = $adapter->select()
  227. ->from($tableName)
  228. ->limit($count, $offset);
  229. $query = $adapter->query($select);
  230. while ($row = $query->fetch()) {
  231. if ($sql === null) {
  232. $sql = sprintf('INSERT INTO %s VALUES ', $adapter->quoteIdentifier($tableName));
  233. } else {
  234. $sql .= ',';
  235. }
  236. $sql .= $this->_quoteRow($tableName, $row);
  237. }
  238. if ($sql !== null) {
  239. $sql .= ';' . "\n";
  240. }
  241. return $sql;
  242. }
  243. /**
  244. * Return table data SQL insert
  245. *
  246. * @param string $tableName
  247. * @return string
  248. */
  249. public function getInsertSql($tableName)
  250. {
  251. return $this->getPartInsertSql($tableName);
  252. }
  253. /**
  254. * Quote Table Row
  255. *
  256. * @param string $tableName
  257. * @param array $row
  258. * @return string
  259. */
  260. protected function _quoteRow($tableName, array $row)
  261. {
  262. $adapter = $this->_getReadAdapter();
  263. $describe = $adapter->describeTable($tableName);
  264. $dataTypes = array('bigint', 'mediumint', 'smallint', 'tinyint');
  265. $rowData = array();
  266. foreach ($row as $k => $v) {
  267. if ($v === null) {
  268. $value = 'NULL';
  269. } elseif (in_array(strtolower($describe[$k]['DATA_TYPE']), $dataTypes)) {
  270. $value = $v;
  271. } else {
  272. $value = $adapter->quoteInto('?', $v);
  273. }
  274. $rowData[] = $value;
  275. }
  276. return sprintf('(%s)', implode(',', $rowData));
  277. }
  278. /**
  279. * Turn on serializable mode
  280. */
  281. public function turnOnSerializableMode()
  282. {
  283. $this->_getReadAdapter()->query("SET SESSION TRANSACTION ISOLATION LEVEL SERIALIZABLE");
  284. }
  285. /**
  286. * Turn on read committed mode
  287. */
  288. public function turnOnReadCommittedMode()
  289. {
  290. $this->_getReadAdapter()->query("SET SESSION TRANSACTION ISOLATION LEVEL READ COMMITTED");
  291. }
  292. }