PageRenderTime 24ms CodeModel.GetById 19ms RepoModel.GetById 0ms app.codeStats 0ms

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

https://bitbucket.org/acidel/buykoala
PHP | 289 lines | 164 code | 27 blank | 98 comment | 15 complexity | 7db08dc783267cf92ae2c133a13c9113 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@magentocommerce.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.magentocommerce.com for more information.
  20. *
  21. * @category Mage
  22. * @package Mage_Backup
  23. * @copyright Copyright (c) 2011 Magento Inc. (http://www.magentocommerce.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. if ($tableName === null) {
  55. $sql = '';
  56. foreach ($this->_foreignKeys as $table => $foreignKeys) {
  57. $sql .= sprintf("ALTER TABLE %s\n %s;\n",
  58. $this->_getReadAdapter()->quoteIdentifier($table),
  59. join(",\n ", $foreignKeys)
  60. );
  61. }
  62. return $sql;
  63. }
  64. return false;
  65. }
  66. /**
  67. * Get create script for table
  68. *
  69. * @param string $tableName
  70. * @param boolean $addDropIfExists
  71. * @return string
  72. */
  73. public function getTableCreateScript($tableName, $addDropIfExists = false)
  74. {
  75. $script = '';
  76. $quotedTableName = $this->_getReadAdapter()->quoteIdentifier($tableName);
  77. if ($addDropIfExists) {
  78. $script .= 'DROP TABLE IF EXISTS ' . $quotedTableName .";\n";
  79. }
  80. //TODO fix me
  81. $sql = 'SHOW CREATE TABLE ' . $quotedTableName;
  82. $data = $this->_getReadAdapter()->fetchRow($sql);
  83. $script .= isset($data['Create Table']) ? $data['Create Table'].";\n" : '';
  84. return $script;
  85. }
  86. /**
  87. * Retrieve SQL fragment for create table
  88. *
  89. * @param string $tableName
  90. * @param bool $withForeignKeys
  91. * @return string
  92. */
  93. public function getTableCreateSql($tableName, $withForeignKeys = false)
  94. {
  95. $adapter = $this->_getReadAdapter();
  96. $quotedTableName = $adapter->quoteIdentifier($tableName);
  97. $query = 'SHOW CREATE TABLE ' . $quotedTableName;
  98. $row = $adapter->fetchRow($query);
  99. if (!$row || !isset($row['Table']) || !isset($row['Create Table'])) {
  100. return false;
  101. }
  102. $regExp = '/,\s+CONSTRAINT `([^`]*)` FOREIGN KEY \(`([^`]*)`\) '
  103. . 'REFERENCES `([^`]*)` \(`([^`]*)`\)'
  104. . '( ON DELETE (RESTRICT|CASCADE|SET NULL|NO ACTION))?'
  105. . '( ON UPDATE (RESTRICT|CASCADE|SET NULL|NO ACTION))?/';
  106. $matches = array();
  107. preg_match_all($regExp, $row['Create Table'], $matches, PREG_SET_ORDER);
  108. if (is_array($matches)) {
  109. foreach ($matches as $match) {
  110. $this->_foreignKeys[$tableName][] = sprintf('ADD CONSTRAINT %s FOREIGN KEY (%s) REFERENCES %s (%s)%s%s',
  111. $adapter->quoteIdentifier($match[1]),
  112. $adapter->quoteIdentifier($match[2]),
  113. $adapter->quoteIdentifier($match[3]),
  114. $adapter->quoteIdentifier($match[4]),
  115. isset($match[5]) ? $match[5] : '',
  116. isset($match[7]) ? $match[7] : ''
  117. );
  118. }
  119. }
  120. if ($withForeignKeys) {
  121. $sql = $row['Create Table'];
  122. } else {
  123. $sql = preg_replace($regExp, '', $row['Create Table']);
  124. }
  125. return $sql . ';';
  126. }
  127. /**
  128. * Returns SQL header data, move from original resource model
  129. *
  130. * @return string
  131. */
  132. public function getHeader()
  133. {
  134. $dbConfig = $this->_getReadAdapter()->getConfig();
  135. $versionRow = $this->_getReadAdapter()->fetchRow('SHOW VARIABLES LIKE \'version\'');
  136. $hostName = !empty($dbConfig['unix_socket']) ? $dbConfig['unix_socket']
  137. : (!empty($dbConfig['host']) ? $dbConfig['host'] : 'localhost');
  138. $header = "-- Magento DB backup\n"
  139. . "--\n"
  140. . "-- Host: {$hostName} Database: {$dbConfig['dbname']}\n"
  141. . "-- ------------------------------------------------------\n"
  142. . "-- Server version: {$versionRow['Value']}\n\n"
  143. . "/*!40101 SET @OLD_CHARACTER_SET_CLIENT=@@CHARACTER_SET_CLIENT */;\n"
  144. . "/*!40101 SET @OLD_CHARACTER_SET_RESULTS=@@CHARACTER_SET_RESULTS */;\n"
  145. . "/*!40101 SET @OLD_COLLATION_CONNECTION=@@COLLATION_CONNECTION */;\n"
  146. . "/*!40101 SET NAMES utf8 */;\n"
  147. . "/*!40014 SET @OLD_UNIQUE_CHECKS=@@UNIQUE_CHECKS, UNIQUE_CHECKS=0 */;\n"
  148. . "/*!40014 SET @OLD_FOREIGN_KEY_CHECKS=@@FOREIGN_KEY_CHECKS, FOREIGN_KEY_CHECKS=0 */;\n"
  149. . "/*!40101 SET @OLD_SQL_MODE=@@SQL_MODE, SQL_MODE='NO_AUTO_VALUE_ON_ZERO' */;\n"
  150. . "/*!40111 SET @OLD_SQL_NOTES=@@SQL_NOTES, SQL_NOTES=0 */;\n";
  151. return $header;
  152. }
  153. /**
  154. * Returns SQL footer data, move from original resource model
  155. *
  156. * @return string
  157. */
  158. public function getFooter()
  159. {
  160. $footer = "\n/*!40101 SET SQL_MODE=@OLD_SQL_MODE */;\n"
  161. . "/*!40014 SET FOREIGN_KEY_CHECKS=@OLD_FOREIGN_KEY_CHECKS */; \n"
  162. . "/*!40014 SET UNIQUE_CHECKS=@OLD_UNIQUE_CHECKS */;\n"
  163. . "/*!40101 SET CHARACTER_SET_CLIENT=@OLD_CHARACTER_SET_CLIENT */;\n"
  164. . "/*!40101 SET CHARACTER_SET_RESULTS=@OLD_CHARACTER_SET_RESULTS */;\n"
  165. . "/*!40101 SET COLLATION_CONNECTION=@OLD_COLLATION_CONNECTION */;\n"
  166. . "/*!40111 SET SQL_NOTES=@OLD_SQL_NOTES */;\n"
  167. . "\n-- Dump completed on " . Mage::getSingleton('core/date')->gmtDate() . " GMT";
  168. return $footer;
  169. }
  170. /**
  171. * Retrieve before insert data SQL fragment
  172. *
  173. * @param string $tableName
  174. * @return string
  175. */
  176. public function getTableDataBeforeSql($tableName)
  177. {
  178. $quotedTableName = $this->_getReadAdapter()->quoteIdentifier($tableName);
  179. return "\n--\n"
  180. . "-- Dumping data for table {$quotedTableName}\n"
  181. . "--\n\n"
  182. . "LOCK TABLES {$quotedTableName} WRITE;\n"
  183. . "/*!40000 ALTER TABLE {$quotedTableName} DISABLE KEYS */;\n";
  184. }
  185. /**
  186. * Retrieve after insert data SQL fragment
  187. *
  188. * @param string $tableName
  189. * @return string
  190. */
  191. public function getTableDataAfterSql($tableName)
  192. {
  193. $quotedTableName = $this->_getReadAdapter()->quoteIdentifier($tableName);
  194. return "/*!40000 ALTER TABLE {$quotedTableName} ENABLE KEYS */;\n"
  195. . "UNLOCK TABLES;\n";
  196. }
  197. /**
  198. * Return table part data SQL insert
  199. *
  200. * @param string $tableName
  201. * @return string
  202. */
  203. public function getInsertSql($tableName)
  204. {
  205. $sql = null;
  206. $adapter = $this->_getWriteAdapter();
  207. $select = $adapter->select()
  208. ->from($tableName);
  209. $query = $adapter->query($select);
  210. while ($row = $query->fetch()) {
  211. if ($sql === null) {
  212. $sql = sprintf('INSERT INTO %s VALUES ', $adapter->quoteIdentifier($tableName));
  213. } else {
  214. $sql .= ',';
  215. }
  216. $sql .= $this->_quoteRow($tableName, $row);
  217. }
  218. if ($sql !== null) {
  219. $sql .= ';' . "\n";
  220. }
  221. return $sql;
  222. }
  223. /**
  224. * Quote Table Row
  225. *
  226. * @param string $tableName
  227. * @param array $row
  228. * @return string
  229. */
  230. protected function _quoteRow($tableName, array $row)
  231. {
  232. $adapter = $this->_getReadAdapter();
  233. $describe = $adapter->describeTable($tableName);
  234. $dataTypes = array('bigint', 'mediumint', 'smallint', 'tinyint');
  235. $rowData = array();
  236. foreach ($row as $k => $v) {
  237. if ($v === null) {
  238. $value = 'NULL';
  239. } elseif (in_array(strtolower($describe[$k]['DATA_TYPE']), $dataTypes)) {
  240. $value = $v;
  241. } else {
  242. $value = $adapter->quoteInto('?', $v);
  243. }
  244. $rowData[] = $value;
  245. }
  246. return sprintf('(%s)', implode(',', $rowData));
  247. }
  248. /**
  249. * Turn on serializable mode
  250. */
  251. public function turnOnSerializableMode()
  252. {
  253. $this->_getReadAdapter()->query("SET SESSION TRANSACTION ISOLATION LEVEL SERIALIZABLE");
  254. }
  255. /**
  256. * Turn on read committed mode
  257. */
  258. public function turnOnReadCommittedMode()
  259. {
  260. $this->_getReadAdapter()->query("SET SESSION TRANSACTION ISOLATION LEVEL READ COMMITTED");
  261. }
  262. }