Search in sources :

Example 6 with KeyHolder

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

the class BaseDalTabelDaoShardByTableTest method testInsertMultipleAsListWithKeyHolderAsyncCallback.

@Test
public void testInsertMultipleAsListWithKeyHolderAsyncCallback() throws SQLException {
    DalHints hints;
    List<ClientTestModel> entities = new ArrayList<ClientTestModel>();
    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();
    int[] res;
    try {
        hints = asyncHints();
        res = dao.insert(hints, holder, entities);
        res = assertIntArray(res, hints);
        fail();
    } catch (Exception e) {
    }
    for (int i = 0; i < mod; i++) {
        int j = 1;
        holder = null;
        // By tabelShard
        // holder = new KeyHolder();
        hints = asyncHints();
        res = dao.insert(hints.inTableShard(i), holder, entities);
        res = assertIntArray(res, hints);
        assertEquals((i + 1) + j++ * 3, getCount(i));
        //			assertEquals(3, res);
        //			assertEquals(3, holder.getKeyList().size());		 
        //			assertTrue(holder.getKey(0).longValue() > 0);
        //			assertTrue(holder.getKeyList().get(0).containsKey("GENERATED_KEYS"));
        // By tableShardValue
        // holder = new KeyHolder();
        hints = intHints();
        res = dao.insert(hints.setTableShardValue(i), holder, entities);
        res = assertIntArray(res, hints);
        assertEquals((i + 1) + j++ * 3, getCount(i));
        // By shardColValue
        // holder = new KeyHolder();
        hints = asyncHints();
        res = dao.insert(hints.setShardColValue("index", i), holder, entities);
        res = assertIntArray(res, hints);
        assertEquals((i + 1) + j++ * 3, getCount(i));
        // By shardColValue
        // holder = new KeyHolder();
        hints = intHints();
        res = dao.insert(hints.setShardColValue("tableIndex", i), holder, entities);
        res = assertIntArray(res, hints);
        assertEquals((i + 1) + j++ * 3, getCount(i));
        // By fields same shard
        // holder = new KeyHolder();
        hints = asyncHints();
        entities.get(0).setTableIndex(i);
        entities.get(1).setTableIndex(i);
        entities.get(2).setTableIndex(i);
        res = dao.insert(hints, holder, entities);
        res = assertIntArray(res, hints);
        assertEquals((i + 1) + j++ * 3, getCount(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);
    hints = intHints();
    res = dao.insert(hints, null, entities);
    res = assertIntArray(res, hints);
    assertEquals(1, getCount(0));
    assertEquals(1, getCount(1));
    assertEquals(1, getCount(2));
//		assertEquals(3, res);
//		assertEquals(3, holder.getKeyList().size());		 
//		assertTrue(holder.getKey(0).longValue() > 0);
//		assertTrue(holder.getKeyList().get(0).containsKey("GENERATED_KEYS"));
}
Also used : DalHints(com.ctrip.platform.dal.dao.DalHints) ArrayList(java.util.ArrayList) KeyHolder(com.ctrip.platform.dal.dao.KeyHolder) SQLException(java.sql.SQLException) Test(org.junit.Test)

Example 7 with KeyHolder

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

the class DalConcurrentMysqlTest method setUpBeforeClass.

@BeforeClass
public static void setUpBeforeClass() throws Exception {
    DalHints hints = new DalHints();
    String[] sqls = new String[] { DROP_TABLE_SQL, CREATE_TABLE_SQL };
    client.batchUpdate(sqls, hints);
    List<ClientTestModel> models = new ArrayList<>();
    for (int i = 0; i < INSERT_COUNT; i++) {
        ClientTestModel model = new ClientTestModel();
        model.setAddress("SH-" + i);
        model.setLastChanged(new Timestamp(System.currentTimeMillis()));
        model.setQuantity(i % 10);
        model.setType((short) (i % 3));
        models.add(model);
    }
    KeyHolder holder = new KeyHolder();
    int[] counts = dao.insert(hints, holder, models);
    System.out.println("The insert ClientTestModel count: " + counts);
    generateIds = holder.getIdList();
    System.out.println("Ids: " + generateIds.size());
    Assert.assertEquals(INSERT_COUNT, generateIds.size());
}
Also used : DalHints(com.ctrip.platform.dal.dao.DalHints) ArrayList(java.util.ArrayList) KeyHolder(com.ctrip.platform.dal.dao.KeyHolder) Timestamp(java.sql.Timestamp) BeforeClass(org.junit.BeforeClass)

Example 8 with KeyHolder

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

the class DalConcurrentSqlServerTestStub method setUpBeforeClass.

/**
	 * Create the test tables
	 * @throws Exception
	 */
@BeforeClass
public static void setUpBeforeClass() throws Exception {
    DalHints hints = new DalHints();
    StatementParameters parameters = new StatementParameters();
    String[] sqls = new String[] { DROP_TABLE_SQL, CREATE_TABLE_SQL };
    for (int i = 0; i < sqls.length; i++) {
        client.update(sqls[i], parameters, hints);
    }
    List<ClientTestModel> models = new ArrayList<>();
    for (int i = 0; i < INSERT_COUNT; i++) {
        ClientTestModel model = new ClientTestModel();
        model.setAddress("SH-" + i);
        model.setLastChanged(new Timestamp(System.currentTimeMillis()));
        model.setQuantity(i % 10);
        model.setType((short) (i % 3));
        models.add(model);
    }
    KeyHolder holder = new KeyHolder();
    int[] counts = dao.insert(hints, holder, models);
    System.out.println("The insert ClientTestModel count: " + counts);
    generateIds = holder.getIdList();
    System.out.println("Ids: " + generateIds.size());
    Assert.assertEquals(INSERT_COUNT, generateIds.size());
}
Also used : DalHints(com.ctrip.platform.dal.dao.DalHints) StatementParameters(com.ctrip.platform.dal.dao.StatementParameters) ArrayList(java.util.ArrayList) KeyHolder(com.ctrip.platform.dal.dao.KeyHolder) Timestamp(java.sql.Timestamp) BeforeClass(org.junit.BeforeClass)

Example 9 with KeyHolder

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

the class DalTableDaoTestStub method testCombinedInsert.

/**
	 * Test Insert multiple entities with one SQL Statement
	 * @throws SQLException
	 */
@Test
public void testCombinedInsert() throws SQLException {
    if (!diff.supportInsertValues)
        return;
    List<ClientTestModel> entities = new ArrayList<ClientTestModel>();
    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 = diff.supportGetGeneratedKeys ? new KeyHolder() : null;
    DalHints hints = new DalHints();
    int res = dao.combinedInsert(hints, holder, entities);
    assertEquals(3, res, 4 + 3);
    if (diff.supportGetGeneratedKeys) {
        Assert.assertEquals(3, holder.size());
        Assert.assertTrue(holder.getKeyList().get(0).containsKey("GENERATED_KEY"));
    }
}
Also used : DalHints(com.ctrip.platform.dal.dao.DalHints) ClientTestModel(test.com.ctrip.platform.dal.dao.unitbase.ClientTestModel) ArrayList(java.util.ArrayList) KeyHolder(com.ctrip.platform.dal.dao.KeyHolder) Test(org.junit.Test)

Example 10 with KeyHolder

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

the class BaseDalTabelDaoShardByTableTest method testCrossShardInsertCallback.

@Test
public void testCrossShardInsertCallback() {
    if (!diff.supportInsertValues)
        return;
    try {
        deleteAllShards();
        ClientTestModel p = new ClientTestModel();
        ClientTestModel[] pList = new ClientTestModel[6];
        p = new ClientTestModel();
        p.setId(1);
        p.setAddress("aaa");
        p.setTableIndex(0);
        pList[0] = p;
        p = new ClientTestModel();
        p.setId(2);
        p.setAddress("aaa");
        p.setTableIndex(1);
        pList[1] = p;
        p = new ClientTestModel();
        p.setId(3);
        p.setAddress("aaa");
        p.setTableIndex(2);
        pList[2] = p;
        p = new ClientTestModel();
        p.setId(4);
        p.setAddress("aaa");
        p.setTableIndex(3);
        pList[3] = p;
        p = new ClientTestModel();
        p.setId(5);
        p.setAddress("aaa");
        p.setTableIndex(4);
        pList[4] = p;
        p = new ClientTestModel();
        p.setId(5);
        p.setAddress("aaa");
        p.setTableIndex(5);
        pList[5] = p;
        //new KeyHolder();
        KeyHolder keyHolder = null;
        DalHints hints = intHints();
        int res = dao.combinedInsert(hints, keyHolder, Arrays.asList(pList));
        res = assertInt(res, hints);
        assertEquals(2, getCount(0));
        assertEquals(2, getCount(1));
        assertEquals(1, getCount(2));
        assertEquals(1, getCount(3));
    } catch (Exception e) {
        fail();
    }
}
Also used : DalHints(com.ctrip.platform.dal.dao.DalHints) KeyHolder(com.ctrip.platform.dal.dao.KeyHolder) SQLException(java.sql.SQLException) Test(org.junit.Test)

Aggregations

KeyHolder (com.ctrip.platform.dal.dao.KeyHolder)42 Test (org.junit.Test)32 SQLException (java.sql.SQLException)28 DalHints (com.ctrip.platform.dal.dao.DalHints)27 ArrayList (java.util.ArrayList)13 Timestamp (java.sql.Timestamp)5 Map (java.util.Map)5 StatementParameters (com.ctrip.platform.dal.dao.StatementParameters)3 CombinedInsertTask (com.ctrip.platform.dal.dao.task.CombinedInsertTask)3 ExecutorService (java.util.concurrent.ExecutorService)2 LinkedBlockingQueue (java.util.concurrent.LinkedBlockingQueue)2 ThreadPoolExecutor (java.util.concurrent.ThreadPoolExecutor)2 BeforeClass (org.junit.BeforeClass)2 ClientTestModel (test.com.ctrip.platform.dal.dao.unitbase.ClientTestModel)2 DalColumnMapRowMapper (com.ctrip.platform.dal.dao.helper.DalColumnMapRowMapper)1 DalDefaultJpaParser (com.ctrip.platform.dal.dao.helper.DalDefaultJpaParser)1 DalRowMapperExtractor (com.ctrip.platform.dal.dao.helper.DalRowMapperExtractor)1 HashSet (java.util.HashSet)1 LinkedHashMap (java.util.LinkedHashMap)1 Future (java.util.concurrent.Future)1