Search in sources :

Example 26 with ResultSet

use of java.sql.ResultSet in project sharding-jdbc by dangdangdotcom.

the class IteratorResultSetTest method assertNextWithLimitForPartData.

@Test
public void assertNextWithLimitForPartData() throws SQLException {
    MergeContext mergeContext = new MergeContext();
    mergeContext.setLimit(new Limit(1, 1, -1, -1));
    ResultSet resultSet = ResultSetFactory.getResultSet(Arrays.<ResultSet>asList(new MockResultSet<>(1), new MockResultSet<>(2, 4), new MockResultSet<Integer>()), mergeContext);
    int count = 0;
    while (resultSet.next()) {
        count++;
    }
    assertThat(count, is(1));
}
Also used : ResultSet(java.sql.ResultSet) MockResultSet(com.dangdang.ddframe.rdb.sharding.merger.fixture.MockResultSet) MergeContext(com.dangdang.ddframe.rdb.sharding.parser.result.merger.MergeContext) Limit(com.dangdang.ddframe.rdb.sharding.parser.result.merger.Limit) MockResultSet(com.dangdang.ddframe.rdb.sharding.merger.fixture.MockResultSet) Test(org.junit.Test)

Example 27 with ResultSet

use of java.sql.ResultSet in project sharding-jdbc by dangdangdotcom.

the class MemoryOrderByResultSetTest method assertSort.

@Test
public void assertSort() throws SQLException {
    AbstractMemoryOrderByResultSet rs = new AbstractMemoryOrderByResultSet(Arrays.<ResultSet>asList(new MockResultSet<>(1, 3, 5, 6, 6), new MockResultSet<>(8, 6, 4, 2)), Collections.singletonList(new OrderByColumn(1, OrderByColumn.OrderByType.ASC))) {
    };
    List<Integer> actualList = new ArrayList<>();
    while (rs.next()) {
        actualList.add(rs.getInt(1));
    }
    assertThat(actualList, is(Arrays.asList(1, 2, 3, 4, 5, 6, 6, 6, 8)));
    rs.close();
    assertThat(rs.isClosed(), is(true));
    rs = new AbstractMemoryOrderByResultSet(Arrays.<ResultSet>asList(new MockResultSet<>(1, 3, 5, 6, 6), new MockResultSet<>(8, 6, 4, 2)), Collections.singletonList(new OrderByColumn(1, OrderByColumn.OrderByType.DESC))) {
    };
    actualList.clear();
    while (rs.next()) {
        actualList.add(rs.getInt("nAmE"));
    }
    assertThat(actualList, is(Arrays.asList(8, 6, 6, 6, 5, 4, 3, 2, 1)));
}
Also used : ArrayList(java.util.ArrayList) MockResultSet(com.dangdang.ddframe.rdb.sharding.merger.fixture.MockResultSet) ResultSet(java.sql.ResultSet) MockResultSet(com.dangdang.ddframe.rdb.sharding.merger.fixture.MockResultSet) OrderByColumn(com.dangdang.ddframe.rdb.sharding.parser.result.merger.OrderByColumn) Test(org.junit.Test)

Example 28 with ResultSet

use of java.sql.ResultSet in project sharding-jdbc by dangdangdotcom.

the class ShardingPreparedStatementTableOnlyTest method assertExecuteQueryWithParameter.

@Test
public void assertExecuteQueryWithParameter() throws SQLException {
    String sql = "SELECT COUNT(*) AS `orders_count` FROM `t_order` WHERE `status` = ?";
    try (Connection connection = shardingDataSource.getConnection();
        PreparedStatement preparedStatement = connection.prepareStatement(sql)) {
        preparedStatement.setString(1, "init");
        ResultSet resultSet = preparedStatement.executeQuery();
        assertTrue(resultSet.next());
        assertThat(resultSet.getLong(1), is(20L));
        ShardingPreparedStatement sps = (ShardingPreparedStatement) preparedStatement;
        assertThat(sps.getRoutedStatements().size(), is(10));
    }
}
Also used : Connection(java.sql.Connection) ResultSet(java.sql.ResultSet) PreparedStatement(java.sql.PreparedStatement) Test(org.junit.Test) AbstractShardingTablesOnlyDBUnitTest(com.dangdang.ddframe.rdb.integrate.tbl.AbstractShardingTablesOnlyDBUnitTest)

