diff --git a/src/main/java/com/aerospike/jdbc/AerospikeDatabaseMetadata.java b/src/main/java/com/aerospike/jdbc/AerospikeDatabaseMetadata.java index f236a58..1f3a0ff 100644 --- a/src/main/java/com/aerospike/jdbc/AerospikeDatabaseMetadata.java +++ b/src/main/java/com/aerospike/jdbc/AerospikeDatabaseMetadata.java @@ -10,6 +10,8 @@ import com.aerospike.jdbc.sql.ListRecordSet; import com.aerospike.jdbc.sql.SimpleWrapper; import com.aerospike.jdbc.util.AerospikeUtils; +import com.google.common.cache.Cache; +import com.google.common.cache.CacheBuilder; import java.io.IOException; import java.io.StringReader; @@ -22,6 +24,7 @@ import java.sql.Statement; import java.util.*; import java.util.concurrent.ConcurrentHashMap; +import java.util.concurrent.ExecutionException; import java.util.function.Function; import java.util.logging.Logger; import java.util.regex.Pattern; @@ -49,7 +52,7 @@ public class AerospikeDatabaseMetadata implements DatabaseMetaData, SimpleWrappe private static final String NEW_LINE = System.lineSeparator(); private final String url; - private final AerospikeConnection connection; + private final Connection connection; private final String dbBuild; private final String dbEdition; private final List catalogs; @@ -57,6 +60,7 @@ public class AerospikeDatabaseMetadata implements DatabaseMetaData, SimpleWrappe private final Map> catalogIndexes; private final Map secondaryIndexes; private final AerospikeSchemaBuilder schemaBuilder; + private final Cache resultSetMetaDataCache; public AerospikeDatabaseMetadata(String url, IAerospikeClient client, AerospikeConnection connection) { logger.info("Init AerospikeDatabaseMetadata"); @@ -103,6 +107,7 @@ public AerospikeDatabaseMetadata(String url, IAerospikeClient client, AerospikeC .collect(Collectors.toMap(AerospikeSecondaryIndex::toKey, Function.identity())); schemaBuilder = new AerospikeSchemaBuilder(client, connection.getConfiguration().getDriverPolicy()); + resultSetMetaDataCache = CacheBuilder.newBuilder().build(); dbBuild = join("N/A", ", ", builds); dbEdition = join("Aerospike", ", ", editions); @@ -1304,13 +1309,19 @@ private int ordinal(ResultSetMetaData md, String columnName) { } private ResultSetMetaData getMetadata(String namespace, String table) { - try (Statement statement = connection.createStatement()) { - String query = format("SELECT * FROM \"%s.%s\" LIMIT %d", namespace, table, - connection.getConfiguration().getDriverPolicy().getSchemaBuilderMaxRecords()); - return statement.executeQuery(query).getMetaData(); - } catch (SQLException e) { - logger.severe(() -> format("Exception in getMetadata, namespace: %s, table: %s", namespace, table)); - throw new IllegalArgumentException(e); + final String key = format("%s.%s", namespace, table); + try { + return resultSetMetaDataCache.get(key, () -> { + try (Statement statement = connection.createStatement()) { + String query = format("SELECT * FROM \"%s.%s\" LIMIT 1", namespace, table); + return statement.executeQuery(query).getMetaData(); + } catch (SQLException e) { + logger.severe(() -> format("Exception in getMetadata, namespace: %s, table: %s", namespace, table)); + throw new IllegalArgumentException(e); + } + }); + } catch (ExecutionException e) { + throw new IllegalArgumentException(e.getCause()); } } diff --git a/src/main/java/com/aerospike/jdbc/AerospikePreparedStatement.java b/src/main/java/com/aerospike/jdbc/AerospikePreparedStatement.java index 1d333b7..24331a8 100644 --- a/src/main/java/com/aerospike/jdbc/AerospikePreparedStatement.java +++ b/src/main/java/com/aerospike/jdbc/AerospikePreparedStatement.java @@ -4,6 +4,7 @@ import com.aerospike.client.Value; import com.aerospike.jdbc.model.AerospikeQuery; import com.aerospike.jdbc.model.DataColumn; +import com.aerospike.jdbc.model.QueryType; import com.aerospike.jdbc.sql.AerospikeResultSetMetaData; import com.aerospike.jdbc.sql.SimpleParameterMetaData; import com.aerospike.jdbc.sql.type.ByteArrayBlob; @@ -26,43 +27,40 @@ import static com.aerospike.jdbc.util.PreparedStatement.parseParameters; import static java.lang.String.format; +import static java.util.Objects.isNull; public class AerospikePreparedStatement extends AerospikeStatement implements PreparedStatement { private static final Logger logger = Logger.getLogger(AerospikePreparedStatement.class.getName()); - private final String sql; - private final AerospikeConnection connection; - private final Object[] parameterValues; - private final AerospikeQuery query; + private final String sqlStatement; + private final Object[] sqlParameters; - public AerospikePreparedStatement(IAerospikeClient client, AerospikeConnection connection, String sql) { + public AerospikePreparedStatement(IAerospikeClient client, AerospikeConnection connection, String sqlStatement) { super(client, connection); - this.sql = sql; - this.connection = connection; - parameterValues = buildParameterValues(sql); - try { - query = parseQuery(sql); - } catch (SQLException e) { - throw new UnsupportedOperationException(e); - } + this.sqlStatement = sqlStatement; + sqlParameters = buildSqlParameters(sqlStatement); + logger.info(() -> format("statement: %s, params: %d", sqlStatement, sqlParameters.length)); } - private Object[] buildParameterValues(String sql) { + private Object[] buildSqlParameters(String sql) { int params = parseParameters(sql, 0).getValue(); return new Object[params]; } @Override public ResultSet executeQuery() throws SQLException { - logger.info("AerospikePreparedStatement executeQuery"); - return super.executeQuery(sql); + String preparedQueryString = prepareQueryString(); + logger.info(() -> "executeQuery: " + preparedQueryString); + AerospikeQuery query = parseQuery(preparedQueryString); + runQuery(query); + return resultSet; } @Override public int executeUpdate() throws SQLException { - logger.info("AerospikePreparedStatement executeUpdate"); - return super.executeUpdate(sql); + executeQuery(); + return updateCount; } @Override @@ -116,7 +114,7 @@ public void setBigDecimal(int parameterIndex, BigDecimal x) throws SQLException @Override public void setString(int parameterIndex, String x) throws SQLException { - setObject(parameterIndex, "\"" + x + "\""); + setObject(parameterIndex, format("\"%s\"", x)); } @Override @@ -149,6 +147,7 @@ public void setAsciiStream(int parameterIndex, InputStream x, int length) throws */ @Override @Deprecated + @SuppressWarnings("java:S1133") public void setUnicodeStream(int parameterIndex, InputStream x, int length) throws SQLException { throw new SQLFeatureNotSupportedException("setUnicodeStream is deprecated"); } @@ -160,7 +159,7 @@ public void setBinaryStream(int parameterIndex, InputStream x, int length) throw @Override public void clearParameters() { - Arrays.fill(parameterValues, null); + Arrays.fill(sqlParameters, null); } @Override @@ -170,28 +169,36 @@ public void setObject(int parameterIndex, Object x, int targetSqlType) throws SQ @Override public void setObject(int parameterIndex, Object x) throws SQLException { - if (parameterIndex <= 0 || parameterIndex > parameterValues.length) { - throw new SQLException(parameterValues.length == 0 ? - "Current SQL statement does not have parameters" : - format("Wrong parameter index. Expected from %d till %d", 1, parameterValues.length)); + if (parameterIndex <= 0 || parameterIndex > sqlParameters.length) { + throw new SQLDataException(sqlParameters.length == 0 + ? "Current SQL statement does not have parameters" + : format("The parameter index %d is out of range, number of parameters: %d", + parameterIndex, sqlParameters.length)); } - parameterValues[parameterIndex - 1] = x; + sqlParameters[parameterIndex - 1] = x; } @Override public boolean execute() throws SQLException { - String preparedQuery = prepareQuery(); - logger.info(preparedQuery); - return execute(preparedQuery); - } - - private String prepareQuery() { - return format(this.sql.replace("?", "%s"), parameterValues); + String preparedQueryString = prepareQueryString(); + logger.info(() -> "execute: " + preparedQueryString); + AerospikeQuery query = parseQuery(preparedQueryString); + runQuery(query); + return query.getQueryType() == QueryType.SELECT; + } + + private String prepareQueryString() { + String preparedQueryString = sqlStatement; + for (Object value : sqlParameters) { + String replacement = isNull(value) ? "?" : value.toString(); + preparedQueryString = preparedQueryString.replaceFirst("\\?", replacement); + } + return preparedQueryString; } @Override public void addBatch() throws SQLException { - addBatch(sql); + addBatch(prepareQueryString()); } @Override @@ -221,6 +228,7 @@ public void setArray(int parameterIndex, Array x) throws SQLException { @Override public ResultSetMetaData getMetaData() throws SQLException { + AerospikeQuery query = parseQuery(prepareQueryString()); List columns = ((AerospikeDatabaseMetadata) connection.getMetaData()) .getSchemaBuilder() .getSchema(query.getSchemaTable()); @@ -254,6 +262,7 @@ public void setURL(int parameterIndex, URL url) throws SQLException { @Override public ParameterMetaData getParameterMetaData() throws SQLException { + AerospikeQuery query = parseQuery(prepareQueryString()); List columns = ((AerospikeDatabaseMetadata) connection.getMetaData()) .getSchemaBuilder() .getSchema(query.getSchemaTable()); @@ -297,9 +306,9 @@ public void setClob(int parameterIndex, Reader reader, long length) throws SQLEx @Override public void setBlob(int parameterIndex, InputStream inputStream, long length) throws SQLException { byte[] bytes = new byte[(int) length]; - DataInputStream dis = new DataInputStream(inputStream); + DataInputStream dataInputStream = new DataInputStream(inputStream); try { - dis.readFully(bytes); + dataInputStream.readFully(bytes); if (inputStream.read() != -1) { throw new SQLException(format("Source contains more bytes than required %d", length)); } diff --git a/src/main/java/com/aerospike/jdbc/AerospikeStatement.java b/src/main/java/com/aerospike/jdbc/AerospikeStatement.java index ac5a672..75b0464 100644 --- a/src/main/java/com/aerospike/jdbc/AerospikeStatement.java +++ b/src/main/java/com/aerospike/jdbc/AerospikeStatement.java @@ -3,6 +3,7 @@ import com.aerospike.client.IAerospikeClient; import com.aerospike.jdbc.model.AerospikeQuery; import com.aerospike.jdbc.model.Pair; +import com.aerospike.jdbc.model.QueryType; import com.aerospike.jdbc.query.QueryPerformer; import com.aerospike.jdbc.sql.SimpleWrapper; import com.aerospike.jdbc.util.AuxStatementParser; @@ -29,12 +30,14 @@ public class AerospikeStatement implements Statement, SimpleWrapper { private static final String AUTO_GENERATED_KEYS_NOT_SUPPORTED_MESSAGE = "Auto-generated keys are not supported"; protected final IAerospikeClient client; - private final Connection connection; + protected final AerospikeConnection connection; + protected String schema; + protected ResultSet resultSet; + protected int updateCount; + private int maxRows = Integer.MAX_VALUE; private int queryTimeout; - private ResultSet resultSet; - private int updateCount; public AerospikeStatement(IAerospikeClient client, AerospikeConnection connection) { this.client = client; @@ -50,12 +53,14 @@ public AerospikeStatement(IAerospikeClient client, AerospikeConnection connectio public ResultSet executeQuery(String sql) throws SQLException { logger.info(() -> "executeQuery: " + sql); AerospikeQuery query = parseQuery(sql); + runQuery(query); + return resultSet; + } + protected void runQuery(AerospikeQuery query) { Pair result = QueryPerformer.executeQuery(client, this, query); resultSet = result.getLeft(); updateCount = result.getRight(); - - return resultSet; } protected AerospikeQuery parseQuery(String sql) throws SQLException { @@ -140,8 +145,10 @@ public void setCursorName(String name) throws SQLException { @Override public boolean execute(String sql) throws SQLException { - resultSet = executeQuery(sql); - return true; + logger.info(() -> "execute: " + sql); + AerospikeQuery query = parseQuery(sql); + runQuery(query); + return query.getQueryType() == QueryType.SELECT; } @Override diff --git a/src/main/java/com/aerospike/jdbc/model/DriverPolicy.java b/src/main/java/com/aerospike/jdbc/model/DriverPolicy.java index c9e9b54..5725c44 100644 --- a/src/main/java/com/aerospike/jdbc/model/DriverPolicy.java +++ b/src/main/java/com/aerospike/jdbc/model/DriverPolicy.java @@ -4,8 +4,8 @@ public class DriverPolicy { - private static final int DEFAULT_CAPACITY = 256; - private static final int DEFAULT_TIMEOUT_MS = 1000; + private static final int DEFAULT_RECORD_SET_QUEUE_CAPACITY = 256; + private static final int DEFAULT_RECORD_SET_TIMEOUT_MS = 1000; private static final int DEFAULT_METADATA_CACHE_TTL_SECONDS = 3600; private static final int DEFAULT_SCHEMA_BUILDER_MAX_RECORDS = 1000; @@ -15,8 +15,10 @@ public class DriverPolicy { private final int schemaBuilderMaxRecords; public DriverPolicy(Properties properties) { - recordSetQueueCapacity = parseInt(properties.getProperty("recordSetQueueCapacity"), DEFAULT_CAPACITY); - recordSetTimeoutMs = parseInt(properties.getProperty("recordSetTimeoutMs"), DEFAULT_TIMEOUT_MS); + recordSetQueueCapacity = parseInt(properties.getProperty("recordSetQueueCapacity"), + DEFAULT_RECORD_SET_QUEUE_CAPACITY); + recordSetTimeoutMs = parseInt(properties.getProperty("recordSetTimeoutMs"), + DEFAULT_RECORD_SET_TIMEOUT_MS); metadataCacheTtlSeconds = parseInt(properties.getProperty("metadataCacheTtlSeconds"), DEFAULT_METADATA_CACHE_TTL_SECONDS); schemaBuilderMaxRecords = parseInt(properties.getProperty("schemaBuilderMaxRecords"), diff --git a/src/main/java/com/aerospike/jdbc/sql/IndexToLabelResultSet.java b/src/main/java/com/aerospike/jdbc/sql/IndexToLabelResultSet.java index 9607b0f..5a95f5e 100644 --- a/src/main/java/com/aerospike/jdbc/sql/IndexToLabelResultSet.java +++ b/src/main/java/com/aerospike/jdbc/sql/IndexToLabelResultSet.java @@ -86,6 +86,7 @@ default InputStream getAsciiStream(int columnIndex) throws SQLException { */ @Override @Deprecated + @SuppressWarnings("java:S1133") default InputStream getUnicodeStream(int columnIndex) throws SQLException { return getUnicodeStream(getColumnLabel(columnIndex)); } diff --git a/src/test/java/com/aerospike/jdbc/DatabaseMetadataTest.java b/src/test/java/com/aerospike/jdbc/DatabaseMetadataTest.java index d9ba837..dfb6119 100644 --- a/src/test/java/com/aerospike/jdbc/DatabaseMetadataTest.java +++ b/src/test/java/com/aerospike/jdbc/DatabaseMetadataTest.java @@ -13,6 +13,7 @@ import java.util.Objects; import static com.aerospike.jdbc.util.TestUtil.closeQuietly; +import static java.lang.String.format; import static org.testng.Assert.assertEquals; import static org.testng.Assert.assertFalse; import static org.testng.Assert.assertTrue; @@ -25,10 +26,8 @@ public void setUp() throws SQLException { Objects.requireNonNull(connection, "connection is null"); PreparedStatement statement = null; int count; - String query = String.format( - "insert into %s (bin1, int1, str1, bool1) values (11100, 1, \"bar\", true)", - tableName - ); + String query = format("insert into %s (bin1, int1, str1, bool1) values (11100, 1, \"bar\", true)", + tableName); try { statement = connection.prepareStatement(query); count = statement.executeUpdate(); @@ -43,7 +42,7 @@ public void tearDown() throws SQLException { Objects.requireNonNull(connection, "connection is null"); PreparedStatement statement = null; ResultSet resultSet = null; - String query = String.format("delete from %s", tableName); + String query = format("delete from %s", tableName); try { statement = connection.prepareStatement(query); resultSet = statement.executeQuery(); @@ -57,7 +56,6 @@ public void tearDown() throws SQLException { @Test public void testGetTables() throws SQLException { DatabaseMetaData databaseMetaData = connection.getMetaData(); - ResultSet rs = databaseMetaData.getTables(namespace, namespace, tableName, null); assertTrue(rs.next()); diff --git a/src/test/java/com/aerospike/jdbc/PreparedQueriesTest.java b/src/test/java/com/aerospike/jdbc/PreparedQueriesTest.java index 2174acd..31751c1 100644 --- a/src/test/java/com/aerospike/jdbc/PreparedQueriesTest.java +++ b/src/test/java/com/aerospike/jdbc/PreparedQueriesTest.java @@ -12,7 +12,9 @@ import static com.aerospike.jdbc.util.Constants.PRIMARY_KEY_COLUMN_NAME; import static com.aerospike.jdbc.util.TestUtil.closeQuietly; +import static java.lang.String.format; import static org.testng.Assert.assertEquals; +import static org.testng.Assert.assertFalse; import static org.testng.Assert.assertTrue; public class PreparedQueriesTest extends JdbcBaseTest { @@ -23,7 +25,7 @@ public void setUp() throws SQLException { Objects.requireNonNull(connection, "connection is null"); PreparedStatement statement = null; int count; - String query = String.format( + String query = format( "insert into %s (%s, bin1, int1, str1, bool1) values (\"key1\", 11100, 1, \"bar\", true)", tableName, PRIMARY_KEY_COLUMN_NAME @@ -42,22 +44,23 @@ public void tearDown() throws SQLException { Objects.requireNonNull(connection, "connection is null"); PreparedStatement statement = null; ResultSet resultSet = null; - String query = String.format("delete from %s", tableName); + String query = format("delete from %s", tableName); try { statement = connection.prepareStatement(query); - resultSet = statement.executeQuery(); - resultSet.next(); + boolean result = statement.execute(); + assertFalse(result); } finally { closeQuietly(statement); closeQuietly(resultSet); } + assertTrue(statement.getUpdateCount() > 0); } @Test public void testSelectQuery() throws SQLException { PreparedStatement statement = null; ResultSet resultSet = null; - String query = String.format("select * from %s limit 10", tableName); + String query = format("select * from %s limit 10", tableName); int total = 0; try { statement = connection.prepareStatement(query); @@ -79,7 +82,7 @@ public void testSelectQuery() throws SQLException { public void testSelectByPrimaryKeyQuery() throws SQLException { PreparedStatement statement = null; ResultSet resultSet = null; - String query = String.format("select *, bin1 from %s where %s='key1'", tableName, PRIMARY_KEY_COLUMN_NAME); + String query = format("select *, bin1 from %s where %s='key1'", tableName, PRIMARY_KEY_COLUMN_NAME); int total = 0; try { statement = connection.prepareStatement(query); @@ -101,7 +104,7 @@ public void testSelectByPrimaryKeyQuery() throws SQLException { public void testInsertQuery() throws SQLException { PreparedStatement statement = null; int count; - String query = String.format("insert into %s (bin1, int1) values (11101, 3), (11102, 4)", tableName); + String query = format("insert into %s (bin1, int1) values (11101, 3), (11102, 4)", tableName); try { statement = connection.prepareStatement(query); count = statement.executeUpdate(); @@ -115,7 +118,7 @@ public void testInsertQuery() throws SQLException { public void testUpdateQuery() throws SQLException { PreparedStatement statement = null; int count; - String query = String.format("update %s set int1=100 where bin1>10000", tableName); + String query = format("update %s set int1=100 where bin1>10000", tableName); try { statement = connection.prepareStatement(query); count = statement.executeUpdate(query); @@ -124,7 +127,7 @@ public void testUpdateQuery() throws SQLException { } assertEquals(count, 1); - query = String.format("update %s set int1=100 where bin1>20000", tableName); + query = format("update %s set int1=100 where bin1>20000", tableName); try { statement = connection.prepareStatement(query); count = statement.executeUpdate(query); @@ -138,7 +141,7 @@ public void testUpdateQuery() throws SQLException { public void testSelectCountQuery() throws SQLException { PreparedStatement statement = null; ResultSet resultSet = null; - String query = String.format("select count(*) from %s", tableName); + String query = format("select count(*) from %s", tableName); try { statement = connection.prepareStatement(query); resultSet = statement.executeQuery(); @@ -155,7 +158,7 @@ public void testSelectCountQuery() throws SQLException { public void testSelectEqualsQuery() throws SQLException { PreparedStatement statement = null; ResultSet resultSet = null; - String query = String.format("select %s from %s where int1 = 1", PRIMARY_KEY_COLUMN_NAME, tableName); + String query = format("select %s from %s where int1 = 1", PRIMARY_KEY_COLUMN_NAME, tableName); try { statement = connection.prepareStatement(query); resultSet = statement.executeQuery(); @@ -173,7 +176,7 @@ public void testSelectEqualsQuery() throws SQLException { public void testSelectNotEqualsQuery() throws SQLException { PreparedStatement statement = null; ResultSet resultSet = null; - String query = String.format("select %s, int1 from %s where int1 <> 2", PRIMARY_KEY_COLUMN_NAME, tableName); + String query = format("select %s, int1 from %s where int1 <> 2", PRIMARY_KEY_COLUMN_NAME, tableName); try { statement = connection.prepareStatement(query); resultSet = statement.executeQuery(); @@ -194,10 +197,13 @@ public void testSelectNotEqualsQuery() throws SQLException { public void testSelectOrQuery() throws SQLException { PreparedStatement statement = null; ResultSet resultSet = null; - String query = String.format("select int1, str1 from %s where int1<>1 or str1 like 'bar' or bin1 is null", + String preparedQuery = format("select int1, str1 from %s where int1<>? or str1 like ? or bin1 is null", tableName); try { - statement = connection.prepareStatement(query); + statement = connection.prepareStatement(preparedQuery); + statement.setInt(1, 1); + statement.setString(2, "bar"); + resultSet = statement.executeQuery(); assertTrue(resultSet.next()); @@ -216,11 +222,17 @@ public void testSelectOrQuery() throws SQLException { public void testSelectAndQuery() throws SQLException { PreparedStatement statement = null; ResultSet resultSet = null; - String query = String.format("select * from %s where int1<=2 and bin1>=1000 and str1 is not null", + String preparedQuery = format("select * from %s where int1<=? and bin1>=? and str1 is not null", tableName); try { - statement = connection.prepareStatement(query); - resultSet = statement.executeQuery(); + statement = connection.prepareStatement(preparedQuery); + statement.setInt(1, 2); + statement.setInt(2, 1000); + + boolean result = statement.execute(); + assertTrue(result); + + resultSet = statement.getResultSet(); assertTrue(resultSet.next()); assertAllByColumnLabel(resultSet); @@ -235,7 +247,7 @@ public void testSelectAndQuery() throws SQLException { public void testSelectInQuery() throws SQLException { PreparedStatement statement = null; ResultSet resultSet = null; - String query = String.format("select * from %s where int1 in (1, 2) and str1 is not null", tableName); + String query = format("select * from %s where int1 in (1, 2) and str1 is not null", tableName); try { statement = connection.prepareStatement(query); resultSet = statement.executeQuery(); @@ -253,7 +265,7 @@ public void testSelectInQuery() throws SQLException { public void testSelectBetweenQuery() throws SQLException { PreparedStatement statement = null; ResultSet resultSet = null; - String query = String.format("select * from %s where int1 between 1 and 3", tableName); + String query = format("select * from %s where int1 between 1 and 3", tableName); try { statement = connection.prepareStatement(query); resultSet = statement.executeQuery(); diff --git a/src/test/java/com/aerospike/jdbc/QueryParserTest.java b/src/test/java/com/aerospike/jdbc/QueryParserTest.java index 70b570b..08e9372 100644 --- a/src/test/java/com/aerospike/jdbc/QueryParserTest.java +++ b/src/test/java/com/aerospike/jdbc/QueryParserTest.java @@ -20,8 +20,10 @@ public class QueryParserTest { @Test public void testSelectQuery() throws SqlParseException { SqlParser parser = SqlParser.create( - "select pkup_datetime, vendor_id from \"test.nyc-data\" where id=112279922 and trip_distance=5.79 or trip_type is not null " + - "and cab_type='green' and archived=false limit 10 offset 5", AerospikeQuery.sqlParserConfig); + "select pkup_datetime, vendor_id from \"test.nyc-data\" where id=112279922 and " + + "trip_distance=5.79 or trip_type is not null and " + + "cab_type='green' and archived=false limit 10 offset 5", + AerospikeQuery.sqlParserConfig); SqlNode parsed = parser.parseQuery(); AerospikeQuery query = parsed.accept(new AerospikeSqlVisitor()); @@ -37,7 +39,8 @@ public void testSelectQuery() throws SqlParseException { @Test public void testSelectCountQuery() throws SqlParseException { SqlParser parser = SqlParser.create( - "select count(*) from \"test.nyc-data\" where archived=false", AerospikeQuery.sqlParserConfig); + "select count(*) from \"test.nyc-data\" where archived=false", + AerospikeQuery.sqlParserConfig); SqlNode parsed = parser.parseQuery(); AerospikeQuery query = parsed.accept(new AerospikeSqlVisitor()); @@ -51,8 +54,8 @@ public void testSelectCountQuery() throws SqlParseException { @Test public void testUpdateQuery() throws SqlParseException { SqlParser parser = SqlParser.create( - "update \"test.nyc-data\" set archived=true where id=112279922 and trip_distance=5.79 or trip_type is not null " + - "and cab_type='green'", AerospikeQuery.sqlParserConfig); + "update \"test.nyc-data\" set archived=true where id=112279922 and trip_distance=5.79 or " + + "trip_type is not null and cab_type='green'", AerospikeQuery.sqlParserConfig); SqlNode parsed = parser.parseQuery(); AerospikeQuery query = parsed.accept(new AerospikeSqlVisitor()); @@ -68,7 +71,8 @@ public void testUpdateQuery() throws SqlParseException { public void testInsertQuery() throws SqlParseException { SqlParser parser = SqlParser.create( "insert into \"test.nyc-data\" (id, cab_type, trip_distance, archived) values " + - "(112279922, 'green', 2.75, false), (112279923, \"yellow\", 5.0, true)", AerospikeQuery.sqlParserConfig); + "(112279922, 'green', 2.75, false), (112279923, \"yellow\", 5.0, true)", + AerospikeQuery.sqlParserConfig); SqlNode parsed = parser.parseQuery(); AerospikeQuery query = parsed.accept(new AerospikeSqlVisitor()); @@ -83,8 +87,8 @@ public void testInsertQuery() throws SqlParseException { @Test public void testDeleteQuery() throws SqlParseException { SqlParser parser = SqlParser.create( - "delete from \"test.nyc-data\" where id=112279922 and not trip_distance=5.79 or trip_type is null " + - "and cab_type like '%green'", AerospikeQuery.sqlParserConfig); + "delete from \"test.nyc-data\" where id=112279922 and not trip_distance=5.79 or " + + "trip_type is null and cab_type like '%green'", AerospikeQuery.sqlParserConfig); SqlNode parsed = parser.parseQuery(); AerospikeQuery query = parsed.accept(new AerospikeSqlVisitor()); @@ -123,7 +127,8 @@ public void testDropSchemaQuery() throws SqlParseException { @Test public void testSelectInQuery() throws SqlParseException { SqlParser parser = SqlParser.create( - "select trip_distance from \"test.nyc-data\" where id in (1234, 1235)", AerospikeQuery.sqlParserConfig); + "select trip_distance from \"test.nyc-data\" where id in (1234, 1235)", + AerospikeQuery.sqlParserConfig); SqlNode parsed = parser.parseQuery(); AerospikeQuery query = parsed.accept(new AerospikeSqlVisitor()); @@ -137,7 +142,8 @@ public void testSelectInQuery() throws SqlParseException { @Test public void testSelectBetweenQuery() throws SqlParseException { SqlParser parser = SqlParser.create( - "select trip_distance from \"test.nyc-data\" where id between 1234 and 1245", AerospikeQuery.sqlParserConfig); + "select trip_distance from \"test.nyc-data\" where id between 1234 and 1245", + AerospikeQuery.sqlParserConfig); SqlNode parsed = parser.parseQuery(); AerospikeQuery query = parsed.accept(new AerospikeSqlVisitor()); diff --git a/src/test/java/com/aerospike/jdbc/SimpleQueriesTest.java b/src/test/java/com/aerospike/jdbc/SimpleQueriesTest.java index 7fc1be1..18b0380 100644 --- a/src/test/java/com/aerospike/jdbc/SimpleQueriesTest.java +++ b/src/test/java/com/aerospike/jdbc/SimpleQueriesTest.java @@ -12,7 +12,9 @@ import static com.aerospike.jdbc.util.Constants.PRIMARY_KEY_COLUMN_NAME; import static com.aerospike.jdbc.util.TestUtil.closeQuietly; +import static java.lang.String.format; import static org.testng.Assert.assertEquals; +import static org.testng.Assert.assertFalse; import static org.testng.Assert.assertTrue; public class SimpleQueriesTest extends JdbcBaseTest { @@ -23,7 +25,7 @@ public void setUp() throws SQLException { Objects.requireNonNull(connection, "connection is null"); Statement statement = null; int count; - String query = String.format( + String query = format( "INSERT INTO %s (%s, bin1, int1, str1, bool1) VALUES (\"key1\", 11100, 1, \"bar\", true)", tableName, PRIMARY_KEY_COLUMN_NAME @@ -42,22 +44,23 @@ public void tearDown() throws SQLException { Objects.requireNonNull(connection, "connection is null"); Statement statement = null; ResultSet resultSet = null; - String query = String.format("DELETE FROM %s", tableName); + String query = format("DELETE FROM %s", tableName); try { statement = connection.createStatement(); - resultSet = statement.executeQuery(query); - resultSet.next(); + boolean result = statement.execute(query); + assertFalse(result); } finally { closeQuietly(statement); closeQuietly(resultSet); } + assertTrue(statement.getUpdateCount() > 0); } @Test public void testSelectQuery() throws SQLException { Statement statement = null; ResultSet resultSet = null; - String query = String.format("SELECT * FROM %s LIMIT 10", tableName); + String query = format("SELECT * FROM %s LIMIT 10", tableName); int total = 0; try { statement = connection.createStatement(); @@ -79,7 +82,7 @@ public void testSelectQuery() throws SQLException { public void testSelectByPrimaryKeyQuery() throws SQLException { Statement statement = null; ResultSet resultSet = null; - String query = String.format("SELECT *, bin1 FROM %s WHERE %s='key1'", tableName, PRIMARY_KEY_COLUMN_NAME); + String query = format("SELECT *, bin1 FROM %s WHERE %s='key1'", tableName, PRIMARY_KEY_COLUMN_NAME); int total = 0; try { statement = connection.createStatement(); @@ -101,7 +104,7 @@ public void testSelectByPrimaryKeyQuery() throws SQLException { public void testInsertQuery() throws SQLException { Statement statement = null; int count; - String query = String.format("INSERT INTO %s (bin1, int1) VALUES (11101, 3), (11102, 4)", tableName); + String query = format("INSERT INTO %s (bin1, int1) VALUES (11101, 3), (11102, 4)", tableName); try { statement = connection.createStatement(); count = statement.executeUpdate(query); @@ -110,7 +113,7 @@ public void testInsertQuery() throws SQLException { } assertEquals(count, 2); - query = String.format("SELECT %s FROM %s WHERE int1 > 3", PRIMARY_KEY_COLUMN_NAME, tableName); + query = format("SELECT %s FROM %s WHERE int1 > 3", PRIMARY_KEY_COLUMN_NAME, tableName); int total = 0; ResultSet resultSet = null; try { @@ -134,7 +137,7 @@ public void testInsertQuery() throws SQLException { public void testUpdateQuery() throws SQLException { Statement statement = null; int count; - String query = String.format("UPDATE %s SET int1=100 WHERE bin1>10000", tableName); + String query = format("UPDATE %s SET int1=100 WHERE bin1>10000", tableName); try { statement = connection.createStatement(); count = statement.executeUpdate(query); @@ -143,7 +146,7 @@ public void testUpdateQuery() throws SQLException { } assertEquals(count, 1); - query = String.format("UPDATE %s SET int1=100 WHERE bin1>20000", tableName); + query = format("UPDATE %s SET int1=100 WHERE bin1>20000", tableName); try { statement = connection.createStatement(); count = statement.executeUpdate(query); @@ -157,7 +160,7 @@ public void testUpdateQuery() throws SQLException { public void testSelectCountQuery() throws SQLException { Statement statement = null; ResultSet resultSet = null; - String query = String.format("SELECT count(*) FROM %s", tableName); + String query = format("SELECT count(*) FROM %s", tableName); try { statement = connection.createStatement(); resultSet = statement.executeQuery(query); @@ -174,7 +177,7 @@ public void testSelectCountQuery() throws SQLException { public void testSelectEqualsQuery() throws SQLException { Statement statement = null; ResultSet resultSet = null; - String query = String.format("SELECT %s FROM %s WHERE int1 = 1", PRIMARY_KEY_COLUMN_NAME, tableName); + String query = format("SELECT %s FROM %s WHERE int1 = 1", PRIMARY_KEY_COLUMN_NAME, tableName); try { statement = connection.createStatement(); resultSet = statement.executeQuery(query); @@ -192,7 +195,7 @@ public void testSelectEqualsQuery() throws SQLException { public void testSelectNotEqualsQuery() throws SQLException { Statement statement = null; ResultSet resultSet = null; - String query = String.format("SELECT %s, int1 FROM %s WHERE int1 <> 2", PRIMARY_KEY_COLUMN_NAME, tableName); + String query = format("SELECT %s, int1 FROM %s WHERE int1 <> 2", PRIMARY_KEY_COLUMN_NAME, tableName); try { statement = connection.createStatement(); resultSet = statement.executeQuery(query); @@ -213,7 +216,7 @@ public void testSelectNotEqualsQuery() throws SQLException { public void testSelectOrQuery() throws SQLException { Statement statement = null; ResultSet resultSet = null; - String query = String.format("SELECT int1, str1 FROM %s WHERE int1<>1 OR str1 LIKE 'bar' OR bin1 IS NULL", + String query = format("SELECT int1, str1 FROM %s WHERE int1<>1 OR str1 LIKE 'bar' OR bin1 IS NULL", tableName); try { statement = connection.createStatement(); @@ -235,11 +238,14 @@ public void testSelectOrQuery() throws SQLException { public void testSelectAndQuery() throws SQLException { Statement statement = null; ResultSet resultSet = null; - String query = String.format("SELECT * FROM %s WHERE int1<=2 AND bin1>=1000 AND str1 IS NOT NULL", + String query = format("SELECT * FROM %s WHERE int1<=2 AND bin1>=1000 AND str1 IS NOT NULL", tableName); try { statement = connection.createStatement(); - resultSet = statement.executeQuery(query); + boolean result = statement.execute(query); + assertTrue(result); + + resultSet = statement.getResultSet(); assertTrue(resultSet.next()); assertAllByColumnLabel(resultSet); @@ -254,7 +260,7 @@ public void testSelectAndQuery() throws SQLException { public void testSelectInQuery() throws SQLException { Statement statement = null; ResultSet resultSet = null; - String query = String.format("SELECT * FROM %s WHERE int1 IN (1, 2) AND str1 IS NOT NULL", tableName); + String query = format("SELECT * FROM %s WHERE int1 IN (1, 2) AND str1 IS NOT NULL", tableName); try { statement = connection.createStatement(); resultSet = statement.executeQuery(query); @@ -272,7 +278,7 @@ public void testSelectInQuery() throws SQLException { public void testSelectBetweenQuery() throws SQLException { Statement statement = null; ResultSet resultSet = null; - String query = String.format("SELECT * FROM %s WHERE int1 BETWEEN 1 AND 3", tableName); + String query = format("SELECT * FROM %s WHERE int1 BETWEEN 1 AND 3", tableName); try { statement = connection.createStatement(); resultSet = statement.executeQuery(query);