Search in sources :

Example 21 with DalTableDao

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

the class BatchUpdateTaskTestStub method testUpdatableWithIntVersion.

@Test
public void testUpdatableWithIntVersion() throws SQLException {
    BatchUpdateTask<UpdatableIntVersionModel> test = new BatchUpdateTask<>();
    DalParser<UpdatableIntVersionModel> parser = new DalDefaultJpaParser<>(UpdatableIntVersionModel.class, getDbName());
    test.initialize(parser);
    DalTableDao<UpdatableIntVersionModel> dao = new DalTableDao<>(parser);
    DalHints hints = new DalHints();
    Integer[] oldValue = new Integer[3];
    List<UpdatableIntVersionModel> pojos = dao.query("1=1", new StatementParameters(), new DalHints());
    int i = 0;
    for (UpdatableIntVersionModel model : pojos) {
        model.setAddress("1122334455");
        oldValue[i++] = model.getTableIndex();
    }
    int[] result = execute(test, hints, pojos);
    assertArrayEquals(new int[] { 1, 1, 1 }, result);
    pojos = dao.query("1=1", new StatementParameters(), new DalHints());
    i = 0;
    for (UpdatableIntVersionModel model : pojos) {
        assertEquals("1122334455", model.getAddress());
        Assert.assertTrue(oldValue[i++] + 1 == model.getTableIndex());
    }
}
Also used : DalHints(com.ctrip.platform.dal.dao.DalHints) StatementParameters(com.ctrip.platform.dal.dao.StatementParameters) BatchUpdateTask(com.ctrip.platform.dal.dao.task.BatchUpdateTask) DalDefaultJpaParser(com.ctrip.platform.dal.dao.helper.DalDefaultJpaParser) DalTableDao(com.ctrip.platform.dal.dao.DalTableDao) Test(org.junit.Test)

Example 22 with DalTableDao

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

the class BatchUpdateTaskTestStub method testVersionNotSet.

@Test
public void testVersionNotSet() throws SQLException {
    BatchUpdateTask<UpdatableVersionModel> test = new BatchUpdateTask<>();
    DalParser<UpdatableVersionModel> parser = new DalDefaultJpaParser<>(UpdatableVersionModel.class, getDbName());
    test.initialize(parser);
    DalTableDao<UpdatableVersionModel> dao = new DalTableDao<UpdatableVersionModel>(parser);
    DalHints hints = new DalHints();
    List<UpdatableVersionModel> pojos = dao.query("1=1", new StatementParameters(), new DalHints());
    for (UpdatableVersionModel model : pojos) {
        model.setAddress("1122334455");
        model.setLastChanged(null);
    }
    try {
        execute(test, hints, pojos);
        fail();
    } catch (SQLException e) {
        assertEquals(ErrorCode.ValidateVersion.getMessage(), e.getMessage());
    }
}
Also used : DalHints(com.ctrip.platform.dal.dao.DalHints) SQLException(java.sql.SQLException) StatementParameters(com.ctrip.platform.dal.dao.StatementParameters) DalDefaultJpaParser(com.ctrip.platform.dal.dao.helper.DalDefaultJpaParser) BatchUpdateTask(com.ctrip.platform.dal.dao.task.BatchUpdateTask) DalTableDao(com.ctrip.platform.dal.dao.DalTableDao) Test(org.junit.Test)

Example 23 with DalTableDao

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

the class BatchUpdateTaskTestStub method testDaoUpdateIfChangedField.

@Test
public void testDaoUpdateIfChangedField() throws SQLException {
    DalTableDao<UpdatableClientTestModel> dao = new DalTableDao<>(UpdatableClientTestModel.class, getDbName());
    DalHints hints = new DalHints();
    try {
        List<UpdatableClientTestModel> pojos = getAll(UpdatableClientTestModel.class);
        int i = 0;
        for (UpdatableClientTestModel model : pojos) {
            model.setType((Short) null);
            if (i % 2 == 0)
                model.setQuantity(-100);
            if (i % 2 == 1)
                model.setDbIndex(-200);
            if (i % 2 == 1)
                model.setTableIndex(-300);
            i++;
        }
        int[] result = dao.batchUpdate(hints, pojos);
        assertArrayEquals(new int[] { 1, 1, 1 }, result);
        i = 0;
        pojos = getAll(UpdatableClientTestModel.class);
        for (UpdatableClientTestModel model : pojos) {
            assertNull(model.getType());
            assertNotNull(model.getLastChanged());
            if (i % 2 == 0)
                assertEquals(model.getQuantity().intValue(), -100);
            else
                assertEquals(model.getQuantity().intValue(), 10 + i);
            if (i % 2 == 1)
                assertEquals(model.getDbIndex().intValue(), -200);
            else
                assertEquals(model.getDbIndex().intValue(), 0);
            if (i % 2 == 1)
                assertEquals(model.getTableIndex().intValue(), -300);
            else
                assertEquals(model.getTableIndex().intValue(), i);
            i++;
        }
    } catch (SQLException e) {
        e.printStackTrace();
        fail();
    }
}
Also used : DalHints(com.ctrip.platform.dal.dao.DalHints) SQLException(java.sql.SQLException) DalTableDao(com.ctrip.platform.dal.dao.DalTableDao) Test(org.junit.Test)

