ScriptExecutor.java 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136
  1. /*
  2. * Copyright (C) 2004-2015 L2J Server
  3. *
  4. * This file is part of L2J Server.
  5. *
  6. * L2J Server is free software: you can redistribute it and/or modify
  7. * it under the terms of the GNU General Public License as published by
  8. * the Free Software Foundation, either version 3 of the License, or
  9. * (at your option) any later version.
  10. *
  11. * L2J Server is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  14. * General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU General Public License
  17. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  18. */
  19. package com.l2jserver.tools.dbinstaller.util.mysql;
  20. import java.awt.HeadlessException;
  21. import java.io.File;
  22. import java.io.FileNotFoundException;
  23. import java.sql.Connection;
  24. import java.sql.SQLException;
  25. import java.sql.Statement;
  26. import java.util.Arrays;
  27. import java.util.Scanner;
  28. import javax.swing.JOptionPane;
  29. import com.l2jserver.tools.dbinstaller.DBOutputInterface;
  30. import com.l2jserver.util.file.filter.SQLFilter;
  31. /**
  32. * @author mrTJO
  33. */
  34. public class ScriptExecutor
  35. {
  36. DBOutputInterface _frame;
  37. public ScriptExecutor(DBOutputInterface frame)
  38. {
  39. _frame = frame;
  40. }
  41. public void execSqlBatch(File dir)
  42. {
  43. execSqlBatch(dir, false);
  44. }
  45. public void execSqlBatch(File dir, boolean skipErrors)
  46. {
  47. final File[] files = dir.listFiles(new SQLFilter());
  48. if (files != null)
  49. {
  50. Arrays.sort(files);
  51. _frame.setProgressIndeterminate(false);
  52. _frame.setProgressMaximum(files.length - 1);
  53. for (int i = 0; i < files.length; i++)
  54. {
  55. _frame.setProgressValue(i);
  56. execSqlFile(files[i], skipErrors);
  57. }
  58. }
  59. }
  60. public void execSqlFile(File file)
  61. {
  62. execSqlFile(file, false);
  63. }
  64. public void execSqlFile(File file, boolean skipErrors)
  65. {
  66. _frame.appendToProgressArea("Installing " + file.getName());
  67. String line = "";
  68. Connection con = _frame.getConnection();
  69. try (Statement stmt = con.createStatement();
  70. Scanner scn = new Scanner(file))
  71. {
  72. StringBuilder sb = new StringBuilder();
  73. while (scn.hasNextLine())
  74. {
  75. line = scn.nextLine();
  76. if (line.startsWith("--"))
  77. {
  78. continue;
  79. }
  80. else if (line.contains("--"))
  81. {
  82. line = line.split("--")[0];
  83. }
  84. line = line.trim();
  85. if (!line.isEmpty())
  86. {
  87. sb.append(line + System.getProperty("line.separator"));
  88. }
  89. if (line.endsWith(";"))
  90. {
  91. stmt.execute(sb.toString());
  92. sb = new StringBuilder();
  93. }
  94. }
  95. }
  96. catch (FileNotFoundException e)
  97. {
  98. JOptionPane.showMessageDialog(null, "File Not Found!: " + e.getMessage(), "Installer Error", JOptionPane.ERROR_MESSAGE);
  99. }
  100. catch (SQLException e)
  101. {
  102. if (!skipErrors)
  103. {
  104. try
  105. {
  106. Object[] options =
  107. {
  108. "Continue",
  109. "Abort"
  110. };
  111. int n = JOptionPane.showOptionDialog(null, "MySQL Error: " + e.getMessage(), "Script Error", JOptionPane.YES_NO_OPTION, JOptionPane.WARNING_MESSAGE, null, options, options[0]);
  112. if (n == 1)
  113. {
  114. System.exit(0);
  115. }
  116. }
  117. catch (HeadlessException h)
  118. {
  119. e.printStackTrace();
  120. }
  121. }
  122. }
  123. }
  124. }