Search in sources :

Example 1 with MockResultSet

use of com.alibaba.druid.mock.MockResultSet in project druid by alibaba.

the class CallableStatmentTest method test_connect.

public void test_connect() throws Exception {
    MockCallableStatement rawStmt = null;
    MockResultSet rawRs = null;
    {
        Connection conn = dataSource.getConnection();
        CallableStatement stmt = conn.prepareCall("select 1");
        stmt.execute();
        rawStmt = stmt.unwrap(MockCallableStatement.class);
        ResultSet rs = (ResultSet) stmt.getObject(0);
        rawRs = rs.unwrap(MockResultSet.class);
        rs.next();
        rs.close();
        stmt.close();
        Assert.assertFalse(rawStmt.isClosed());
        Assert.assertTrue(rawRs.isClosed());
        rawRs = rs.unwrap(MockResultSet.class);
        Assert.assertNotNull(rawRs);
        conn.close();
    }
    {
        Connection conn = dataSource.getConnection();
        CallableStatement stmt = conn.prepareCall("select 1");
        stmt.execute();
        Assert.assertSame(rawStmt, stmt.unwrap(MockCallableStatement.class));
        Assert.assertFalse(rawStmt.isClosed());
        stmt.getObject(0);
        ResultSet rs = (ResultSet) stmt.getObject(0);
        rs.next();
        rs.close();
        stmt.close();
        conn.close();
    }
}
Also used : MockCallableStatement(com.alibaba.druid.mock.MockCallableStatement) MockCallableStatement(com.alibaba.druid.mock.MockCallableStatement) CallableStatement(java.sql.CallableStatement) Connection(java.sql.Connection) MockConnection(com.alibaba.druid.mock.MockConnection) ResultSet(java.sql.ResultSet) MockResultSet(com.alibaba.druid.mock.MockResultSet) MockResultSet(com.alibaba.druid.mock.MockResultSet)

Example 2 with MockResultSet

use of com.alibaba.druid.mock.MockResultSet in project druid by alibaba.

the class ResultSetTest method setUp.

protected void setUp() throws Exception {
    stmt = new DruidPooledStatement(null, null) {

        protected SQLException checkException(Throwable error) throws SQLException {
            if (error instanceof SQLException) {
                return (SQLException) error;
            }
            return new SQLException(error);
        }
    };
    raw = new MockResultSet(null);
    raw.getRows().add(new Object[] { null });
    resultSet = new DruidPooledResultSet(stmt, raw);
}
Also used : DruidPooledStatement(com.alibaba.druid.pool.DruidPooledStatement) SQLException(java.sql.SQLException) MockResultSet(com.alibaba.druid.mock.MockResultSet) DruidPooledResultSet(com.alibaba.druid.pool.DruidPooledResultSet)

Example 3 with MockResultSet

use of com.alibaba.druid.mock.MockResultSet in project druid by alibaba.

the class DruidLobHandlerTest method test_0.

public void test_0() throws Exception {
    DruidLobHandler handler = new DruidLobHandler();
    List<Object[]> rows = new ArrayList<Object[]>();
    rows.add(new Object[] { null, new MockBlob(), new MockClob() });
    MockResultSet rs = new MockResultSet(null, rows);
    rs.next();
    handler.getBlobAsBinaryStream(rs, 1);
    handler.getBlobAsBinaryStream(rs, "1");
    handler.getBlobAsBytes(rs, 1);
    handler.getBlobAsBytes(rs, "1");
    handler.getBlobAsBinaryStream(rs, 2);
    handler.getBlobAsBinaryStream(rs, "2");
    handler.getBlobAsBytes(rs, 2);
    handler.getBlobAsBytes(rs, "2");
    handler.getClobAsAsciiStream(rs, 1);
    handler.getClobAsAsciiStream(rs, "1");
    handler.getClobAsCharacterStream(rs, 1);
    handler.getClobAsCharacterStream(rs, "1");
    handler.getClobAsString(rs, 1);
    handler.getClobAsString(rs, "1");
    handler.getClobAsAsciiStream(rs, 3);
    handler.getClobAsAsciiStream(rs, "3");
    handler.getClobAsCharacterStream(rs, 3);
    handler.getClobAsCharacterStream(rs, "3");
    handler.getClobAsString(rs, 3);
    handler.getClobAsString(rs, "3");
    handler.getLobCreator();
}
Also used : MockBlob(com.alibaba.druid.mock.MockBlob) MockClob(com.alibaba.druid.mock.MockClob) ArrayList(java.util.ArrayList) MockResultSet(com.alibaba.druid.mock.MockResultSet) DruidLobHandler(com.alibaba.druid.support.spring.DruidLobHandler)

