Search in sources :

Example 16 with StatementParameters

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

the class DalTableDaoShardByDbTableSqlSvrTest method tearDown.

@After
public void tearDown() throws SQLException {
    String sql = "DELETE FROM " + TABLE_NAME;
    StatementParameters parameters = new StatementParameters();
    DalHints hints = new DalHints();
    sql = "DELETE FROM " + TABLE_NAME;
    parameters = new StatementParameters();
    hints = new DalHints();
    try {
        for (int j = 0; j < mod; j++) {
            for (int i = 0; i < tableMod; i++) {
                clientSqlSvr.update(sql + "_" + i, parameters, hints.inShard(j));
            }
        }
    } catch (SQLException e) {
        e.printStackTrace();
    }
}
Also used : DalHints(com.ctrip.platform.dal.dao.DalHints) SQLException(java.sql.SQLException) StatementParameters(com.ctrip.platform.dal.dao.StatementParameters) After(org.junit.After)

Example 17 with StatementParameters

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

the class BaseDalTableDaoShardByDbTableTest method testQueryTopWithWhereClause.

/**
	 * Test Query the top rows with where clause
	 * @throws SQLException
	 */
private void testQueryTopWithWhereClause(int shardId, DalHints oldhints) throws SQLException {
    List<ClientTestModel> models = null;
    DalHints hints;
    for (int i = 0; i < tableMod; i++) {
        String whereClause = "type=?";
        StatementParameters parameters = new StatementParameters();
        parameters.set(1, Types.SMALLINT, 1);
        // By tabelShard
        hints = copy(oldhints);
        models = dao.queryTop(whereClause, parameters, hints.inTableShard(i), i + 1);
        models = assertModels(models, hints);
        assertQueryX(shardId, models, i);
        // By tableShardValue
        hints = copy(oldhints);
        models = dao.queryTop(whereClause, parameters, hints.setTableShardValue(i), i + 1);
        models = assertModels(models, hints);
        assertQueryX(shardId, models, i);
        // By shardColValue
        hints = copy(oldhints);
        models = dao.queryTop(whereClause, parameters, hints.setShardColValue("table", i), i + 1);
        models = assertModels(models, hints);
        assertQueryX(shardId, models, i);
        // By shardColValue
        hints = copy(oldhints);
        models = dao.queryTop(whereClause, parameters, hints.setShardColValue("tableIndex", i), i + 1);
        models = assertModels(models, hints);
        assertQueryX(shardId, models, i);
        whereClause += " and tableIndex=? and dbIndex=?";
        // By parameters
        parameters = new StatementParameters();
        parameters.set(1, "type", Types.SMALLINT, 1);
        parameters.set(2, "tableIndex", Types.SMALLINT, i);
        parameters.set(3, "dbIndex", Types.SMALLINT, shardId);
        hints = copy(oldhints);
        models = dao.queryTop(whereClause, parameters, hints, i + 1);
        models = assertModels(models, hints);
        assertQueryX(shardId, models, i);
    }
}
Also used : DalHints(com.ctrip.platform.dal.dao.DalHints) StatementParameters(com.ctrip.platform.dal.dao.StatementParameters)

Example 18 with StatementParameters

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

the class BaseDalTabelDaoShardByTableTest method testQueryTopWithWhereClauseFailedCallback.

@Test
public void testQueryTopWithWhereClauseFailedCallback() throws SQLException {
    String whereClause = "type=?";
    StatementParameters parameters = new StatementParameters();
    parameters.set(1, Types.SMALLINT, 10);
    List<ClientTestModel> models;
    DefaultResultCallback callback = new DefaultResultCallback();
    DalHints hints = new DalHints().callbackWith(callback);
    models = dao.queryTop(whereClause, parameters, hints, 2);
    try {
        callback.waitForDone();
    } catch (InterruptedException e) {
        e.printStackTrace();
    }
    assertTrue(!callback.isSuccess());
    assertNull(callback.getResult());
    assertNotNull(callback.getError());
    models = dao.queryTop(whereClause, parameters, new DalHints().inTableShard(1), 2);
    assertTrue(null != models);
    assertEquals(0, models.size());
}
Also used : DalHints(com.ctrip.platform.dal.dao.DalHints) DefaultResultCallback(com.ctrip.platform.dal.dao.helper.DefaultResultCallback) StatementParameters(com.ctrip.platform.dal.dao.StatementParameters) Test(org.junit.Test)

Example 19 with StatementParameters

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

the class BaseDalTabelDaoShardByTableTest method testDeleteMultiple.

/**
	 * Test delete multiple entities
	 * @throws SQLException
	 */
