Search in sources :

Example 11 with BatchUpdateTask

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

the class BatchUpdateTaskTestStub method testExcludeColumns.

@Test
public void testExcludeColumns() 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");
    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 12 with BatchUpdateTask

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

the class BatchUpdateTaskTestStub method testVersionIncorrect.

@Test
public void testVersionIncorrect() 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");
        Timestamp t = model.getLastChanged();
        t.setTime(t.getTime() + 100);
        model.setLastChanged(t);
    }
    int[] result = execute(test, hints, pojos);
    assertArrayEquals(new int[] { 0, 0, 0 }, result);
    pojos = dao.query("1=1", new StatementParameters(), new DalHints());
    for (UpdatableVersionModel model : pojos) assertEquals("SH INFO", model.getAddress());
}
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) Timestamp(java.sql.Timestamp) DalTableDao(com.ctrip.platform.dal.dao.DalTableDao) Test(org.junit.Test)

Example 13 with BatchUpdateTask

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

the class BatchUpdateTaskTestStub method testUpdateIfNullField.

@Test
public void testUpdateIfNullField() {
    BatchUpdateTask<ClientTestModel> test = new BatchUpdateTask<>();
    test.initialize(getParser());
    DalHints hints = new DalHints();
    try {
        List<ClientTestModel> pojos = getAll();
        int i = 0;
        for (ClientTestModel model : pojos) {
            model.setType((Short) null);
            if (i % 2 == 0)
                model.setQuantity(-100);
            else
                model.setQuantity(null);
            if (i % 2 == 1)
                model.setDbIndex(-200);
            else
                model.setDbIndex(null);
            if (i % 2 == 1)
                model.setTableIndex(-300);
            else
                model.setTableIndex(null);
            i++;
        }
        int[] result = execute(test, hints, pojos);
        assertArrayEquals(new int[] { 1, 1, 1 }, result);
        i = 0;
        pojos = getAll();
        for (ClientTestModel model : pojos) {
            assertEquals(1, model.getType().intValue());
            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) BatchUpdateTask(com.ctrip.platform.dal.dao.task.BatchUpdateTask) Test(org.junit.Test)

Example 14 with BatchUpdateTask

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

the class BatchUpdateTaskTestStub method testIncludeColumns.

@Test
public void testIncludeColumns() 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().include("address", "quantity");
    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 15 with BatchUpdateTask

use of com.ctrip.platform.dal.dao.task.BatchUpdateTask 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)

Aggregations

DalHints (com.ctrip.platform.dal.dao.DalHints)18 BatchUpdateTask (com.ctrip.platform.dal.dao.task.BatchUpdateTask)18 Test (org.junit.Test)18 SQLException (java.sql.SQLException)11 DalTableDao (com.ctrip.platform.dal.dao.DalTableDao)8 StatementParameters (com.ctrip.platform.dal.dao.StatementParameters)8 DalDefaultJpaParser (com.ctrip.platform.dal.dao.helper.DalDefaultJpaParser)8 Timestamp (java.sql.Timestamp)1