Search in sources :

Example 31 with StatementParameters

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

the class BaseDalTableDaoShardByDbTest method testBatchUpdateMultiple.

@Test
public void testBatchUpdateMultiple() throws SQLException {
    DalHints hints = new DalHints();
    List<ClientTestModel> entities = new ArrayList<>();
    for (int i = 0; i < 3; i++) {
        ClientTestModel model = new ClientTestModel();
        model.setId(i + 1);
        model.setAddress("CTRIP");
        entities.add(model);
    }
    int[] ress;
    try {
        ress = dao.batchUpdate(hints, entities);
        fail();
    } catch (Exception e) {
    }
    // By fields same shard
    // holder = createKeyHolder();
    entities.get(0).setTableIndex(0);
    entities.get(0).setAddress("1234");
    entities.get(1).setTableIndex(0);
    entities.get(1).setAddress("1234");
    entities.get(2).setTableIndex(0);
    entities.get(2).setAddress("1234");
    ress = dao.batchUpdate(new DalHints(), entities);
    assertResEquals(3, ress);
    List<ClientTestModel> result = dao.query("1=1", new StatementParameters(), new DalHints().inShard(0));
    for (ClientTestModel m : result) assertEquals("1234", m.getAddress());
}
Also used : DalHints(com.ctrip.platform.dal.dao.DalHints) StatementParameters(com.ctrip.platform.dal.dao.StatementParameters) ArrayList(java.util.ArrayList) SQLException(java.sql.SQLException) Test(org.junit.Test)

Example 32 with StatementParameters

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

the class BaseDalTableDaoShardByDbTest method testUpdateMultipleAsyncCallback.

@Test
public void testUpdateMultipleAsyncCallback() throws SQLException {
    DalHints hints;
    IntCallback callback;
    hints = new DalHints();
    List<ClientTestModel> entities = new ArrayList<>();
    for (int i = 0; i < 3; i++) {
        ClientTestModel model = new ClientTestModel();
        model.setId(i + 1);
        model.setAddress("CTRIP");
        entities.add(model);
    }
    int[] ress;
    try {
        ress = dao.update(hints, entities);
        fail();
    } catch (Exception e) {
    }
    int res;
    // By shard
    hints = new DalHints().asyncExecution();
    entities.get(0).setAddress("test1");
    res = dao.update(hints.inShard(0), entities.get(0));
    assertEquals(0, res);
    res = getInt(hints);
    assertResEquals(1, res);
    assertEquals("test1", dao.queryByPk(1, new DalHints().inShard(0)).getAddress());
    // By shardValue
    callback = new IntCallback();
    hints = new DalHints().callbackWith(callback);
    entities.get(1).setQuantity(-11);
    res = dao.update(hints.setShardValue(1), entities.get(1));
    assertEquals(0, res);
    res = callback.getInt();
    assertResEquals(1, res);
    assertEquals(-11, dao.queryByPk(2, new DalHints().inShard(1)).getQuantity().intValue());
    // By shardColValue
    hints = new DalHints().asyncExecution();
    entities.get(2).setType((short) 3);
    res = dao.update(hints.setShardColValue("index", 2), entities.get(2));
    assertEquals(0, res);
    res = getInt(hints);
    assertResEquals(1, res);
    assertEquals((short) 3, dao.queryByPk(3, new DalHints().inShard(0)).getType().shortValue());
    // By shardColValue
    callback = new IntCallback();
    hints = new DalHints().callbackWith(callback);
    entities.get(0).setAddress("testa");
    res = dao.update(hints.setShardColValue("tableIndex", 3), entities.get(0));
    assertEquals(0, res);
    res = callback.getInt();
    assertResEquals(1, res);
    assertEquals("testa", dao.queryByPk(1, new DalHints().inShard(1)).getAddress());
    // By fields same shard
    // holder = createKeyHolder();
    hints = new DalHints().asyncExecution();
    entities.get(0).setTableIndex(0);
    entities.get(0).setAddress("1234");
    entities.get(1).setTableIndex(0);
    entities.get(1).setAddress("1234");
    entities.get(2).setTableIndex(0);
    entities.get(2).setAddress("1234");
    ress = dao.update(hints, entities);
    assertNull(ress);
    res = getInt(hints);
    assertResEquals(3, res);
    List<ClientTestModel> result = dao.query("1=1", new StatementParameters(), new DalHints().inShard(0));
    for (ClientTestModel m : result) assertEquals("1234", m.getAddress());
}
Also used : DalHints(com.ctrip.platform.dal.dao.DalHints) StatementParameters(com.ctrip.platform.dal.dao.StatementParameters) ArrayList(java.util.ArrayList) SQLException(java.sql.SQLException) Test(org.junit.Test)