Example 24 with DalTableDao

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

the class BatchUpdateTaskTestStub method testIncludeExcludeColumns.

@Test
public void testIncludeExcludeColumns() throws SQLException {
    BatchUpdateTask<UpdatableIntVersionModel> test = new BatchUpdateTask<>();
    DalParser<UpdatableIntVersionModel> parser = new DalDefaultJpaParser<>(UpdatableIntVersionModel.class, getDbName());
    test.initialize(parser);
    DalTableDao<UpdatableIntVersionModel> dao = new DalTableDao<>(parser);
    DalHints hints = new DalHints().exclude("dbIndex").include("quantity", "dbIndex", "address");
    List<UpdatableIntVersionModel> pojos = dao.query("1=1", new StatementParameters(), new DalHints());
    for (UpdatableIntVersionModel model : pojos) {
        model.setQuantity(500);
        model.setDbIndex(100);
        model.setAddress("1122334455");
    }
    int[] result = execute(test, hints, pojos);
    assertArrayEquals(new int[] { 1, 1, 1 }, result);
    pojos = dao.query("1=1", new StatementParameters(), new DalHints());
    for (UpdatableIntVersionModel model : pojos) {
        assertEquals("1122334455", model.getAddress());
        assertEquals(500, model.getQuantity().intValue());
        assertEquals(0, model.getDbIndex().intValue());
    }
}
Also used : DalHints(com.ctrip.platform.dal.dao.DalHints) StatementParameters(com.ctrip.platform.dal.dao.StatementParameters) DalDefaultJpaParser(com.ctrip.platform.dal.dao.helper.DalDefaultJpaParser) BatchUpdateTask(com.ctrip.platform.dal.dao.task.BatchUpdateTask) DalTableDao(com.ctrip.platform.dal.dao.DalTableDao) Test(org.junit.Test)

Example 25 with DalTableDao

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

the class DalTableDaoTestStub method testCombinedInsertWithKeyInsertBack.

/**
 * Test Insert multiple entities with one SQL Statement
 * @throws SQLException
 */
@Test
public void testCombinedInsertWithKeyInsertBack() throws SQLException {
    if (!diff.supportInsertValues || !diff.supportGetGeneratedKeys)
        return;
    DalTableDao<ClientTestModelJpa> dao = new DalTableDao(new DalDefaultJpaParser<>(ClientTestModelJpa.class, dbName));
    List<ClientTestModelJpa> entities = new ArrayList<>();
    for (int i = 0; i < 3; i++) {
        ClientTestModelJpa model = new ClientTestModelJpa();
        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.setIdentityBack(), holder, entities);
    assertEquals(3, res, 4 + 3);
    Assert.assertEquals(3, holder.size());
    Assert.assertTrue(holder.getKeyList().get(0).containsKey("GENERATED_KEY"));
    int i = 0;
    for (ClientTestModelJpa pojo : entities) Assert.assertEquals(holder.getKey(i++).intValue(), pojo.getId().intValue());
}
Also used : DalHints(com.ctrip.platform.dal.dao.DalHints) ArrayList(java.util.ArrayList) KeyHolder(com.ctrip.platform.dal.dao.KeyHolder) DalTableDao(com.ctrip.platform.dal.dao.DalTableDao) Test(org.junit.Test)

Aggregations

DalHints (com.ctrip.platform.dal.dao.DalHints)29 DalTableDao (com.ctrip.platform.dal.dao.DalTableDao)29 Test (org.junit.Test)29 StatementParameters (com.ctrip.platform.dal.dao.StatementParameters)17 DalDefaultJpaParser (com.ctrip.platform.dal.dao.helper.DalDefaultJpaParser)13 BatchUpdateTask (com.ctrip.platform.dal.dao.task.BatchUpdateTask)8 KeyHolder (com.ctrip.platform.dal.dao.KeyHolder)7 ArrayList (java.util.ArrayList)7 SelectSqlBuilder (com.ctrip.platform.dal.dao.sqlbuilder.SelectSqlBuilder)4 SQLException (java.sql.SQLException)3 DalException (com.ctrip.platform.dal.exceptions.DalException)2 Timestamp (java.sql.Timestamp)2