Search in sources :

Example 1 with ClientTestModel

use of com.ctrip.platform.dal.dao.unitbase.ClientTestModel in project dal by ctripcorp.

the class DalDirectClientTestStub method executeTestWithOneCommand.

/**
 * Test execute command function
 * @throws SQLException
 */
@Test
public void executeTestWithOneCommand() throws SQLException {
    final DalHints hints = new DalHints();
    DalCommand command = new DalCommand() {

        @Override
        public boolean execute(DalClient client) throws SQLException {
            String sql = "DELETE FROM " + TABLE_NAME + " WHERE id = 1";
            StatementParameters parameters = new StatementParameters();
            return client.update(sql, parameters, hints) == 1;
        }
    };
    client.execute(command, hints);
    List<ClientTestModel> models = this.queryModelsByIds(1);
    Assert.assertEquals(0, models.size());
}
Also used : DalCommand(com.ctrip.platform.dal.dao.DalCommand) DalHints(com.ctrip.platform.dal.dao.DalHints) DalClient(com.ctrip.platform.dal.dao.DalClient) ClientTestModel(com.ctrip.platform.dal.dao.unitbase.ClientTestModel) StatementParameters(com.ctrip.platform.dal.dao.StatementParameters) Test(org.junit.Test)

Example 2 with ClientTestModel

use of com.ctrip.platform.dal.dao.unitbase.ClientTestModel in project dal by ctripcorp.

the class DalDirectClientTestStub method updateTestWithDelete.

/**
 * Test the update function with delete SQL statement
 *
 * @throws SQLException
 */
@Test
public void updateTestWithDelete() throws SQLException {
    String deleteSql = String.format("DELETE FROM %s WHERE id=?", TABLE_NAME);
    StatementParameters parameters = new StatementParameters();
    parameters.set(1, Types.INTEGER, 1);
    DalHints hints = new DalHints();
    int count = client.update(deleteSql, parameters, hints);
    assertEquals(1, count, 2);
    List<ClientTestModel> po_models = this.queryModelsByIds(1);
    Assert.assertTrue(null != po_models);
    Assert.assertEquals(0, po_models.size());
}
Also used : DalHints(com.ctrip.platform.dal.dao.DalHints) ClientTestModel(com.ctrip.platform.dal.dao.unitbase.ClientTestModel) StatementParameters(com.ctrip.platform.dal.dao.StatementParameters) Test(org.junit.Test)

Example 3 with ClientTestModel

use of com.ctrip.platform.dal.dao.unitbase.ClientTestModel in project dal by ctripcorp.

the class DalDirectClientTestStub method batchUpdateTestWithoutRollback.

// The SQL server does not support auto commit for batch update
@Test
public void batchUpdateTestWithoutRollback() throws SQLException {
    if (diff.category == DatabaseCategory.SqlServer)
        return;
    String[] sqls = new String[] { "DELETE FROM " + TABLE_NAME + " WHERE ID = 1", "DELETE FROM " + TABLE_NAME + " WHERE _ID = 2", "DELETE FROM " + TABLE_NAME + " WHERE ID = 3" };
    DalHints hints = new DalHints();
    hints.set(DalHintEnum.forceAutoCommit);
    try {
        client.batchUpdate(sqls, hints);
        Assert.fail();
    } catch (Exception e) {
    }
    List<ClientTestModel> models = this.queryModelsByIds();
    Assert.assertEquals(3 - 1, models.size());
}
Also used : DalHints(com.ctrip.platform.dal.dao.DalHints) ClientTestModel(com.ctrip.platform.dal.dao.unitbase.ClientTestModel) SQLException(java.sql.SQLException) Test(org.junit.Test)

Example 4 with ClientTestModel

use of com.ctrip.platform.dal.dao.unitbase.ClientTestModel in project dal by ctripcorp.

the class DalDirectClientTestStub method executeTestWithMultipleCommandsNotAllSuccessed.

/**
 * Test execute multiple commands and not all successes.
 * If one failed, the after commands will be dropped.
 * @throws SQLException
 */
