Search in sources :

Example 1 with MockResultSetMetaData

use of com.alibaba.druid.mock.MockResultSetMetaData 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)

Example 2 with MockResultSetMetaData

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

the class JdbcUtilsTest method test_print.

public void test_print() throws Exception {
    final AtomicInteger nextCount = new AtomicInteger(2);
    final MockResultSetMetaData rsMeta = new MockResultSetMetaData() {

        private int[] types = new int[] { Types.DATE, Types.BIT, Types.BOOLEAN, Types.TINYINT, Types.SMALLINT, Types.INTEGER, Types.CLOB, Types.LONGVARCHAR, Types.OTHER, Types.VARCHAR, Types.CHAR, Types.NVARCHAR, Types.NCHAR };

        @Override
        public int getColumnCount() throws SQLException {
            return types.length;
        }

        @Override
        public int getColumnType(int column) throws SQLException {
            return types[column - 1];
        }

        @Override
        public String getColumnName(int column) throws SQLException {
            return "C" + column;
        }
    };
    MockResultSet rs = new MockResultSet(null) {

        @Override
        public boolean next() throws SQLException {
            return nextCount.getAndDecrement() > 0;
        }

        @Override
        public ResultSetMetaData getMetaData() throws SQLException {
            return rsMeta;
        }

        @Override
        public java.sql.Date getDate(int columnIndex) throws SQLException {
            return new java.sql.Date(System.currentTimeMillis());
        }

        @Override
        public boolean wasNull() throws SQLException {
            if (nextCount.get() == 1) {
                return true;
            }
            return false;
        }

        public Object getObject(int columnIndex) throws SQLException {
            return null;
        }
    };
    JdbcUtils.printResultSet(rs);
}
Also used : AtomicInteger(java.util.concurrent.atomic.AtomicInteger) MockResultSetMetaData(com.alibaba.druid.mock.MockResultSetMetaData) MockResultSet(com.alibaba.druid.mock.MockResultSet)

Example 3 with MockResultSetMetaData

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

the class MySqlMockExecuteHandlerImpl method showStatus.

public ResultSet showStatus(MockStatementBase statement) throws SQLException {
    MockResultSet rs = new MockResultSet(statement);
    MockResultSetMetaData metaData = rs.getMockMetaData();
    Object[] row = new Object[] { "on" };
    ColumnMetaData column = new ColumnMetaData();
    column.setColumnType(Types.NVARCHAR);
    metaData.getColumns().add(column);
    rs.getRows().add(row);
    return rs;
}
Also used : MockResultSetMetaData(com.alibaba.druid.mock.MockResultSetMetaData) MockResultSet(com.alibaba.druid.mock.MockResultSet) ColumnMetaData(com.alibaba.druid.util.jdbc.ResultSetMetaDataBase.ColumnMetaData)

Example 4 with MockResultSetMetaData

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

the class MockResultSetMetaDataTest method test_resultSet_metadata.

public void test_resultSet_metadata() throws Exception {
    MockResultSetMetaData meta = new MockResultSetMetaData();
    Assert.assertTrue(meta.isWrapperFor(MockResultSetMetaData.class));
    Assert.assertFalse(meta.isWrapperFor(BigDecimal.class));
    Assert.assertTrue(meta.unwrap(MockResultSetMetaData.class) instanceof MockResultSetMetaData);
    Assert.assertTrue(meta.unwrap(ResultSetMetaDataBase.class) instanceof MockResultSetMetaData);
    Assert.assertTrue(meta.unwrap(ResultSetMetaData.class) instanceof MockResultSetMetaData);
    Assert.assertTrue(meta.unwrap(null) == null);
    Assert.assertTrue(meta.unwrap(java.sql.ResultSetMetaData.class) != null);
    Assert.assertTrue(meta.unwrap(Object.class) != null);
    Assert.assertTrue(meta.unwrap(Date.class) == null);
    ColumnMetaData column = new ColumnMetaData();
    meta.getColumns().add(column);
    meta.isAutoIncrement(1);
    meta.isCaseSensitive(1);
    meta.isSearchable(1);
    meta.isCurrency(1);
    meta.isNullable(1);
    meta.isSigned(1);
    meta.getColumnDisplaySize(1);
    meta.getColumnLabel(1);
    meta.getSchemaName(1);
    meta.getPrecision(1);
    meta.getScale(1);
    meta.getTableName(1);
    meta.getCatalogName(1);
    meta.getColumnTypeName(1);
    meta.isReadOnly(1);
    meta.isWritable(1);
    meta.isDefinitelyWritable(1);
    meta.getColumnClassName(1);
}
Also used : MockResultSetMetaData(com.alibaba.druid.mock.MockResultSetMetaData) ColumnMetaData(com.alibaba.druid.util.jdbc.ResultSetMetaDataBase.ColumnMetaData) BigDecimal(java.math.BigDecimal)

Example 5 with MockResultSetMetaData

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

the class LogFilterTest method test_logFilter_2.

