-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathQueries.java
345 lines (307 loc) · 13.4 KB
/
Queries.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
package app.velodata;
import org.postgresql.util.PGobject;
import java.io.PrintWriter;
import java.io.StringWriter;
import java.sql.*;
import java.time.Instant;
import java.time.format.DateTimeParseException;
import java.util.*;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.LinkedBlockingDeque;
public class Queries {
private final ExecutorService threads;
private final LinkedBlockingDeque<String> output;
private final Map<Integer, Connection> connections;
private final Set<Integer> txns;
private final int queryTimeout;
public Queries(ExecutorService threads, LinkedBlockingDeque<String> output, Map<Integer, Connection> connections, Set<Integer> txns, int queryTimeout) {
this.threads = threads;
this.output = output;
this.connections = connections;
this.txns = txns;
this.queryTimeout = queryTimeout;
}
private boolean isUpdate(String query) {
String test = query.trim().toUpperCase();
return test.startsWith("CREATE") || test.startsWith("ALTER") || test.startsWith("DROP") ||
test.startsWith("INSERT") || test.startsWith("UPDATE") || test.startsWith("DELETE") || test.startsWith("LOCK");
}
private boolean isReturning(String query) {
return query.toUpperCase().contains("RETURNING");
}
private boolean isSupportedType(String type) {
return type.equals("text") || type.equals("jsonb") || type.equals("varchar") || type.equals("timestamptz") ||
type.equals("int4") || type.equals("int8") || type.equals("numeric") || type.equals("bigserial") ||
type.equals("float8") || type.equals("bool") || type.equals("void");
}
private String encodeToString(Object output) {
if (output == null) { return ""; }
return output.toString();
}
private List<Integer> findIndexes(String str, String find) {
List<Integer> results = new LinkedList<>();
int idx = str.indexOf(find);
if (idx < 0) { return results; }
while (idx >= 0) {
results.add(idx);
idx = str.indexOf(find, idx + 1);
}
return results;
}
private void bindArg(PreparedStatement stmt, String type, int idx, String arg) throws SQLException {
if (!isSupportedType(type)) { throw new SQLException("bind arg - unsupported sql type " + type); }
Object obj = null;
if (arg.isEmpty() || type.equals("void")) {
stmt.setObject(idx, null);
return;
}
try {
switch (type) {
case "text":
case "jsonb":
case "varchar":
case "timestamptz":
if (arg.startsWith("s")) {
byte[] decoded = Base64.getDecoder().decode(arg.substring(1));
if (new String(decoded).equals("\"\"")) { decoded = new byte[0]; }
obj = new String(decoded);
stmt.setObject(idx, obj);
} else if (arg.startsWith("j")) {
byte[] decoded = Base64.getDecoder().decode(arg.substring(1));
PGobject pgObject = new PGobject();
pgObject.setType("json");
pgObject.setValue(new String(decoded));
stmt.setObject(idx, pgObject);
} else if (arg.startsWith("t")) {
stmt.setTimestamp(idx, Timestamp.from(Instant.parse(arg.substring(1))));
} else {
stmt.setObject(idx, arg);
}
break;
case "bool":
obj = arg.equals("true") ? Boolean.TRUE : Boolean.FALSE;
stmt.setObject(idx, obj);
break;
case "int4":
obj = Integer.parseInt(arg);
stmt.setObject(idx, obj);
break;
case "int8":
case "numeric":
case "bigserial":
obj = Long.parseLong(arg);
stmt.setObject(idx, obj);
break;
case "float8":
obj = Double.parseDouble(arg);
stmt.setObject(idx, obj);
break;
}
} catch (NumberFormatException e) {
throw new SQLException("bind arg - failed to parse idx " + idx + " to number");
} catch (DateTimeParseException e) {
throw new SQLException("bind arg - failed to parse idx " + idx + " to timestamp");
} catch (Exception e) {
throw new SQLException("bind arg - failed to base64 decode idx " + idx);
}
}
private String[] readRow(String[] cols, ResultSet from) throws SQLException {
String[] row = new String[cols.length];
for (int i = 0; i < cols.length; i++) {
String col = cols[i].split(":")[0];
String type = cols[i].split(":")[1];
if (!isSupportedType(type)) { throw new SQLException("read row - unsupported col type " + col + " = " + type); }
switch (type) {
case "text":
case "jsonb":
case "varchar":
row[i] = from.getString(i+1);
if (row[i] != null && row[i].isEmpty()) { row[i] = "\"\""; }
if (row[i] != null) { row[i] = "s" + Base64.getEncoder().encodeToString(row[i].getBytes()); }
row[i] = encodeToString(row[i]);
break;
case "timestamptz":
row[i] = encodeToString(from.getString(i+1));
break;
case "bool":
row[i] = encodeToString(from.getObject(i+1, Boolean.class));
break;
case "int4":
row[i] = encodeToString(from.getObject(i+1, Integer.class));
break;
case "int8":
case "numeric":
case "bigserial":
row[i] = encodeToString(from.getObject(i+1, Long.class));
break;
case "float8":
row[i] = encodeToString(from.getObject(i+1, Double.class));
break;
case "void":
row[i] = encodeToString(null);
break;
}
}
return row;
}
private static class BindArg implements Comparable<BindArg> {
final String arg;
final int index;
public BindArg(String arg, int index) {
this.arg = arg;
this.index = index;
}
@Override
public int compareTo(BindArg other) {
return this.index - other.index;
}
}
// translate SELECT $1, $2, $3 to SELECT ?, ?, ?
private List<String> prepQuery(String query, List<String> args) throws SQLException {
String queryOut = query;
List<BindArg> lookup = new LinkedList<>();
for (int i = args.size(); i > 0; i--) {
String token = "\\$"+i;
String test = token.replace("\\", "");
if (!queryOut.contains(test)) { throw new SQLException("query args do not match template string: " + query); }
List<Integer> indexes = findIndexes(queryOut, test);
for (Integer idx : indexes) { lookup.add(new BindArg(args.get(i-1), idx)); }
queryOut = queryOut.replaceAll(token, "?");
}
if (queryOut.contains("$")) { throw new SQLException("query args do not match template string: " + query); }
List<String> result = new LinkedList<>();
Collections.sort(lookup);
result.add(queryOut);
for (BindArg arg : lookup) { result.add(arg.arg); }
return result;
}
public void queue(Integer connNum, String queryId, Connection conn, String query, List<String> args) throws AppException {
try {
args = prepQuery(query, args);
query = args.get(0);
args = args.subList(1, args.size());
threads.submit(new QueryTask(connNum, queryId, conn, query, args));
} catch (Exception e) {
throw new AppException(connNum, queryId, e);
}
}
private class QueryTask implements Runnable {
private final Integer connNum;
private final String queryId;
private final Connection conn;
private final String query;
private final List<String> args;
public QueryTask(Integer connNum, String queryId, Connection conn, String query, List<String> args) {
this.connNum = connNum;
this.queryId = queryId;
this.conn = conn;
this.query = query;
this.args = args;
}
private List<String[]> run(PreparedStatement stmt) throws SQLException {
List<String[]> result = new LinkedList<>();
// bind args
ParameterMetaData params = stmt.getParameterMetaData();
for (int i = 0; i < params.getParameterCount(); i++) {
String type = params.getParameterTypeName(i + 1);
bindArg(stmt, type, i + 1, args.get(i));
}
// exec update without RETURNING keyword
if (isUpdate(query) && !isReturning(query)) {
int count = stmt.executeUpdate();
// first item is cols + types (none in this case)
result.add(null);
// second item is update count and row count
String[] counts = new String[]{""+count, "0"};
result.add(counts);
return result;
}
// exec update with RETURNING or select
ResultSet rows = stmt.executeQuery();
// add column names and types to result
ResultSetMetaData meta = rows.getMetaData();
String[] cols = new String[meta.getColumnCount()];
int c = 0;
for (int i = 1; i <= meta.getColumnCount(); i++) {
String columnName = meta.getColumnName(i);
String columnType = meta.getColumnTypeName(i);
if (isSupportedType(columnName)) { columnName = ++c + ""; }
else if (columnName.startsWith("?")) { columnName = ++c + ""; }
cols[i-1] = columnName + ":" + columnType;
}
result.add(cols);
// second item is update count and row count
String[] counts = new String[]{"0", "0"};
result.add(counts);
// add rows to result
int count = 0;
while (rows.next()) {
result.add(readRow(cols, rows));
count++;
}
// second item is update count and row count
if (isReturning(query)) { result.get(1)[0] = ""+count; }
result.get(1)[1] = ""+count;
return result;
}
private void queueOutput(String data) {
output.add("o:" + connNum + "," + queryId + data);
}
private void queueStackTrace(Exception e) {
StringWriter stack = new StringWriter();
e.printStackTrace(new PrintWriter(stack));
String info = "i," + stack;
info = info.replace("\n", " ");
output.add("i:" + info);
}
private void handleClose() {
try {
txns.remove(connNum);
Connection conn = connections.remove(connNum);
if (conn != null) { conn.close(); }
} catch (Exception ignore) { }
}
private void queueError(String error) {
error = error.replace("\n", " ").replace(",", " ");
if (error.toLowerCase().contains("closed")) {
handleClose();
output.add("e:" + connNum + ",closed");
output.add("i:i,connection " + connNum + " closed unexpectedly");
return;
}
output.add("e:" + connNum + "," + queryId + "," + error);
}
private void queueError(Exception e) {
String error = e.getMessage();
if (error == null) { error = e.getClass().getName(); }
queueError(error);
queueStackTrace(e);
}
@Override
public void run() {
try (PreparedStatement stmt = conn.prepareStatement(query)) {
stmt.setQueryTimeout(queryTimeout);
List<String[]> result = run(stmt);
String[] cols = result.get(0);
String[] counts = result.get(1);
List<String[]> rows = result.subList(2, result.size());
if (cols == null) {
queueOutput("," + counts[0] + "," + counts[1]);
return;
}
StringBuilder data = new StringBuilder();
for (String col : cols) { data.append(",").append(col); }
queueOutput("," + counts[0] + "," + counts[1] + data);
for (String[] row : rows) {
data = new StringBuilder();
for (String val : row) { data.append(",").append(val); }
queueOutput(data.toString());
}
} catch (SQLTimeoutException e1) {
queueError("Query read timeout");
} catch (Exception e2) {
queueError(e2);
}
}
}
}