@Test
public void executeTestWithMultipleCommandsNotAllSuccessed() throws SQLException {
    final DalHints hints = new DalHints();
    List<DalCommand> commands = new ArrayList<DalCommand>();
    commands.add(new DalCommand() {

        @Override
        public boolean execute(DalClient client) throws SQLException {
            String sql = "DELETE FROM " + TABLE_NAME + " WHERE id = 1";
            StatementParameters parameters = new StatementParameters();
            client.update(sql, parameters, hints);
            return false;
        }
    });
    commands.add(new DalCommand() {

        @Override
        public boolean execute(DalClient client) throws SQLException {
            String sql = "DELETE FROM " + TABLE_NAME + " WHERE id = 100";
            StatementParameters parameters = new StatementParameters();
            return client.update(sql, parameters, hints) == 1;
        }
    });
    commands.add(new DalCommand() {

        @Override
        public boolean execute(DalClient client) throws SQLException {
            String sql = "DELETE FROM " + TABLE_NAME + " WHERE id = 3";
            StatementParameters parameters = new StatementParameters();
            return client.update(sql, parameters, hints) == 1;
        }
    });
    client.execute(commands, hints);
    List<ClientTestModel> models = this.queryModelsByIds();
    Assert.assertEquals(2, models.size());
}
Also used : DalCommand(com.ctrip.platform.dal.dao.DalCommand) DalHints(com.ctrip.platform.dal.dao.DalHints) DalClient(com.ctrip.platform.dal.dao.DalClient) ClientTestModel(com.ctrip.platform.dal.dao.unitbase.ClientTestModel) SQLException(java.sql.SQLException) StatementParameters(com.ctrip.platform.dal.dao.StatementParameters) ArrayList(java.util.ArrayList) Test(org.junit.Test)

Example 5 with ClientTestModel

use of com.ctrip.platform.dal.dao.unitbase.ClientTestModel in project dal by ctripcorp.

the class DalDirectClientTestStub method queryTestWithFetchSizeLimit.

/**
 * Test the basic query function with maxRows limit
 *
 * @throws SQLException
 */
@Test
public void queryTestWithFetchSizeLimit() throws SQLException {
    String querySql = "SELECT * FROM " + TABLE_NAME;
    StatementParameters parameters = new StatementParameters();
    ClientTestDalRowMapper mapper = new ClientTestDalRowMapper();
    DalHints hints = new DalHints();
    // Set fetch size limit
    hints.set(DalHintEnum.maxRows, 1);
    List<ClientTestModel> res = client.query(querySql, parameters, hints, new DalRowMapperExtractor<ClientTestModel>(mapper));
    Assert.assertTrue(null != res);
    Assert.assertEquals(1, res.size());
}
Also used : DalHints(com.ctrip.platform.dal.dao.DalHints) ClientTestModel(com.ctrip.platform.dal.dao.unitbase.ClientTestModel) StatementParameters(com.ctrip.platform.dal.dao.StatementParameters) ClientTestDalRowMapper(com.ctrip.platform.dal.dao.unitbase.ClientTestDalRowMapper) Test(org.junit.Test)

Aggregations

ClientTestModel (com.ctrip.platform.dal.dao.unitbase.ClientTestModel)57 Test (org.junit.Test)55 DalHints (com.ctrip.platform.dal.dao.DalHints)29 StatementParameters (com.ctrip.platform.dal.dao.StatementParameters)27 ArrayList (java.util.ArrayList)13 SQLException (java.sql.SQLException)9 SelectSqlBuilder (com.ctrip.platform.dal.dao.sqlbuilder.SelectSqlBuilder)5 DalClient (com.ctrip.platform.dal.dao.DalClient)3 DalCommand (com.ctrip.platform.dal.dao.DalCommand)3 ClientTestDalRowMapper (com.ctrip.platform.dal.dao.unitbase.ClientTestDalRowMapper)3 DalRowMapperExtractor (com.ctrip.platform.dal.dao.helper.DalRowMapperExtractor)2 DalCustomRowMapper (com.ctrip.platform.dal.dao.helper.DalCustomRowMapper)1 FreeSelectSqlBuilder (com.ctrip.platform.dal.dao.sqlbuilder.FreeSelectSqlBuilder)1 ResultSet (java.sql.ResultSet)1 Timestamp (java.sql.Timestamp)1 List (java.util.List)1 Map (java.util.Map)1