Search in sources :

Example 26 with DalTableDao

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

the class BatchUpdateTaskTestStub method testUpdatableWithVersionByDao.

@Test
public void testUpdatableWithVersionByDao() throws SQLException {
    DalParser<UpdatableVersionModel> parser = new DalDefaultJpaParser<>(UpdatableVersionModel.class, getDbName());
    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) {
        // Old value is SH INFO
        model.setAddress("1122334455");
        Timestamp t = model.getLastChanged();
        t.setTime(t.getTime() + 100);
        model.setLastChanged(t);
    }
    int[] result = dao.batchUpdate(hints, pojos);
    assertArrayEquals(new int[] { 0, 0, 0 }, result);
    pojos = dao.query("1=1", new StatementParameters(), new DalHints());
    for (UpdatableVersionModel model : pojos) // Still old value because version is incorrect
    assertEquals("SH INFO", model.getAddress());
    // Now the right case
    pojos = dao.query("1=1", new StatementParameters(), new DalHints());
    long[] oldVer = new long[3];
    int i = 0;
    for (UpdatableVersionModel model : pojos) {
        model.setAddress("1122334455");
        oldVer[i++] = model.getLastChanged().getTime();
    }
    result = dao.batchUpdate(hints, pojos);
    assertArrayEquals(new int[] { 1, 1, 1 }, result);
    pojos = dao.query("1=1", new StatementParameters(), new DalHints());
    i = 0;
    for (UpdatableVersionModel model : pojos) {
        assertEquals("1122334455", model.getAddress());
        Assert.assertTrue(oldVer[i++] <= model.getLastChanged().getTime());
    }
}
Also used : DalHints(com.ctrip.platform.dal.dao.DalHints) StatementParameters(com.ctrip.platform.dal.dao.StatementParameters) DalDefaultJpaParser(com.ctrip.platform.dal.dao.helper.DalDefaultJpaParser) Timestamp(java.sql.Timestamp) DalTableDao(com.ctrip.platform.dal.dao.DalTableDao) Test(org.junit.Test)

Example 27 with DalTableDao

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

the class BaseDalTabelDaoShardByTableTest method testCombinedInsertAsyncCallbackWithPkInsertBack.

@Test
public void testCombinedInsertAsyncCallbackWithPkInsertBack() throws SQLException {
    if (!INSERT_PK_BACK_ALLOWED)
        return;
    DalTableDao<ClientTestModel> dao = new DalTableDao<ClientTestModel>(ClientTestModel.class, databaseName, TABLE_NAME);
    ClientTestModel[] entities = new ClientTestModel[3];
    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" + i);
        entities[i] = model;
    }
    int res;
    for (int i = 0; i < mod; i++) {
        int j = 1;
        KeyHolder holder = new KeyHolder();
        // By tabelShard
        // holder = new KeyHolder();
        DalHints hints = asyncHints();
        res = dao.combinedInsert(hints.inTableShard(i).setIdentityBack(), holder, Arrays.asList(entities));
        res = assertInt(res, hints);
        for (ClientTestModel model : entities) {
            assertEquals(dao.queryByPk(model, new DalHints().inTableShard(i)).getAddress(), model.getAddress());
        }
        // By tableShardValue
        holder = new KeyHolder();
        hints = intHints();
        res = dao.combinedInsert(hints.setTableShardValue(i).setIdentityBack(), holder, Arrays.asList(entities));
        res = assertInt(res, hints);
        for (ClientTestModel model : entities) {
            assertEquals(dao.queryByPk(model, new DalHints().inTableShard(i)).getAddress(), model.getAddress());
        }
    }
}
Also used : DalHints(com.ctrip.platform.dal.dao.DalHints) KeyHolder(com.ctrip.platform.dal.dao.KeyHolder) DalTableDao(com.ctrip.platform.dal.dao.DalTableDao) Test(org.junit.Test)

Example 28 with DalTableDao

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

the class BaseDalTabelDaoShardByTableTest method testQueryByColumnNames.

/**
 * Query by Entity with Primary key
 * @throws SQLException
 */
@Test
public void testQueryByColumnNames() throws SQLException {
    ClientTestModel pk = null;
    ClientTestModel model = null;
    for (int i = 0; i < mod; i++) {
        pk = new ClientTestModel();
        pk.setId(1);
        // By tabelShard
        DalTableDao<ClientTestModel> dao = new DalTableDao(ClientTestModel.class, databaseName, "dal_client_test");
        model = dao.queryByPk(pk, new DalHints().inTableShard(i).selectByNames());
        assertEquals(1, model.getId().intValue());
        assertEquals(i, model.getTableIndex().intValue());
        dao.queryLike(model, new DalHints().inTableShard(i).selectByNames());
        dao.count("id > 0", new StatementParameters(), new DalHints().inTableShard(i).selectByNames());
        Long L = dao.queryObject(new SelectSqlBuilder().select("id").requireFirst().where("id > 0"), new DalHints().inTableShard(i).selectByNames(), Long.class);
    }
}
Also used : DalHints(com.ctrip.platform.dal.dao.DalHints) StatementParameters(com.ctrip.platform.dal.dao.StatementParameters) SelectSqlBuilder(com.ctrip.platform.dal.dao.sqlbuilder.SelectSqlBuilder) DalTableDao(com.ctrip.platform.dal.dao.DalTableDao) Test(org.junit.Test)

Example 29 with DalTableDao

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

the class BaseDalTableDaoShardByDbTest method testQueryListPartial.

@Test
public void testQueryListPartial() throws SQLException {
    List<ClientTestModel> models = null;
    DalTableDao<ClientTestModel> dao = new DalTableDao<>(ClientTestModel.class, databaseName, TABLE_NAME);
    for (int i = 0; i < mod; i++) {
        SelectSqlBuilder builder = new SelectSqlBuilder();
        builder.equal("type", 1, Types.SMALLINT);
        builder.select("id", "tableIndex");
        DalHints hints = new DalHints();
        models = dao.query(builder, hints.inShard(i));
        Assert.assertTrue(null != models);
        Assert.assertEquals(3, models.size());
        ClientTestModel model = models.get(0);
        Assert.assertNull(model.getAddress());
        Assert.assertNull(model.getLastChanged());
        Assert.assertNull(model.getQuantity());
        Assert.assertNull(hints.get(DalHintEnum.partialQuery));
    }
}
Also used : DalHints(com.ctrip.platform.dal.dao.DalHints) SelectSqlBuilder(com.ctrip.platform.dal.dao.sqlbuilder.SelectSqlBuilder) 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