Advertisement
Guest User

Untitled

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