From b5004abd8fd40c0f0395f4ca34f65b4cc87390ef Mon Sep 17 00:00:00 2001 From: Eugene R Date: Mon, 1 Jan 2024 16:09:54 +0200 Subject: [PATCH] FMWK-302 Fix query by primary key written with sendKey false (#66) --- .../aerospike/jdbc/model/AerospikeQuery.java | 10 ++ .../jdbc/predicate/QueryPredicate.java | 2 +- .../jdbc/predicate/QueryPredicateBase.java | 9 +- .../jdbc/predicate/QueryPredicateBinary.java | 9 +- .../jdbc/predicate/QueryPredicateBoolean.java | 19 ++- .../predicate/QueryPredicateIsNotNull.java | 2 +- .../jdbc/predicate/QueryPredicateIsNull.java | 2 +- .../jdbc/predicate/QueryPredicateLike.java | 2 +- .../jdbc/predicate/QueryPredicateList.java | 9 +- .../jdbc/predicate/QueryPredicatePrefix.java | 8 +- .../jdbc/predicate/QueryPredicateRange.java | 2 +- .../aerospike/jdbc/query/PolicyBuilder.java | 13 +- .../aerospike/jdbc/DatabaseMetadataTest.java | 18 ++- .../com/aerospike/jdbc/IndexQueriesTest.java | 28 ++-- .../java/com/aerospike/jdbc/JdbcBaseTest.java | 38 +---- .../aerospike/jdbc/PreparedQueriesTest.java | 123 ++++++++------- .../aerospike/jdbc/QuerySendKeyFalseTest.java | 145 ++++++++++++++++++ .../com/aerospike/jdbc/SimpleQueriesTest.java | 110 ++++++++----- .../com/aerospike/jdbc/util/TestConfig.java | 13 ++ .../com/aerospike/jdbc/util/TestRecord.java | 128 ++++++++++++++++ .../com/aerospike/jdbc/util/TestUtil.java | 26 ++-- 21 files changed, 528 insertions(+), 188 deletions(-) create mode 100644 src/test/java/com/aerospike/jdbc/QuerySendKeyFalseTest.java create mode 100644 src/test/java/com/aerospike/jdbc/util/TestConfig.java create mode 100644 src/test/java/com/aerospike/jdbc/util/TestRecord.java diff --git a/src/main/java/com/aerospike/jdbc/model/AerospikeQuery.java b/src/main/java/com/aerospike/jdbc/model/AerospikeQuery.java index bf36379..4304cc2 100644 --- a/src/main/java/com/aerospike/jdbc/model/AerospikeQuery.java +++ b/src/main/java/com/aerospike/jdbc/model/AerospikeQuery.java @@ -1,5 +1,7 @@ package com.aerospike.jdbc.model; +import com.aerospike.client.exp.Exp; +import com.aerospike.client.exp.Expression; import com.aerospike.jdbc.predicate.QueryPredicate; import com.fasterxml.jackson.core.JsonProcessingException; import com.fasterxml.jackson.databind.ObjectMapper; @@ -176,6 +178,14 @@ public boolean isIndexable() { return Objects.nonNull(predicate) && predicate.isIndexable() && Objects.isNull(offset); } + public Expression toFilterExpression(boolean withPrimaryKey) { + if (predicate == null) { + return null; + } + Exp exp = predicate.toFilterExpression(withPrimaryKey); + return exp == null ? null : Exp.build(exp); + } + @Override public String toString() { try { diff --git a/src/main/java/com/aerospike/jdbc/predicate/QueryPredicate.java b/src/main/java/com/aerospike/jdbc/predicate/QueryPredicate.java index d3ac0f0..1c542fd 100644 --- a/src/main/java/com/aerospike/jdbc/predicate/QueryPredicate.java +++ b/src/main/java/com/aerospike/jdbc/predicate/QueryPredicate.java @@ -10,7 +10,7 @@ public interface QueryPredicate { - Exp toFilterExpression(); + Exp toFilterExpression(boolean withPrimaryKey); Optional toFilter(String binName); diff --git a/src/main/java/com/aerospike/jdbc/predicate/QueryPredicateBase.java b/src/main/java/com/aerospike/jdbc/predicate/QueryPredicateBase.java index dfa325e..0705cd9 100644 --- a/src/main/java/com/aerospike/jdbc/predicate/QueryPredicateBase.java +++ b/src/main/java/com/aerospike/jdbc/predicate/QueryPredicateBase.java @@ -1,11 +1,12 @@ package com.aerospike.jdbc.predicate; import com.aerospike.client.exp.Exp; -import com.aerospike.jdbc.util.Constants; import java.util.Collections; import java.util.List; +import static com.aerospike.jdbc.util.Constants.PRIMARY_KEY_COLUMN_NAME; + public abstract class QueryPredicateBase implements QueryPredicate { protected final String binName; @@ -64,8 +65,12 @@ protected Exp getValueExp(Object value) { } } + protected boolean isPrimaryKeyPredicate() { + return binName.equals(PRIMARY_KEY_COLUMN_NAME); + } + protected Exp buildLeftExp() { - return binName.equals(Constants.PRIMARY_KEY_COLUMN_NAME) + return isPrimaryKeyPredicate() ? Exp.key(valueType) : Exp.bin(binName, valueType); } diff --git a/src/main/java/com/aerospike/jdbc/predicate/QueryPredicateBinary.java b/src/main/java/com/aerospike/jdbc/predicate/QueryPredicateBinary.java index 0b012d8..edf77ea 100644 --- a/src/main/java/com/aerospike/jdbc/predicate/QueryPredicateBinary.java +++ b/src/main/java/com/aerospike/jdbc/predicate/QueryPredicateBinary.java @@ -7,8 +7,6 @@ import java.util.Collections; import java.util.Optional; -import static com.aerospike.jdbc.util.Constants.PRIMARY_KEY_COLUMN_NAME; - public class QueryPredicateBinary extends QueryPredicateBase { private final Operator operator; @@ -21,7 +19,10 @@ public QueryPredicateBinary(String binName, Operator operator, Object value) { } @Override - public Exp toFilterExpression() { + public Exp toFilterExpression(boolean withPrimaryKey) { + if (isPrimaryKeyPredicate() && !withPrimaryKey) { + return null; + } return operator.exp(buildLeftExp(), getValueExp(value)); } @@ -39,7 +40,7 @@ public Optional toFilter(String binName) { @Override public Collection getPrimaryKeys() { - if (binName.equals(PRIMARY_KEY_COLUMN_NAME) && operator == OperatorBinary.EQ) { + if (isPrimaryKeyPredicate() && operator == OperatorBinary.EQ) { return Collections.singletonList(value); } return Collections.emptyList(); diff --git a/src/main/java/com/aerospike/jdbc/predicate/QueryPredicateBoolean.java b/src/main/java/com/aerospike/jdbc/predicate/QueryPredicateBoolean.java index 2bf1335..345013c 100644 --- a/src/main/java/com/aerospike/jdbc/predicate/QueryPredicateBoolean.java +++ b/src/main/java/com/aerospike/jdbc/predicate/QueryPredicateBoolean.java @@ -26,9 +26,19 @@ public QueryPredicateBoolean( this.right = right; } + @SuppressWarnings("OptionalUsedAsFieldOrParameterType") + private static Optional or(Optional optional, Optional fallback) { + return optional.isPresent() ? optional : fallback; + } + @Override - public Exp toFilterExpression() { - return operator.exp(left.toFilterExpression(), right.toFilterExpression()); + public Exp toFilterExpression(boolean withPrimaryKey) { + Exp leftExp = left.toFilterExpression(withPrimaryKey); + Exp rightExp = right.toFilterExpression(withPrimaryKey); + if (leftExp == null || rightExp == null) { + return leftExp == null ? rightExp : leftExp; + } + return operator.exp(leftExp, rightExp); } @Override @@ -65,9 +75,4 @@ public Collection getPrimaryKeys() { } return Collections.emptyList(); } - - @SuppressWarnings("OptionalUsedAsFieldOrParameterType") - private static Optional or(Optional optional, Optional fallback) { - return optional.isPresent() ? optional : fallback; - } } diff --git a/src/main/java/com/aerospike/jdbc/predicate/QueryPredicateIsNotNull.java b/src/main/java/com/aerospike/jdbc/predicate/QueryPredicateIsNotNull.java index 8c05681..d085367 100644 --- a/src/main/java/com/aerospike/jdbc/predicate/QueryPredicateIsNotNull.java +++ b/src/main/java/com/aerospike/jdbc/predicate/QueryPredicateIsNotNull.java @@ -18,7 +18,7 @@ public QueryPredicateIsNotNull(String binName) { } @Override - public Exp toFilterExpression() { + public Exp toFilterExpression(boolean withPrimaryKey) { return binName.equals(PRIMARY_KEY_COLUMN_NAME) ? Exp.keyExists() : Exp.binExists(binName); diff --git a/src/main/java/com/aerospike/jdbc/predicate/QueryPredicateIsNull.java b/src/main/java/com/aerospike/jdbc/predicate/QueryPredicateIsNull.java index eefdcbb..99d0030 100644 --- a/src/main/java/com/aerospike/jdbc/predicate/QueryPredicateIsNull.java +++ b/src/main/java/com/aerospike/jdbc/predicate/QueryPredicateIsNull.java @@ -18,7 +18,7 @@ public QueryPredicateIsNull(String binName) { } @Override - public Exp toFilterExpression() { + public Exp toFilterExpression(boolean withPrimaryKey) { return Exp.not( binName.equals(PRIMARY_KEY_COLUMN_NAME) ? Exp.keyExists() diff --git a/src/main/java/com/aerospike/jdbc/predicate/QueryPredicateLike.java b/src/main/java/com/aerospike/jdbc/predicate/QueryPredicateLike.java index de9b099..0e7d685 100644 --- a/src/main/java/com/aerospike/jdbc/predicate/QueryPredicateLike.java +++ b/src/main/java/com/aerospike/jdbc/predicate/QueryPredicateLike.java @@ -18,7 +18,7 @@ public QueryPredicateLike(String binName, String expression) { } @Override - public Exp toFilterExpression() { + public Exp toFilterExpression(boolean withPrimaryKey) { return Exp.regexCompare( expression, RegexFlag.ICASE | RegexFlag.NEWLINE, diff --git a/src/main/java/com/aerospike/jdbc/predicate/QueryPredicateList.java b/src/main/java/com/aerospike/jdbc/predicate/QueryPredicateList.java index 28945f1..cddb33b 100644 --- a/src/main/java/com/aerospike/jdbc/predicate/QueryPredicateList.java +++ b/src/main/java/com/aerospike/jdbc/predicate/QueryPredicateList.java @@ -8,8 +8,6 @@ import java.util.Collections; import java.util.Optional; -import static com.aerospike.jdbc.util.Constants.PRIMARY_KEY_COLUMN_NAME; - public class QueryPredicateList extends QueryPredicateBase { private final Operator operator; @@ -22,7 +20,10 @@ public QueryPredicateList(String binName, Operator operator, Object[] values) { } @Override - public Exp toFilterExpression() { + public Exp toFilterExpression(boolean withPrimaryKey) { + if (isPrimaryKeyPredicate() && !withPrimaryKey) { + return null; + } return operator.exp( Arrays.stream(values) .map(v -> Exp.eq(buildLeftExp(), getValueExp(v))) @@ -42,7 +43,7 @@ public boolean isIndexable() { @Override public Collection getPrimaryKeys() { - if (binName.equals(PRIMARY_KEY_COLUMN_NAME)) { + if (isPrimaryKeyPredicate()) { return Arrays.asList(values); } return Collections.emptyList(); diff --git a/src/main/java/com/aerospike/jdbc/predicate/QueryPredicatePrefix.java b/src/main/java/com/aerospike/jdbc/predicate/QueryPredicatePrefix.java index 1d55fe1..f15a826 100644 --- a/src/main/java/com/aerospike/jdbc/predicate/QueryPredicatePrefix.java +++ b/src/main/java/com/aerospike/jdbc/predicate/QueryPredicatePrefix.java @@ -21,8 +21,12 @@ public QueryPredicatePrefix( } @Override - public Exp toFilterExpression() { - return operator.exp(right.toFilterExpression()); + public Exp toFilterExpression(boolean withPrimaryKey) { + Exp rightExp = right.toFilterExpression(withPrimaryKey); + if (rightExp == null) { + return null; + } + return operator.exp(rightExp); } @Override diff --git a/src/main/java/com/aerospike/jdbc/predicate/QueryPredicateRange.java b/src/main/java/com/aerospike/jdbc/predicate/QueryPredicateRange.java index 1dd55e8..f384daa 100644 --- a/src/main/java/com/aerospike/jdbc/predicate/QueryPredicateRange.java +++ b/src/main/java/com/aerospike/jdbc/predicate/QueryPredicateRange.java @@ -23,7 +23,7 @@ public QueryPredicateRange( } @Override - public Exp toFilterExpression() { + public Exp toFilterExpression(boolean withPrimaryKey) { return Exp.and( Exp.ge(buildLeftExp(), getValueExp(lowValue)), Exp.lt(buildLeftExp(), getValueExp(highValue)) diff --git a/src/main/java/com/aerospike/jdbc/query/PolicyBuilder.java b/src/main/java/com/aerospike/jdbc/query/PolicyBuilder.java index 836d61b..c76c5df 100644 --- a/src/main/java/com/aerospike/jdbc/query/PolicyBuilder.java +++ b/src/main/java/com/aerospike/jdbc/query/PolicyBuilder.java @@ -1,7 +1,6 @@ package com.aerospike.jdbc.query; import com.aerospike.client.IAerospikeClient; -import com.aerospike.client.exp.Exp; import com.aerospike.client.policy.BatchReadPolicy; import com.aerospike.client.policy.BatchWritePolicy; import com.aerospike.client.policy.QueryPolicy; @@ -23,15 +22,13 @@ public PolicyBuilder(IAerospikeClient client) { public ScanPolicy buildScanPolicy(AerospikeQuery query) { ScanPolicy scanPolicy = new ScanPolicy(client.getScanPolicyDefault()); scanPolicy.maxRecords = Objects.isNull(query.getLimit()) ? 0 : query.getLimit(); - scanPolicy.filterExp = Objects.isNull(query.getPredicate()) - ? null : Exp.build(query.getPredicate().toFilterExpression()); + scanPolicy.filterExp = query.toFilterExpression(true); return scanPolicy; } public QueryPolicy buildQueryPolicy(AerospikeQuery query) { QueryPolicy queryPolicy = new QueryPolicy(client.getQueryPolicyDefault()); - queryPolicy.filterExp = Objects.isNull(query.getPredicate()) - ? null : Exp.build(query.getPredicate().toFilterExpression()); + queryPolicy.filterExp = query.toFilterExpression(true); return queryPolicy; } @@ -43,8 +40,7 @@ public ScanPolicy buildScanNoBinDataPolicy(AerospikeQuery query) { public WritePolicy buildWritePolicy(AerospikeQuery query) { WritePolicy writePolicy = new WritePolicy(client.getWritePolicyDefault()); - writePolicy.filterExp = Objects.isNull(query.getPredicate()) - ? null : Exp.build(query.getPredicate().toFilterExpression()); + writePolicy.filterExp = query.toFilterExpression(true); return writePolicy; } @@ -56,8 +52,7 @@ public WritePolicy buildDeleteWritePolicy() { public BatchReadPolicy buildBatchReadPolicy(AerospikeQuery query) { BatchReadPolicy batchReadPolicy = new BatchReadPolicy(); - batchReadPolicy.filterExp = Objects.isNull(query.getPredicate()) - ? null : Exp.build(query.getPredicate().toFilterExpression()); + batchReadPolicy.filterExp = query.toFilterExpression(false); return batchReadPolicy; } diff --git a/src/test/java/com/aerospike/jdbc/DatabaseMetadataTest.java b/src/test/java/com/aerospike/jdbc/DatabaseMetadataTest.java index 1e53edb..2d13396 100644 --- a/src/test/java/com/aerospike/jdbc/DatabaseMetadataTest.java +++ b/src/test/java/com/aerospike/jdbc/DatabaseMetadataTest.java @@ -1,5 +1,6 @@ package com.aerospike.jdbc; +import com.aerospike.jdbc.util.TestRecord; import com.aerospike.jdbc.util.TestUtil; import org.testng.annotations.AfterClass; import org.testng.annotations.BeforeClass; @@ -11,6 +12,8 @@ import java.sql.SQLException; import java.util.Objects; +import static com.aerospike.jdbc.util.TestConfig.NAMESPACE; +import static com.aerospike.jdbc.util.TestConfig.TABLE_NAME; import static com.aerospike.jdbc.util.TestUtil.closeQuietly; import static java.lang.String.format; import static org.testng.Assert.assertEquals; @@ -19,13 +22,18 @@ public class DatabaseMetadataTest extends JdbcBaseTest { + private final TestRecord testRecord; + + DatabaseMetadataTest() { + testRecord = new TestRecord("key1", true, 11100, 1, "bar"); + } + @BeforeClass public void setUp() throws SQLException { Objects.requireNonNull(connection, "connection is null"); PreparedStatement statement = null; int count; - String query = format("insert into %s (bin1, int1, str1, bool1) values (11100, 1, \"bar\", true)", - tableName); + String query = testRecord.toInsertQuery(); try { statement = connection.prepareStatement(query); count = statement.executeUpdate(); @@ -39,7 +47,7 @@ public void setUp() throws SQLException { public void tearDown() throws SQLException { Objects.requireNonNull(connection, "connection is null"); PreparedStatement statement = null; - String query = format("delete from %s", tableName); + String query = format("delete from %s", TABLE_NAME); try { statement = connection.prepareStatement(query); boolean result = statement.execute(); @@ -53,10 +61,10 @@ public void tearDown() throws SQLException { @Test public void testGetTables() throws SQLException { DatabaseMetaData databaseMetaData = connection.getMetaData(); - ResultSet tables = databaseMetaData.getTables(namespace, "", tableName, null); + ResultSet tables = databaseMetaData.getTables(NAMESPACE, "", TABLE_NAME, null); if (tables.next()) { - assertEquals(tables.getString("TABLE_NAME"), tableName); + assertEquals(tables.getString("TABLE_NAME"), TABLE_NAME); assertFalse(tables.next()); } TestUtil.closeQuietly(tables); diff --git a/src/test/java/com/aerospike/jdbc/IndexQueriesTest.java b/src/test/java/com/aerospike/jdbc/IndexQueriesTest.java index aca53da..24c30a1 100644 --- a/src/test/java/com/aerospike/jdbc/IndexQueriesTest.java +++ b/src/test/java/com/aerospike/jdbc/IndexQueriesTest.java @@ -1,5 +1,6 @@ package com.aerospike.jdbc; +import com.aerospike.jdbc.util.TestRecord; import org.testng.annotations.AfterClass; import org.testng.annotations.BeforeClass; import org.testng.annotations.Test; @@ -8,8 +9,9 @@ import java.sql.Statement; import java.util.Objects; -import static com.aerospike.jdbc.util.Constants.PRIMARY_KEY_COLUMN_NAME; +import static com.aerospike.jdbc.util.TestConfig.TABLE_NAME; import static com.aerospike.jdbc.util.TestUtil.closeQuietly; +import static com.aerospike.jdbc.util.TestUtil.sleep; import static java.lang.String.format; import static org.testng.Assert.assertEquals; import static org.testng.Assert.assertFalse; @@ -17,16 +19,18 @@ public class IndexQueriesTest extends JdbcBaseTest { + private final TestRecord testRecord; + + IndexQueriesTest() { + testRecord = new TestRecord("key1", true, 11100, 1, "bar"); + } + @BeforeClass public void setUp() throws SQLException { Objects.requireNonNull(connection, "connection is null"); Statement statement = null; int count; - String query = format( - "INSERT INTO %s (%s, bin1, int1, str1, bool1) VALUES (\"key1\", 11100, 1, \"bar\", true)", - tableName, - PRIMARY_KEY_COLUMN_NAME - ); + String query = testRecord.toInsertQuery(); try { statement = connection.createStatement(); count = statement.executeUpdate(query); @@ -40,7 +44,7 @@ public void setUp() throws SQLException { public void tearDown() throws SQLException { Objects.requireNonNull(connection, "connection is null"); Statement statement = null; - String query = format("TRUNCATE TABLE %s", tableName); + String query = format("TRUNCATE TABLE %s", TABLE_NAME); try { statement = connection.createStatement(); boolean result = statement.execute(query); @@ -56,7 +60,7 @@ public void tearDown() throws SQLException { public void testIndexCreateSuccess() throws SQLException { Statement statement = null; int count; - String query = format("CREATE INDEX str1_idx ON %s (str1);", tableName); + String query = format("CREATE INDEX str1_idx ON %s (str1);", TABLE_NAME); try { statement = connection.createStatement(); count = statement.executeUpdate(query); @@ -68,7 +72,7 @@ public void testIndexCreateSuccess() throws SQLException { @Test public void testIndexCreateMultiColumn() throws SQLException { - String query = format("CREATE INDEX multi_idx ON %s (str1, int1)", tableName); + String query = format("CREATE INDEX multi_idx ON %s (str1, int1)", TABLE_NAME); final Statement statement = connection.createStatement(); assertThrows(UnsupportedOperationException.class, () -> statement.executeUpdate(query)); closeQuietly(statement); @@ -76,7 +80,7 @@ public void testIndexCreateMultiColumn() throws SQLException { @Test public void testIndexCreateUnsupportedType() throws SQLException { - String query = format("CREATE INDEX bool1_idx ON %s (bool1)", tableName); + String query = format("CREATE INDEX bool1_idx ON %s (bool1)", TABLE_NAME); final Statement statement = connection.createStatement(); assertThrows(UnsupportedOperationException.class, () -> statement.executeUpdate(query)); closeQuietly(statement); @@ -84,7 +88,7 @@ public void testIndexCreateUnsupportedType() throws SQLException { @Test public void testIndexCreateNonExistentColumn() throws SQLException { - String query = format("CREATE INDEX ne_idx ON %s (ne)", tableName); + String query = format("CREATE INDEX ne_idx ON %s (ne)", TABLE_NAME); final Statement statement = connection.createStatement(); assertThrows(IllegalArgumentException.class, () -> statement.executeUpdate(query)); closeQuietly(statement); @@ -94,7 +98,7 @@ public void testIndexCreateNonExistentColumn() throws SQLException { public void testIndexDropSuccess() throws SQLException { Statement statement = null; int count; - String query = format("DROP INDEX str1_idx ON %s;", tableName); + String query = format("DROP INDEX str1_idx ON %s;", TABLE_NAME); try { statement = connection.createStatement(); count = statement.executeUpdate(query); diff --git a/src/test/java/com/aerospike/jdbc/JdbcBaseTest.java b/src/test/java/com/aerospike/jdbc/JdbcBaseTest.java index 2379a2a..1bdb16e 100644 --- a/src/test/java/com/aerospike/jdbc/JdbcBaseTest.java +++ b/src/test/java/com/aerospike/jdbc/JdbcBaseTest.java @@ -5,23 +5,17 @@ import java.sql.Connection; import java.sql.DriverManager; -import java.sql.ResultSet; import java.sql.SQLException; import java.util.concurrent.Executors; import java.util.logging.Logger; -import static com.aerospike.jdbc.util.Constants.PRIMARY_KEY_COLUMN_NAME; -import static org.testng.Assert.assertEquals; -import static org.testng.Assert.assertTrue; +import static com.aerospike.jdbc.util.TestConfig.HOSTNAME; +import static com.aerospike.jdbc.util.TestConfig.NAMESPACE; +import static com.aerospike.jdbc.util.TestConfig.PORT; public abstract class JdbcBaseTest { - protected static final String namespace = "test"; - protected static final String tableName = "jdbc"; - private static final Logger logger = Logger.getLogger(JdbcBaseTest.class.getName()); - private static final String hostname = "localhost"; - private static final int port = 3000; protected static Connection connection; @@ -29,7 +23,7 @@ public abstract class JdbcBaseTest { public static void connectionInit() throws Exception { logger.info("connectionInit"); Class.forName("com.aerospike.jdbc.AerospikeDriver").newInstance(); - String url = String.format("jdbc:aerospike:%s:%d/%s?sendKey=true", hostname, port, namespace); + String url = String.format("jdbc:aerospike:%s:%d/%s?sendKey=true", HOSTNAME, PORT, NAMESPACE); connection = DriverManager.getConnection(url); connection.setNetworkTimeout(Executors.newSingleThreadExecutor(), 5000); } @@ -39,28 +33,4 @@ public static void connectionClose() throws SQLException { logger.info("connectionClose"); connection.close(); } - - protected void assertAllByColumnLabel(ResultSet resultSet) throws SQLException { - assertEquals(resultSet.getString(PRIMARY_KEY_COLUMN_NAME), "key1"); - assertEquals(resultSet.getInt("bin1"), 11100); - assertTrue(resultSet.getBoolean("bool1")); - assertEquals(resultSet.getInt("int1"), 1); - assertEquals(resultSet.getString("str1"), "bar"); - } - - protected void assertAllByColumnIndex(ResultSet resultSet) throws SQLException { - assertEquals(resultSet.getString(1), "key1"); - assertEquals(resultSet.getInt(2), 11100); - assertTrue(resultSet.getBoolean(3)); - assertEquals(resultSet.getInt(4), 1); - assertEquals(resultSet.getString(5), "bar"); - } - - @SuppressWarnings("all") - protected void sleep(long millis) { - try { - Thread.sleep(millis); - } catch (InterruptedException e) { - } - } } diff --git a/src/test/java/com/aerospike/jdbc/PreparedQueriesTest.java b/src/test/java/com/aerospike/jdbc/PreparedQueriesTest.java index 77ce9dc..086e1b8 100644 --- a/src/test/java/com/aerospike/jdbc/PreparedQueriesTest.java +++ b/src/test/java/com/aerospike/jdbc/PreparedQueriesTest.java @@ -1,39 +1,68 @@ package com.aerospike.jdbc; +import com.aerospike.jdbc.util.TestRecord; +import org.testng.annotations.AfterClass; import org.testng.annotations.AfterMethod; +import org.testng.annotations.BeforeClass; import org.testng.annotations.BeforeMethod; import org.testng.annotations.Test; +import java.sql.Connection; +import java.sql.DriverManager; import java.sql.PreparedStatement; import java.sql.ResultSet; import java.sql.SQLException; import java.util.Objects; +import java.util.concurrent.Executors; +import java.util.logging.Logger; import static com.aerospike.jdbc.util.Constants.PRIMARY_KEY_COLUMN_NAME; +import static com.aerospike.jdbc.util.TestConfig.HOSTNAME; +import static com.aerospike.jdbc.util.TestConfig.NAMESPACE; +import static com.aerospike.jdbc.util.TestConfig.PORT; +import static com.aerospike.jdbc.util.TestConfig.TABLE_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 { +public class PreparedQueriesTest { - private final byte[] blobValue = new byte[]{1, 2, 3, 4}; + private static final Logger logger = Logger.getLogger(PreparedQueriesTest.class.getName()); + private static Connection connection; + + private final TestRecord testRecord; + + PreparedQueriesTest() { + testRecord = new TestRecord("key1", true, 11100, 1, "bar", + new byte[]{1, 2, 3, 4}, null); + } + + @BeforeClass + public static void connectionInit() throws Exception { + logger.info("connectionInit"); + Class.forName("com.aerospike.jdbc.AerospikeDriver").newInstance(); + String url = String.format("jdbc:aerospike:%s:%d/%s?sendKey=true", HOSTNAME, PORT, NAMESPACE); + connection = DriverManager.getConnection(url); + connection.setNetworkTimeout(Executors.newSingleThreadExecutor(), 5000); + } + + @AfterClass + public static void connectionClose() throws SQLException { + logger.info("connectionClose"); + connection.close(); + } @BeforeMethod public void setUp() throws SQLException { Objects.requireNonNull(connection, "connection is null"); PreparedStatement statement = null; int count; - String query = format( - "insert into %s (%s, bin1, int1, str1, bool1, val1, val2) values " - + "(\"key1\", 11100, 1, \"bar\", true, ?, ?)", - tableName, - PRIMARY_KEY_COLUMN_NAME - ); + String query = testRecord.toPreparedInsertQuery(); try { statement = connection.prepareStatement(query); - statement.setBytes(1, blobValue); + statement.setBytes(1, testRecord.getVal1()); statement.setNull(2, 0); count = statement.executeUpdate(); } finally { @@ -46,7 +75,7 @@ public void setUp() throws SQLException { public void tearDown() throws SQLException { Objects.requireNonNull(connection, "connection is null"); PreparedStatement statement = null; - String query = format("delete from %s", tableName); + String query = format("delete from %s", TABLE_NAME); try { statement = connection.prepareStatement(query); boolean result = statement.execute(); @@ -61,15 +90,13 @@ public void tearDown() throws SQLException { public void testSelectQuery() throws SQLException { PreparedStatement statement = null; ResultSet resultSet = null; - String query = format("select * from %s limit 10", tableName); + String query = format("select * from %s limit 10", TABLE_NAME); int total = 0; try { statement = connection.prepareStatement(query); resultSet = statement.executeQuery(); while (resultSet.next()) { - assertAllByColumnLabel(resultSet); - assertAllByColumnIndex(resultSet); - assertBlobValue(resultSet); + testRecord.assertPreparedResultSet(resultSet); total++; } @@ -84,17 +111,15 @@ public void testSelectQuery() throws SQLException { public void testSelectByPrimaryKeyQuery() throws SQLException { PreparedStatement statement = null; ResultSet resultSet = null; - String query = format("select *, bin1 from %s where %s=?", tableName, PRIMARY_KEY_COLUMN_NAME); + String query = format("select *, int1 from %s where %s=?", TABLE_NAME, PRIMARY_KEY_COLUMN_NAME); int total = 0; try { statement = connection.prepareStatement(query); - statement.setString(1, "key1"); + statement.setString(1, testRecord.getPrimaryKey()); resultSet = statement.executeQuery(); while (resultSet.next()) { - assertAllByColumnLabel(resultSet); - assertAllByColumnIndex(resultSet); - assertBlobValue(resultSet); + testRecord.assertPreparedResultSet(resultSet); total++; } @@ -109,7 +134,7 @@ public void testSelectByPrimaryKeyQuery() throws SQLException { public void testInsertQuery() throws SQLException { PreparedStatement statement = null; int count; - String query = format("insert into %s (bin1, int1) values (?, ?), (?, ?)", tableName); + String query = format("insert into %s (int1, int2) values (?, ?), (?, ?)", TABLE_NAME); try { statement = connection.prepareStatement(query); statement.setInt(1, 11101); @@ -127,7 +152,7 @@ public void testInsertQuery() throws SQLException { public void testUpdateQuery() throws SQLException { PreparedStatement statement = null; int count; - String query = format("update %s set int1=? where bin1>?", tableName); + String query = format("update %s set int2=? where int1>?", TABLE_NAME); try { statement = connection.prepareStatement(query); statement.setInt(1, 100); @@ -138,7 +163,7 @@ public void testUpdateQuery() throws SQLException { } assertEquals(count, 1); - query = format("update %s set int1=? where bin1>?", tableName); + query = format("update %s set int2=? where int1>?", TABLE_NAME); try { statement = connection.prepareStatement(query); statement.setInt(1, 100); @@ -154,7 +179,7 @@ public void testUpdateQuery() throws SQLException { public void testSelectCountQuery() throws SQLException { PreparedStatement statement = null; ResultSet resultSet = null; - String query = format("select count(*) from %s", tableName); + String query = format("select count(*) from %s", TABLE_NAME); try { statement = connection.prepareStatement(query); resultSet = statement.executeQuery(); @@ -171,7 +196,7 @@ public void testSelectCountQuery() throws SQLException { public void testSelectEqualsQuery() throws SQLException { PreparedStatement statement = null; ResultSet resultSet = null; - String query = format("select %s from %s where int1 = ?", PRIMARY_KEY_COLUMN_NAME, tableName); + String query = format("select %s from %s where int2 = ?", PRIMARY_KEY_COLUMN_NAME, TABLE_NAME); try { statement = connection.prepareStatement(query); statement.setInt(1, 1); @@ -179,8 +204,8 @@ public void testSelectEqualsQuery() throws SQLException { resultSet = statement.executeQuery(); assertTrue(resultSet.next()); - assertEquals(resultSet.getString(PRIMARY_KEY_COLUMN_NAME), "key1"); - assertEquals(resultSet.getString(1), "key1"); + assertEquals(resultSet.getString(PRIMARY_KEY_COLUMN_NAME), testRecord.getPrimaryKey()); + assertEquals(resultSet.getString(1), testRecord.getPrimaryKey()); } finally { closeQuietly(statement); closeQuietly(resultSet); @@ -191,7 +216,7 @@ public void testSelectEqualsQuery() throws SQLException { public void testSelectNotEqualsQuery() throws SQLException { PreparedStatement statement = null; ResultSet resultSet = null; - String query = format("select %s, int1 from %s where int1 <> ?", PRIMARY_KEY_COLUMN_NAME, tableName); + String query = format("select %s, int2 from %s where int2 <> ?", PRIMARY_KEY_COLUMN_NAME, TABLE_NAME); try { statement = connection.prepareStatement(query); statement.setInt(1, 2); @@ -199,11 +224,11 @@ public void testSelectNotEqualsQuery() throws SQLException { resultSet = statement.executeQuery(); assertTrue(resultSet.next()); - assertEquals(resultSet.getString(PRIMARY_KEY_COLUMN_NAME), "key1"); - assertEquals(resultSet.getInt("int1"), 1); + assertEquals(resultSet.getString(PRIMARY_KEY_COLUMN_NAME), testRecord.getPrimaryKey()); + assertEquals(resultSet.getInt("int2"), testRecord.getInt2()); - assertEquals(resultSet.getString(1), "key1"); - assertEquals(resultSet.getInt(2), 1); + assertEquals(resultSet.getString(1), testRecord.getPrimaryKey()); + assertEquals(resultSet.getInt(2), testRecord.getInt2()); } finally { closeQuietly(statement); closeQuietly(resultSet); @@ -214,8 +239,8 @@ public void testSelectNotEqualsQuery() throws SQLException { public void testSelectOrQuery() throws SQLException { PreparedStatement statement = null; ResultSet resultSet = null; - String preparedQuery = format("select int1, str1 from %s where int1<>? or str1 like ? or bin1 is null", - tableName); + String preparedQuery = format("select int2, str1 from %s where int2<>? or str1 like ? or int1 is null", + TABLE_NAME); try { statement = connection.prepareStatement(preparedQuery); statement.setInt(1, 1); @@ -224,11 +249,11 @@ public void testSelectOrQuery() throws SQLException { resultSet = statement.executeQuery(); assertTrue(resultSet.next()); - assertEquals(resultSet.getInt("int1"), 1); - assertEquals(resultSet.getString("str1"), "bar"); + assertEquals(resultSet.getInt("int2"), testRecord.getInt2()); + assertEquals(resultSet.getString("str1"), testRecord.getStr1()); - assertEquals(resultSet.getInt(1), 1); - assertEquals(resultSet.getString(2), "bar"); + assertEquals(resultSet.getInt(1), testRecord.getInt2()); + assertEquals(resultSet.getString(2), testRecord.getStr1()); } finally { closeQuietly(statement); closeQuietly(resultSet); @@ -239,8 +264,8 @@ public void testSelectOrQuery() throws SQLException { public void testSelectAndQuery() throws SQLException { PreparedStatement statement = null; ResultSet resultSet = null; - String preparedQuery = format("select * from %s where int1<=? and bin1>=? and str1 is not null", - tableName); + String preparedQuery = format("select * from %s where int2<=? and int1>=? and str1 is not null", + TABLE_NAME); try { statement = connection.prepareStatement(preparedQuery); statement.setInt(1, 2); @@ -252,8 +277,7 @@ public void testSelectAndQuery() throws SQLException { resultSet = statement.getResultSet(); assertTrue(resultSet.next()); - assertAllByColumnLabel(resultSet); - assertAllByColumnIndex(resultSet); + testRecord.assertPreparedResultSet(resultSet); } finally { closeQuietly(statement); closeQuietly(resultSet); @@ -264,7 +288,7 @@ public void testSelectAndQuery() throws SQLException { public void testSelectInQuery() throws SQLException { PreparedStatement statement = null; ResultSet resultSet = null; - String query = format("select * from %s where int1 in (?, ?) and str1 is not null", tableName); + String query = format("select * from %s where int2 in (?, ?) and str1 is not null", TABLE_NAME); try { statement = connection.prepareStatement(query); statement.setInt(1, 1); @@ -273,8 +297,7 @@ public void testSelectInQuery() throws SQLException { resultSet = statement.executeQuery(); assertTrue(resultSet.next()); - assertAllByColumnLabel(resultSet); - assertAllByColumnIndex(resultSet); + testRecord.assertPreparedResultSet(resultSet); } finally { closeQuietly(statement); closeQuietly(resultSet); @@ -285,26 +308,20 @@ public void testSelectInQuery() throws SQLException { public void testSelectBetweenQuery() throws SQLException { PreparedStatement statement = null; ResultSet resultSet = null; - String query = format("select * from %s where int1 between ? and ? and val1=?", tableName); + String query = format("select * from %s where int2 between ? and ? and val1=?", TABLE_NAME); try { statement = connection.prepareStatement(query); statement.setInt(1, 1); statement.setInt(2, 3); - statement.setBytes(3, blobValue); + statement.setBytes(3, testRecord.getVal1()); resultSet = statement.executeQuery(); assertTrue(resultSet.next()); - assertAllByColumnLabel(resultSet); - assertAllByColumnIndex(resultSet); + testRecord.assertPreparedResultSet(resultSet); } finally { closeQuietly(statement); closeQuietly(resultSet); } } - - private void assertBlobValue(ResultSet resultSet) throws SQLException { - assertEquals(resultSet.getBytes("val1"), blobValue); - assertEquals(resultSet.getBytes(6), blobValue); - } } diff --git a/src/test/java/com/aerospike/jdbc/QuerySendKeyFalseTest.java b/src/test/java/com/aerospike/jdbc/QuerySendKeyFalseTest.java new file mode 100644 index 0000000..38029cb --- /dev/null +++ b/src/test/java/com/aerospike/jdbc/QuerySendKeyFalseTest.java @@ -0,0 +1,145 @@ +package com.aerospike.jdbc; + +import com.aerospike.jdbc.util.TestRecord; +import org.testng.annotations.AfterClass; +import org.testng.annotations.AfterMethod; +import org.testng.annotations.BeforeClass; +import org.testng.annotations.BeforeMethod; +import org.testng.annotations.Test; + +import java.sql.Connection; +import java.sql.DriverManager; +import java.sql.ResultSet; +import java.sql.SQLException; +import java.sql.Statement; +import java.util.Objects; +import java.util.concurrent.Executors; +import java.util.logging.Logger; + +import static com.aerospike.jdbc.util.Constants.PRIMARY_KEY_COLUMN_NAME; +import static com.aerospike.jdbc.util.TestConfig.HOSTNAME; +import static com.aerospike.jdbc.util.TestConfig.NAMESPACE; +import static com.aerospike.jdbc.util.TestConfig.PORT; +import static com.aerospike.jdbc.util.TestConfig.TABLE_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 QuerySendKeyFalseTest { + + private static final Logger logger = Logger.getLogger(QuerySendKeyFalseTest.class.getName()); + private static Connection connection; + + private final TestRecord testRecord; + + QuerySendKeyFalseTest() { + testRecord = new TestRecord("key1", true, 11100, 1, "bar"); + } + + @BeforeClass + public static void connectionInit() throws Exception { + logger.info("connectionInit"); + Class.forName("com.aerospike.jdbc.AerospikeDriver").newInstance(); + String url = String.format("jdbc:aerospike:%s:%d/%s?sendKey=false", HOSTNAME, PORT, NAMESPACE); + connection = DriverManager.getConnection(url); + connection.setNetworkTimeout(Executors.newSingleThreadExecutor(), 5000); + } + + @AfterClass + public static void connectionClose() throws SQLException { + logger.info("connectionClose"); + connection.close(); + } + + @BeforeMethod + public void setUp() throws SQLException { + Objects.requireNonNull(connection, "connection is null"); + Statement statement = null; + int count; + String query = testRecord.toInsertQuery(); + try { + statement = connection.createStatement(); + count = statement.executeUpdate(query); + } finally { + closeQuietly(statement); + } + assertEquals(count, 1); + } + + @AfterMethod + public void tearDown() throws SQLException { + Objects.requireNonNull(connection, "connection is null"); + Statement statement = null; + String query = format("DELETE FROM %s", TABLE_NAME); + try { + statement = connection.createStatement(); + boolean result = statement.execute(query); + assertFalse(result); + } finally { + closeQuietly(statement); + } + assertTrue(statement.getUpdateCount() > 0); + } + + @Test + public void testSelectByPrimaryKeyQuery() throws SQLException { + Statement statement = null; + ResultSet resultSet = null; + String query = format("SELECT * FROM %s WHERE %s='%s' AND int1=%d", + TABLE_NAME, PRIMARY_KEY_COLUMN_NAME, testRecord.getPrimaryKey(), testRecord.getInt1()); + int total = 0; + try { + statement = connection.createStatement(); + resultSet = statement.executeQuery(query); + while (resultSet.next()) { + testRecord.assertResultSet(resultSet); + + total++; + } + assertEquals(total, 1); + } finally { + closeQuietly(statement); + closeQuietly(resultSet); + } + } + + @Test + public void testRecordFoundPrimaryKeyNull() throws SQLException { + Statement statement = null; + ResultSet resultSet = null; + String query = format("SELECT * FROM %s WHERE int1=%d", TABLE_NAME, testRecord.getInt1()); + int total = 0; + try { + statement = connection.createStatement(); + resultSet = statement.executeQuery(query); + while (resultSet.next()) { + testRecord.assertResultSet(resultSet, false); + + total++; + } + assertEquals(total, 1); + } finally { + closeQuietly(statement); + closeQuietly(resultSet); + } + } + + @Test + public void testSelectByPrimaryKeyQueryFilteredOut() throws SQLException { + Statement statement = null; + ResultSet resultSet = null; + String query = format("SELECT * FROM %s WHERE %s='%s' AND int1=%d", + TABLE_NAME, PRIMARY_KEY_COLUMN_NAME, testRecord.getPrimaryKey(), 10); + try { + statement = connection.createStatement(); + resultSet = statement.executeQuery(query); + + assertFalse(resultSet.next()); + } finally { + closeQuietly(statement); + closeQuietly(resultSet); + } + } +} diff --git a/src/test/java/com/aerospike/jdbc/SimpleQueriesTest.java b/src/test/java/com/aerospike/jdbc/SimpleQueriesTest.java index 7d907db..f9875ac 100644 --- a/src/test/java/com/aerospike/jdbc/SimpleQueriesTest.java +++ b/src/test/java/com/aerospike/jdbc/SimpleQueriesTest.java @@ -1,33 +1,64 @@ package com.aerospike.jdbc; +import com.aerospike.jdbc.util.TestRecord; +import org.testng.annotations.AfterClass; import org.testng.annotations.AfterMethod; +import org.testng.annotations.BeforeClass; import org.testng.annotations.BeforeMethod; import org.testng.annotations.Test; +import java.sql.Connection; +import java.sql.DriverManager; import java.sql.ResultSet; import java.sql.SQLException; import java.sql.Statement; import java.util.Objects; +import java.util.concurrent.Executors; +import java.util.logging.Logger; import static com.aerospike.jdbc.util.Constants.PRIMARY_KEY_COLUMN_NAME; +import static com.aerospike.jdbc.util.TestConfig.HOSTNAME; +import static com.aerospike.jdbc.util.TestConfig.NAMESPACE; +import static com.aerospike.jdbc.util.TestConfig.PORT; +import static com.aerospike.jdbc.util.TestConfig.TABLE_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 { +public class SimpleQueriesTest { + + private static final Logger logger = Logger.getLogger(SimpleQueriesTest.class.getName()); + private static Connection connection; + + private final TestRecord testRecord; + + SimpleQueriesTest() { + testRecord = new TestRecord("key1", true, 11100, 1, "bar"); + } + + @BeforeClass + public static void connectionInit() throws Exception { + logger.info("connectionInit"); + Class.forName("com.aerospike.jdbc.AerospikeDriver").newInstance(); + String url = String.format("jdbc:aerospike:%s:%d/%s?sendKey=true", HOSTNAME, PORT, NAMESPACE); + connection = DriverManager.getConnection(url); + connection.setNetworkTimeout(Executors.newSingleThreadExecutor(), 5000); + } + + @AfterClass + public static void connectionClose() throws SQLException { + logger.info("connectionClose"); + connection.close(); + } @BeforeMethod public void setUp() throws SQLException { Objects.requireNonNull(connection, "connection is null"); Statement statement = null; int count; - String query = format( - "INSERT INTO %s (%s, bin1, int1, str1, bool1) VALUES (\"key1\", 11100, 1, \"bar\", true)", - tableName, - PRIMARY_KEY_COLUMN_NAME - ); + String query = testRecord.toInsertQuery(); try { statement = connection.createStatement(); count = statement.executeUpdate(query); @@ -41,7 +72,7 @@ public void setUp() throws SQLException { public void tearDown() throws SQLException { Objects.requireNonNull(connection, "connection is null"); Statement statement = null; - String query = format("DELETE FROM %s", tableName); + String query = format("DELETE FROM %s", TABLE_NAME); try { statement = connection.createStatement(); boolean result = statement.execute(query); @@ -56,14 +87,13 @@ public void tearDown() throws SQLException { public void testSelectQuery() throws SQLException { Statement statement = null; ResultSet resultSet = null; - String query = format("SELECT * FROM %s LIMIT 10", tableName); + String query = format("SELECT * FROM %s LIMIT 10", TABLE_NAME); int total = 0; try { statement = connection.createStatement(); resultSet = statement.executeQuery(query); while (resultSet.next()) { - assertAllByColumnLabel(resultSet); - assertAllByColumnIndex(resultSet); + testRecord.assertResultSet(resultSet); total++; } @@ -78,14 +108,13 @@ public void testSelectQuery() throws SQLException { public void testSelectByPrimaryKeyQuery() throws SQLException { Statement statement = null; ResultSet resultSet = null; - String query = format("SELECT *, bin1 FROM %s WHERE %s='key1'", tableName, PRIMARY_KEY_COLUMN_NAME); + String query = format("SELECT *, int1 FROM %s WHERE %s='key1'", TABLE_NAME, PRIMARY_KEY_COLUMN_NAME); int total = 0; try { statement = connection.createStatement(); resultSet = statement.executeQuery(query); while (resultSet.next()) { - assertAllByColumnLabel(resultSet); - assertAllByColumnIndex(resultSet); + testRecord.assertResultSet(resultSet); total++; } @@ -100,7 +129,7 @@ public void testSelectByPrimaryKeyQuery() throws SQLException { public void testInsertQuery() throws SQLException { Statement statement = null; int count; - String query = format("INSERT INTO %s (bin1, int1) VALUES (11101, 3), (11102, 4)", tableName); + String query = format("INSERT INTO %s (int1, int2) VALUES (11101, 3), (11102, 4)", TABLE_NAME); try { statement = connection.createStatement(); count = statement.executeUpdate(query); @@ -109,7 +138,7 @@ public void testInsertQuery() throws SQLException { } assertEquals(count, 2); - query = format("SELECT %s FROM %s WHERE int1 > 3", PRIMARY_KEY_COLUMN_NAME, tableName); + query = format("SELECT %s FROM %s WHERE int2 > 3", PRIMARY_KEY_COLUMN_NAME, TABLE_NAME); int total = 0; ResultSet resultSet = null; try { @@ -133,7 +162,7 @@ public void testInsertQuery() throws SQLException { public void testUpdateQuery() throws SQLException { Statement statement = null; int count; - String query = format("UPDATE %s SET int1=100 WHERE bin1>10000", tableName); + String query = format("UPDATE %s SET int2=100 WHERE int1>10000", TABLE_NAME); try { statement = connection.createStatement(); count = statement.executeUpdate(query); @@ -142,7 +171,7 @@ public void testUpdateQuery() throws SQLException { } assertEquals(count, 1); - query = format("UPDATE %s SET int1=100 WHERE bin1>20000", tableName); + query = format("UPDATE %s SET int2=100 WHERE int1>20000", TABLE_NAME); try { statement = connection.createStatement(); count = statement.executeUpdate(query); @@ -156,7 +185,7 @@ public void testUpdateQuery() throws SQLException { public void testSelectCountQuery() throws SQLException { Statement statement = null; ResultSet resultSet = null; - String query = format("SELECT count(*) FROM %s", tableName); + String query = format("SELECT count(*) FROM %s", TABLE_NAME); try { statement = connection.createStatement(); resultSet = statement.executeQuery(query); @@ -173,14 +202,14 @@ public void testSelectCountQuery() throws SQLException { public void testSelectEqualsQuery() throws SQLException { Statement statement = null; ResultSet resultSet = null; - String query = format("SELECT %s FROM %s WHERE int1 = 1", PRIMARY_KEY_COLUMN_NAME, tableName); + String query = format("SELECT %s FROM %s WHERE int2 = 1", PRIMARY_KEY_COLUMN_NAME, TABLE_NAME); try { statement = connection.createStatement(); resultSet = statement.executeQuery(query); assertTrue(resultSet.next()); - assertEquals(resultSet.getString(PRIMARY_KEY_COLUMN_NAME), "key1"); - assertEquals(resultSet.getString(1), "key1"); + assertEquals(resultSet.getString(PRIMARY_KEY_COLUMN_NAME), testRecord.getPrimaryKey()); + assertEquals(resultSet.getString(1), testRecord.getPrimaryKey()); } finally { closeQuietly(statement); closeQuietly(resultSet); @@ -191,17 +220,17 @@ public void testSelectEqualsQuery() throws SQLException { public void testSelectNotEqualsQuery() throws SQLException { Statement statement = null; ResultSet resultSet = null; - String query = format("SELECT %s, int1 FROM %s WHERE int1 <> 2", PRIMARY_KEY_COLUMN_NAME, tableName); + String query = format("SELECT %s, int2 FROM %s WHERE int2 <> 2", PRIMARY_KEY_COLUMN_NAME, TABLE_NAME); try { statement = connection.createStatement(); resultSet = statement.executeQuery(query); assertTrue(resultSet.next()); - assertEquals(resultSet.getString(PRIMARY_KEY_COLUMN_NAME), "key1"); - assertEquals(resultSet.getInt("int1"), 1); + assertEquals(resultSet.getString(PRIMARY_KEY_COLUMN_NAME), testRecord.getPrimaryKey()); + assertEquals(resultSet.getInt("int2"), testRecord.getInt2()); - assertEquals(resultSet.getString(1), "key1"); - assertEquals(resultSet.getInt(2), 1); + assertEquals(resultSet.getString(1), testRecord.getPrimaryKey()); + assertEquals(resultSet.getInt(2), testRecord.getInt2()); } finally { closeQuietly(statement); closeQuietly(resultSet); @@ -212,18 +241,18 @@ public void testSelectNotEqualsQuery() throws SQLException { public void testSelectOrQuery() throws SQLException { Statement statement = null; ResultSet resultSet = null; - String query = format("SELECT int1, str1 FROM %s WHERE int1<>1 OR str1 LIKE 'bar' OR bin1 IS NULL", - tableName); + String query = format("SELECT int2, str1 FROM %s WHERE int2<>1 OR str1 LIKE 'bar' OR int1 IS NULL", + TABLE_NAME); try { statement = connection.createStatement(); resultSet = statement.executeQuery(query); assertTrue(resultSet.next()); - assertEquals(resultSet.getInt("int1"), 1); - assertEquals(resultSet.getString("str1"), "bar"); + assertEquals(resultSet.getInt("int2"), testRecord.getInt2()); + assertEquals(resultSet.getString("str1"), testRecord.getStr1()); - assertEquals(resultSet.getInt(1), 1); - assertEquals(resultSet.getString(2), "bar"); + assertEquals(resultSet.getInt(1), testRecord.getInt2()); + assertEquals(resultSet.getString(2), testRecord.getStr1()); } finally { closeQuietly(statement); closeQuietly(resultSet); @@ -234,8 +263,8 @@ public void testSelectOrQuery() throws SQLException { public void testSelectAndQuery() throws SQLException { Statement statement = null; ResultSet resultSet = null; - String query = format("SELECT * FROM %s WHERE int1<=2 AND bin1>=1000 AND str1 IS NOT NULL", - tableName); + String query = format("SELECT * FROM %s WHERE int2<=2 AND int1>=1000 AND str1 IS NOT NULL", + TABLE_NAME); try { statement = connection.createStatement(); boolean result = statement.execute(query); @@ -244,8 +273,7 @@ public void testSelectAndQuery() throws SQLException { resultSet = statement.getResultSet(); assertTrue(resultSet.next()); - assertAllByColumnLabel(resultSet); - assertAllByColumnIndex(resultSet); + testRecord.assertResultSet(resultSet); } finally { closeQuietly(statement); closeQuietly(resultSet); @@ -256,14 +284,13 @@ public void testSelectAndQuery() throws SQLException { public void testSelectInQuery() throws SQLException { Statement statement = null; ResultSet resultSet = null; - String query = format("SELECT * FROM %s WHERE int1 IN (1, 2) AND str1 IS NOT NULL", tableName); + String query = format("SELECT * FROM %s WHERE int2 IN (1, 2) AND str1 IS NOT NULL", TABLE_NAME); try { statement = connection.createStatement(); resultSet = statement.executeQuery(query); assertTrue(resultSet.next()); - assertAllByColumnLabel(resultSet); - assertAllByColumnIndex(resultSet); + testRecord.assertResultSet(resultSet); } finally { closeQuietly(statement); closeQuietly(resultSet); @@ -274,14 +301,13 @@ public void testSelectInQuery() throws SQLException { public void testSelectBetweenQuery() throws SQLException { Statement statement = null; ResultSet resultSet = null; - String query = format("SELECT * FROM %s WHERE int1 BETWEEN 1 AND 3", tableName); + String query = format("SELECT * FROM %s WHERE int2 BETWEEN 1 AND 3", TABLE_NAME); try { statement = connection.createStatement(); resultSet = statement.executeQuery(query); assertTrue(resultSet.next()); - assertAllByColumnLabel(resultSet); - assertAllByColumnIndex(resultSet); + testRecord.assertResultSet(resultSet); } finally { closeQuietly(statement); closeQuietly(resultSet); diff --git a/src/test/java/com/aerospike/jdbc/util/TestConfig.java b/src/test/java/com/aerospike/jdbc/util/TestConfig.java new file mode 100644 index 0000000..ea3cd59 --- /dev/null +++ b/src/test/java/com/aerospike/jdbc/util/TestConfig.java @@ -0,0 +1,13 @@ +package com.aerospike.jdbc.util; + +public class TestConfig { + + public static final String NAMESPACE = "test"; + public static final String TABLE_NAME = "jdbc"; + + public static final String HOSTNAME = "localhost"; + public static final int PORT = 3000; + + private TestConfig() { + } +} diff --git a/src/test/java/com/aerospike/jdbc/util/TestRecord.java b/src/test/java/com/aerospike/jdbc/util/TestRecord.java new file mode 100644 index 0000000..b230bb8 --- /dev/null +++ b/src/test/java/com/aerospike/jdbc/util/TestRecord.java @@ -0,0 +1,128 @@ +package com.aerospike.jdbc.util; + +import java.sql.ResultSet; +import java.sql.SQLException; + +import static com.aerospike.jdbc.util.Constants.PRIMARY_KEY_COLUMN_NAME; +import static com.aerospike.jdbc.util.TestConfig.TABLE_NAME; +import static java.lang.String.format; +import static org.testng.Assert.assertEquals; + +public class TestRecord { + + private final String primaryKey; + private final boolean bool1; + private final int int1; + private final int int2; + private final String str1; + private final byte[] val1; + private final Object val2; + + public TestRecord( + String primaryKey, + boolean bool1, + int int1, + int int2, + String str1 + ) { + this.primaryKey = primaryKey; + this.bool1 = bool1; + this.int1 = int1; + this.int2 = int2; + this.str1 = str1; + this.val1 = null; + this.val2 = null; + } + + public TestRecord( + String primaryKey, + boolean bool1, + int int1, + int int2, + String str1, + byte[] val1, + Object val2 + ) { + this.primaryKey = primaryKey; + this.bool1 = bool1; + this.int1 = int1; + this.int2 = int2; + this.str1 = str1; + this.val1 = val1; + this.val2 = val2; + } + + public String getPrimaryKey() { + return primaryKey; + } + + public boolean getBool1() { + return bool1; + } + + public byte[] getVal1() { + return val1; + } + + public int getInt1() { + return int1; + } + + public int getInt2() { + return int2; + } + + public String getStr1() { + return str1; + } + + public String toInsertQuery() { + return format( + "INSERT INTO %s (%s, bool1, int1, int2, str1) VALUES (%s, %b, %d, %d, %s)", + TABLE_NAME, + PRIMARY_KEY_COLUMN_NAME, + primaryKey, + bool1, + int1, + int2, + str1 + ); + } + + public String toPreparedInsertQuery() { + return format( + "INSERT INTO %s (%s, bool1, int1, int2, str1, val1, val2) VALUES (%s, %b, %d, %d, %s, ?, ?)", + TABLE_NAME, + PRIMARY_KEY_COLUMN_NAME, + primaryKey, + bool1, + int1, + int2, + str1 + ); + } + + public void assertResultSet(ResultSet resultSet) throws SQLException { + assertResultSet(resultSet, true); + } + + public void assertResultSet(ResultSet resultSet, boolean sendKey) throws SQLException { + assertEquals(resultSet.getString(PRIMARY_KEY_COLUMN_NAME), sendKey ? primaryKey : null); + assertEquals(resultSet.getBoolean("bool1"), bool1); + assertEquals(resultSet.getInt("int1"), int1); + assertEquals(resultSet.getInt("int2"), int2); + assertEquals(resultSet.getString("str1"), str1); + + assertEquals(resultSet.getString(1), sendKey ? primaryKey : null); + assertEquals(resultSet.getBoolean(2), bool1); + assertEquals(resultSet.getInt(3), int1); + assertEquals(resultSet.getInt(4), int2); + assertEquals(resultSet.getString(5), str1); + } + + public void assertPreparedResultSet(ResultSet resultSet) throws SQLException { + assertResultSet(resultSet); + assertEquals(resultSet.getBytes("val1"), val1); + assertEquals(resultSet.getBytes(6), val1); + } +} diff --git a/src/test/java/com/aerospike/jdbc/util/TestUtil.java b/src/test/java/com/aerospike/jdbc/util/TestUtil.java index f4f4846..bf09245 100644 --- a/src/test/java/com/aerospike/jdbc/util/TestUtil.java +++ b/src/test/java/com/aerospike/jdbc/util/TestUtil.java @@ -12,16 +12,16 @@ private TestUtil() { } public static void printResultSet(ResultSet rs) throws SQLException { - ResultSetMetaData rsmd = rs.getMetaData(); - for (int i = 1; i <= rsmd.getColumnCount(); i++) { + ResultSetMetaData resultSetMetaData = rs.getMetaData(); + for (int i = 1; i <= resultSetMetaData.getColumnCount(); i++) { if (i != 1) { System.out.print(", "); } - System.out.print(rsmd.getColumnName(i)); + System.out.print(resultSetMetaData.getColumnName(i)); } System.out.println(); while (rs.next()) { - for (int i = 1; i <= rsmd.getColumnCount(); i++) { + for (int i = 1; i <= resultSetMetaData.getColumnCount(); i++) { if (i != 1) { System.out.print(", "); } @@ -41,11 +41,19 @@ public static void closeQuietly(AutoCloseable resource) { } public static boolean executeQuery(Connection conn, String sql) throws SQLException { - Statement stmt = conn.createStatement(); - ResultSet rs = stmt.executeQuery(sql); - boolean hasNext = rs.next(); - rs.close(); - stmt.close(); + Statement statement = conn.createStatement(); + ResultSet resultSet = statement.executeQuery(sql); + boolean hasNext = resultSet.next(); + resultSet.close(); + statement.close(); return hasNext; } + + @SuppressWarnings("all") + public static void sleep(long millis) { + try { + Thread.sleep(millis); + } catch (InterruptedException e) { + } + } }