spuerx %!s(int64=9) %!d(string=hai) anos
pai
achega
4cf026930b

+ 2 - 0
pom.xml

@@ -134,6 +134,7 @@
                     </execution>
                 </executions>
             </plugin>
+            <!--
             <plugin>
                 <artifactId>maven-assembly-plugin</artifactId>
                 <version>2.3</version>
@@ -150,6 +151,7 @@
                     </execution>
                 </executions>
             </plugin>
+            -->
         </plugins>
     </build>
 </project>

+ 22 - 6
src/main/java/org/elasticsearch/jdbc/AbstractFeatureNotSupportedPreparedStatement.java

@@ -5,6 +5,7 @@ import java.io.InputStream;
 import java.io.Reader;
 import java.net.URL;
 import java.sql.*;
+import java.util.Calendar;
 
 public abstract class AbstractFeatureNotSupportedPreparedStatement extends ElasticStatement implements PreparedStatement {
 
@@ -13,13 +14,8 @@ public abstract class AbstractFeatureNotSupportedPreparedStatement extends Elast
     }
 
     @Override
-    public ResultSet executeQuery() throws SQLException {
-        throw new SQLFeatureNotSupportedException("executeQuery");
-    }
-
-    @Override
     public int executeUpdate() throws SQLException {
-        throw new SQLFeatureNotSupportedException("executeQuery");
+        throw new SQLFeatureNotSupportedException("executeUpdate");
     }
 
     @Override
@@ -183,4 +179,24 @@ public abstract class AbstractFeatureNotSupportedPreparedStatement extends Elast
     public void setNClob(int parameterIndex, Reader reader) throws SQLException {
         throw new SQLFeatureNotSupportedException("setNClob");
     }
+
+    @Override
+    public void setNull(int parameterIndex, int sqlType, String typeName) throws SQLException {
+        throw new SQLFeatureNotSupportedException("setNull");
+    }
+
+    @Override
+    public void setDate(int parameterIndex, Date x, Calendar cal) throws SQLException {
+        throw new SQLFeatureNotSupportedException("setDate");
+    }
+
+    @Override
+    public void setTime(int parameterIndex, Time x, Calendar cal) throws SQLException {
+        throw new SQLFeatureNotSupportedException("setTime");
+    }
+
+    @Override
+    public void setTimestamp(int parameterIndex, Timestamp x, Calendar cal) throws SQLException {
+        throw new SQLFeatureNotSupportedException("setTimestamp");
+    }
 }

+ 0 - 117
src/main/java/org/elasticsearch/jdbc/AbstractPreparedStatement.java

@@ -1,117 +0,0 @@
-package org.elasticsearch.jdbc;
-
-import java.math.BigDecimal;
-import java.sql.*;
-import java.util.Calendar;
-
-public abstract class AbstractPreparedStatement extends AbstractFeatureNotSupportedPreparedStatement {
-
-    public AbstractPreparedStatement(ElasticConnection connection) {
-        super(connection);
-    }
-
-    @Override
-    public void setBoolean(int parameterIndex, boolean x) throws SQLException {
-
-    }
-
-    @Override
-    public void setByte(int parameterIndex, byte x) throws SQLException {
-
-    }
-
-    @Override
-    public void setShort(int parameterIndex, short x) throws SQLException {
-
-    }
-
-    @Override
-    public void setInt(int parameterIndex, int x) throws SQLException {
-
-    }
-
-    @Override
-    public void setLong(int parameterIndex, long x) throws SQLException {
-
-    }
-
-    @Override
-    public void setFloat(int parameterIndex, float x) throws SQLException {
-
-    }
-
-    @Override
-    public void setDouble(int parameterIndex, double x) throws SQLException {
-
-    }
-
-    @Override
-    public void setBigDecimal(int parameterIndex, BigDecimal x) throws SQLException {
-
-    }
-
-    @Override
-    public void setString(int parameterIndex, String x) throws SQLException {
-
-    }
-
-    @Override
-    public void setDate(int parameterIndex, Date x) throws SQLException {
-
-    }
-
-    @Override
-    public void setTime(int parameterIndex, Time x) throws SQLException {
-
-    }
-
-    @Override
-    public void setTimestamp(int parameterIndex, Timestamp x) throws SQLException {
-
-    }
-
-    @Override
-    public void clearParameters() throws SQLException {
-
-    }
-
-    @Override
-    public void setObject(int parameterIndex, Object x) throws SQLException {
-
-    }
-
-    @Override
-    public boolean execute() throws SQLException {
-        return false;
-    }
-
-    @Override
-    public ResultSetMetaData getMetaData() throws SQLException {
-        return new ElasticResultSetMetaData();
-    }
-
-    @Override
-    public void setDate(int parameterIndex, Date x, Calendar cal) throws SQLException {
-
-    }
-
-    @Override
-    public void setTime(int parameterIndex, Time x, Calendar cal) throws SQLException {
-
-    }
-
-    @Override
-    public void setTimestamp(int parameterIndex, Timestamp x, Calendar cal) throws SQLException {
-
-    }
-
-    @Override
-    public void setNull(int parameterIndex, int sqlType, String typeName) throws SQLException {
-
-    }
-
-    @Override
-    public ParameterMetaData getParameterMetaData() throws SQLException {
-        return null;
-    }
-}

