Search in sources :

Example 11 with DalTableDao

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

the class PartialQueryTableDaoUnitTest method testDetectFieldNotExist.

@Test
public void testDetectFieldNotExist() throws Exception {
    DalTableDao<PersonWithoutName> client = new DalTableDao<>(new DalDefaultJpaParser<>(PersonWithoutName.class));
    List<Integer> peopleIds = new ArrayList<>();
    peopleIds.add(1);
    peopleIds.add(2);
    peopleIds.add(3);
    List<Integer> cityIds = new ArrayList<>();
    cityIds.add(1);
    cityIds.add(2);
    cityIds.add(3);
    SelectSqlBuilder builder = new SelectSqlBuilder();
    builder.select("DataChange_LastTime", "CityID", "Name", "ProvinceID", "PeopleID", "CountryID");
    builder.in("PeopleID", peopleIds, Types.INTEGER, false);
    builder.and();
    builder.in("CityID", cityIds, Types.INTEGER, false);
    try {
        client.query(builder, new DalHints().inAllShards().inTableShard(1));
        Assert.fail();
    } catch (DalException e) {
        e.printStackTrace();
        assertEquals(ErrorCode.FieldNotExists.getCode(), e.getErrorCode());
    }
}
Also used : DalHints(com.ctrip.platform.dal.dao.DalHints) DalException(com.ctrip.platform.dal.exceptions.DalException) ArrayList(java.util.ArrayList) SelectSqlBuilder(com.ctrip.platform.dal.dao.sqlbuilder.SelectSqlBuilder) DalTableDao(com.ctrip.platform.dal.dao.DalTableDao) Test(org.junit.Test)

Example 12 with DalTableDao

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

the class BaseDalTabelDaoShardByTableTest method testInsertMultipleAsListWithKeyHolderWithPkInsertBack.

@Test
public void testInsertMultipleAsListWithKeyHolderWithPkInsertBack() throws SQLException {
    if (!INSERT_PK_BACK_ALLOWED)
        return;
    DalTableDao<ClientTestModel> dao = new DalTableDao<ClientTestModel>(ClientTestModel.class, databaseName, TABLE_NAME);
    List<ClientTestModel> entities = new ArrayList<>();
    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");
        entities.add(model);
    }
    KeyHolder holder = new KeyHolder();
    for (int i = 0; i < mod; i++) {
        int j = 1;
        IdentitySetBackHelper.clearId(entities);
        dao.insert(new DalHints().inTableShard(i).setIdentityBack(), holder, entities);
        assertEquals(3, holder.size());
        IdentitySetBackHelper.assertIdentityTableShard(dao, entities, i);
    }
    deleteAllShards();
    // Test without keyholder 1
    for (int i = 0; i < mod; i++) {
        int j = 1;
        IdentitySetBackHelper.clearId(entities);
        dao.insert(new DalHints().inTableShard(i).setIdentityBack(), null, entities);
        assertEquals(3, holder.size());
        IdentitySetBackHelper.assertIdentityTableShard(dao, entities, i);
    }
    deleteAllShards();
    // Test without keyholder 2
    for (int i = 0; i < mod; i++) {
        int j = 1;
        IdentitySetBackHelper.clearId(entities);
        dao.insert(new DalHints().inTableShard(i).setIdentityBack(), entities);
        assertEquals(3, holder.size());
        IdentitySetBackHelper.assertIdentityTableShard(dao, entities, i);
    }
    deleteAllShards();
    // By fields not same shard
    // holder = new KeyHolder();
    entities.get(0).setTableIndex(0);
    entities.get(1).setTableIndex(1);
    entities.get(2).setTableIndex(2);
    IdentitySetBackHelper.clearId(entities);
    dao.insert(new DalHints().setIdentityBack(), holder, entities);
    assertEquals(3, holder.size());
    assertEquals(1, getCount(0));
    assertEquals(1, getCount(1));
    assertEquals(1, getCount(2));
    IdentitySetBackHelper.assertIdentity(dao, entities);
    dao.insert(new DalHints().setIdentityBack(), holder, entities);
    assertEquals(3, holder.size());
}
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)

Example 13 with DalTableDao

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

the class BaseDalTabelDaoShardByTableTest method testCombinedInsertWithPkInsertBack.

@Test
public void testCombinedInsertWithPkInsertBack() 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;
    }
    for (int i = 0; i < mod; i++) {
        KeyHolder holder = new KeyHolder();
        dao.combinedInsert(new DalHints().inTableShard(i).setIdentityBack(), holder, Arrays.asList(entities));
        for (ClientTestModel model : entities) {
            assertEquals(dao.queryByPk(model, new DalHints().inTableShard(i)).getAddress(), model.getAddress());
        }
    }
    // Test holder reuse case
    KeyHolder holder = new KeyHolder();
    for (int i = 0; i < mod; i++) {
        dao.combinedInsert(new DalHints().inTableShard(i).setIdentityBack(), holder, Arrays.asList(entities));
        for (ClientTestModel model : entities) {
            assertEquals(dao.queryByPk(model, new DalHints().inTableShard(i)).getAddress(), model.getAddress());
        }
    }
    // Test with out kh
    for (int i = 0; i < mod; i++) {
        dao.combinedInsert(new DalHints().inTableShard(i).setIdentityBack(), Arrays.asList(entities));
        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 14 with DalTableDao

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

the class BaseDalTabelDaoShardByTableTest method testInsertMultipleAsListWithKeyHolderAsyncCallbackWithPkInsertBack.

@Test
public void testInsertMultipleAsListWithKeyHolderAsyncCallbackWithPkInsertBack() throws SQLException {
    if (!INSERT_PK_BACK_ALLOWED)
        return;
    DalHints hints;
    DalTableDao<ClientTestModel> dao = new DalTableDao<ClientTestModel>(ClientTestModel.class, databaseName, TABLE_NAME);
    List<ClientTestModel> entities = new ArrayList<>();
    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.add(model);
    }
    KeyHolder holder;
    int[] res;
    for (int i = 0; i < mod; i++) {
        int j = 1;
        holder = new KeyHolder();
        // By tabelShard
        // holder = new KeyHolder();
        hints = asyncHints();
        res = dao.insert(hints.inTableShard(i).setIdentityBack(), holder, entities);
        res = assertIntArray(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.insert(hints.setTableShardValue(i).setIdentityBack(), holder, entities);
        res = assertIntArray(res, hints);
        for (ClientTestModel model : entities) {
            assertEquals(dao.queryByPk(model, new DalHints().inTableShard(i)).getAddress(), model.getAddress());
        }
    }
    deleteAllShards();
    // By fields not same shard
    holder = new KeyHolder();
    entities.get(0).setTableIndex(0);
    entities.get(1).setTableIndex(1);
    entities.get(2).setTableIndex(2);
    hints = intHints().setIdentityBack();
    res = dao.insert(hints, holder, entities);
    res = assertIntArray(res, hints);
    assertEquals(1, getCount(0));
    assertEquals(1, getCount(1));
    assertEquals(1, getCount(2));
    for (ClientTestModel model : entities) {
        assertEquals(dao.queryByPk(model, new DalHints()).getAddress(), model.getAddress());
    }
}
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)

Example 15 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)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