This commit is contained in:
黎明伟 2020-06-17 12:04:37 +08:00
parent 7a2ecbfc3e
commit 596e42607a
4 changed files with 205 additions and 0 deletions

View File

@ -0,0 +1,53 @@
package org.budo.solr.jdbc.driver.parser.jsqlparser;
import java.util.List;
import org.budo.jdbc.driver.exception.BudoJdbcDriverNotSupportedException;
import org.budo.jdbc.driver.parser.AbstractSqlStatementParser;
import org.budo.jdbc.driver.statement.JdbcStatement;
import org.budo.solr.jdbc.driver.statement.SelectStatement;
import net.sf.jsqlparser.parser.CCJSqlParserUtil;
import net.sf.jsqlparser.statement.Statement;
import net.sf.jsqlparser.statement.select.FromItem;
import net.sf.jsqlparser.statement.select.PlainSelect;
import net.sf.jsqlparser.statement.select.Select;
import net.sf.jsqlparser.statement.select.SelectBody;
import net.sf.jsqlparser.statement.select.SelectItem;
/**
* @author lmw
*/
public class JsqlParserSolrStatementParser extends AbstractSqlStatementParser {
@Override
protected JdbcStatement doParseSql(String sql) throws Throwable {
Statement statement = CCJSqlParserUtil.parse(sql);
if (statement instanceof Select) {
return this.parseSelect((Select) statement);
}
throw new BudoJdbcDriverNotSupportedException("#26 sql=" + sql);
}
private JdbcStatement parseSelect(Select select) {
SelectBody selectBody = select.getSelectBody();
if (selectBody instanceof PlainSelect) {
return this.parsePlainSelect((PlainSelect) selectBody);
}
throw new BudoJdbcDriverNotSupportedException("#36 select=" + select);
}
private JdbcStatement parsePlainSelect(PlainSelect plainSelect) {
List<SelectItem> selectItems = plainSelect.getSelectItems();
for (SelectItem selectItem : selectItems) {
System.err.println("selectItem=" + selectItem + ", " + selectItem.getClass());
}
FromItem fromItem = plainSelect.getFromItem();
System.err.println("fromItem=" + fromItem + ", " + fromItem.getClass());
SelectStatement selectStatement = new SelectStatement();
return selectStatement;
}
}

View File

@ -0,0 +1,123 @@
package org.budo.solr.jdbc.driver.statement;
import java.sql.PreparedStatement;
import java.sql.SQLException;
import java.util.Map;
import java.util.Set;
import org.budo.jdbc.driver.AbstractBudoJdbcPreparedStatement;
import org.budo.jdbc.driver.BudoJdbcResultSetMetaData;
import org.budo.jdbc.driver.statement.AbstractJdbcStatement;
import org.budo.solr.jdbc.driver.BudoSolrJdbcResultSet;
import org.budo.support.http.client.Request;
import org.budo.support.http.client.Response;
import org.budo.support.lang.util.StringUtil;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
/**
* @author lmw
*/
public class SelectStatement extends AbstractJdbcStatement {
private String fieldList;
private String query;
private String filter;
private String sort;
private Integer offset;
private Integer limit;
@Override
public void execute(PreparedStatement preparedStatement) throws SQLException{
String requestUrl = "http://localhost:8984/solr/new_core/select";
Request request = new Request();
request.setUrl(requestUrl);
request.setParameter("q", "*:*");
if (!StringUtil.isEmpty(this.getFieldList())) {
request.setParameter("fl", this.getFieldList());
}
Response response = request.execute();
String responseBody = response.getBody();
JSONObject responseBodyJsonObject = JSON.parseObject(responseBody);
JSONObject responseValueJsonObject = responseBodyJsonObject.getJSONObject("response");
JSONArray docs = responseValueJsonObject.getJSONArray("docs");
int size = docs.size();
if (size < 1) {
((AbstractBudoJdbcPreparedStatement) preparedStatement).setResultSet(new BudoSolrJdbcResultSet());
return;
}
Map[] valueArray = new Map[size];
for (int i = 0; i < size; i++) {
valueArray[i] = docs.getJSONObject(i);
}
JSONObject firstDoc = docs.getJSONObject(0);
Set<String> keySet = firstDoc.keySet();
String[] columnNames = keySet.toArray(new String[keySet.size()]);
BudoJdbcResultSetMetaData metaData = new BudoJdbcResultSetMetaData(columnNames);
BudoSolrJdbcResultSet resultSet = new BudoSolrJdbcResultSet();
resultSet.setValueArray(valueArray);
resultSet.setMetaData(metaData);
((AbstractBudoJdbcPreparedStatement) preparedStatement).setResultSet(resultSet);
}
public String getFieldList() {
return this.fieldList;
}
public String getFilter() {
return this.filter;
}
public Integer getLimit() {
return this.limit;
}
public Integer getOffset() {
return this.offset;
}
public String getQuery() {
return this.query;
}
public String getSort() {
return this.sort;
}
public void setFieldList(String fieldList) {
this.fieldList = fieldList;
}
public void setFilter(String filter) {
this.filter = filter;
}
public void setLimit(Integer limit) {
this.limit = limit;
}
public void setOffset(Integer offset) {
this.offset = offset;
}
public void setQuery(String query) {
this.query = query;
}
public void setSort(String sort) {
this.sort = sort;
}
}

View File

@ -0,0 +1,23 @@
package org.budo.solr.jdbc.driver;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import org.budo.support.javax.sql.util.ResultSetUtil;
/**
* @author lmw
*/
public class AllTest {
public static void main(String[] args) throws Throwable {
String sql = " SELECT * FROM xxxxxxxx WHERE id!=1 ORDER BY aaa LIMIT 10,3 ";
Connection connection = DriverManager.getConnection("jdbc:solr://localhost:8984/solr/new_core");
PreparedStatement preparedStatement = connection.prepareStatement(sql);
ResultSet resultSet = preparedStatement.executeQuery();
System.err.println(ResultSetUtil.toMapList(resultSet));
}
}

6
pom.xml Normal file
View File

@ -0,0 +1,6 @@
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>budo-jdbc</groupId>
<artifactId>budo-jdbc</artifactId>
<version>0.0.1-SNAPSHOT</version>
</project>