Skip to content

Commit 63700fe

Browse files
committed
SQL: Extract SQL request and response classes (#30457)
Extracts SQL request and response classes. This is the first step towards creation of a small minimal dependencies jdbc driver. Relates #29856
1 parent 56c3414 commit 63700fe

File tree

61 files changed

+909
-589
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

61 files changed

+909
-589
lines changed

x-pack/plugin/sql/jdbc/src/main/java/org/elasticsearch/xpack/sql/jdbc/jdbc/JdbcConnection.java

+5-1
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,10 @@ public class JdbcConnection implements Connection, JdbcWrapper {
4343
private String catalog;
4444
private String schema;
4545

46+
/**
47+
* The SQLException is the only type of Exception the JDBC API can throw (and that the user expects).
48+
* If we remove it, we need to make sure no other types of Exceptions (runtime or otherwise) are thrown
49+
*/
4650
public JdbcConnection(JdbcConfiguration connectionInfo) throws SQLException {
4751
cfg = connectionInfo;
4852
client = new JdbcHttpClient(connectionInfo);
@@ -428,4 +432,4 @@ int esInfoMajorVersion() throws SQLException {
428432
int esInfoMinorVersion() throws SQLException {
429433
return client.serverInfo().minorVersion;
430434
}
431-
}
435+
}

x-pack/plugin/sql/jdbc/src/main/java/org/elasticsearch/xpack/sql/jdbc/jdbc/JdbcStatement.java

+3-3
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77

88
import org.elasticsearch.xpack.sql.jdbc.net.client.Cursor;
99
import org.elasticsearch.xpack.sql.jdbc.net.client.RequestMeta;
10-
import org.elasticsearch.xpack.sql.plugin.SqlTypedParamValue;
10+
import org.elasticsearch.xpack.sql.proto.SqlTypedParamValue;
1111

1212
import java.sql.Connection;
1313
import java.sql.ResultSet;
@@ -220,7 +220,7 @@ public int getFetchSize() throws SQLException {
220220
// unset (in this case -1 which the user cannot set) - in this case, the default fetch size is returned
221221
// 0 meaning the hint is disabled (the user has called setFetch)
222222
// >0 means actual hint
223-
223+
224224
// tl;dr - unless the user set it, returning the default is fine
225225
return requestMeta.fetchSize();
226226
}
@@ -402,4 +402,4 @@ final void resultSetWasClosed() throws SQLException {
402402
close();
403403
}
404404
}
405-
}
405+
}

x-pack/plugin/sql/jdbc/src/main/java/org/elasticsearch/xpack/sql/jdbc/jdbc/PreparedQuery.java

+3-3
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
package org.elasticsearch.xpack.sql.jdbc.jdbc;
77

88
import org.elasticsearch.xpack.sql.jdbc.JdbcSQLException;
9-
import org.elasticsearch.xpack.sql.plugin.SqlTypedParamValue;
9+
import org.elasticsearch.xpack.sql.proto.SqlTypedParamValue;
1010
import org.elasticsearch.xpack.sql.type.DataType;
1111

1212
import java.sql.JDBCType;
@@ -73,7 +73,7 @@ String sql() {
7373
*/
7474
List<SqlTypedParamValue> params() {
7575
return Arrays.stream(this.params).map(
76-
paramInfo -> new SqlTypedParamValue(paramInfo.value, DataType.fromJdbcType(paramInfo.type))
76+
paramInfo -> new SqlTypedParamValue(DataType.fromJdbcType(paramInfo.type), paramInfo.value)
7777
).collect(Collectors.toList());
7878
}
7979

@@ -86,4 +86,4 @@ public String toString() {
8686
static PreparedQuery prepare(String sql) throws SQLException {
8787
return new PreparedQuery(sql, SqlQueryParameterAnalyzer.parametersCount(sql));
8888
}
89-
}
89+
}

x-pack/plugin/sql/jdbc/src/main/java/org/elasticsearch/xpack/sql/jdbc/net/client/JdbcHttpClient.java

+17-16
Original file line numberDiff line numberDiff line change
@@ -5,22 +5,21 @@
55
*/
66
package org.elasticsearch.xpack.sql.jdbc.net.client;
77

