use of io.shardingjdbc.core.common.env.DatabaseEnvironment in project sharding-jdbc by shardingjdbc.
the class AbstractSQLTest method buildDataSource.
private static BasicDataSource buildDataSource(final String dbName, final DatabaseType type) {
DatabaseEnvironment dbEnv = new DatabaseEnvironment(type);
BasicDataSource result = new BasicDataSource();
result.setDriverClassName(dbEnv.getDriverClassName());
result.setUrl(dbEnv.getURL(dbName));
result.setUsername(dbEnv.getUsername());
result.setPassword(dbEnv.getPassword());
result.setMaxTotal(1);
if (DatabaseType.Oracle == dbEnv.getDatabaseType()) {
result.setConnectionInitSqls(Collections.singleton("ALTER SESSION SET CURRENT_SCHEMA = " + dbName));
}
return result;
}
use of io.shardingjdbc.core.common.env.DatabaseEnvironment in project sharding-jdbc by shardingjdbc.
the class SQLAssertHelper method executeQueryWithPreparedStatement.
/**
* Execute query with prepared statement.
*
* @param isExecute is execute method
* @param abstractDataSourceAdapter data source adapter
* @param parameters parameters
* @param file file
*
* @throws MalformedURLException malformed URL exception
* @throws SQLException SQL exception
* @throws DatabaseUnitException database unit exception
*/
public void executeQueryWithPreparedStatement(final boolean isExecute, final AbstractDataSourceAdapter abstractDataSourceAdapter, final List<String> parameters, final File file) throws MalformedURLException, SQLException, DatabaseUnitException {
try (Connection conn = abstractDataSourceAdapter.getConnection();
PreparedStatement preparedStatement = conn.prepareStatement(SQLPlaceholderUtil.replacePreparedStatement(sql))) {
setParameters(preparedStatement, parameters);
ReplacementDataSet expectedDataSet = new ReplacementDataSet(new FlatXmlDataSetBuilder().build(file));
expectedDataSet.addReplacementObject("[null]", null);
for (ITable each : expectedDataSet.getTables()) {
String tableName = each.getTableMetaData().getTableName();
IDatabaseConnection connection = DBUnitUtil.getConnection(new DatabaseEnvironment(DatabaseType.valueFrom(conn.getMetaData().getDatabaseProductName())), conn);
ITable actualTable;
if (isExecute) {
actualTable = createTable(tableName, preparedStatement, connection);
} else {
actualTable = connection.createTable(tableName, preparedStatement);
}
Assertion.assertEquals(expectedDataSet.getTable(tableName), actualTable);
}
}
}
use of io.shardingjdbc.core.common.env.DatabaseEnvironment in project sharding-jdbc by shardingjdbc.
the class SQLAssertHelper method assertResult.
public void assertResult(final Connection connection, final File file) throws MalformedURLException, SQLException, DatabaseUnitException {
if (sql.contains("TEMP")) {
return;
}
ITableIterator expectedTableIterator = new FlatXmlDataSetBuilder().build(file).iterator();
try (Connection conn = connection) {
while (expectedTableIterator.next()) {
ITable expectedTable = expectedTableIterator.getTable();
String actualTableName = expectedTable.getTableMetaData().getTableName();
String verifySql = "SELECT * FROM " + actualTableName + " WHERE status = '" + getStatus(file) + "'" + getOrderByCondition(actualTableName);
ITable actualTable = DBUnitUtil.getConnection(new DatabaseEnvironment(DatabaseType.valueFrom(conn.getMetaData().getDatabaseProductName())), conn).createQueryTable(actualTableName, verifySql);
Assertion.assertEquals(expectedTable, actualTable);
}
}
}
use of io.shardingjdbc.core.common.env.DatabaseEnvironment in project sharding-jdbc by shardingjdbc.
the class SQLAssertHelper method executeQueryWithStatement.
/**
* Execute query with statement.
*
* @param abstractDataSourceAdapter data source adapter
* @param parameters parameters
* @param file file
*
* @throws MalformedURLException malformed URL exception
* @throws SQLException SQL exception
* @throws DatabaseUnitException database unit exception
*/
public void executeQueryWithStatement(final AbstractDataSourceAdapter abstractDataSourceAdapter, final List<String> parameters, final File file) throws MalformedURLException, SQLException, DatabaseUnitException {
try (Connection conn = abstractDataSourceAdapter.getConnection()) {
String querySql = SQLPlaceholderUtil.replaceStatement(sql, parameters.toArray());
ReplacementDataSet expectedDataSet = new ReplacementDataSet(new FlatXmlDataSetBuilder().build(file));
expectedDataSet.addReplacementObject("[null]", null);
for (ITable each : expectedDataSet.getTables()) {
String tableName = each.getTableMetaData().getTableName();
IDatabaseConnection connection = DBUnitUtil.getConnection(new DatabaseEnvironment(DatabaseType.valueFrom(conn.getMetaData().getDatabaseProductName())), conn);
ITable actualTable = connection.createQueryTable(tableName, querySql);
// TODO customized CachedResultSetTable with statement
Assertion.assertEquals(expectedDataSet.getTable(tableName), actualTable);
}
}
}
use of io.shardingjdbc.core.common.env.DatabaseEnvironment in project sharding-jdbc by shardingjdbc.
the class AbstractHintTest method assertDataSetEquals.
private void assertDataSetEquals(final String expectedDataSetFile, final Connection connection, final String sql, final DatabaseType type, final Object[] params) throws SQLException, DatabaseUnitException {
try (Connection conn = connection;
PreparedStatement ps = conn.prepareStatement(sql)) {
int i = 1;
for (Object param : params) {
ps.setObject(i++, param);
}
ITable actualTable = DBUnitUtil.getConnection(new DatabaseEnvironment(type), connection).createTable("t_order", ps);
IDataSet expectedDataSet = new FlatXmlDataSetBuilder().build(new InputStreamReader(AbstractHintTest.class.getClassLoader().getResourceAsStream(expectedDataSetFile)));
Assertion.assertEquals(expectedDataSet.getTable("t_order"), actualTable);
}
}
Aggregations