public void test_logFilter_2() throws Exception {
    DataSourceProxyConfig config = new DataSourceProxyConfig();
    config.setRawUrl("jdbc:mock:");
    DataSourceProxyImpl dataSource = new DataSourceProxyImpl(new MockDriver(), config);
    Log4jFilter log4jFilter = new Log4jFilter();
    {
        log4jFilter.init(dataSource);
        setLogDisableAll(log4jFilter, true);
        config.getFilters().add(log4jFilter);
    }
    CommonsLogFilter logFilter = new CommonsLogFilter();
    {
        logFilter.init(dataSource);
        setLogDisableAll(logFilter, true);
        config.getFilters().add(logFilter);
    }
    final MockResultSetMetaData rsMeta = new MockResultSetMetaData() {

        private int[] types = new int[] { Types.BLOB, Types.CLOB, Types.NCLOB, Types.BINARY, Types.OTHER };

        @Override
        public int getColumnCount() throws SQLException {
            return types.length;
        }

        @Override
        public int getColumnType(int column) throws SQLException {
            return types[column - 1];
        }
    };
    ConnectionProxy conn = (ConnectionProxy) dataSource.connect(new Properties());
    {
        StatementProxy stmt = (StatementProxy) conn.createStatement();
        MockResultSet rs = new MockResultSet(null) {

            @Override
            public ResultSetMetaData getMetaData() throws SQLException {
                return rsMeta;
            }

            @Override
            public boolean next() throws SQLException {
                return true;
            }

            @Override
            public Object getObject(int columnIndex) throws SQLException {
                if (columnIndex == 5) {
                    throw new SQLException();
                }
                return null;
            }
        };
        FilterChainImpl chain = new FilterChainImpl(dataSource);
        chain.resultSet_next(new ResultSetProxyImpl(stmt, rs, 1001, null));
    }
    {
        final MockResultSet rs = new MockResultSet(null) {

            @Override
            public ResultSetMetaData getMetaData() throws SQLException {
                throw new SQLException();
            }
        };
        StatementProxy stmt = new StatementProxyImpl(conn, new MockStatement(conn) {

            public ResultSet getResultSet() throws SQLException {
                return rs;
            }
        }, 0);
        FilterChainImpl chain = new FilterChainImpl(dataSource);
        chain.statement_getResultSet(stmt);
    }
    {
        StatementProxy stmt = (StatementProxy) conn.createStatement();
        MockResultSet rs = new MockResultSet(null) {

            @Override
            public ResultSetMetaData getMetaData() throws SQLException {
                return rsMeta;
            }

            @Override
            public boolean next() throws SQLException {
                return true;
            }

            @Override
            public Object getObject(int columnIndex) throws SQLException {
                if (columnIndex == 5) {
                    throw new SQLException();
                }
                return null;
            }
        };
        {
            logFilter.setResultSetLogEnabled(false);
            FilterChainImpl chain = new FilterChainImpl(dataSource);
            chain.resultSet_next(new ResultSetProxyImpl(stmt, rs, 1001, null));
        }
        {
            logFilter.setResultSetNextAfterLogEnabled(false);
            FilterChainImpl chain = new FilterChainImpl(dataSource);
            chain.resultSet_next(new ResultSetProxyImpl(stmt, rs, 1001, null));
        }
    }
}
Also used : MockDriver(com.alibaba.druid.mock.MockDriver) CommonsLogFilter(com.alibaba.druid.filter.logging.CommonsLogFilter) FilterChainImpl(com.alibaba.druid.filter.FilterChainImpl) DataSourceProxyConfig(com.alibaba.druid.proxy.jdbc.DataSourceProxyConfig) DataSourceProxyImpl(com.alibaba.druid.proxy.jdbc.DataSourceProxyImpl) MockResultSetMetaData(com.alibaba.druid.mock.MockResultSetMetaData) SQLException(java.sql.SQLException) MockResultSet(com.alibaba.druid.mock.MockResultSet) Properties(java.util.Properties) ResultSetProxyImpl(com.alibaba.druid.proxy.jdbc.ResultSetProxyImpl) ConnectionProxy(com.alibaba.druid.proxy.jdbc.ConnectionProxy) StatementProxyImpl(com.alibaba.druid.proxy.jdbc.StatementProxyImpl) MockResultSetMetaData(com.alibaba.druid.mock.MockResultSetMetaData) ResultSetMetaData(java.sql.ResultSetMetaData) Log4jFilter(com.alibaba.druid.filter.logging.Log4jFilter) StatementProxy(com.alibaba.druid.proxy.jdbc.StatementProxy) MockStatement(com.alibaba.druid.mock.MockStatement)

Aggregations

MockResultSetMetaData (com.alibaba.druid.mock.MockResultSetMetaData)5 MockResultSet (com.alibaba.druid.mock.MockResultSet)4 ColumnMetaData (com.alibaba.druid.util.jdbc.ResultSetMetaDataBase.ColumnMetaData)3 SQLException (java.sql.SQLException)2 FilterChainImpl (com.alibaba.druid.filter.FilterChainImpl)1 CommonsLogFilter (com.alibaba.druid.filter.logging.CommonsLogFilter)1 Log4jFilter (com.alibaba.druid.filter.logging.Log4jFilter)1 MockDriver (com.alibaba.druid.mock.MockDriver)1 MockPreparedStatement (com.alibaba.druid.mock.MockPreparedStatement)1 MockStatement (com.alibaba.druid.mock.MockStatement)1 ConnectionProxy (com.alibaba.druid.proxy.jdbc.ConnectionProxy)1 DataSourceProxyConfig (com.alibaba.druid.proxy.jdbc.DataSourceProxyConfig)1 DataSourceProxyImpl (com.alibaba.druid.proxy.jdbc.DataSourceProxyImpl)1 ResultSetProxyImpl (com.alibaba.druid.proxy.jdbc.ResultSetProxyImpl)1 StatementProxy (com.alibaba.druid.proxy.jdbc.StatementProxy)1 StatementProxyImpl (com.alibaba.druid.proxy.jdbc.StatementProxyImpl)1 SQLExpr (com.alibaba.druid.sql.ast.SQLExpr)1 SQLBooleanExpr (com.alibaba.druid.sql.ast.expr.SQLBooleanExpr)1 SQLCharExpr (com.alibaba.druid.sql.ast.expr.SQLCharExpr)1 SQLIntegerExpr (com.alibaba.druid.sql.ast.expr.SQLIntegerExpr)1