Example 4 with MockResultSet

use of com.alibaba.druid.mock.MockResultSet in project druid by alibaba.

the class FilterChainTest_Clob method setUp.

protected void setUp() throws Exception {
    dataSource = new DruidDataSource();
    dataSource.setUrl("jdbc:mock:xxx");
    ConnectionProxyImpl conn = new ConnectionProxyImpl(dataSource, null, new Properties(), 0);
    statement = new StatementProxyImpl(conn, null, 1);
    mockResultSet = new MockResultSet(null) {

        public Object getObject(int columnIndex) throws SQLException {
            invokeCount++;
            return new MockClob();
        }
    };
}
Also used : SQLException(java.sql.SQLException) MockClob(com.alibaba.druid.mock.MockClob) ConnectionProxyImpl(com.alibaba.druid.proxy.jdbc.ConnectionProxyImpl) MockResultSet(com.alibaba.druid.mock.MockResultSet) Properties(java.util.Properties) DruidDataSource(com.alibaba.druid.pool.DruidDataSource) StatementProxyImpl(com.alibaba.druid.proxy.jdbc.StatementProxyImpl)

Example 5 with MockResultSet

use of com.alibaba.druid.mock.MockResultSet in project druid by alibaba.

the class MySqlMockExecuteHandlerImpl method executeQueryFromDual.

