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;
}
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();
}
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");
}
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();
}
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);
}
Aggregations