+ 6 - 1
src/main/java/org/elasticsearch/jdbc/AbstractStatement.java

@@ -1,6 +1,8 @@
 package org.elasticsearch.jdbc;
 
-import java.sql.*;
+import java.sql.ResultSet;
+import java.sql.SQLException;
+import java.sql.SQLWarning;
 
 public abstract class AbstractStatement extends AbstractFeatureNotSupportedStatement {
 
@@ -121,4 +123,7 @@ public abstract class AbstractStatement extends AbstractFeatureNotSupportedState
     public final boolean isWrapperFor(final Class<?> iface) throws SQLException {
         return iface.isInstance(this);
     }
+
+
+    protected abstract ResultSet executeQuery(String sql, Object[] args) throws SQLException;
 }

+ 1 - 1
src/main/java/org/elasticsearch/jdbc/ElasticConnection.java

@@ -28,7 +28,7 @@ public class ElasticConnection extends AbstractConnection {
 
     @Override
     public PreparedStatement prepareStatement(String sql) throws SQLException {
-        return null;
+        return new ElasticPreparedStatement(this, sql);
     }
 
     @Override

+ 182 - 0
src/main/java/org/elasticsearch/jdbc/ElasticPreparedStatement.java

@@ -0,0 +1,182 @@
+package org.elasticsearch.jdbc;
+
+import com.google.common.base.Function;
+import com.google.common.collect.Lists;
+import com.google.common.collect.Maps;
+
+import java.math.BigDecimal;
+import java.sql.*;
+import java.text.SimpleDateFormat;
+import java.util.Collections;
+import java.util.Comparator;
+import java.util.List;
+import java.util.Map;
+
+public class ElasticPreparedStatement extends AbstractFeatureNotSupportedPreparedStatement {
+
+    public static final String DEFAULT_ES_DATE_FORMAT = "yyyy-MM-dd'T'HH:mm:ss.SSSZ";
+
+    private Map<Integer, SQLParam> paramMap = Maps.newHashMap();
+
+    private String sql;
+
+    public ElasticPreparedStatement(ElasticConnection connection, String sql) {
+        super(connection);
+        this.sql = sql;
+    }
+
+    @Override
+    public boolean execute() throws SQLException {
+        executeQuery();
+        return true;
+    }
+
+    @Override
+    public ResultSet executeQuery() throws SQLException {
+        if (paramMap.size() > 0) {
+            List<SQLParam> paramList = Lists.newArrayList(paramMap.values());
+            Collections.sort(paramList, new Comparator<SQLParam>() {
+                @Override
+                public int compare(SQLParam o1, SQLParam o2) {
+                    if (o1.getParamIndex() < o2.getParamIndex()) {
+                        return -1;
+                    }
+                    if (o1.getParamIndex() > o2.getParamIndex()) {
+                        return 1;
+                    }
+                    return 0;
+                }
+            });
+
+            List<Object> argList = Lists.transform(paramList, new Function<SQLParam, Object>() {
+                @Override
+                public Object apply(SQLParam sqlParam) {
+                    return sqlParam.getParamVal();
+                }
+            });
+
+            return executeQuery(sql, argList.toArray(new Object[argList.size()]));
+        }
+        return executeQuery(sql);
+    }
+
+    @Override
+    public void setBoolean(int parameterIndex, boolean x) throws SQLException {
+        paramMap.put(parameterIndex, new SQLParam(parameterIndex, x));
+    }
+
+    @Override
+    public void setByte(int parameterIndex, byte x) throws SQLException {
+        paramMap.put(parameterIndex, new SQLParam(parameterIndex, x));
+    }
+
+    @Override
+    public void setShort(int parameterIndex, short x) throws SQLException {
+        paramMap.put(parameterIndex, new SQLParam(parameterIndex, x));
+    }
+
+    @Override
+    public void setInt(int parameterIndex, int x) throws SQLException {
+        paramMap.put(parameterIndex, new SQLParam(parameterIndex, x));
+    }
+
+    @Override
+    public void setLong(int parameterIndex, long x) throws SQLException {
+        paramMap.put(parameterIndex, new SQLParam(parameterIndex, x));
+    }
+
+    @Override
+    public void setFloat(int parameterIndex, float x) throws SQLException {
+        paramMap.put(parameterIndex, new SQLParam(parameterIndex, x));
+    }
+
+    @Override
+    public void setDouble(int parameterIndex, double x) throws SQLException {
+        paramMap.put(parameterIndex, new SQLParam(parameterIndex, x));
+    }
+
+    @Override
+    public void setBigDecimal(int parameterIndex, BigDecimal x) throws SQLException {
+        paramMap.put(parameterIndex, new SQLParam(parameterIndex, x));
+    }
+
+    @Override
+    public void setString(int parameterIndex, String x) throws SQLException {
+        paramMap.put(parameterIndex, new SQLParam(parameterIndex, x));
+    }
+
+    @Override
+    public void setDate(int parameterIndex, Date x) throws SQLException {
+        SimpleDateFormat dateFormat = new SimpleDateFormat(DEFAULT_ES_DATE_FORMAT);
+        String dateStr = dateFormat.format(x);
+        paramMap.put(parameterIndex, new SQLParam(parameterIndex, dateStr));
+    }
+
+    @Override
+    public void setTime(int parameterIndex, Time x) throws SQLException {
+        SimpleDateFormat dateFormat = new SimpleDateFormat(DEFAULT_ES_DATE_FORMAT);
+        String dateStr = dateFormat.format(x);
+        paramMap.put(parameterIndex, new SQLParam(parameterIndex, dateStr));
+    }
+
+    @Override
+    public void setTimestamp(int parameterIndex, Timestamp x) throws SQLException {
+        SimpleDateFormat dateFormat = new SimpleDateFormat(DEFAULT_ES_DATE_FORMAT);
+        String dateStr = dateFormat.format(x);
+        paramMap.put(parameterIndex, new SQLParam(parameterIndex, dateStr));
+    }
+
+    @Override
+    public void clearParameters() throws SQLException {
+        paramMap.clear();
+    }
+
+    @Override
+    public void setObject(int parameterIndex, Object x) throws SQLException {
+        if (x instanceof Date) {
+            setDate(parameterIndex, (Date) x);
+        }
+        else if (x instanceof Time) {
+            setTime(parameterIndex, (Time) x);
+        }
+        else if (x instanceof java.util.Date) {
+            SimpleDateFormat dateFormat = new SimpleDateFormat(DEFAULT_ES_DATE_FORMAT);
+            String dateStr = dateFormat.format(x);
+            paramMap.put(parameterIndex, new SQLParam(parameterIndex, dateStr));
+        }
+        else {
+            paramMap.put(parameterIndex, new SQLParam(parameterIndex, x));
+        }
+    }
+
+    @Override
+    public ResultSetMetaData getMetaData() throws SQLException {
+        return new ElasticResultSetMetaData();
+    }
+
+    @Override
+    public ParameterMetaData getParameterMetaData() throws SQLException {
+        return null;
+    }
+
+    private class SQLParam {
+
+        private int paramIndex;
+
+        private Object paramVal;
+
+        public SQLParam(int paramIndex, Object paramVal) {
+            this.paramIndex = paramIndex;
+            this.paramVal = paramVal;
+        }
+
+        public Object getParamVal() {
+            return paramVal;
+        }
+
+        public int getParamIndex() {
+            return paramIndex;
+        }
+
+    }
+}

+ 6 - 1
src/main/java/org/elasticsearch/jdbc/ElasticStatement.java

@@ -24,8 +24,13 @@ public class ElasticStatement extends AbstractStatement {
 
     @Override
     public ResultSet executeQuery(String sql) throws SQLException {
+        return executeQuery(sql, null);
+    }
+
+    @Override
+    public ResultSet executeQuery(String sql, Object[] args) throws SQLException {
         ElasticSql2DslParser sql2DslParser = new ElasticSql2DslParser();
-        ElasticSqlParseResult parseResult = sql2DslParser.parse(sql);
+        ElasticSqlParseResult parseResult = sql2DslParser.parse(sql, args);
 
         SearchRequestBuilder searchRequest = parseResult.toRequest(connection.getClient());
         SearchResponse searchResponse = SearchActionExecutor.get().syncExecute(searchRequest);

+ 26 - 5
src/main/java/org/elasticsearch/jdbc/SearchActionExecutor.java

@@ -1,12 +1,13 @@
 package org.elasticsearch.jdbc;
 
-import org.elasticsearch.action.ActionRequest;
-import org.elasticsearch.action.ActionRequestBuilder;
-import org.elasticsearch.action.ActionResponse;
-import org.elasticsearch.action.ListenableActionFuture;
+import org.apache.logging.log4j.LogManager;
+import org.apache.logging.log4j.Logger;
+import org.elasticsearch.action.*;
 
 public class SearchActionExecutor {
 
+    private static final Logger logger = LogManager.getLogger(SearchActionExecutor.class);
+
     private static final SearchActionExecutor searchActionExecutor = new SearchActionExecutor();
 
     private SearchActionExecutor() {
@@ -19,12 +20,14 @@ public class SearchActionExecutor {
 
     public <Request extends ActionRequest, Response extends ActionResponse, RequestBuilder extends ActionRequestBuilder<Request, Response, RequestBuilder>> Response syncExecuteWithException(RequestBuilder requestBuilder) {
         ListenableActionFuture<Response> searchActionFuture = requestBuilder.execute();
+        searchActionFuture.addListener(defaultActionListener(requestBuilder));
         return searchActionFuture.actionGet();
     }
 
     public <Request extends ActionRequest, Response extends ActionResponse, RequestBuilder extends ActionRequestBuilder<Request, Response, RequestBuilder>> Response syncExecute(RequestBuilder requestBuilder) {
         try {
             ListenableActionFuture<Response> searchActionFuture = requestBuilder.execute();
+            searchActionFuture.addListener(defaultActionListener(requestBuilder));
             return searchActionFuture.actionGet();
         }
         catch (Exception ex) {
@@ -33,6 +36,24 @@ public class SearchActionExecutor {
     }
 
     public <Request extends ActionRequest, Response extends ActionResponse, RequestBuilder extends ActionRequestBuilder<Request, Response, RequestBuilder>> void asyncExecute(RequestBuilder requestBuilder) {
-        requestBuilder.execute();
+        ListenableActionFuture<Response> searchActionFuture = requestBuilder.execute();
+        searchActionFuture.addListener(defaultActionListener(requestBuilder));
     }
+
+    private <Response extends ActionResponse> ActionListener<Response> defaultActionListener(ActionRequestBuilder requestBuilder) {
+        return new ActionListener<Response>() {
+            @Override
+            public void onResponse(Response response) {
+                System.out.println((String.format("[Search_Request] %s", requestBuilder.toString())));
+                //logger.debug(String.format("[Search_Request] %s", requestBuilder.toString()));
+                //logger.debug(String.format("[Search_Response] %s", response.toString()));
+            }
+
+            @Override
+            public void onFailure(Throwable throwable) {
+
+            }
+        };
+    }
+
 }

+ 19 - 23
src/test/java/org/elasticsearch/jdbc/ElasticDriverTest.java

@@ -1,22 +1,16 @@
 package org.elasticsearch.jdbc;
 
 
-import com.google.common.reflect.TypeToken;
 import com.google.gson.Gson;
 import org.junit.Test;
 
-import java.sql.Connection;
-import java.sql.Driver;
-import java.sql.DriverManager;
-import java.sql.ResultSet;
+import java.sql.*;
 import java.util.Enumeration;
-import java.util.List;
 
 public class ElasticDriverTest {
     private static final String driver = "org.elasticsearch.jdbc.ElasticDriver";
     private static final String url = "jdbc:elastic:192.168.0.109:9300/judge_cluster";
 
-
     @Test
     public void testLoadDriver() throws Exception {
         Class.forName(driver);
@@ -56,29 +50,31 @@ public class ElasticDriverTest {
         Connection connection = dataSource.getConnection();
         ResultSet resultSet = connection.createStatement().executeQuery("select * from index.library where manager.managerName='lcy'");
 
-        while(resultSet.next()) {
+        while (resultSet.next()) {
             String json = resultSet.getString(1);
             SearchResponseGson searchResponse = new Gson().fromJson(json, SearchResponseGson.class);
-
             System.out.println(searchResponse.getTookInMillis());
-
-            List<Lib> libList = searchResponse.getDocList(new TypeToken<Lib>(){});
-
-            for (Lib lib : libList) {
-                System.out.println(lib.getName());
-            }
         }
     }
-}
 
-class Lib {
-    private String name;
+    @Test
+    public void testQuery2() throws Exception {
+        ElasticSingleConnectionDataSource dataSource = new ElasticSingleConnectionDataSource(url, true);
+        dataSource.setDriverClassName(driver);
+
+        Connection connection = dataSource.getConnection();
+        String sql = "select * from index.library where (manager.managerName=? or manager.managerName=?)";
+
+        PreparedStatement preparedStatement = connection.prepareStatement(sql);
+        preparedStatement.setString(1, "lcy");
+        preparedStatement.setString(2, "chennan");
 
-    public String getName() {
-        return name;
-    }
+        ResultSet resultSet = preparedStatement.executeQuery();
 
-    public void setName(String name) {
-        this.name = name;
+        while (resultSet.next()) {
+            String json = resultSet.getString(1);
+            SearchResponseGson searchResponse = new Gson().fromJson(json, SearchResponseGson.class);
+            System.out.println(searchResponse.getTookInMillis());
+        }
     }
 }