public ResultSet executeQueryFromDual(MockStatementBase statement, SQLSelectQueryBlock query) throws SQLException {
    MockResultSet rs = statement.getConnection().getDriver().createMockResultSet(statement);
    MockResultSetMetaData metaData = rs.getMockMetaData();
    Object[] row = new Object[query.getSelectList().size()];
    for (int i = 0, size = query.getSelectList().size(); i < size; ++i) {
        ColumnMetaData column = new ColumnMetaData();
        SQLSelectItem item = query.getSelectList().get(i);
        SQLExpr expr = item.getExpr();
        if (expr instanceof SQLIntegerExpr) {
            row[i] = ((SQLNumericLiteralExpr) expr).getNumber();
            column.setColumnType(Types.INTEGER);
        } else if (expr instanceof SQLNumberExpr) {
            row[i] = ((SQLNumericLiteralExpr) expr).getNumber();
            column.setColumnType(Types.DECIMAL);
        } else if (expr instanceof SQLCharExpr) {
            row[i] = ((SQLCharExpr) expr).getText();
            column.setColumnType(Types.VARCHAR);
        } else if (expr instanceof SQLNCharExpr) {
            row[i] = ((SQLNCharExpr) expr).getText();
            column.setColumnType(Types.NVARCHAR);
        } else if (expr instanceof SQLBooleanExpr) {
            row[i] = ((SQLBooleanExpr) expr).getBooleanValue();
            column.setColumnType(Types.NVARCHAR);
        } else if (expr instanceof SQLNullExpr) {
            row[i] = null;
        } else if (expr instanceof SQLMethodInvokeExpr) {
            SQLMethodInvokeExpr methodInvokeExpr = (SQLMethodInvokeExpr) expr;
            if ("NOW".equalsIgnoreCase(methodInvokeExpr.getMethodName())) {
                row[i] = new Timestamp(System.currentTimeMillis());
            } else {
                throw new SQLException("TODO");
            }
        } else if (expr instanceof SQLVariantRefExpr) {
            SQLVariantRefExpr varExpr = (SQLVariantRefExpr) expr;
            int varIndex = varExpr.getIndex();
            if (statement instanceof MockPreparedStatement) {
                MockPreparedStatement mockPstmt = (MockPreparedStatement) statement;
                row[i] = mockPstmt.getParameters().get(varIndex);
            } else {
                row[i] = null;
            }
        } else {
            row[i] = null;
        }
        metaData.getColumns().add(column);
    }
    rs.getRows().add(row);
    return rs;
}
Also used : SQLCharExpr(com.alibaba.druid.sql.ast.expr.SQLCharExpr) MockResultSetMetaData(com.alibaba.druid.mock.MockResultSetMetaData) SQLMethodInvokeExpr(com.alibaba.druid.sql.ast.expr.SQLMethodInvokeExpr) SQLException(java.sql.SQLException) SQLNCharExpr(com.alibaba.druid.sql.ast.expr.SQLNCharExpr) MockResultSet(com.alibaba.druid.mock.MockResultSet) MockPreparedStatement(com.alibaba.druid.mock.MockPreparedStatement) SQLNumberExpr(com.alibaba.druid.sql.ast.expr.SQLNumberExpr) SQLNullExpr(com.alibaba.druid.sql.ast.expr.SQLNullExpr) Timestamp(java.sql.Timestamp) SQLExpr(com.alibaba.druid.sql.ast.SQLExpr) SQLNumericLiteralExpr(com.alibaba.druid.sql.ast.expr.SQLNumericLiteralExpr) SQLBooleanExpr(com.alibaba.druid.sql.ast.expr.SQLBooleanExpr) SQLSelectItem(com.alibaba.druid.sql.ast.statement.SQLSelectItem) SQLIntegerExpr(com.alibaba.druid.sql.ast.expr.SQLIntegerExpr) SQLVariantRefExpr(com.alibaba.druid.sql.ast.expr.SQLVariantRefExpr) ColumnMetaData(com.alibaba.druid.util.jdbc.ResultSetMetaDataBase.ColumnMetaData)

Aggregations

MockResultSet (com.alibaba.druid.mock.MockResultSet)22 SQLException (java.sql.SQLException)11 ResultSet (java.sql.ResultSet)7 ResultSetProxyImpl (com.alibaba.druid.proxy.jdbc.ResultSetProxyImpl)6 FilterChainImpl (com.alibaba.druid.filter.FilterChainImpl)5 DruidDataSource (com.alibaba.druid.pool.DruidDataSource)5 ResultSetProxy (com.alibaba.druid.proxy.jdbc.ResultSetProxy)5 MockResultSetMetaData (com.alibaba.druid.mock.MockResultSetMetaData)4 DruidPooledResultSet (com.alibaba.druid.pool.DruidPooledResultSet)4 StatementProxyImpl (com.alibaba.druid.proxy.jdbc.StatementProxyImpl)4 Properties (java.util.Properties)4 DruidPooledStatement (com.alibaba.druid.pool.DruidPooledStatement)3 ConnectionProxyImpl (com.alibaba.druid.proxy.jdbc.ConnectionProxyImpl)3 Connection (java.sql.Connection)3 MockCallableStatement (com.alibaba.druid.mock.MockCallableStatement)2 MockClob (com.alibaba.druid.mock.MockClob)2 MockConnection (com.alibaba.druid.mock.MockConnection)2 MockStatement (com.alibaba.druid.mock.MockStatement)2 StatementProxy (com.alibaba.druid.proxy.jdbc.StatementProxy)2 ColumnMetaData (com.alibaba.druid.util.jdbc.ResultSetMetaDataBase.ColumnMetaData)2