Example 33 with StatementParameters

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

the class BaseDalTableDaoShardByDbTest method testDeleteWithWhereClauseShardsAsync.

@Test
public void testDeleteWithWhereClauseShardsAsync() throws SQLException {
    String whereClause = "type=?";
    StatementParameters parameters = new StatementParameters();
    parameters.set(1, Types.SMALLINT, 1);
    DalHints hints = new DalHints().asyncExecution();
    int res;
    // By shards
    Set<String> shards = new HashSet<>();
    shards.add("0");
    shards.add("1");
    assertEquals(3, getCountByDb(dao, 0));
    assertEquals(3, getCountByDb(dao, 1));
    res = dao.delete(whereClause, parameters, hints.inShards(shards));
    assertEquals(0, res);
    res = getInt(hints);
    assertResEquals(6, res);
    assertEquals(0, dao.query(whereClause, parameters, new DalHints().inShards(shards)).size());
}
Also used : DalHints(com.ctrip.platform.dal.dao.DalHints) StatementParameters(com.ctrip.platform.dal.dao.StatementParameters) HashSet(java.util.HashSet) Test(org.junit.Test)

Example 34 with StatementParameters

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

the class BaseDalTableDaoShardByDbTableTest method deleteAllShards.

public void deleteAllShards(int shardId) throws SQLException {
    for (int i = 0; i < tableMod; i++) {
        int j = 1;
        dao.delete("1=1", new StatementParameters(), new DalHints().inShard(shardId).inTableShard(i));
    }
}
Also used : DalHints(com.ctrip.platform.dal.dao.DalHints) StatementParameters(com.ctrip.platform.dal.dao.StatementParameters)

Example 35 with StatementParameters

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

the class BaseDalTableDaoShardByDbTest method testUpdatePlainAllShardsAsync.

@Test
public void testUpdatePlainAllShardsAsync() throws SQLException {
    String sql = "UPDATE " + TABLE_NAME + " SET address = 'CTRIP' WHERE id = 1";
    StatementParameters parameters = new StatementParameters();
    DalHints hints = new DalHints().asyncExecution();
    int res;
    // By allShards
    sql = "UPDATE " + TABLE_NAME + " SET address = 'CTRIP' WHERE id = 1";
    res = dao.update(sql, parameters, hints.inAllShards());
    assertEquals(0, res);
    res = getInt(hints);
    assertResEquals(2, res);
    assertEquals("CTRIP", dao.queryByPk(1, new DalHints().inShard(0)).getAddress());
    assertEquals("CTRIP", dao.queryByPk(1, new DalHints().inShard(1)).getAddress());
}
Also used : DalHints(com.ctrip.platform.dal.dao.DalHints) StatementParameters(com.ctrip.platform.dal.dao.StatementParameters) Test(org.junit.Test)

Aggregations

StatementParameters (com.ctrip.platform.dal.dao.StatementParameters)360 DalHints (com.ctrip.platform.dal.dao.DalHints)317 Test (org.junit.Test)209 SQLException (java.sql.SQLException)102 ArrayList (java.util.ArrayList)51 FreeSelectSqlBuilder (com.ctrip.platform.dal.dao.sqlbuilder.FreeSelectSqlBuilder)40 ClientTestModel (test.com.ctrip.platform.dal.dao.unitbase.ClientTestModel)40 List (java.util.List)29 FreeUpdateSqlBuilder (com.ctrip.platform.dal.dao.sqlbuilder.FreeUpdateSqlBuilder)20 DalQueryDao (com.ctrip.platform.dal.dao.DalQueryDao)19 DalTableDao (com.ctrip.platform.dal.dao.DalTableDao)17 After (org.junit.After)17 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)15 DalDefaultJpaParser (com.ctrip.platform.dal.dao.helper.DalDefaultJpaParser)14 ResultSet (java.sql.ResultSet)10 DalClient (com.ctrip.platform.dal.dao.DalClient)9 AfterClass (org.junit.AfterClass)9 BeforeClass (org.junit.BeforeClass)9 AbstractFreeSqlBuilder (com.ctrip.platform.dal.dao.sqlbuilder.AbstractFreeSqlBuilder)8 Map (java.util.Map)8