DBDumper.java 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188
  1. /*
  2. * This program is free software: you can redistribute it and/or modify it under
  3. * the terms of the GNU General Public License as published by the Free Software
  4. * Foundation, either version 3 of the License, or (at your option) any later
  5. * version.
  6. *
  7. * This program is distributed in the hope that it will be useful, but WITHOUT
  8. * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
  9. * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
  10. * details.
  11. *
  12. * You should have received a copy of the GNU General Public License along with
  13. * this program. If not, see <http://www.gnu.org/licenses/>.
  14. */
  15. package com.l2jserver.dbinstaller.util.mysql;
  16. import java.io.File;
  17. import java.io.IOException;
  18. import java.sql.Connection;
  19. import java.sql.PreparedStatement;
  20. import java.sql.ResultSet;
  21. import java.sql.SQLException;
  22. import java.util.ArrayList;
  23. import java.util.Formatter;
  24. import java.util.GregorianCalendar;
  25. import java.util.HashMap;
  26. import java.util.List;
  27. import java.util.Map;
  28. import com.l2jserver.dbinstaller.DBOutputInterface;
  29. import com.l2jserver.dbinstaller.util.FileWriterStdout;
  30. /**
  31. * @author mrTJO
  32. */
  33. public class DBDumper
  34. {
  35. DBOutputInterface _frame;
  36. String _db;
  37. public DBDumper(DBOutputInterface frame, String db)
  38. {
  39. _frame = frame;
  40. _db = db;
  41. createDump();
  42. }
  43. public void createDump()
  44. {
  45. try
  46. {
  47. Connection con = _frame.getConnection();
  48. Formatter form = new Formatter();
  49. PreparedStatement stmt = con.prepareStatement("SHOW TABLES");
  50. ResultSet rset = stmt.executeQuery();
  51. File dump = new File("dumps", form.format("%1$s_dump_%2$tY%2$tm%2$td-%2$tH%2$tM%2$tS.sql", _db, new GregorianCalendar().getTime()).toString());
  52. new File("dumps").mkdir();
  53. dump.createNewFile();
  54. _frame.appendToProgressArea("Writing dump " + dump.getName());
  55. if (rset.last())
  56. {
  57. int rows = rset.getRow();
  58. rset.beforeFirst();
  59. if (rows > 0)
  60. {
  61. _frame.setProgressIndeterminate(false);
  62. _frame.setProgressMaximum(rows);
  63. }
  64. }
  65. FileWriterStdout ps = new FileWriterStdout(dump);
  66. while (rset.next())
  67. {
  68. _frame.setProgressValue(rset.getRow());
  69. _frame.appendToProgressArea("Dumping Table " + rset.getString(1));
  70. ps.println("CREATE TABLE `" + rset.getString(1) + "`");
  71. ps.println("(");
  72. PreparedStatement desc = con.prepareStatement("DESC " + rset.getString(1));
  73. ResultSet dset = desc.executeQuery();
  74. Map<String, List<String>> keys = new HashMap<String, List<String>>();
  75. boolean isFirst = true;
  76. while (dset.next())
  77. {
  78. if (!isFirst)
  79. ps.println(",");
  80. ps.print("\t`" + dset.getString(1) + "`");
  81. ps.print(" " + dset.getString(2));
  82. if (dset.getString(3).equals("NO"))
  83. ps.print(" NOT NULL");
  84. if (!dset.getString(4).isEmpty())
  85. {
  86. if (!keys.containsKey(dset.getString(4)))
  87. keys.put(dset.getString(4), new ArrayList<String>());
  88. keys.get(dset.getString(4)).add(dset.getString(1));
  89. }
  90. if (dset.getString(5) != null)
  91. ps.print(" DEFAULT '" + dset.getString(5) + "'");
  92. if (!dset.getString(6).isEmpty())
  93. ps.print(" " + dset.getString(6));
  94. isFirst = false;
  95. }
  96. if (keys.containsKey("PRI"))
  97. {
  98. ps.println(",");
  99. ps.print("\tPRIMARY KEY (");
  100. isFirst = true;
  101. for (String key : keys.get("PRI"))
  102. {
  103. if (!isFirst)
  104. ps.print(", ");
  105. ps.print("`" + key + "`");
  106. isFirst = false;
  107. }
  108. ps.print(")");
  109. }
  110. if (keys.containsKey("MUL"))
  111. {
  112. ps.println(",");
  113. isFirst = true;
  114. for (String key : keys.get("MUL"))
  115. {
  116. if (!isFirst)
  117. ps.println(", ");
  118. ps.print("\tKEY `key_" + key + "` (`" + key + "`)");
  119. isFirst = false;
  120. }
  121. }
  122. ps.println();
  123. ps.println(");");
  124. ps.flush();
  125. dset.close();
  126. desc.close();
  127. desc = con.prepareStatement("SELECT * FROM " + rset.getString(1));
  128. dset = desc.executeQuery();
  129. isFirst = true;
  130. int cnt = 0;
  131. while (dset.next())
  132. {
  133. if ((cnt % 100) == 0)
  134. ps.println("INSERT INTO `" + rset.getString(1) + "` VALUES ");
  135. else
  136. ps.println(",");
  137. ps.print("\t(");
  138. boolean isInFirst = true;
  139. for (int i = 1; i <= dset.getMetaData().getColumnCount(); i++)
  140. {
  141. if (!isInFirst)
  142. ps.print(", ");
  143. if (dset.getString(i) == null)
  144. ps.print("NULL");
  145. else
  146. ps.print("'" + dset.getString(i).replace("\'", "\\\'") + "'");
  147. isInFirst = false;
  148. }
  149. ps.print(")");
  150. isFirst = false;
  151. if ((cnt % 100) == 99)
  152. ps.println(";");
  153. cnt++;
  154. }
  155. if (!isFirst && (cnt % 100) != 0)
  156. ps.println(";");
  157. ps.println();
  158. ps.flush();
  159. dset.close();
  160. desc.close();
  161. }
  162. rset.close();
  163. stmt.close();
  164. ps.flush();
  165. ps.close();
  166. }
  167. catch (SQLException e)
  168. {
  169. e.printStackTrace();
  170. }
  171. catch (IOException e)
  172. {
  173. e.printStackTrace();
  174. }
  175. _frame.appendToProgressArea("Dump Complete!");
  176. }
  177. }