Search in sources :

Example 6 with DalTableDao

use of com.ctrip.platform.dal.dao.DalTableDao 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 7 with DalTableDao

use of com.ctrip.platform.dal.dao.DalTableDao 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 8 with DalTableDao

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

the class BatchUpdateTaskTestStub method testDaoExecuteUpdatableEntity.

@Test
public void testDaoExecuteUpdatableEntity() throws SQLException {
    DalTableDao<UpdatableClientTestModel> dao = new DalTableDao<>(UpdatableClientTestModel.class, getDbName());
    DalHints hints = new DalHints();
    try {
        List<UpdatableClientTestModel> pojos = getAll(UpdatableClientTestModel.class);
        for (UpdatableClientTestModel model : pojos) model.setAddress("1122334455");
        int[] result = dao.batchUpdate(hints, pojos);
        assertEquals(3, result.length);
        assertArrayEquals(new int[] { 1, 1, 1 }, result);
        assertEquals(3, getCount());
        pojos = getAll(UpdatableClientTestModel.class);
        for (UpdatableClientTestModel model : pojos) assertEquals("1122334455", model.getAddress());
    } 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 9 with DalTableDao

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

the class BatchUpdateTaskTestStub method testDaoExcludeColumns.

@Test
public void testDaoExcludeColumns() throws SQLException {
    DalTableDao<UpdatableIntVersionModel> dao = new DalTableDao<>(UpdatableIntVersionModel.class, getDbName());
    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 = dao.batchUpdate(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) DalTableDao(com.ctrip.platform.dal.dao.DalTableDao) Test(org.junit.Test)

Example 10 with DalTableDao

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

the class PartialQueryTableDaoUnitTest method testFindByPartial.

@Test
public void testFindByPartial() throws Exception {
    DalTableDao<Person> client = new DalTableDao<>(new DalDefaultJpaParser<>(Person.class));
    List<Person> pl;
    pl = client.query("1=1", new StatementParameters(), new DalHints().partialQuery("Name", "CountryID").inShard(1).inTableShard(1));
    assertPersonList(pl);
    pl = client.queryFrom("1=1", new StatementParameters(), new DalHints().partialQuery("Name", "CountryID").inAllShards().inTableShard(1), 1, 10);
    assertPersonList(pl);
    Person sample = new Person();
    sample.setCountryID(1);
    pl = client.queryLike(sample, new DalHints().partialQuery("Name", "CountryID").inAllShards().inTableShard(1));
    assertPersonList(pl);
    pl = client.queryTop("1=1", new StatementParameters(), new DalHints().partialQuery("Name", "CountryID").inAllShards().inTableShard(1), 100);
    assertPersonList(pl);
    Person test = client.queryByPk(1, new DalHints().partialQuery("Name", "CountryID").inShard(1).inTableShard(1));
    assertPerson(test);
    test.setPeopleID(1);
    test = client.queryByPk(test, new DalHints().partialQuery("Name", "CountryID").inShard(1).inTableShard(1));
    assertPerson(test);
    test = client.queryFirst("1=1", new StatementParameters(), new DalHints().partialQuery("Name", "CountryID").inShard(1).inTableShard(1));
    assertPerson(test);
}
Also used : DalHints(com.ctrip.platform.dal.dao.DalHints) StatementParameters(com.ctrip.platform.dal.dao.StatementParameters) DalTableDao(com.ctrip.platform.dal.dao.DalTableDao) Test(org.junit.Test)

Aggregations

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