-
Notifications
You must be signed in to change notification settings - Fork 24.3k
/
SqlQueryResponse.java
232 lines (208 loc) · 7.29 KB
/
SqlQueryResponse.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
/*
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one
* or more contributor license agreements. Licensed under the Elastic License;
* you may not use this file except in compliance with the Elastic License.
*/
package org.elasticsearch.xpack.sql.action;
import org.elasticsearch.action.ActionResponse;
import org.elasticsearch.common.Nullable;
import org.elasticsearch.common.Strings;
import org.elasticsearch.common.io.stream.StreamInput;
import org.elasticsearch.common.io.stream.StreamOutput;
import org.elasticsearch.common.xcontent.ToXContentObject;
import org.elasticsearch.common.xcontent.XContentBuilder;
import org.elasticsearch.xpack.sql.proto.ColumnInfo;
import org.elasticsearch.xpack.sql.proto.Mode;
import org.joda.time.ReadableDateTime;
import java.io.IOException;
import java.sql.JDBCType;
import java.util.ArrayList;
import java.util.List;
import java.util.Objects;
import static java.util.Collections.unmodifiableList;
/**
* Response to perform an sql query
*/
public class SqlQueryResponse extends ActionResponse implements ToXContentObject {
// TODO: Simplify cursor handling
private String cursor;
private List<ColumnInfo> columns;
// TODO investigate reusing Page here - it probably is much more efficient
private List<List<Object>> rows;
public SqlQueryResponse() {
}
public SqlQueryResponse(String cursor, @Nullable List<ColumnInfo> columns, List<List<Object>> rows) {
this.cursor = cursor;
this.columns = columns;
this.rows = rows;
}
/**
* The key that must be sent back to SQL to access the next page of
* results. If equal to "" then there is no next page.
*/
public String cursor() {
return cursor;
}
public long size() {
return rows.size();
}
public List<ColumnInfo> columns() {
return columns;
}
public List<List<Object>> rows() {
return rows;
}
public SqlQueryResponse cursor(String cursor) {
this.cursor = cursor;
return this;
}
public SqlQueryResponse columns(List<ColumnInfo> columns) {
this.columns = columns;
return this;
}
public SqlQueryResponse rows(List<List<Object>> rows) {
this.rows = rows;
return this;
}
@Override
public void readFrom(StreamInput in) throws IOException {
cursor = in.readString();
if (in.readBoolean()) {
// We might have rows without columns and we might have columns without rows
// So we send the column size twice, just to keep the protocol simple
int columnCount = in.readVInt();
List<ColumnInfo> columns = new ArrayList<>(columnCount);
for (int c = 0; c < columnCount; c++) {
columns.add(readColumnInfo(in));
}
this.columns = unmodifiableList(columns);
} else {
this.columns = null;
}
int rowCount = in.readVInt();
List<List<Object>> rows = new ArrayList<>(rowCount);
if (rowCount > 0) {
int columnCount = in.readVInt();
for (int r = 0; r < rowCount; r++) {
List<Object> row = new ArrayList<>(columnCount);
for (int c = 0; c < columnCount; c++) {
row.add(in.readGenericValue());
}
rows.add(unmodifiableList(row));
}
}
this.rows = unmodifiableList(rows);
}
@Override
public void writeTo(StreamOutput out) throws IOException {
out.writeString(cursor);
if (columns == null) {
out.writeBoolean(false);
} else {
out.writeBoolean(true);
out.writeVInt(columns.size());
for (ColumnInfo column : columns) {
writeColumnInfo(out, column);
}
}
out.writeVInt(rows.size());
if (rows.size() > 0) {
out.writeVInt(rows.get(0).size());
for (List<Object> row : rows) {
for (Object value : row) {
out.writeGenericValue(value);
}
}
}
}
@Override
public XContentBuilder toXContent(XContentBuilder builder, Params params) throws IOException {
Mode mode = Mode.fromString(params.param("mode"));
builder.startObject();
{
if (columns != null) {
builder.startArray("columns");
{
for (ColumnInfo column : columns) {
column.toXContent(builder, params);
}
}
builder.endArray();
}
builder.startArray("rows");
for (List<Object> row : rows()) {
builder.startArray();
for (Object value : row) {
value(builder, mode, value);
}
builder.endArray();
}
builder.endArray();
if (cursor.equals("") == false) {
builder.field(SqlQueryRequest.CURSOR.getPreferredName(), cursor);
}
}
return builder.endObject();
}
/**
* Serializes the provided value in SQL-compatible way based on the client mode
*/
public static XContentBuilder value(XContentBuilder builder, Mode mode, Object value) throws IOException {
if (Mode.isDriver(mode) && value instanceof ReadableDateTime) {
// JDBC cannot parse dates in string format
builder.value(((ReadableDateTime) value).getMillis());
} else {
builder.value(value);
}
return builder;
}
public static ColumnInfo readColumnInfo(StreamInput in) throws IOException {
String table = in.readString();
String name = in.readString();
String esType = in.readString();
JDBCType jdbcType;
int displaySize;
if (in.readBoolean()) {
// FIXME: this needs changing to allow custom types
jdbcType = JDBCType.valueOf(in.readVInt());
displaySize = in.readVInt();
} else {
jdbcType = null;
displaySize = 0;
}
return new ColumnInfo(table, name, esType, jdbcType, displaySize);
}
public static void writeColumnInfo(StreamOutput out, ColumnInfo columnInfo) throws IOException {
out.writeString(columnInfo.table());
out.writeString(columnInfo.name());
out.writeString(columnInfo.esType());
if (columnInfo.jdbcType() != null) {
out.writeBoolean(true);
out.writeVInt(columnInfo.jdbcType().getVendorTypeNumber());
out.writeVInt(columnInfo.displaySize());
} else {
out.writeBoolean(false);
}
}
@Override
public boolean equals(Object o) {
if (this == o) {
return true;
}
if (o == null || getClass() != o.getClass()) {
return false;
}
SqlQueryResponse that = (SqlQueryResponse) o;
return Objects.equals(cursor, that.cursor) &&
Objects.equals(columns, that.columns) &&
Objects.equals(rows, that.rows);
}
@Override
public int hashCode() {
return Objects.hash(cursor, columns, rows);
}
@Override
public String toString() {
return Strings.toString(this);
}
}