Example 29 with ResultSet

use of java.sql.ResultSet in project sharding-jdbc by dangdangdotcom.

the class ShardingPreparedStatementTest method assertExecuteQueryWithoutParameter.

@Test
public void assertExecuteQueryWithoutParameter() throws SQLException {
    String sql = "SELECT COUNT(*) AS `orders_count` FROM `t_order` WHERE `status` = 'init'";
    try (Connection connection = shardingDataSource.getConnection();
        PreparedStatement preparedStatement = connection.prepareStatement(sql)) {
        ResultSet resultSet = preparedStatement.executeQuery();
        assertTrue(resultSet.next());
        assertThat(resultSet.getLong(1), is(40L));
        resultSet = preparedStatement.executeQuery();
        assertTrue(resultSet.next());
        assertThat(resultSet.getLong(1), is(40L));
        resultSet = preparedStatement.executeQuery();
        assertTrue(resultSet.next());
        assertThat(resultSet.getLong(1), is(40L));
    }
}
Also used : Connection(java.sql.Connection) ResultSet(java.sql.ResultSet) PreparedStatement(java.sql.PreparedStatement) Test(org.junit.Test) AbstractShardingDataBasesOnlyDBUnitTest(com.dangdang.ddframe.rdb.integrate.db.AbstractShardingDataBasesOnlyDBUnitTest)

Example 30 with ResultSet

use of java.sql.ResultSet in project sharding-jdbc by dangdangdotcom.

the class ShardingPreparedStatementTest method assertExecuteQueryWithColumnNames.

@Test
public void assertExecuteQueryWithColumnNames() throws SQLException {
    String sql = "SELECT COUNT(*) AS `orders_count` FROM `t_order` WHERE `status` = ?";
    try (Connection connection = shardingDataSource.getConnection();
        PreparedStatement preparedStatement = connection.prepareStatement(sql, new String[] { "orders_count" })) {
        preparedStatement.setNull(1, java.sql.Types.VARCHAR);
        ResultSet resultSet = preparedStatement.executeQuery();
        assertTrue(resultSet.next());
        assertThat(resultSet.getLong(1), is(0L));
    }
}
Also used : Connection(java.sql.Connection) ResultSet(java.sql.ResultSet) PreparedStatement(java.sql.PreparedStatement) Test(org.junit.Test) AbstractShardingDataBasesOnlyDBUnitTest(com.dangdang.ddframe.rdb.integrate.db.AbstractShardingDataBasesOnlyDBUnitTest)

Aggregations

ResultSet (java.sql.ResultSet)16614 PreparedStatement (java.sql.PreparedStatement)9996 SQLException (java.sql.SQLException)7083 Connection (java.sql.Connection)6929 Statement (java.sql.Statement)4787 Test (org.junit.Test)3656 ArrayList (java.util.ArrayList)2584 Properties (java.util.Properties)1232 HashMap (java.util.HashMap)681 ResultSetMetaData (java.sql.ResultSetMetaData)660 CallableStatement (java.sql.CallableStatement)580 DatabaseMetaData (java.sql.DatabaseMetaData)512 List (java.util.List)471 IOException (java.io.IOException)465 PhoenixConnection (org.apache.phoenix.jdbc.PhoenixConnection)414 Map (java.util.Map)409 Timestamp (java.sql.Timestamp)384 BigDecimal (java.math.BigDecimal)358 CloudRuntimeException (com.cloud.utils.exception.CloudRuntimeException)292 HashSet (java.util.HashSet)282