@Test
public void testDeleteMultiple() throws SQLException {
    List<ClientTestModel> entities = new ArrayList<>();
    for (int i = 0; i < 3; i++) {
        ClientTestModel model = new ClientTestModel();
        model.setId(i + 1);
        model.setQuantity(10 + 1 % 3);
        model.setType(((Number) (1 % 3)).shortValue());
        model.setAddress("CTRIP");
        entities.add(model);
    }
    int[] res;
    // By tabelShard
    assertEquals(1, getCount(0));
    res = dao.delete(new DalHints().inTableShard(0), entities);
    assertEquals(0, getCount(0));
    // By tableShardValue
    assertEquals(2, getCount(1));
    res = dao.delete(new DalHints().setTableShardValue(1), entities);
    assertEquals(0, getCount(1));
    // By shardColValue
    assertEquals(3, getCount(2));
    res = dao.delete(new DalHints().setShardColValue("index", 2), entities);
    assertEquals(0, getCount(2));
    // By shardColValue
    assertEquals(4, getCount(3));
    res = dao.delete(new DalHints().setShardColValue("tableIndex", 3), entities);
    assertEquals(1, getCount(3));
    // By fields same shard
    // holder = new KeyHolder();
    entities.get(0).setTableIndex(0);
    entities.get(1).setTableIndex(1);
    entities.get(2).setTableIndex(2);
    dao.insert(new DalHints(), entities);
    assertEquals(1, getCount(0));
    assertEquals(1, getCount(1));
    assertEquals(1, getCount(2));
    entities.set(0, dao.queryFirst("1=1", new StatementParameters(), new DalHints().inTableShard(0)));
    entities.set(1, dao.queryFirst("1=1", new StatementParameters(), new DalHints().inTableShard(1)));
    entities.set(2, dao.queryFirst("1=1", new StatementParameters(), new DalHints().inTableShard(2)));
    res = dao.delete(new DalHints(), entities);
    assertEquals(0, getCount(0));
    assertEquals(0, getCount(1));
    assertEquals(0, getCount(2));
}
Also used : DalHints(com.ctrip.platform.dal.dao.DalHints) StatementParameters(com.ctrip.platform.dal.dao.StatementParameters) ArrayList(java.util.ArrayList) Test(org.junit.Test)

Example 20 with StatementParameters

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

the class BaseDalTabelDaoShardByTableTest method testUpdatePlain.

/**
	 * Test plain update with SQL
	 * @throws SQLException
	 */
@Test
public void testUpdatePlain() throws SQLException {
    String sql = "UPDATE " + TABLE_NAME + " SET address = 'CTRIP' WHERE id = 1";
    StatementParameters parameters = new StatementParameters();
    DalHints hints = new DalHints();
    int res;
    try {
        res = dao.update(sql, parameters, hints);
        fail();
    } catch (Exception e) {
    }
    // By tabelShard
    UpdateSqlBuilder usb = new UpdateSqlBuilder(TABLE_NAME, dao.getDatabaseCategory());
    usb.update("address", "CTRIP", Types.VARCHAR);
    usb.equal("id", "1", Types.INTEGER);
    res = dao.update(usb, new DalHints().inTableShard(0));
    assertEquals("CTRIP", dao.queryByPk(1, new DalHints().inTableShard(0)).getAddress());
    // By tableShardValue
    assertEquals(2, getCount(1));
    res = dao.update(usb, new DalHints().setTableShardValue(1));
    assertEquals("CTRIP", dao.queryByPk(1, new DalHints().setTableShardValue(1)).getAddress());
    // By shardColValue
    assertEquals(3, getCount(2));
    res = dao.update(usb, new DalHints().setShardColValue("index", 2));
    assertEquals("CTRIP", dao.queryByPk(1, new DalHints().setShardColValue("index", 2)).getAddress());
    // By shardColValue
    assertEquals(4, getCount(3));
    res = dao.update(usb, new DalHints().setShardColValue("tableIndex", 3));
    assertEquals("CTRIP", dao.queryByPk(1, new DalHints().setShardColValue("tableIndex", 3)).getAddress());
}
Also used : DalHints(com.ctrip.platform.dal.dao.DalHints) StatementParameters(com.ctrip.platform.dal.dao.StatementParameters) UpdateSqlBuilder(com.ctrip.platform.dal.dao.sqlbuilder.UpdateSqlBuilder) SQLException(java.sql.SQLException) Test(org.junit.Test)

Aggregations

StatementParameters (com.ctrip.platform.dal.dao.StatementParameters)276 DalHints (com.ctrip.platform.dal.dao.DalHints)245 Test (org.junit.Test)193 SQLException (java.sql.SQLException)97 ClientTestModel (test.com.ctrip.platform.dal.dao.unitbase.ClientTestModel)40 ArrayList (java.util.ArrayList)32 After (org.junit.After)17 DalTableDao (com.ctrip.platform.dal.dao.DalTableDao)16 DalDefaultJpaParser (com.ctrip.platform.dal.dao.helper.DalDefaultJpaParser)13 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)11 ResultSet (java.sql.ResultSet)10 DalClient (com.ctrip.platform.dal.dao.DalClient)9 BatchUpdateTask (com.ctrip.platform.dal.dao.task.BatchUpdateTask)8 DalQueryDao (com.ctrip.platform.dal.dao.DalQueryDao)7 DalResultSetExtractor (com.ctrip.platform.dal.dao.DalResultSetExtractor)7 Map (java.util.Map)7 AfterClass (org.junit.AfterClass)7 BeforeClass (org.junit.BeforeClass)7 DalCommand (com.ctrip.platform.dal.dao.DalCommand)6 HashSet (java.util.HashSet)6