This repository was archived by the owner on Feb 13, 2025. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathDefaultQueryDialect.java
265 lines (230 loc) · 10.8 KB
/
DefaultQueryDialect.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
package com.upsolver.datasources.jdbc.querybuilders;
import com.upsolver.datasources.jdbc.JDBCTaskMetadata;
import com.upsolver.datasources.jdbc.metadata.SimpleSqlType;
import com.upsolver.datasources.jdbc.metadata.TableInfo;
import com.upsolver.datasources.jdbc.utils.NamedPreparedStatment;
import com.upsolver.datasources.jdbc.utils.ThrowingBiFunction;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.JDBCType;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.SQLType;
import java.time.Instant;
import java.util.Arrays;
import java.util.Collection;
import java.util.Collections;
import java.util.HashSet;
import java.util.Map;
public class DefaultQueryDialect implements QueryDialect {
private static final Logger logger = LoggerFactory.getLogger(DefaultQueryDialect.class);
private static final Collection<SQLType> timeStampTypes = new HashSet<>(Arrays.asList(
JDBCType.DATE,
JDBCType.TIMESTAMP,
JDBCType.TIMESTAMP_WITH_TIMEZONE
));
private static final ThrowingBiFunction<ResultSet, Integer, String, SQLException> getString = ResultSet::getString;
private final Map<Integer, ThrowingBiFunction<ResultSet, Integer, String, SQLException>> valueGetters;
public DefaultQueryDialect() {
this(Collections.emptyMap());
}
public DefaultQueryDialect(Map<Integer, ThrowingBiFunction<ResultSet, Integer, String, SQLException>> valueGetters) {
this.valueGetters = valueGetters;
}
@Override
public long utcOffsetSeconds(Connection connection) throws SQLException {
var rs = connection.prepareStatement("SELECT TIMEDIFF(NOW(), UTC_TIMESTAMP)").executeQuery();
rs.next();
return rs.getTime(1).getTime() / 1000;
}
@Override
public NamedPreparedStatment taskInfoByInc(TableInfo tableInfo,
JDBCTaskMetadata metadata,
Connection connection) throws SQLException {
String incColumn = tableInfo.getIncColumn();
String query = "SELECT MIN(" + incColumn + ") AS MIN," +
" MAX(" + incColumn + ") AS MAX" +
" FROM " + fullTableName(tableInfo) +
" WHERE " + incColumn + " >= :startFrom" +
" HAVING MIN( " + incColumn + ") IS NOT NULL";
var statement = new NamedPreparedStatment(connection, query);
statement.setLong("startFrom", metadata.getExclusiveEnd());
return statement;
}
@Override
public NamedPreparedStatment taskInfoByTime(TableInfo tableInfo,
JDBCTaskMetadata metadata,
Instant maxTime,
Connection connection) throws SQLException {
String coalescedTimes = coalesceTimeColumns(tableInfo);
String query = "SELECT MAX(" + coalescedTimes + ") AS last_time" +
" FROM " + fullTableName(tableInfo) +
" WHERE " + coalescedTimes + " < :maxTime" +
" AND " + coalescedTimes + " > :startTime" +
" HAVING MAX( " + coalescedTimes + ") IS NOT NULL";
var statement = new NamedPreparedStatment(connection, query);
statement.setTime("startTime", metadata.getEndTime());
statement.setTime("maxTime", maxTime);
return statement;
}
@Override
public NamedPreparedStatment taskInfoByIncAndTime(TableInfo tableInfo,
JDBCTaskMetadata metadata,
Instant maxTime,
Connection connection) throws SQLException {
String coalescedTimes = coalesceTimeColumns(tableInfo);
String incColumn = tableInfo.getIncColumn();
String query = "SELECT MIN(" + incColumn + ") AS MIN," +
" MAX(" + incColumn + ") AS MAX," +
" MAX(" + coalescedTimes + ") AS last_time" +
" FROM " + fullTableName(tableInfo) +
" WHERE " + coalescedTimes + " < :maxTime" +
" AND ((" + coalescedTimes + " = :startTime AND " + incColumn + " >= :startFrom)" +
" OR (" + coalescedTimes + " > :startTime))" +
" HAVING MIN( " + incColumn + ") IS NOT NULL";
var statement = new NamedPreparedStatment(connection, query);
statement.setLong("startFrom", metadata.getExclusiveEnd());
statement.setTime("startTime", metadata.getEndTime());
statement.setTime("maxTime", maxTime);
return statement;
}
@Override
public NamedPreparedStatment queryByIncAndTime(TableInfo tableInfo,
JDBCTaskMetadata metadata,
int limit,
Connection connection) throws SQLException {
String coalesce = coalesceTimeColumns(tableInfo);
String incColumn = tableInfo.getIncColumn();
String query = "SELECT " + topLimit(limit) + " *" +
" FROM " + fullTableName(tableInfo) +
" WHERE " + coalesce + " < :endTime" +
" AND ((" + coalesce + " = :startTime AND " + incColumn + " >= :incStart)" +
" OR (" + coalesce + " > :startTime))" +
rownumCondition(limit, true, false) +
" ORDER BY " + coalesce + ", " + incColumn + " ASC" +
" " + endLimit(limit);
var statement = new NamedPreparedStatment(connection, query);
statement.setLong("incStart", metadata.getInclusiveStart());
statement.setTime("startTime", metadata.getStartTime());
statement.setTime("endTime", metadata.getEndTime());
return statement;
}
@Override
public NamedPreparedStatment queryByTime(TableInfo tableInfo,
JDBCTaskMetadata metadata,
int limit,
Connection connection) throws SQLException {
String coalesce = coalesceTimeColumns(tableInfo);
String query = "SELECT " + topLimit(limit) + " *" +
" FROM " + fullTableName(tableInfo) +
" WHERE " + coalesce + " > :startTime AND " + coalesce + " <= :endTime" +
rownumCondition(limit, true, false) +
" ORDER BY " + coalesce + " ASC" +
" " + endLimit(limit);
var statement = new NamedPreparedStatment(connection, query);
statement.setTime("startTime", metadata.getStartTime());
statement.setTime("endTime", metadata.getEndTime());
return statement;
}
@Override
public NamedPreparedStatment queryByInc(TableInfo tableInfo,
JDBCTaskMetadata metadata,
int limit,
Connection connection) throws SQLException {
String incColumn = tableInfo.getIncColumn();
String query = "SELECT " + topLimit(limit) + " *" +
" FROM " + fullTableName(tableInfo) +
" WHERE " + incColumn + " BETWEEN :incStart AND :incEnd" +
rownumCondition(limit, true, false) +
" " + endLimit(limit);
var statement = new NamedPreparedStatment(connection, query);
statement.setLong("incStart", metadata.getInclusiveStart());
statement.setLong("incEnd", metadata.getExclusiveEnd() - 1);
return statement;
}
@Override
public NamedPreparedStatment queryFullTable(TableInfo tableInfo,
JDBCTaskMetadata metadata,
int limit,
Connection connection) throws SQLException {
String query = "SELECT " + topLimit(limit) + " *" +
" FROM " + fullTableName(tableInfo) +
rownumCondition(limit, false, true) +
" " + endLimit(limit);
return new NamedPreparedStatment(connection, query);
}
@Override
public PreparedStatement getCurrentTimestamp(Connection connection) throws SQLException {
return connection.prepareStatement(currentTimeQuery());
}
protected String currentTimeQuery() {
return "SELECT CURRENT_TIMESTAMP";
}
private String coalesce(String columns) {
return "COALESCE(" + columns + ")";
}
private String coalesceTimeColumns(TableInfo tableInfo) {
return tableInfo.getTimeColumns().length > 1 ? coalesce(tableInfo.getTimeColumnsAsString()) : tableInfo.getTimeColumnsAsString();
}
protected String topLimit(long amount) {
return "";
}
protected String rownumCondition(long amount, boolean includeAnd, boolean includeWhere) {
return "";
}
@Override
public boolean requiresUppercaseNames() {
return false;
}
@Override
public String toUpperCaseIfRequired(String s) {
return s != null && requiresUppercaseNames() ? s.toUpperCase() : s;
}
protected String endLimit(long amount) {
return amount >= 0 ? "limit " + amount : "";
}
@Override
public boolean isAutoIncrementColumn(ResultSet columnsResultSet) throws SQLException {
return "yes".equalsIgnoreCase(columnsResultSet.getString("IS_AUTOINCREMENT"));
}
@Override
public boolean isTimeType(SQLType sqlType) throws SQLException {
return timeStampTypes.contains(getJdbcType(sqlType));
}
@Override
public SQLType getSqlType(int code) throws SQLException {
try {
return JDBCType.valueOf(code);
} catch (IllegalArgumentException e) {
logger.warn("Illegal data type {}", code);
return new SimpleSqlType("unknown", "default", code);
}
}
@Override
public SQLType getJdbcType(SQLType sqlType) {
return sqlType;
}
@Override
public String fullTableName(TableInfo tableInfo) {
if(tableInfo.getSchema() != null && !tableInfo.getSchema().isEmpty()){
return tableInfo.getSchema() + "." + tableInfo.getName();
} else {
return tableInfo.getName();
}
}
@Override
public Connection getConnection(String url, java.util.Properties info) throws SQLException {
return DriverManager.getConnection(url, info);
}
@Override
public String getDriverClassName() {
return null;
}
@Override
public ThrowingBiFunction<ResultSet, Integer, String, SQLException> getStringValueGetter(int sqlType) {
return valueGetters.getOrDefault(sqlType, getString);
}
}