Skip to content

Commit 6bd4459

Browse files
authored
add desc support (#98)
* desc query * desc query * desc query * chore: optimize imports
1 parent 5ac6b15 commit 6bd4459

File tree

5 files changed

+176
-6
lines changed

5 files changed

+176
-6
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
package io.github.iamazy.elasticsearch.dsl.jdbc.elastic;
2+
3+
import java.util.List;
4+
import java.util.Map;
5+
6+
/**
7+
* @author wellCh4n
8+
* @date 2022/10/21
9+
*/
10+
public class JdbcDescResponse {
11+
12+
private List<Map<String,Object>> result;
13+
14+
private int size;
15+
16+
17+
public List<Map<String, Object>> getResult() {
18+
return result;
19+
}
20+
21+
public void setResult(List<Map<String, Object>> result) {
22+
this.result = result;
23+
}
24+
25+
public int getSize() {
26+
return size;
27+
}
28+
29+
public void setSize(int size) {
30+
this.size = size;
31+
}
32+
}

elasticsearch-sql-jdbc/src/main/java/io/github/iamazy/elasticsearch/dsl/jdbc/elastic/JdbcResponseExtractor.java

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@
44
import org.apache.commons.collections4.MapUtils;
55
import org.apache.commons.lang3.StringUtils;
66
import org.elasticsearch.action.search.SearchResponse;
7+
import org.elasticsearch.client.indices.GetMappingsResponse;
8+
import org.elasticsearch.cluster.metadata.MappingMetadata;
79
import org.elasticsearch.search.SearchHit;
810
import org.elasticsearch.search.aggregations.Aggregation;
911
import org.elasticsearch.search.aggregations.Aggregations;
@@ -21,11 +23,13 @@
2123
import org.elasticsearch.search.aggregations.metrics.ParsedTopHits;
2224
import org.elasticsearch.search.aggregations.metrics.TopHits;
2325

26+
import java.sql.SQLException;
2427
import java.util.ArrayList;
2528
import java.util.HashMap;
2629
import java.util.LinkedHashMap;
2730
import java.util.List;
2831
import java.util.Map;
32+
import java.util.Optional;
2933

3034
/**
3135
* @author iamazy
@@ -188,4 +192,30 @@ public JdbcSearchResponse parseScrollSearchResponse(SearchResponse response,Map<
188192
String scrollId = response.getScrollId();
189193
return new JdbcScrollSearchResponse(searchResponse, scrollId);
190194
}
195+
196+
public JdbcDescResponse parseDescResponse(GetMappingsResponse response) throws SQLException {
197+
JdbcDescResponse jdbcDescResponse = new JdbcDescResponse();
198+
Optional<Map.Entry<String, MappingMetadata>> mapping = response.mappings().entrySet().stream().findFirst();
199+
if (!mapping.isPresent()) {
200+
throw new SQLException("mapping is not exists in response");
201+
}
202+
MappingMetadata mappingMetadata = mapping.get().getValue();
203+
Map<String, Object> mappingMetaMap = mappingMetadata.getSourceAsMap();
204+
//noinspection unchecked
205+
Map<String, Map<String, Object>> propertiesMap = (Map<String, Map<String, Object>>) mappingMetaMap.get("properties");
206+
207+
List<Map<String, Object>> result = new ArrayList<>();
208+
for (Map.Entry<String, Map<String, Object>> property : propertiesMap.entrySet()) {
209+
Map<String, Object> desc = new HashMap<>();
210+
desc.put("name", property.getKey());
211+
desc.put("type", property.getValue().get("type"));
212+
property.getValue().remove("type");
213+
desc.put("extra", property.getValue());
214+
result.add(desc);
215+
}
216+
217+
jdbcDescResponse.setSize(result.size());
218+
jdbcDescResponse.setResult(result);
219+
return jdbcDescResponse;
220+
}
191221
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
package io.github.iamazy.elasticsearch.dsl.jdbc.result;
2+
3+
import io.github.iamazy.elasticsearch.dsl.jdbc.elastic.JdbcDescResponse;
4+
5+
import java.sql.ResultSetMetaData;
6+
import java.sql.SQLException;
7+
import java.sql.Statement;
8+
9+
/**
10+
* @author wellCh4n
11+
* @date 2022/10/21
12+
*/
13+
public class ElasticDescResultSet extends AbstractResultSet {
14+
15+
private static final ElasticResultSetMetaData RESULT_SET_META_DATA = new ElasticResultSetMetaData();
16+
17+
private final JdbcDescResponse response;
18+
19+
private int rowCursor = -1;
20+
21+
public ElasticDescResultSet(Statement statement, JdbcDescResponse response) {
22+
super(statement);
23+
this.response = response;
24+
}
25+
26+
@Override
27+
public boolean next() throws SQLException {
28+
rowCursor++;
29+
return rowCursor + 1 <= getFetchSize();
30+
}
31+
32+
33+
@Override
34+
public String getString(String columnLabel) throws SQLException {
35+
return (String) response.getResult().get(rowCursor).get(columnLabel);
36+
}
37+
38+
@Override
39+
public boolean getBoolean(String columnLabel) throws SQLException {
40+
return Boolean.parseBoolean(getString(columnLabel));
41+
}
42+
43+
@Override
44+
public ResultSetMetaData getMetaData() throws SQLException {
45+
return RESULT_SET_META_DATA;
46+
}
47+
48+
@Override
49+
public void beforeFirst() throws SQLException {
50+
rowCursor = 0;
51+
}
52+
53+
@Override
54+
public void afterLast() throws SQLException {
55+
rowCursor = 1;
56+
}
57+
58+
@Override
59+
public int getRow() throws SQLException {
60+
return rowCursor;
61+
}
62+
63+
@Override
64+
public int getFetchSize() throws SQLException {
65+
return response.getSize();
66+
}
67+
}

elasticsearch-sql-jdbc/src/main/java/io/github/iamazy/elasticsearch/dsl/jdbc/statement/ElasticStatement.java

Lines changed: 22 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
import io.github.iamazy.elasticsearch.dsl.jdbc.cons.JdbcConstants;
66
import io.github.iamazy.elasticsearch.dsl.jdbc.elastic.JdbcResponseExtractor;
77
import io.github.iamazy.elasticsearch.dsl.jdbc.elastic.JdbcSearchResponse;
8+
import io.github.iamazy.elasticsearch.dsl.jdbc.result.ElasticDescResultSet;
89
import io.github.iamazy.elasticsearch.dsl.jdbc.result.ElasticResultSet;
910
import io.github.iamazy.elasticsearch.dsl.sql.ElasticSql2DslParser;
1011
import io.github.iamazy.elasticsearch.dsl.sql.enums.SqlOperation;
@@ -13,6 +14,8 @@
1314
import org.elasticsearch.action.search.SearchResponse;
1415
import org.elasticsearch.action.search.SearchScrollRequest;
1516
import org.elasticsearch.client.RequestOptions;
17+
import org.elasticsearch.client.indices.GetMappingsRequest;
18+
import org.elasticsearch.client.indices.GetMappingsResponse;
1619
import org.elasticsearch.index.reindex.BulkByScrollResponse;
1720

1821
import java.io.IOException;
@@ -42,13 +45,26 @@ public ElasticStatement(ElasticConnection connection) {
4245
public ResultSet executeQuery(String sql) throws SQLException {
4346
ElasticSqlParseResult parseResult = elasticSql2DslParser.parse(sql);
4447
checkDatabase(parseResult.getIndices());
45-
assert parseResult.getSqlOperation() == SqlOperation.SELECT;
48+
JdbcResponseExtractor jdbcResponseExtractor = new JdbcResponseExtractor();
4649
try {
47-
SearchResponse searchResponse = connection.getRestClient().search(parseResult.getSearchRequest(), RequestOptions.DEFAULT);
48-
JdbcResponseExtractor jdbcResponseExtractor = new JdbcResponseExtractor();
49-
this.aliasMap=parseResult.getAliasMap();
50-
this.resultSet = new ElasticResultSet(this, jdbcResponseExtractor.parseSearchResponse(searchResponse,parseResult.getAliasMap()));
51-
return resultSet;
50+
switch (parseResult.getSqlOperation()) {
51+
case SELECT:
52+
SearchResponse searchResponse = connection.getRestClient().search(parseResult.getSearchRequest(), RequestOptions.DEFAULT);
53+
this.aliasMap=parseResult.getAliasMap();
54+
this.resultSet = new ElasticResultSet(this, jdbcResponseExtractor.parseSearchResponse(searchResponse,parseResult.getAliasMap()));
55+
return resultSet;
56+
case DESC:
57+
if (parseResult.getMappingsRequest().indices().length > 1) {
58+
throw new SQLException("more than one index in desc");
59+
}
60+
GetMappingsRequest getMappingsRequest = new GetMappingsRequest();
61+
getMappingsRequest.indices(parseResult.getMappingsRequest().indices());
62+
GetMappingsResponse mappingResponse = connection.getRestClient().indices().getMapping(getMappingsRequest, RequestOptions.DEFAULT);
63+
this.resultSet = new ElasticDescResultSet(this, jdbcResponseExtractor.parseDescResponse(mappingResponse));
64+
return resultSet;
65+
default:
66+
throw new SQLException("only support [select, desc] operation");
67+
}
5268
} catch (IOException e) {
5369
throw new SQLException(e.getMessage());
5470
}

elasticsearch-sql-jdbc/src/test/java/io/github/iamazy/elasticsearch/dsl/sql/JdbcTest.java

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -192,6 +192,31 @@ public static void scroll(){
192192
}
193193
}
194194

195+
public static void desc() {
196+
Connection conn = null;
197+
PreparedStatement ps = null;
198+
try {
199+
Class.forName(JDBC_DRIVER);
200+
conn = DriverManager.getConnection(DB_URL);
201+
String sql = "DESC device_search";
202+
ps = conn.prepareStatement(sql);
203+
ResultSet rs = ps.executeQuery();
204+
System.out.println(rs);
205+
ps.close();
206+
conn.close();
207+
} catch (Exception e) {
208+
e.printStackTrace();
209+
} finally {
210+
try {
211+
if(ps != null) ps.close();
212+
} catch (SQLException ignored) {}
213+
try {
214+
if (conn != null) conn.close();
215+
} catch (SQLException ignored) {}
216+
}
217+
}
218+
219+
195220
public static void main(String[] args) {
196221
scroll();
197222
}

0 commit comments

Comments
 (0)