8-
import org.elasticsearch.action.main.MainResponse;
98
import org.elasticsearch.common.collect.Tuple;
109
import org.elasticsearch.common.unit.TimeValue;
1110
import org.elasticsearch.xpack.sql.client.HttpClient;
1211
import org.elasticsearch.xpack.sql.jdbc.jdbc.JdbcConfiguration;
1312
import org.elasticsearch.xpack.sql.jdbc.net.protocol.ColumnInfo;
1413
import org.elasticsearch.xpack.sql.jdbc.net.protocol.InfoResponse;
15-
import org.elasticsearch.xpack.sql.plugin.AbstractSqlQueryRequest;
16-
import org.elasticsearch.xpack.sql.plugin.AbstractSqlRequest;
17-
import org.elasticsearch.xpack.sql.plugin.SqlQueryRequest;
18-
import org.elasticsearch.xpack.sql.plugin.SqlQueryResponse;
19-
import org.elasticsearch.xpack.sql.plugin.SqlTypedParamValue;
14+
import org.elasticsearch.xpack.sql.proto.Mode;
15+
import org.elasticsearch.xpack.sql.proto.Protocol;
16+
import org.elasticsearch.xpack.sql.proto.SqlQueryRequest;
17+
import org.elasticsearch.xpack.sql.proto.MainResponse;
18+
import org.elasticsearch.xpack.sql.proto.SqlQueryResponse;
19+
import org.elasticsearch.xpack.sql.proto.SqlTypedParamValue;
2020

2121
import java.sql.SQLException;
2222
import java.util.List;
23-
import java.util.TimeZone;
2423
import java.util.stream.Collectors;
2524

