Advertisement
Guest User

Untitled

a guest
May 3rd, 2018
73
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.87 KB | None | 0 0
  1. package eu.fastcore.core.mysql.store.modes;
  2.  
  3. import org.bukkit.scheduler.*;
  4.  
  5. import eu.fastcore.core.Main;
  6. import eu.fastcore.core.mysql.store.Callback;
  7. import eu.fastcore.core.mysql.store.Store;
  8. import eu.fastcore.core.utils.Logger;
  9. import eu.fastcore.core.utils.TimeUtil;
  10. import eu.fastcore.core.utils.Timming;
  11.  
  12. import org.bukkit.plugin.*;
  13.  
  14. import java.sql.*;
  15. import java.util.concurrent.Executor;
  16. import java.util.concurrent.Executors;
  17. import java.util.concurrent.atomic.AtomicInteger;
  18.  
  19. public class StoreMySQL implements Store
  20. {
  21. private final String host;
  22. private final String user;
  23. private final String pass;
  24. private final String name;
  25. private final String prefix;
  26. private final int port;
  27. private Connection conn;
  28. private long time;
  29. private Executor executor;
  30. private AtomicInteger ai;
  31.  
  32. public StoreMySQL(final String host, final int port, final String user, final String pass, final String name, final String prefix) {
  33. super();
  34. this.host = host;
  35. this.port = port;
  36. this.user = user;
  37. this.pass = pass;
  38. this.name = name;
  39. this.prefix = prefix;
  40. this.executor = Executors.newSingleThreadExecutor();
  41. this.time = System.currentTimeMillis();
  42. this.ai = new AtomicInteger();
  43. new BukkitRunnable() {
  44. public void run() {
  45. if (System.currentTimeMillis() - StoreMySQL.this.time > TimeUtil.SECOND.getTime(30)) {
  46. StoreMySQL.this.update(false, "DO 1");
  47. }
  48. }
  49. }.runTaskTimer((Plugin) Main.getPlugin(), 600L, 600L);
  50. }
  51.  
  52. public boolean connect() {
  53. final Timming t = new Timming("MySQL ping").start();
  54. try {
  55. Class.forName("com.mysql.jdbc.Driver");
  56. Logger.info("jdbc:mysql://" + this.host + ":" + this.port + "/" + this.name, this.user, this.pass);
  57. this.conn = DriverManager.getConnection("jdbc:mysql://" + this.host + ":" + this.port + "/" + this.name, this.user, this.pass);
  58. Logger.info("Connected to the MySQL server!", "Connection ping " + t.stop().getExecutingTime() + "ms!");
  59. return true;
  60. }
  61. catch (ClassNotFoundException e) {
  62. Logger.warning("JDBC driver not found!", "Error: " + e.getMessage());
  63. e.printStackTrace();
  64. }
  65. catch (SQLException e2) {
  66. Logger.warning("Can not connect to a MySQL server!", "Error: " + e2.getMessage());
  67. e2.printStackTrace();
  68. }
  69. return false;
  70. }
  71.  
  72. public void update(final boolean now, final String update) {
  73. this.time = System.currentTimeMillis();
  74. final Runnable r = new Runnable() {
  75. public void run() {
  76. try {
  77. StoreMySQL.this.conn.createStatement().executeUpdate(update.replace("{P}", StoreMySQL.this.prefix));
  78. }
  79. catch (SQLException e) {
  80. Logger.warning("An error occurred with given query '" + update.replace("{P}", StoreMySQL.this.prefix) + "'!", "Error: " + e.getMessage());
  81. e.printStackTrace();
  82. }
  83. }
  84. };
  85. if (now) {
  86. r.run();
  87. }
  88. else {
  89. this.executor.execute(r);
  90. }
  91. }
  92.  
  93. public ResultSet update(final String update) {
  94. try {
  95. final Statement statement = this.conn.createStatement();
  96. statement.executeUpdate(update.replace("{P}", this.prefix), 1);
  97. final ResultSet rs = statement.getGeneratedKeys();
  98. if (rs.next()) {
  99. return rs;
  100. }
  101. }
  102. catch (SQLException e) {
  103. Logger.warning("An error occurred with given query '" + update.replace("{P}", this.prefix) + "'!", "Error: " + e.getMessage());
  104. e.printStackTrace();
  105. }
  106. return null;
  107. }
  108.  
  109. public void disconnect() {
  110. if (this.conn != null) {
  111. try {
  112. this.conn.close();
  113. }
  114. catch (SQLException e) {
  115. Logger.warning("Can not close the connection to the MySQL server!", "Error: " + e.getMessage());
  116. e.printStackTrace();
  117. }
  118. }
  119. }
  120.  
  121. public void reconnect() {
  122. this.connect();
  123. }
  124.  
  125. public boolean isConnected() {
  126. try {
  127. return !this.conn.isClosed() || this.conn == null;
  128. }
  129. catch (SQLException e) {
  130. e.printStackTrace();
  131. return false;
  132. }
  133. }
  134.  
  135. public ResultSet query(final String query) {
  136. try {
  137. return this.conn.createStatement().executeQuery(query.replace("{P}", this.prefix));
  138. }
  139. catch (SQLException e) {
  140. Logger.warning("An error occurred with given query '" + query.replace("{P}", this.prefix) + "'!", "Error: " + e.getMessage());
  141. e.printStackTrace();
  142. return null;
  143. }
  144. }
  145.  
  146. public void query(final String query, final Callback<ResultSet> cb) {
  147. new Thread(new Runnable() {
  148. public void run() {
  149. try {
  150. final ResultSet rs = StoreMySQL.this.conn.createStatement().executeQuery(query.replace("{P}", StoreMySQL.this.prefix));
  151. cb.done(rs);
  152. }
  153. catch (SQLException e) {
  154. Logger.warning("An error occurred with given query '" + query.replace("{P}", StoreMySQL.this.prefix) + "'!", "Error: " + e.getMessage());
  155. cb.error(e);
  156. e.printStackTrace();
  157. }
  158. }
  159. }, "MySQL Thread #" + this.ai.getAndIncrement()).start();
  160. }
  161.  
  162. public Connection getConnection() {
  163. return this.conn;
  164. }
  165.  
  166. public StoreMode getStoreMode() {
  167. return StoreMode.MYSQL;
  168. }
  169. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement