Search in sources :

Example 1 with ColumnMetaData

use of com.alibaba.druid.util.jdbc.ResultSetMetaDataBase.ColumnMetaData 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).getValue();
            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 ColumnMetaData

use of com.alibaba.druid.util.jdbc.ResultSetMetaDataBase.ColumnMetaData 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 3 with ColumnMetaData

use of com.alibaba.druid.util.jdbc.ResultSetMetaDataBase.ColumnMetaData 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)

Aggregations

MockResultSetMetaData (com.alibaba.druid.mock.MockResultSetMetaData)3 ColumnMetaData (com.alibaba.druid.util.jdbc.ResultSetMetaDataBase.ColumnMetaData)3 MockResultSet (com.alibaba.druid.mock.MockResultSet)2 MockPreparedStatement (com.alibaba.druid.mock.MockPreparedStatement)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 SQLMethodInvokeExpr (com.alibaba.druid.sql.ast.expr.SQLMethodInvokeExpr)1 SQLNCharExpr (com.alibaba.druid.sql.ast.expr.SQLNCharExpr)1 SQLNullExpr (com.alibaba.druid.sql.ast.expr.SQLNullExpr)1 SQLNumberExpr (com.alibaba.druid.sql.ast.expr.SQLNumberExpr)1 SQLNumericLiteralExpr (com.alibaba.druid.sql.ast.expr.SQLNumericLiteralExpr)1 SQLVariantRefExpr (com.alibaba.druid.sql.ast.expr.SQLVariantRefExpr)1 SQLSelectItem (com.alibaba.druid.sql.ast.statement.SQLSelectItem)1 BigDecimal (java.math.BigDecimal)1 SQLException (java.sql.SQLException)1 Timestamp (java.sql.Timestamp)1