2625
import static org.elasticsearch.xpack.sql.client.shared.StringUtils.EMPTY;
@@ -34,6 +33,10 @@ public class JdbcHttpClient {
3433
private final JdbcConfiguration conCfg;
3534
private InfoResponse serverInfo;
3635

36+
/**
37+
* The SQLException is the only type of Exception the JDBC API can throw (and that the user expects).
38+
* If we remove it, we need to make sure no other types of Exceptions (runtime or otherwise) are thrown
39+
*/
3740
public JdbcHttpClient(JdbcConfiguration conCfg) throws SQLException {
3841
httpClient = new HttpClient(conCfg);
3942
this.conCfg = conCfg;
@@ -45,9 +48,9 @@ public boolean ping(long timeoutInMs) throws SQLException {
4548

4649
public Cursor query(String sql, List<SqlTypedParamValue> params, RequestMeta meta) throws SQLException {
4750
int fetch = meta.fetchSize() > 0 ? meta.fetchSize() : conCfg.pageSize();
48-
SqlQueryRequest sqlRequest = new SqlQueryRequest(AbstractSqlRequest.Mode.JDBC, sql, params, null,
49-
AbstractSqlQueryRequest.DEFAULT_TIME_ZONE,
50-
fetch, TimeValue.timeValueMillis(meta.timeoutInMs()), TimeValue.timeValueMillis(meta.queryTimeoutInMs()), "");
51+
SqlQueryRequest sqlRequest = new SqlQueryRequest(Mode.JDBC, sql, params, null,
52+
Protocol.TIME_ZONE,
53+
fetch, TimeValue.timeValueMillis(meta.timeoutInMs()), TimeValue.timeValueMillis(meta.queryTimeoutInMs()));
5154
SqlQueryResponse response = httpClient.query(sqlRequest);
5255
return new DefaultCursor(this, response.cursor(), toJdbcColumnInfo(response.columns()), response.rows(), meta);
5356
}
@@ -57,10 +60,8 @@ public Cursor query(String sql, List<SqlTypedParamValue> params, RequestMeta met
5760
* the scroll id to use to fetch the next page.
5861
*/
5962
public Tuple<String, List<List<Object>>> nextPage(String cursor, RequestMeta meta) throws SQLException {
60-
SqlQueryRequest sqlRequest = new SqlQueryRequest().cursor(cursor);
61-
sqlRequest.mode(AbstractSqlRequest.Mode.JDBC);
62-
sqlRequest.requestTimeout(TimeValue.timeValueMillis(meta.timeoutInMs()));
63-
sqlRequest.pageTimeout(TimeValue.timeValueMillis(meta.queryTimeoutInMs()));
63+
SqlQueryRequest sqlRequest = new SqlQueryRequest(Mode.JDBC, cursor, TimeValue.timeValueMillis(meta.timeoutInMs()),
64+
TimeValue.timeValueMillis(meta.queryTimeoutInMs()));
6465
SqlQueryResponse response = httpClient.query(sqlRequest);
6566
return new Tuple<>(response.cursor(), response.rows());
6667
}
@@ -78,13 +79,13 @@ public InfoResponse serverInfo() throws SQLException {
7879

7980
private InfoResponse fetchServerInfo() throws SQLException {
8081
MainResponse mainResponse = httpClient.serverInfo();
81-
return new InfoResponse(mainResponse.getClusterName().value(), mainResponse.getVersion().major, mainResponse.getVersion().minor);
82+
return new InfoResponse(mainResponse.getClusterName(), mainResponse.getVersion().major, mainResponse.getVersion().minor);
8283
}
8384

8485
/**
8586
* Converts REST column metadata into JDBC column metadata
8687
*/
87-
private List<ColumnInfo> toJdbcColumnInfo(List<org.elasticsearch.xpack.sql.plugin.ColumnInfo> columns) {
88+
private List<ColumnInfo> toJdbcColumnInfo(List<org.elasticsearch.xpack.sql.proto.ColumnInfo> columns) {
8889
return columns.stream().map(columnInfo ->
8990
new ColumnInfo(columnInfo.name(), columnInfo.jdbcType(), EMPTY, EMPTY, EMPTY, EMPTY, columnInfo.displaySize())
9091
).collect(Collectors.toList());

x-pack/plugin/sql/jdbc/src/test/java/org/elasticsearch/xpack/sql/jdbc/jdbc/TypeConverterTests.java

+2-2
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,8 @@
1010
import org.elasticsearch.common.xcontent.XContentHelper;
1111
import org.elasticsearch.common.xcontent.json.JsonXContent;
1212
import org.elasticsearch.test.ESTestCase;
13-
import org.elasticsearch.xpack.sql.plugin.AbstractSqlRequest;
1413
import org.elasticsearch.xpack.sql.plugin.SqlQueryResponse;
14+
import org.elasticsearch.xpack.sql.proto.Mode;
1515
import org.joda.time.DateTime;
1616

1717
import java.sql.JDBCType;
@@ -51,7 +51,7 @@ private Object convertAsNative(Object value, JDBCType type) throws Exception {
5151
XContentBuilder builder = JsonXContent.contentBuilder();
5252
builder.startObject();
5353
builder.field("value");
54-
SqlQueryResponse.value(builder, AbstractSqlRequest.Mode.JDBC, value);
54+
SqlQueryResponse.value(builder, Mode.JDBC, value);
5555
builder.endObject();
5656
builder.close();
5757
Object copy = XContentHelper.convertToMap(BytesReference.bytes(builder), false, builder.contentType()).v2().get("value");

x-pack/plugin/sql/sql-cli/src/main/java/org/elasticsearch/xpack/sql/cli/command/CliSession.java

+3-2
Original file line numberDiff line numberDiff line change
@@ -5,11 +5,12 @@
55
*/
66
package org.elasticsearch.xpack.sql.cli.command;
77

8-
import org.elasticsearch.action.main.MainResponse;
98
import org.elasticsearch.xpack.sql.client.HttpClient;
109
import org.elasticsearch.xpack.sql.client.shared.ClientException;
1110
import org.elasticsearch.xpack.sql.client.shared.Version;
1211
import org.elasticsearch.xpack.sql.plugin.AbstractSqlQueryRequest;
12+
import org.elasticsearch.xpack.sql.proto.MainResponse;
13+
import org.elasticsearch.xpack.sql.proto.Protocol;
1314

1415
import java.sql.SQLException;
1516

@@ -18,7 +19,7 @@
1819
*/
1920
public class CliSession {
2021
private final HttpClient httpClient;
21-
private int fetchSize = AbstractSqlQueryRequest.DEFAULT_FETCH_SIZE;
22+
private int fetchSize = Protocol.FETCH_SIZE;
2223
private String fetchSeparator = "";
2324
private boolean debug;
2425

x-pack/plugin/sql/sql-cli/src/main/java/org/elasticsearch/xpack/sql/cli/command/ServerInfoCliCommand.java

+2-2
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,8 @@
55
*/
66
package org.elasticsearch.xpack.sql.cli.command;
77

8-
import org.elasticsearch.action.main.MainResponse;
98
import org.elasticsearch.xpack.sql.cli.CliTerminal;
9+
import org.elasticsearch.xpack.sql.proto.MainResponse;
1010

1111
import java.sql.SQLException;
1212
import java.util.Locale;
@@ -30,7 +30,7 @@ public boolean doHandle(CliTerminal terminal, CliSession cliSession, String line
3030
}
3131
terminal.line()
3232
.text("Node:").em(info.getNodeName())
33-
.text(" Cluster:").em(info.getClusterName().value())
33+
.text(" Cluster:").em(info.getClusterName())
3434
.text(" Version:").em(info.getVersion().toString())
3535
.ln();
3636
return true;

x-pack/plugin/sql/sql-cli/src/main/java/org/elasticsearch/xpack/sql/cli/command/ServerQueryCliCommand.java

+4-4
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
import org.elasticsearch.xpack.sql.client.HttpClient;
1010
import org.elasticsearch.xpack.sql.client.shared.JreHttpUrlConnection;
1111
import org.elasticsearch.xpack.sql.plugin.CliFormatter;
12-
import org.elasticsearch.xpack.sql.plugin.SqlQueryResponse;
12+
import org.elasticsearch.xpack.sql.proto.SqlQueryResponse;
1313

1414
import java.sql.SQLException;
1515

@@ -23,8 +23,8 @@ protected boolean doHandle(CliTerminal terminal, CliSession cliSession, String l
2323
String data;
2424
try {
2525
response = cliClient.queryInit(line, cliSession.getFetchSize());
26-
cliFormatter = new CliFormatter(response);
27-
data = cliFormatter.formatWithHeader(response);
26+
cliFormatter = new CliFormatter(response.columns(), response.rows());
27+
data = cliFormatter.formatWithHeader(response.columns(), response.rows());
2828
while (true) {
2929
handleText(terminal, data);
3030
if (response.cursor().isEmpty()) {
@@ -36,7 +36,7 @@ protected boolean doHandle(CliTerminal terminal, CliSession cliSession, String l
3636
terminal.println(cliSession.getFetchSeparator());
3737
}
3838
response = cliSession.getClient().nextPage(response.cursor());
39-
data = cliFormatter.formatWithoutHeader(response);
39+
data = cliFormatter.formatWithoutHeader(response.rows());
4040
}
4141
} catch (SQLException e) {
4242
if (JreHttpUrlConnection.SQL_STATE_BAD_SERVER.equals(e.getSQLState())) {

x-pack/plugin/sql/sql-cli/src/test/java/org/elasticsearch/xpack/sql/cli/CliSessionTests.java

+4-4
Original file line numberDiff line numberDiff line change
@@ -6,14 +6,14 @@
66
package org.elasticsearch.xpack.sql.cli;
77

88
import org.elasticsearch.Build;
9-
import org.elasticsearch.action.main.MainResponse;
109
import org.elasticsearch.cluster.ClusterName;
1110
import org.elasticsearch.common.UUIDs;
1211
import org.elasticsearch.test.ESTestCase;
1312
import org.elasticsearch.xpack.sql.cli.command.CliSession;
1413
import org.elasticsearch.xpack.sql.client.HttpClient;
1514
import org.elasticsearch.xpack.sql.client.shared.ClientException;
1615
import org.elasticsearch.xpack.sql.client.shared.Version;
16+
import org.elasticsearch.xpack.sql.proto.MainResponse;
1717

1818
import java.sql.SQLException;
1919

@@ -28,7 +28,7 @@ public class CliSessionTests extends ESTestCase {
2828
public void testProperConnection() throws Exception {
2929
HttpClient httpClient = mock(HttpClient.class);
3030
when(httpClient.serverInfo()).thenReturn(new MainResponse(randomAlphaOfLength(5), org.elasticsearch.Version.CURRENT,
31-
ClusterName.DEFAULT, UUIDs.randomBase64UUID(), Build.CURRENT, randomBoolean()));
31+
ClusterName.DEFAULT.value(), UUIDs.randomBase64UUID(), Build.CURRENT));
3232
CliSession cliSession = new CliSession(httpClient);
3333
cliSession.checkConnection();
3434
verify(httpClient, times(1)).serverInfo();
@@ -58,10 +58,10 @@ public void testWrongServerVersion() throws Exception {
5858
}
5959
when(httpClient.serverInfo()).thenReturn(new MainResponse(randomAlphaOfLength(5),
6060
org.elasticsearch.Version.fromString(major + "." + minor + ".23"),
61-
ClusterName.DEFAULT, UUIDs.randomBase64UUID(), Build.CURRENT, randomBoolean()));
61+
ClusterName.DEFAULT.value(), UUIDs.randomBase64UUID(), Build.CURRENT));
6262
CliSession cliSession = new CliSession(httpClient);
6363
expectThrows(ClientException.class, cliSession::checkConnection);
6464
verify(httpClient, times(1)).serverInfo();
6565
verifyNoMoreInteractions(httpClient);
6666
}
67-
}
67+
}

x-pack/plugin/sql/sql-cli/src/test/java/org/elasticsearch/xpack/sql/cli/command/ServerInfoCliCommandTests.java

+3-3
Original file line numberDiff line numberDiff line change
@@ -6,12 +6,12 @@
66
package org.elasticsearch.xpack.sql.cli.command;
77

88
import org.elasticsearch.Build;
9-
import org.elasticsearch.action.main.MainResponse;
109
import org.elasticsearch.cluster.ClusterName;
1110
import org.elasticsearch.common.UUIDs;
1211
import org.elasticsearch.test.ESTestCase;
1312
import org.elasticsearch.xpack.sql.cli.TestTerminal;
1413
import org.elasticsearch.xpack.sql.client.HttpClient;
14+
import org.elasticsearch.xpack.sql.proto.MainResponse;
1515

1616
import static org.mockito.Mockito.mock;
1717
import static org.mockito.Mockito.times;
@@ -36,12 +36,12 @@ public void testShowInfo() throws Exception {
3636
HttpClient client = mock(HttpClient.class);
3737
CliSession cliSession = new CliSession(client);
3838
when(client.serverInfo()).thenReturn(new MainResponse("my_node", org.elasticsearch.Version.fromString("1.2.3"),
39-
new ClusterName("my_cluster"), UUIDs.randomBase64UUID(), Build.CURRENT, randomBoolean()));
39+
new ClusterName("my_cluster").value(), UUIDs.randomBase64UUID(), Build.CURRENT));
4040
ServerInfoCliCommand cliCommand = new ServerInfoCliCommand();
4141
assertTrue(cliCommand.handle(testTerminal, cliSession, "info"));
4242
assertEquals(testTerminal.toString(), "Node:<em>my_node</em> Cluster:<em>my_cluster</em> Version:<em>1.2.3</em>\n");
4343
verify(client, times(1)).serverInfo();
4444
verifyNoMoreInteractions(client);
4545
}
4646

47-
}
47+
}

x-pack/plugin/sql/sql-cli/src/test/java/org/elasticsearch/xpack/sql/cli/command/ServerQueryCliCommandTests.java

+3-3
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,8 @@
88
import org.elasticsearch.test.ESTestCase;
99
import org.elasticsearch.xpack.sql.cli.TestTerminal;
1010
import org.elasticsearch.xpack.sql.client.HttpClient;
11-
import org.elasticsearch.xpack.sql.plugin.ColumnInfo;
12-
import org.elasticsearch.xpack.sql.plugin.SqlQueryResponse;
11+
import org.elasticsearch.xpack.sql.proto.ColumnInfo;
12+
import org.elasticsearch.xpack.sql.proto.SqlQueryResponse;
1313

1414
import java.sql.JDBCType;
1515
import java.sql.SQLException;
@@ -119,4 +119,4 @@ private SqlQueryResponse fakeResponse(String cursor, boolean includeColumns, Str
119119
}
120120
return new SqlQueryResponse(cursor, columns, rows);
121121
}
122-
}
122+
}

0 commit comments

Comments
 (0)