DBDumper.java 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187
  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. *
  32. * @author mrTJO
  33. */
  34. public class DBDumper
  35. {
  36. DBOutputInterface _frame;
  37. String _db;
  38. public DBDumper(DBOutputInterface frame, String db)
  39. {
  40. _frame = frame;
  41. _db = db;
  42. createDump();
  43. }
  44. public void createDump()
  45. {
  46. try
  47. {
  48. Connection con = _frame.getConnection();
  49. Formatter form = new Formatter();
  50. PreparedStatement stmt = con.prepareStatement("SHOW TABLES");
  51. ResultSet rset = stmt.executeQuery();
  52. File dump = new File("dumps", form.format("%1$s_dump_%2$tY%2$tm%2$td-%2$tH%2$tM%2$tS.sql",
  53. _db, new GregorianCalendar().getTime()).toString());
  54. new File("dumps").mkdir();
  55. dump.createNewFile();
  56. _frame.appendToProgressArea("Writing dump "+dump.getName());
  57. if (rset.last())
  58. {
  59. int rows = rset.getRow();
  60. rset.beforeFirst();
  61. if (rows > 0)
  62. {
  63. _frame.setProgressIndeterminate(false);
  64. _frame.setProgressMaximum(rows);
  65. }
  66. }
  67. FileWriterStdout ps = new FileWriterStdout(dump);
  68. while (rset.next())
  69. {
  70. _frame.setProgressValue(rset.getRow());
  71. _frame.appendToProgressArea("Dumping Table "+rset.getString(1));
  72. ps.println("CREATE TABLE `"+rset.getString(1)+"`");
  73. ps.println("(");
  74. PreparedStatement desc = con.prepareStatement("DESC "+rset.getString(1));
  75. ResultSet dset = desc.executeQuery();
  76. Map<String, List<String>> keys = new HashMap<String, List<String>>();
  77. boolean isFirst = true;
  78. while (dset.next())
  79. {
  80. if (!isFirst) ps.println(",");
  81. ps.print("\t`"+dset.getString(1)+"`");
  82. ps.print(" "+dset.getString(2));
  83. if (dset.getString(3).equals("NO"))
  84. ps.print(" NOT NULL");
  85. if (!dset.getString(4).isEmpty())
  86. {
  87. if (!keys.containsKey(dset.getString(4)))
  88. keys.put(dset.getString(4), new ArrayList<String>());
  89. keys.get(dset.getString(4)).add(dset.getString(1));
  90. }
  91. if (dset.getString(5) != null)
  92. ps.print(" DEFAULT '"+dset.getString(5)+"'");
  93. if (!dset.getString(6).isEmpty())
  94. ps.print(" "+dset.getString(6));
  95. isFirst = false;
  96. }
  97. if (keys.containsKey("PRI"))
  98. {
  99. ps.println(",");
  100. ps.print("\tPRIMARY KEY (");
  101. isFirst = true;
  102. for (String key : keys.get("PRI"))
  103. {
  104. if (!isFirst) 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) ps.println(", ");
  117. ps.print("\tKEY `key_"+key+"` (`"+key+"`)");
  118. isFirst = false;
  119. }
  120. }
  121. ps.println();
  122. ps.println(");");
  123. ps.flush();
  124. dset.close();
  125. desc.close();
  126. desc = con.prepareStatement("SELECT * FROM "+rset.getString(1));
  127. dset = desc.executeQuery();
  128. isFirst = true;
  129. int cnt = 0;
  130. while (dset.next())
  131. {
  132. if ((cnt%100) == 0)
  133. ps.println("INSERT INTO `"+rset.getString(1)+"` VALUES ");
  134. else
  135. ps.println(",");
  136. ps.print("\t(");
  137. boolean isInFirst = true;
  138. for (int i = 1; i <= dset.getMetaData().getColumnCount(); i++)
  139. {
  140. if (!isInFirst)
  141. ps.print(", ");
  142. if (dset.getString(i) == null)
  143. ps.print("NULL");
  144. else
  145. ps.print("'"+dset.getString(i).replace("\'", "\\\'")+"'");
  146. isInFirst = false;
  147. }
  148. ps.print(")");
  149. isFirst = false;
  150. if ((cnt%100) == 99)
  151. ps.println(";");
  152. cnt++;
  153. }
  154. if (!isFirst && (cnt%100) != 0)
  155. ps.println(";");
  156. ps.println();
  157. ps.flush();
  158. dset.close();
  159. desc.close();
  160. }
  161. rset.close();
  162. stmt.close();
  163. ps.flush();
  164. ps.close();
  165. }
  166. catch (SQLException e)
  167. {
  168. e.printStackTrace();
  169. }
  170. catch (IOException e)
  171. {
  172. e.printStackTrace();
  173. }
  174. _frame.appendToProgressArea("Dump Complete!");
  175. }
  176. }