Search in sources :

Example 11 with ClientTestModel

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

the class DalDirectClientTestStub method callTestForSpWithIntermediateParameters.

/**
 * Test the call function with retrieveAllResultsFromSp parameters
 * @throws SQLException
 */
@Test
public void callTestForSpWithIntermediateParameters() throws SQLException {
    String callSql = "{call " + SP_WITH_INTERMEDIATE_RESULT + "(?,?,?,?)}";
    StatementParameters parameters = new StatementParameters();
    parameters.set("v_id", Types.INTEGER, 1);
    parameters.set("v_quantity", Types.INTEGER, 10);
    parameters.set("v_type", Types.SMALLINT, 3);
    parameters.registerInOut("v_address", Types.VARCHAR, "SZ INFO");
    DalHints hints = new DalHints().retrieveAllResultsFromSp();
    Map<String, ?> res = client.call(callSql, parameters, hints);
    System.out.println(res);
    Assert.assertTrue(null != res);
    if (diff.supportSpIntermediateResult)
        // mysql will return update count as well, while sqlserver will not return update count
        Assert.assertTrue(res.size() >= 5);
    else
        Assert.assertEquals(1, res.size());
    Assert.assertTrue(res.containsKey("v_address"));
    Assert.assertEquals("output", res.get("v_address"));
    Assert.assertEquals("output", parameters.get("v_address", ParameterDirection.InputOutput).getValue());
    List<ClientTestModel> models = this.queryModelsByIds(1);
    Assert.assertEquals(1, models.size());
    Assert.assertEquals("aaa", models.get(0).getAddress());
}
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 12 with ClientTestModel

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

the class DalDirectClientTestStub method executeTestWithMultipleCommandsAllSuccessed.

/**
 * Test execute multiple commands and all successes.
 * @throws SQLException
 */
@Test
public void executeTestWithMultipleCommandsAllSuccessed() 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 true;
        }
    });
    commands.add(new DalCommand() {

        @Override
        public boolean execute(DalClient client) throws SQLException {
            String sql = "DELETE FROM " + TABLE_NAME + " WHERE id = 2";
            StatementParameters parameters = new StatementParameters();
            client.update(sql, parameters, hints);
            return true;
        }
    });
    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();
            client.update(sql, parameters, hints);
            return true;
        }
    });
    client.execute(commands, hints);
    List<ClientTestModel> models = this.queryModelsByIds();
    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) SQLException(java.sql.SQLException) StatementParameters(com.ctrip.platform.dal.dao.StatementParameters) ArrayList(java.util.ArrayList) Test(org.junit.Test)

Example 13 with ClientTestModel

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

the class DalDirectClientTestStub method batchUpdateTestWithParametersNotAllSuccessed.

/**
 * Test the batch update function with parameters not all success.
 *
 * @throws SQLException
 */
@Test
public void batchUpdateTestWithParametersNotAllSuccessed() throws SQLException {
    String sql = "DELETE FROM " + TABLE_NAME + " WHERE id = ?";
    StatementParameters[] parameterList = new StatementParameters[2];
    parameterList[0] = new StatementParameters();
    parameterList[0].set(1, Types.INTEGER, 1);
    parameterList[1] = new StatementParameters();
    parameterList[1].set(1, Types.INTEGER, 100);
    DalHints hints = new DalHints();
    int[] counts = client.batchUpdate(sql, parameterList, hints);
    Assert.assertEquals(2, counts.length);
    assertEqualsBatch(new int[] { 1, 0 }, counts, 3 - 1);
    List<ClientTestModel> models = this.queryModelsByIds();
    Assert.assertEquals(2, 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 14 with ClientTestModel

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

the class DalTableDaoTestStub method testCombinedInsert.

/**
 * Test Insert multiple entities with one SQL Statement
 *
 * @throws SQLException
 */
@Test
public void testCombinedInsert() throws SQLException {
    if (!diff.supportInsertValues)
        return;
    List<ClientTestModel> entities = new ArrayList<ClientTestModel>();
    for (int i = 0; i < 3; i++) {
        ClientTestModel model = new ClientTestModel();
        model.setQuantity(10 + 1 % 3);
        model.setType(((Number) (1 % 3)).shortValue());
        model.setAddress("CTRIP");
        entities.add(model);
    }
    KeyHolder holder = diff.supportGetGeneratedKeys ? new KeyHolder() : null;
    DalHints hints = new DalHints();
    int res = dao.combinedInsert(hints, holder, entities);
    assertEquals(3, res, 4 + 3);
    if (diff.supportGetGeneratedKeys) {
        Assert.assertEquals(3, holder.size());
        Assert.assertTrue(holder.getKeyList().get(0).containsKey("GENERATED_KEY"));
    }
}
Also used : ClientTestModel(com.ctrip.platform.dal.dao.unitbase.ClientTestModel) ArrayList(java.util.ArrayList) Test(org.junit.Test)

Example 15 with ClientTestModel

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

the class DalTableDaoTestStub method testUpdateMultiple.

/**
 * Test update multiple entities with primary key
 *
 * @throws SQLException
 */
@Test
public void testUpdateMultiple() throws SQLException {
    DalHints hints = new DalHints();
    List<ClientTestModel> models = new ArrayList<>();
    for (int i = 0; i < 3; i++) {
        ClientTestModel model = new ClientTestModel();
        model.setId(i + 1);
        model.setAddress("CTRIP");
        models.add(model);
    }
    int[] res = dao.update(hints, models);
    assertEquals(new int[] { 1, 1, 1 }, res, 3, "address='CTRIP'");
    ClientTestModel model = dao.queryByPk(1, hints);
    Assert.assertTrue(null != model);
    Assert.assertEquals("CTRIP", model.getAddress());
}
Also used : ClientTestModel(com.ctrip.platform.dal.dao.unitbase.ClientTestModel) ArrayList(java.util.ArrayList) 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