/
SQLCommand.java
332 lines (313 loc) · 16.3 KB
/
SQLCommand.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
package net.aufdemrand.denizen.scripts.commands.core;
import net.aufdemrand.denizen.utilities.DenizenAPI;
import net.aufdemrand.denizencore.exceptions.InvalidArgumentsException;
import net.aufdemrand.denizencore.objects.Element;
import net.aufdemrand.denizencore.objects.aH;
import net.aufdemrand.denizencore.objects.dList;
import net.aufdemrand.denizencore.scripts.ScriptEntry;
import net.aufdemrand.denizencore.scripts.commands.AbstractCommand;
import net.aufdemrand.denizencore.scripts.commands.Holdable;
import net.aufdemrand.denizencore.tags.core.EscapeTags;
import net.aufdemrand.denizencore.utilities.debugging.dB;
import org.bukkit.Bukkit;
import java.sql.*;
import java.util.HashMap;
import java.util.Map;
import java.util.Properties;
public class SQLCommand extends AbstractCommand implements Holdable {
public static Map<String, Connection> connections = new HashMap<>();
@Override
public void onDisable() {
for (Map.Entry<String, Connection> entry : connections.entrySet()) {
try {
entry.getValue().close();
}
catch (SQLException e) {
dB.echoError(e);
}
}
connections.clear();
}
@Override
public void parseArgs(ScriptEntry scriptEntry) throws InvalidArgumentsException {
for (aH.Argument arg : aH.interpret(scriptEntry.getArguments())) {
if (!scriptEntry.hasObject("sqlid")
&& arg.matchesPrefix("id")) {
scriptEntry.addObject("sqlid", arg.asElement());
}
else if (!scriptEntry.hasObject("action")
&& arg.matchesPrefix("connect")) {
scriptEntry.addObject("action", new Element("CONNECT"));
scriptEntry.addObject("server", arg.asElement());
}
else if (!scriptEntry.hasObject("action")
&& arg.matches("disconnect")) {
scriptEntry.addObject("action", new Element("DISCONNECT"));
}
else if (!scriptEntry.hasObject("query")
&& arg.matchesPrefix("query")) {
scriptEntry.addObject("action", new Element("QUERY"));
scriptEntry.addObject("query", arg.asElement());
}
else if (!scriptEntry.hasObject("query")
&& arg.matchesPrefix("update")) {
scriptEntry.addObject("action", new Element("UPDATE"));
scriptEntry.addObject("query", arg.asElement());
}
else if (!scriptEntry.hasObject("username")
&& arg.matchesPrefix("username")) {
scriptEntry.addObject("username", arg.asElement());
}
else if (!scriptEntry.hasObject("password")
&& arg.matchesPrefix("password")) {
scriptEntry.addObject("password", arg.asElement());
}
else if (!scriptEntry.hasObject("ssl")
&& arg.matchesPrefix("ssl")
&& arg.asElement().isBoolean()) {
scriptEntry.addObject("ssl", arg.asElement());
}
else {
arg.reportUnhandled();
}
}
if (!scriptEntry.hasObject("sqlid")) {
throw new InvalidArgumentsException("Must specify an ID!");
}
if (!scriptEntry.hasObject("ssl")) {
scriptEntry.defaultObject("ssl", new Element("false"));
}
if (!scriptEntry.hasObject("action")) {
throw new InvalidArgumentsException("Must specify an action!");
}
}
@Override
public void execute(final ScriptEntry scriptEntry) {
Element action = scriptEntry.getElement("action");
final Element server = scriptEntry.getElement("server");
final Element username = scriptEntry.getElement("username");
final Element password = scriptEntry.getElement("password");
final Element ssl = scriptEntry.getElement("ssl");
final Element sqlID = scriptEntry.getElement("sqlid");
final Element query = scriptEntry.getElement("query");
if (scriptEntry.dbCallShouldDebug()) {
dB.report(scriptEntry, getName(), sqlID.debug()
+ action.debug()
+ (server != null ? server.debug() : "")
+ (username != null ? username.debug() : "")
+ (password != null ? aH.debugObj("password", "NotLogged") : "")
+ (query != null ? query.debug() : ""));
}
if (!action.asString().equalsIgnoreCase("connect") &&
(!action.asString().equalsIgnoreCase("query") || !scriptEntry.shouldWaitFor())) {
scriptEntry.setFinished(true);
}
try {
if (action.asString().equalsIgnoreCase("connect")) {
if (server == null) {
dB.echoError(scriptEntry.getResidingQueue(), "Must specify a server!");
return;
}
if (username == null) {
dB.echoError(scriptEntry.getResidingQueue(), "Must specify a username!");
return;
}
if (password == null) {
dB.echoError(scriptEntry.getResidingQueue(), "Must specify a password!");
return;
}
if (connections.containsKey(sqlID.asString().toUpperCase())) {
dB.echoError(scriptEntry.getResidingQueue(), "Already connected to a server with ID '" + sqlID.asString() + "'!");
return;
}
Bukkit.getScheduler().runTaskLaterAsynchronously(DenizenAPI.getCurrentInstance(), new Runnable() {
@Override
public void run() {
Connection con = null;
if (dB.verbose) {
dB.echoDebug(scriptEntry, "Connecting to " + server.asString());
}
try {
con = getConnection(username.asString(), password.asString(), server.asString(), ssl.asString());
}
catch (final Exception e) {
Bukkit.getScheduler().runTaskLater(DenizenAPI.getCurrentInstance(), new Runnable() {
@Override
public void run() {
dB.echoError(scriptEntry.getResidingQueue(), "SQL Exception: " + e.getMessage());
scriptEntry.setFinished(true);
if (dB.verbose) {
dB.echoError(scriptEntry.getResidingQueue(), e);
}
}
}, 1);
}
if (dB.verbose) {
dB.echoDebug(scriptEntry, "Connection did not error");
}
final Connection conn = con;
if (con != null) {
Bukkit.getScheduler().runTaskLater(DenizenAPI.getCurrentInstance(), new Runnable() {
@Override
public void run() {
connections.put(sqlID.asString().toUpperCase(), conn);
dB.echoDebug(scriptEntry, "Successfully connected to " + server);
scriptEntry.setFinished(true);
}
}, 1);
}
else {
Bukkit.getScheduler().runTaskLater(DenizenAPI.getCurrentInstance(), new Runnable() {
@Override
public void run() {
scriptEntry.setFinished(true);
if (dB.verbose) {
dB.echoDebug(scriptEntry, "Connecting errored!");
}
}
}, 1);
}
}
}, 1);
}
else if (action.asString().equalsIgnoreCase("disconnect")) {
Connection con = connections.get(sqlID.asString().toUpperCase());
if (con == null) {
dB.echoError(scriptEntry.getResidingQueue(), "Not connected to server with ID '" + sqlID.asString() + "'!");
return;
}
con.close();
connections.remove(sqlID.asString().toUpperCase());
dB.echoDebug(scriptEntry, "Disconnected from '" + sqlID.asString() + "'.");
}
else if (action.asString().equalsIgnoreCase("query")) {
if (query == null) {
dB.echoError(scriptEntry.getResidingQueue(), "Must specify a query!");
return;
}
final Connection con = connections.get(sqlID.asString().toUpperCase());
if (con == null) {
dB.echoError(scriptEntry.getResidingQueue(), "Not connected to server with ID '" + sqlID.asString() + "'!");
return;
}
dB.echoDebug(scriptEntry, "Running query " + query.asString());
if (scriptEntry.shouldWaitFor()) {
Bukkit.getScheduler().runTaskLaterAsynchronously(DenizenAPI.getCurrentInstance(), new Runnable() {
@Override
public void run() {
try {
Statement statement = con.createStatement();
ResultSet set = statement.executeQuery(query.asString());
ResultSetMetaData rsmd = set.getMetaData();
final int columns = rsmd.getColumnCount();
Bukkit.getScheduler().runTaskLater(DenizenAPI.getCurrentInstance(), new Runnable() {
@Override
public void run() {
dB.echoDebug(scriptEntry, "Got a query result of " + columns + " columns");
}
}, 1);
int count = 0;
dList rows = new dList();
while (set.next()) {
count++;
StringBuilder current = new StringBuilder();
for (int i = 0; i < columns; i++) {
current.append(EscapeTags.escape(set.getString(i + 1))).append("/");
}
rows.add(current.toString());
}
scriptEntry.addObject("result", rows);
final int finalCount = count;
Bukkit.getScheduler().runTaskLater(DenizenAPI.getCurrentInstance(), new Runnable() {
@Override
public void run() {
dB.echoDebug(scriptEntry, "Got a query result of " + finalCount + " rows");
scriptEntry.setFinished(true);
}
}, 1);
}
catch (final Exception e) {
Bukkit.getScheduler().runTaskLater(DenizenAPI.getCurrentInstance(), new Runnable() {
@Override
public void run() {
dB.echoError(scriptEntry.getResidingQueue(), "SQL Exception: " + e.getMessage());
scriptEntry.setFinished(true);
if (dB.verbose) {
dB.echoError(scriptEntry.getResidingQueue(), e);
}
}
}, 1);
}
}
}, 1);
}
else {
Statement statement = con.createStatement();
ResultSet set = statement.executeQuery(query.asString());
ResultSetMetaData rsmd = set.getMetaData();
final int columns = rsmd.getColumnCount();
dB.echoDebug(scriptEntry, "Got a query result of " + columns + " columns");
int count = 0;
dList rows = new dList();
while (set.next()) {
count++;
StringBuilder current = new StringBuilder();
for (int i = 0; i < columns; i++) {
current.append(EscapeTags.escape(set.getString(i + 1))).append("/");
}
rows.add(current.toString());
}
scriptEntry.addObject("result", rows);
final int finalCount = count;
dB.echoDebug(scriptEntry, "Got a query result of " + finalCount + " rows");
}
}
else if (action.asString().equalsIgnoreCase("update")) {
if (query == null) {
dB.echoError(scriptEntry.getResidingQueue(), "Must specify an update query!");
return;
}
Connection con = connections.get(sqlID.asString().toUpperCase());
if (con == null) {
dB.echoError(scriptEntry.getResidingQueue(), "Not connected to server with ID '" + sqlID.asString() + "'!");
return;
}
dB.echoDebug(scriptEntry, "Running update " + query.asString());
Statement statement = con.createStatement();
int affected = statement.executeUpdate(query.asString(), Statement.RETURN_GENERATED_KEYS);
scriptEntry.addObject("affected_rows", new Element(affected));
ResultSet set = statement.getGeneratedKeys();
ResultSetMetaData rsmd = set.getMetaData();
int columns = rsmd.getColumnCount();
dB.echoDebug(scriptEntry, "Got a query result of " + columns + " columns");
dList rows = new dList();
while (set.next()) {
StringBuilder current = new StringBuilder();
for (int i = 0; i < columns; i++) {
current.append(EscapeTags.escape(set.getString(i + 1))).append("/");
}
rows.add(current.toString());
}
scriptEntry.addObject("result", rows);
dB.echoDebug(scriptEntry, "Updated " + affected + " rows");
}
else {
dB.echoError(scriptEntry.getResidingQueue(), "Unknown action '" + action.asString() + "'");
}
}
catch (SQLException e) {
dB.echoError(scriptEntry.getResidingQueue(), "SQL Exception: " + e.getMessage());
if (dB.verbose) {
dB.echoError(scriptEntry.getResidingQueue(), e);
}
}
}
public Connection getConnection(String userName, String password, String server, String ssl) throws SQLException {
Properties connectionProps = new Properties();
connectionProps.put("user", userName);
connectionProps.put("password", password);
connectionProps.put("useSSL", ssl);
connectionProps.put("LoginTimeout", "7");
return DriverManager.getConnection("jdbc:mysql://" + server, connectionProps);
}
}