· 9 years ago · Oct 02, 2016, 03:10 PM
1
2import net.md_5.bungee.api.ProxyServer;
3import tn.bungeemanager.ConfigLibrary.FileConfiguration;
4import tn.bungeemanager.Core;
5
6import java.sql.*;
7import java.util.HashMap;
8import java.util.concurrent.TimeUnit;
9import java.util.logging.Level;
10
11public class Database {
12
13 private Core plugin;
14 //private Connection connection;
15
16 private enum ConnectionType {
17 PUNISHMENT, PLAYER, SERVER, PERMISSIONS, GAMESTATS
18 }
19
20 private HashMap<ConnectionType, Connection> connections;
21
22 public Database(Core plugin) {
23 this.plugin = plugin;
24
25 if (!init()) {
26 plugin.log(Level.SEVERE, "MySQL - SQL Connection has failed (Check config)", "BungeeManager - Plugin has been disabled");
27 ProxyServer.getInstance().getPluginManager().getPlugins().remove(plugin);
28 }
29
30 ProxyServer.getInstance().getScheduler().schedule(plugin, () -> {
31 try {
32 // //if (connection != null && !connection.isClosed()) {
33 /// connection.createStatement().execute("/* ping */ SELECT 1");
34 /// }
35 for(Connection connection : connections.values()) {
36 if (connection != null && !connection.isClosed()) {
37 connection.createStatement().execute("/* ping */ SELECT 1");
38 }
39 }
40 } catch (SQLException e) {
41 // connection = getNewConnection();
42 closeAll();
43 for(ConnectionType type : ConnectionType.values()) {
44 getNewConnection(type);
45 }
46 }
47 }, 5, 5, TimeUnit.MINUTES);
48 }
49
50 private void close(Connection c) {
51 try {
52 c.close();
53 } catch (SQLException e) {
54 e.printStackTrace();
55 plugin.log(Level.SEVERE, "MySQL - Failed to close connection ");
56 }
57 }
58
59 public void closeAll() {
60 for(Connection c : connections.values()) {
61 close(c);
62 }
63 connections.clear();
64 }
65
66 /**
67 * @return - Return connection to the SQL
68 * Initialise the connection to the SQL
69 */
70 protected Connection getNewConnection(ConnectionType type) {
71 FileConfiguration cf = plugin.getConfig();
72 try {
73 Class.forName("com.mysql.jdbc.Driver");
74
75 String url = "jdbc:mysql://" + cf.getString("Database.Host") + ":" + cf.getString("Database.Port") + "/" + type.toString();
76 return DriverManager.getConnection(url, cf.getString("Database.Username"), cf.getString("Database.Password"));
77 } catch (Exception e) {
78 e.printStackTrace();
79 plugin.log(Level.SEVERE, "MySQL - SQL Connection has failed to create a new connection");
80 return null;
81 }
82 }
83
84 public boolean init() {
85 //return checkConnection();
86 return checkAllConnections();
87 }
88
89 private boolean checkAllConnections() {
90 for(ConnectionType con : ConnectionType.values()) {
91 try {
92 if(connections.get(con) == null || connections.get(con).isClosed()) {
93 Connection connection = getNewConnection(con);
94 connections.put(con, connection);
95 }
96
97 if(connections.get(con) == null || connections.get(con).isClosed())
98 return false;
99 } catch (SQLException e) {
100 e.printStackTrace();
101 return false;
102 }
103 }
104 return true;
105 }
106
107 /* private boolean checkConnection() {
108 try {
109 if (connection == null || connection.isClosed()) {
110 connection = getNewConnection();
111
112 if (connection == null || connection.isClosed())
113 return false;
114
115 query("CREATE TABLE IF NOT EXISTS MCParkourTimes (ID INT(10) NOT NULL auto_increment, UUID VARCHAR(36) NOT NULL, ParkourName VARCHAR(20) NOT NULL, Time INT(10) NOT NULL, Name VARCHAR(16), PRIMARY KEY (ID))");
116 query("CREATE TABLE IF NOT EXISTS MCPlayers (UUID VARCHAR(36), XP INT(10), Rank VARCHAR(30), ParkoursCompleted INT(10))");
117 query("CREATE TABLE IF NOT EXISTS MCVis (UUID VARCHAR(36), Visibility BOOLEAN)");
118
119 query("CREATE TABLE IF NOT EXISTS TN_Players (UUID VARCHAR(36), Name VARCHAR(16), Credits INT(10), XP INT(10), Level INT(10))");
120
121 query("CREATE TABLE IF NOT EXISTS GM_Groups (Name VARCHAR(36), Prefix VARCHAR(30), Suffix VARCHAR(30), Weight INT(3))");
122 query("CREATE TABLE IF NOT EXISTS GM_Inheritance (Parent VARCHAR(36), Child (36))");
123 query("CREATE TABLE IF NOT EXISTS GM_Players (UUID VARCHAR(36), PrimaryGroup VARCHAR(36), SecondaryGroups VARCHAR(200))");
124 }
125 } catch (SQLException e) {
126 e.printStackTrace();
127 plugin.log(Level.SEVERE, "MySQL - Failed to check connection");
128 return false;
129 }
130
131 return true;
132 }*/
133
134 private boolean query(ConnectionType type, String sql) throws SQLException {
135 //return connection.createStatement().execute(sql);
136 return connections.get(type).createStatement().execute(sql);
137 }
138
139 /*public void close() {
140 try {
141 if (connection != null)
142 connection.close();
143 } catch (SQLException e) {
144 e.printStackTrace();
145 }
146 }*/
147
148 /**
149 * Main Query
150 */
151
152 private synchronized ResultSet sqlQuery(String query, Connection connection) {
153 Statement statement = null;
154 try {
155 statement = connection.createStatement();
156 } catch ( SQLException e ) {
157 e.printStackTrace();
158 plugin.log(Level.SEVERE, "MySQL - Failed to create statement in sqlQuery ");
159 }
160 ResultSet result = null;
161 try {
162 assert statement != null;
163 result = statement.executeQuery( query );
164 } catch ( SQLException e ) {
165 e.printStackTrace();
166 plugin.log(Level.SEVERE, "MySQL - Failed to perform statement in sqlQuery ");
167 }
168 return result;
169 }
170
171 /**
172 * Any query which does not return a ResultSet object. Such as : INSERT,
173 * UPDATE, CREATE TABLE...
174 *
175 */
176 public void standardQuery(ConnectionType type, String query ) {
177 // standardQuery( query, connection );
178 standardQuery(query, connections.get(type));
179 }
180
181 /**
182 * Check whether a field/entry exists in a database.
183 * @return Whether or not a result has been found in the query.
184 */
185 public boolean existanceQuery(ConnectionType type, String query ) {
186 boolean check = false;
187 try {
188 //check = sqlQuery( query, connection ).next();
189 check = sqlQuery(query, connections.get(type)).next();
190 } catch (SQLException e) {
191 e.printStackTrace();
192 plugin.log(Level.SEVERE, "MySQL - Failed to perform existance query ");
193 }
194 return check;
195 }
196
197 /**
198 * Any query which returns a ResultSet object. Such as : SELECT Remember to
199 * close the ResultSet object after you are done with it to free up
200 * resources immediately. ----- ResultSet set =
201 * sqlQuery("SELECT * FROM sometable;"); set.doSomething(); set.close();
202 * -----
203 * @return ResultSet
204 */
205 public ResultSet sqlQuery(ConnectionType type, String query ) {
206 ResultSet res;
207 // res = sqlQuery( query, connection );
208 res = sqlQuery(query, connections.get(type));
209 return res;
210 }
211
212 /**
213 * Check whether the table name exists.
214 */
215
216 private synchronized int standardQuery(String query, Connection connection) {
217 Statement statement;
218 int rowsUpdated = 0;
219
220 try {
221 statement = connection.createStatement();
222
223 rowsUpdated = statement.executeUpdate( query );
224
225 statement.close();
226 } catch (SQLException e) {
227 e.printStackTrace();
228 plugin.log(Level.SEVERE, "MySQL - Failed to perform Standard Query");
229 }
230
231 return rowsUpdated;
232 }
233
234}