Search in sources :

Example 6 with DalDefaultJpaParser

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

the class SingleUpdateTaskTestStub method testIncludeColumnsByDao.

@Test
public void testIncludeColumnsByDao() throws SQLException {
    //Table Index and Address is not updatable
    DalParser<NonUpdatableModel> parser = new DalDefaultJpaParser<>(NonUpdatableModel.class, getDbName());
    DalHints hints = new DalHints();
    DalTableDao<NonUpdatableModel> dao = new DalTableDao<>(parser);
    NonUpdatableModel model = dao.query("1=1", new StatementParameters(), new DalHints()).get(0);
    String oldAddr = model.getAddress();
    Integer oldTableIndex = model.getTableIndex();
    Integer oldQuantity = model.getQuantity();
    model.setDbIndex(-100);
    model.setAddress("1122334455");
    model.setTableIndex(100);
    model.setQuantity(500);
    model.setType((short) 8);
    int result = dao.update(hints.include("dbIndex", "type"), model);
    assertIntEquals(1, result);
    model = dao.queryByPk(model, new DalHints());
    assertEquals(oldAddr, model.getAddress());
    assertEquals(oldTableIndex, model.getTableIndex());
    assertEquals(oldQuantity, model.getQuantity());
    assertEquals(-100, model.getDbIndex().intValue());
    assertEquals(8, model.getType().shortValue());
}
Also used : DalHints(com.ctrip.platform.dal.dao.DalHints) StatementParameters(com.ctrip.platform.dal.dao.StatementParameters) DalDefaultJpaParser(com.ctrip.platform.dal.dao.helper.DalDefaultJpaParser) DalTableDao(com.ctrip.platform.dal.dao.DalTableDao) Test(org.junit.Test)

Example 7 with DalDefaultJpaParser

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

the class DalDefaultJpaParserTest method testConstructByDbname.

@Test
public void testConstructByDbname() throws Exception {
    DalParser<PersonEntity> parser = new DalDefaultJpaParser(PersonEntity.class);
    Assert.assertEquals("dao_test_M", parser.getDatabaseName());
    Assert.assertEquals("person", parser.getTableName());
    String testDbName = "test";
    String testTabeleName = "test2";
    parser = new DalDefaultJpaParser(PersonEntity.class, testDbName);
    Assert.assertEquals(testDbName, parser.getDatabaseName());
    Assert.assertEquals("person", parser.getTableName());
    parser = new DalDefaultJpaParser(PersonEntity.class, testDbName, testTabeleName);
    Assert.assertEquals(testDbName, parser.getDatabaseName());
    Assert.assertEquals(testTabeleName, parser.getTableName());
}
Also used : DalDefaultJpaParser(com.ctrip.platform.dal.dao.helper.DalDefaultJpaParser) Test(org.junit.Test)

Example 8 with DalDefaultJpaParser

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

the class BatchUpdateTaskTestStub method testUpdatableWithIntVersionByDao.

@Test
public void testUpdatableWithIntVersionByDao() throws SQLException {
    DalParser<UpdatableIntVersionModel> parser = new DalDefaultJpaParser<>(UpdatableIntVersionModel.class, getDbName());
    DalTableDao<UpdatableIntVersionModel> dao = new DalTableDao<>(parser);
    DalHints hints = new DalHints();
    List<UpdatableIntVersionModel> pojos = dao.query("1=1", new StatementParameters(), new DalHints());
    int i = 0;
    for (UpdatableIntVersionModel model : pojos) {
        model.setAddress("1122334455");
        // Make the version incorrect
        model.setTableIndex(-1);
    }
    int[] result = dao.batchUpdate(hints, pojos);
    assertArrayEquals(new int[] { 0, 0, 0 }, result);
    pojos = dao.query("1=1", new StatementParameters(), new DalHints());
    for (UpdatableIntVersionModel model : pojos) //Still old value because version is incorrect
    assertEquals("SH INFO", model.getAddress());
    // Now the right case
    Integer[] oldValue = new Integer[3];
    pojos = dao.query("1=1", new StatementParameters(), new DalHints());
    i = 0;
    for (UpdatableIntVersionModel model : pojos) {
        model.setAddress("1122334455");
        oldValue[i++] = model.getTableIndex();
    }
    result = dao.batchUpdate(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) DalDefaultJpaParser(com.ctrip.platform.dal.dao.helper.DalDefaultJpaParser) DalTableDao(com.ctrip.platform.dal.dao.DalTableDao) Test(org.junit.Test)

Example 9 with DalDefaultJpaParser

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

the class BatchUpdateTaskTestStub method testNotUpdatableVersion.

@Test
public void testNotUpdatableVersion() throws SQLException {
    BatchUpdateTask<NonUpdatableVersionModel> test = new BatchUpdateTask<>();
    DalParser<NonUpdatableVersionModel> parser = new DalDefaultJpaParser<>(NonUpdatableVersionModel.class, getDbName());
    test.initialize(parser);
    DalTableDao<NonUpdatableVersionModel> dao = new DalTableDao<>(parser);
    DalHints hints = new DalHints();
    List<NonUpdatableVersionModel> pojos = dao.query("1=1", new StatementParameters(), new DalHints());
    for (NonUpdatableVersionModel model : pojos) {
        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 (NonUpdatableVersionModel model : pojos) assertEquals("1122334455", 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) DalTableDao(com.ctrip.platform.dal.dao.DalTableDao) Test(org.junit.Test)

Example 10 with DalDefaultJpaParser

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

the class DalDefaultJpaParserTest method setUpBeforeClass.

@BeforeClass
public static void setUpBeforeClass() throws Exception {
    DalClientFactory.initClientFactory();
    personParser = new DalDefaultJpaParser(PersonEntity.class);
    allTypesParser = new DalDefaultJpaParser(AllTypesEntity.class);
}
Also used : DalDefaultJpaParser(com.ctrip.platform.dal.dao.helper.DalDefaultJpaParser) BeforeClass(org.junit.BeforeClass)

Aggregations

DalDefaultJpaParser (com.ctrip.platform.dal.dao.helper.DalDefaultJpaParser)27 Test (org.junit.Test)26 DalHints (com.ctrip.platform.dal.dao.DalHints)25 DalTableDao (com.ctrip.platform.dal.dao.DalTableDao)13 StatementParameters (com.ctrip.platform.dal.dao.StatementParameters)13 BatchUpdateTask (com.ctrip.platform.dal.dao.task.BatchUpdateTask)8 SingleUpdateTask (com.ctrip.platform.dal.dao.task.SingleUpdateTask)8 SQLException (java.sql.SQLException)5 Map (java.util.Map)4 Timestamp (java.sql.Timestamp)3 BatchInsertTask (com.ctrip.platform.dal.dao.task.BatchInsertTask)2 KeyHolder (com.ctrip.platform.dal.dao.KeyHolder)1 CombinedInsertTask (com.ctrip.platform.dal.dao.task.CombinedInsertTask)1 SingleInsertTask (com.ctrip.platform.dal.dao.task.SingleInsertTask)1 BeforeClass (org.junit.BeforeClass)1