Search in sources :

Example 1 with MockPreparedStatement

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

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

the class Bug_for_happyday517 method test_for_happyday517_3.

public void test_for_happyday517_3() throws Exception {
    Connection conn = dataSource.getConnection();
    String sql = "select 1";
    Statement stmt = conn.prepareStatement(sql, ResultSet.TYPE_SCROLL_SENSITIVE, ResultSet.CONCUR_UPDATABLE, ResultSet.CLOSE_CURSORS_AT_COMMIT);
    MockPreparedStatement mockStmt = stmt.unwrap(MockPreparedStatement.class);
    Assert.assertEquals(ResultSet.TYPE_SCROLL_SENSITIVE, mockStmt.getResultSetType());
    Assert.assertEquals(ResultSet.CONCUR_UPDATABLE, mockStmt.getResultSetConcurrency());
    Assert.assertEquals(ResultSet.CLOSE_CURSORS_AT_COMMIT, mockStmt.getResultSetHoldability());
    stmt.close();
    conn.close();
}
Also used : MockPreparedStatement(com.alibaba.druid.mock.MockPreparedStatement) MockStatement(com.alibaba.druid.mock.MockStatement) Statement(java.sql.Statement) Connection(java.sql.Connection) MockPreparedStatement(com.alibaba.druid.mock.MockPreparedStatement)

Example 3 with MockPreparedStatement

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

the class Bug_for_happyday517_2 method setUp.

protected void setUp() throws Exception {
    originalDataSourceCount = DruidDataSourceStatManager.getInstance().getDataSourceList().size();
    final MockPreparedStatement mockStatement = new MockPreparedStatement(null, null) {

        public boolean execute() throws SQLException {
            throw exception;
        }
    };
    driver = new MockDriver() {

        public Connection connect(String url, Properties info) throws SQLException {
            super.connect(url, info);
            return new MockConnection(driver, url, info) {

                public PreparedStatement prepareStatement(String sql) throws SQLException {
                    return mockStatement;
                }
            };
        }
    };
    dataSource = new DruidDataSource();
    dataSource.setDriver(driver);
    dataSource.setUrl("jdbc:mock:xxx");
    dataSource.setFilters("stat,trace,log4j,encoding");
}
Also used : MockDriver(com.alibaba.druid.mock.MockDriver) SQLException(java.sql.SQLException) Connection(java.sql.Connection) MockConnection(com.alibaba.druid.mock.MockConnection) MockPreparedStatement(com.alibaba.druid.mock.MockPreparedStatement) PreparedStatement(java.sql.PreparedStatement) MockPreparedStatement(com.alibaba.druid.mock.MockPreparedStatement) Properties(java.util.Properties) MockConnection(com.alibaba.druid.mock.MockConnection) DruidDataSource(com.alibaba.druid.pool.DruidDataSource)

Example 4 with MockPreparedStatement

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

the class EncodingConvertFilterTest method test_stat.

public void test_stat() throws Exception {
    Assert.assertTrue(dataSource.isInited());
    EncodingConvertFilter filter = (EncodingConvertFilter) dataSource.getProxyFilters().get(0);
    DruidPooledConnection conn = dataSource.getConnection();
    final String PARAM_VALUE = "中国";
    PreparedStatement stmt = conn.prepareStatement("select ?");
    stmt.setString(1, PARAM_VALUE);
    MockPreparedStatement raw = stmt.unwrap(MockPreparedStatement.class);
    String param1 = (String) raw.getParameters().get(0);
    Assert.assertEquals(PARAM_VALUE, new String(param1.getBytes(SERVER_ENCODING), CLIENT_ENCODING));
    Assert.assertFalse(param1.equals(PARAM_VALUE));
    ResultSet rs = stmt.executeQuery();
    MyResultSet rawRs = rs.unwrap(MyResultSet.class);
    rawRs.setValue(filter.encode((ConnectionProxy) conn.getConnection(), text));
    rs.next();
    Assert.assertEquals(text, rs.getString(1));
    rs.close();
    stmt.close();
    conn.close();
}
Also used : DruidPooledConnection(com.alibaba.druid.pool.DruidPooledConnection) MockResultSet(com.alibaba.druid.mock.MockResultSet) ResultSet(java.sql.ResultSet) EncodingConvertFilter(com.alibaba.druid.filter.encoding.EncodingConvertFilter) MockPreparedStatement(com.alibaba.druid.mock.MockPreparedStatement) PreparedStatement(java.sql.PreparedStatement) MockPreparedStatement(com.alibaba.druid.mock.MockPreparedStatement) ConnectionProxy(com.alibaba.druid.proxy.jdbc.ConnectionProxy)

Example 5 with MockPreparedStatement

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

the class Bug_for_xuershan method setUp.

protected void setUp() throws Exception {
    driver = new MockDriver() {

        public ResultSet createResultSet(MockPreparedStatement stmt) {
            return null;
        }
    };
    dataSource = new DruidDataSource();
    dataSource.setUrl("jdbc:mock:xx");
    dataSource.setDriver(driver);
}
Also used : MockDriver(com.alibaba.druid.mock.MockDriver) ResultSet(java.sql.ResultSet) MockPreparedStatement(com.alibaba.druid.mock.MockPreparedStatement) DruidDataSource(com.alibaba.druid.pool.DruidDataSource)

Aggregations

MockPreparedStatement (com.alibaba.druid.mock.MockPreparedStatement)24 PreparedStatement (java.sql.PreparedStatement)14 DruidDataSource (com.alibaba.druid.pool.DruidDataSource)10 DruidPooledConnection (com.alibaba.druid.pool.DruidPooledConnection)9 Connection (java.sql.Connection)8 MockConnection (com.alibaba.druid.mock.MockConnection)6 MockDriver (com.alibaba.druid.mock.MockDriver)5 ResultSet (java.sql.ResultSet)5 SQLException (java.sql.SQLException)5 MockResultSet (com.alibaba.druid.mock.MockResultSet)4 MockStatement (com.alibaba.druid.mock.MockStatement)4 Statement (java.sql.Statement)4 MockCallableStatement (com.alibaba.druid.mock.MockCallableStatement)2 MockStatementBase (com.alibaba.druid.mock.MockStatementBase)2 DruidConnectionHolder (com.alibaba.druid.pool.DruidConnectionHolder)2 PreparedStatementKey (com.alibaba.druid.pool.DruidPooledPreparedStatement.PreparedStatementKey)2 PreparedStatementHolder (com.alibaba.druid.pool.PreparedStatementHolder)2 DruidNativeJdbcExtractor (com.alibaba.druid.support.spring.DruidNativeJdbcExtractor)2 EncodingConvertFilter (com.alibaba.druid.filter.encoding.EncodingConvertFilter)1 MockResultSetMetaData (com.alibaba.druid.mock.MockResultSetMetaData)1