Search in sources :

Example 1 with DatabaseEnvironment

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;
}
Also used : DatabaseEnvironment(io.shardingjdbc.core.common.env.DatabaseEnvironment) BasicDataSource(org.apache.commons.dbcp2.BasicDataSource)

Example 2 with DatabaseEnvironment

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);
        }
    }
}
Also used : DatabaseEnvironment(io.shardingjdbc.core.common.env.DatabaseEnvironment) ReplacementDataSet(org.dbunit.dataset.ReplacementDataSet) Connection(java.sql.Connection) IDatabaseConnection(org.dbunit.database.IDatabaseConnection) PreparedStatement(java.sql.PreparedStatement) ITable(org.dbunit.dataset.ITable) IDatabaseConnection(org.dbunit.database.IDatabaseConnection) FlatXmlDataSetBuilder(org.dbunit.dataset.xml.FlatXmlDataSetBuilder)

Example 3 with DatabaseEnvironment

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);
        }
    }
}
Also used : DatabaseEnvironment(io.shardingjdbc.core.common.env.DatabaseEnvironment) Connection(java.sql.Connection) IDatabaseConnection(org.dbunit.database.IDatabaseConnection) ITableIterator(org.dbunit.dataset.ITableIterator) ITable(org.dbunit.dataset.ITable) FlatXmlDataSetBuilder(org.dbunit.dataset.xml.FlatXmlDataSetBuilder)

Example 4 with DatabaseEnvironment

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);
        }
    }
}
Also used : DatabaseEnvironment(io.shardingjdbc.core.common.env.DatabaseEnvironment) ReplacementDataSet(org.dbunit.dataset.ReplacementDataSet) Connection(java.sql.Connection) IDatabaseConnection(org.dbunit.database.IDatabaseConnection) ITable(org.dbunit.dataset.ITable) IDatabaseConnection(org.dbunit.database.IDatabaseConnection) FlatXmlDataSetBuilder(org.dbunit.dataset.xml.FlatXmlDataSetBuilder)

Example 5 with DatabaseEnvironment

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);
    }
}
Also used : DatabaseEnvironment(io.shardingjdbc.core.common.env.DatabaseEnvironment) InputStreamReader(java.io.InputStreamReader) Connection(java.sql.Connection) PreparedStatement(java.sql.PreparedStatement) ITable(org.dbunit.dataset.ITable) IDataSet(org.dbunit.dataset.IDataSet) FlatXmlDataSetBuilder(org.dbunit.dataset.xml.FlatXmlDataSetBuilder)

Aggregations

DatabaseEnvironment (io.shardingjdbc.core.common.env.DatabaseEnvironment)7 FlatXmlDataSetBuilder (org.dbunit.dataset.xml.FlatXmlDataSetBuilder)6 Connection (java.sql.Connection)4 ITable (org.dbunit.dataset.ITable)4 InputStreamReader (java.io.InputStreamReader)3 IDatabaseConnection (org.dbunit.database.IDatabaseConnection)3 IDataSet (org.dbunit.dataset.IDataSet)3 ShardingJdbcDatabaseTester (io.shardingjdbc.core.common.env.ShardingJdbcDatabaseTester)2 DatabaseType (io.shardingjdbc.core.constant.DatabaseType)2 InputStream (java.io.InputStream)2 PreparedStatement (java.sql.PreparedStatement)2 IDatabaseTester (org.dbunit.IDatabaseTester)2 ReplacementDataSet (org.dbunit.dataset.ReplacementDataSet)2 BasicDataSource (org.apache.commons.dbcp2.BasicDataSource)1 ITableIterator (org.dbunit.dataset.ITableIterator)1