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