/
SQLCommand.java
392 lines (372 loc) · 19.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
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
package com.denizenscript.denizencore.scripts.commands.core;
import com.denizenscript.denizencore.exceptions.InvalidArgumentsException;
import com.denizenscript.denizencore.scripts.commands.AbstractCommand;
import com.denizenscript.denizencore.scripts.commands.Holdable;
import com.denizenscript.denizencore.utilities.debugging.dB;
import com.denizenscript.denizencore.utilities.scheduling.AsyncSchedulable;
import com.denizenscript.denizencore.utilities.scheduling.OneTimeSchedulable;
import com.denizenscript.denizencore.DenizenCore;
import com.denizenscript.denizencore.objects.Element;
import com.denizenscript.denizencore.objects.aH;
import com.denizenscript.denizencore.objects.dList;
import com.denizenscript.denizencore.scripts.ScriptEntry;
import com.denizenscript.denizencore.tags.core.EscapeTags;
import java.sql.*;
import java.util.HashMap;
import java.util.Map;
import java.util.Properties;
public class SQLCommand extends AbstractCommand implements Holdable {
// <--[command]
// @Name SQL
// @Syntax sql [id:<ID>] [disconnect/connect:<server> (username:<username>) (password:<password>) (ssl:true/{false})/query:<query>/update:<update>]
// @Required 2
// @Short Interacts with a MySQL server.
// @Group core
//
// @Description
// This command is used to interact with a MySQL server. It can update the database or query it for information.
// The general usage order is connect -> update/query -> disconnect.
// It is not required that you disconnect right after using, and in fact encouraged that you keep a connection open where possible.
// When connecting, the server format is IP:Port/Database, EG 'localhost:3306/test'.
// You can switch whether SSL is used for the connection (defaults to false).
// Note that when using tag, it is recommended you escape unusual inputs to avoid SQL injection.
// The SQL command is merely a wrapper for SQL queries, and further usage details should be gathered from an official
// MySQL query reference rather than from Denizen command help.
// SQL connections are not instant - they can take several seconds, or just never connect at all.
// It is recommended you hold the connection command by doing "- ~sql ..." rather than just "- sql ..."
// as this will delay the commands following the connect command until after the connection is established.
//
// @Tags
// <entry[saveName].result> returns a dList of all rows from a query or update command, of the form li@escaped_text/escaped_text|escaped_text/escaped_text
// <entry[saveName].affected_rows> returns how many rows were affected by an update command.
//
// @Usage
// Use to connect to an SQL server.
// - ~sql id:name connect:localhost:3306/test username:space password:space
//
// @Usage
// Use to connect to an SQL server over an SSL connection.
// - ~sql id:name connect:localhost:3306/test username:space password:space ssl:true
//
// @Usage
// Use to connect to an SQL server with a UTF8 text encoding.
// - ~sql id:name connect:localhost:3306/test?characterEncoding=utf8 username:space password:space
//
// @Usage
// Use to update an SQL server.
// - sql id:name "update:CREATE table things(id int,column_name1 varchar(255),column_name2 varchar(255));"
//
// @Usage
// Use to update an SQL server.
// - sql id:name "update:INSERT INTO things VALUES (3, 'hello', 'space');"
//
// @Usage
// Use to query an SQL server.
// - sql id:name "query:SELECT id,column_name1,column_name2 FROM things;" save:saveName
// - narrate <entry[saveName].result>
//
// @Usage
// Use to query an SQL server.
// - sql id:name "query:SELECT id,column_name1,column_name2 FROM things WHERE id=3;" save:saveName2
// - narrate <entry[saveName2].result>
//
// @Usage
// Use to disconnect from an SQL server.
// - sql disconnect id:name
// -->
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.interpretArguments(scriptEntry.aHArgs)) {
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;
}
DenizenCore.schedule(new AsyncSchedulable(new OneTimeSchedulable(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) {
DenizenCore.schedule(new OneTimeSchedulable(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);
}
}
}, 0));
}
if (dB.verbose) {
dB.echoDebug(scriptEntry, "Connection did not error");
}
final Connection conn = con;
if (con != null) {
DenizenCore.schedule(new OneTimeSchedulable(new Runnable() {
@Override
public void run() {
connections.put(sqlID.asString().toUpperCase(), conn);
dB.echoDebug(scriptEntry, "Successfully connected to " + server);
scriptEntry.setFinished(true);
}
}, 0));
}
else {
DenizenCore.schedule(new OneTimeSchedulable(new Runnable() {
@Override
public void run() {
scriptEntry.setFinished(true);
if (dB.verbose) {
dB.echoDebug(scriptEntry, "Connecting errored!");
}
}
}, 0));
}
}
}, 0)));
}
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()) {
DenizenCore.schedule(new AsyncSchedulable(new OneTimeSchedulable(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();
DenizenCore.schedule(new OneTimeSchedulable(new Runnable() {
@Override
public void run() {
dB.echoDebug(scriptEntry, "Got a query result of " + columns + " columns");
}
}, 0));
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;
DenizenCore.schedule(new OneTimeSchedulable(new Runnable() {
@Override
public void run() {
dB.echoDebug(scriptEntry, "Got a query result of " + finalCount + " rows");
scriptEntry.setFinished(true);
}
}, 0));
}
catch (final Exception e) {
DenizenCore.schedule(new OneTimeSchedulable(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);
}
}
}, 0));
}
}
}, 0)));
}
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);
}
}