Search in sources :

Example 46 with StatementParameters

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

the class BaseDalTabelDaoShardByTableTest method testDeleteWithWhereClauseAsyncCallback.

@Test
public void testDeleteWithWhereClauseAsyncCallback() throws SQLException {
    DalHints hints;
    String whereClause = "type=?";
    StatementParameters parameters = new StatementParameters();
    parameters.set(1, Types.SMALLINT, 1);
    int res;
    try {
        hints = asyncHints();
        res = dao.delete(whereClause, parameters, hints);
        res = assertInt(res, hints);
        fail();
    } catch (Exception e) {
    }
    // By tabelShard
    hints = asyncHints();
    res = dao.delete(whereClause, parameters, hints.inTableShard(0));
    res = assertInt(res, hints);
    assertEquals(0, dao.query(whereClause, parameters, new DalHints().inTableShard(0)).size());
    // By tableShardValue
    assertEquals(2, getCount(1));
    hints = intHints();
    res = dao.delete(whereClause, parameters, hints.setTableShardValue(1));
    res = assertInt(res, hints);
    assertEquals(0, dao.query(whereClause, parameters, new DalHints().setTableShardValue(1)).size());
    // By shardColValue
    assertEquals(3, getCount(2));
    hints = asyncHints();
    res = dao.delete(whereClause, parameters, hints.setShardColValue("index", 2));
    res = assertInt(res, hints);
    assertEquals(0, dao.query(whereClause, parameters, new DalHints().setShardColValue("index", 2)).size());
    // By shardColValue
    assertEquals(4, getCount(3));
    hints = intHints();
    res = dao.delete(whereClause, parameters, hints.setShardColValue("tableIndex", 3));
    res = assertInt(res, hints);
    assertEquals(0, dao.query(whereClause, parameters, new DalHints().setShardColValue("tableIndex", 3)).size());
}
Also used : DalHints(com.ctrip.platform.dal.dao.DalHints) StatementParameters(com.ctrip.platform.dal.dao.StatementParameters) SQLException(java.sql.SQLException) Test(org.junit.Test)

Example 47 with StatementParameters

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

the class BaseDalTabelDaoShardByTableTest method deleteAllShards.

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

Example 48 with StatementParameters

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

the class BatchUpdateTaskTestStub method testDaoIncludeExcludeColumns.

@Test
public void testDaoIncludeExcludeColumns() throws SQLException {
    DalTableDao<UpdatableIntVersionModel> dao = new DalTableDao<>(UpdatableIntVersionModel.class, getDbName());
    DalHints hints = new DalHints().exclude("dbIndex").include("quantity", "dbIndex", "address");
    List<UpdatableIntVersionModel> pojos = dao.query("1=1", new StatementParameters(), new DalHints());
    for (UpdatableIntVersionModel model : pojos) {
        model.setQuantity(500);
        model.setDbIndex(100);
        model.setAddress("1122334455");
    }
    int[] result = dao.batchUpdate(hints, pojos);
    assertArrayEquals(new int[] { 1, 1, 1 }, result);
    pojos = dao.query("1=1", new StatementParameters(), new DalHints());
    for (UpdatableIntVersionModel model : pojos) {
        assertEquals("1122334455", model.getAddress());
        assertEquals(500, model.getQuantity().intValue());
        assertEquals(0, model.getDbIndex().intValue());
    }
}
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)

Example 49 with StatementParameters

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

the class InsertSqlBuilderTest method testBuildSqlsvr.

@Test
public void testBuildSqlsvr() throws SQLException {
    InsertSqlBuilder isb = new InsertSqlBuilder().from("table").setDatabaseCategory(DatabaseCategory.SqlServer);
    isb.set("f1", 1, Types.INTEGER);
    isb.setSensitive("f2", "abc", Types.VARBINARY);
    assertEquals("INSERT INTO [table] ([f1], [f2]) VALUES(?, ?)", isb.build());
    assertEquals("INSERT INTO [table_0] ([f1], [f2]) VALUES(?, ?)", isb.build("_0"));
    StatementParameters p = isb.buildParameters();
    assertEquals(2, p.size());
}
Also used : StatementParameters(com.ctrip.platform.dal.dao.StatementParameters) InsertSqlBuilder(com.ctrip.platform.dal.dao.sqlbuilder.InsertSqlBuilder) Test(org.junit.Test)

Example 50 with StatementParameters

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

the class InsertSqlBuilderTest method testBuildMySql.

@Test
public void testBuildMySql() throws SQLException {
    InsertSqlBuilder isb = new InsertSqlBuilder().from("table").setDatabaseCategory(DatabaseCategory.MySql);
    isb.set("f1", 1, Types.INTEGER);
    isb.setSensitive("f2", "abc", Types.VARBINARY);
    assertEquals("INSERT INTO `table` (`f1`, `f2`) VALUES(?, ?)", isb.build());
    assertEquals("INSERT INTO `table_0` (`f1`, `f2`) VALUES(?, ?)", isb.build("_0"));
    StatementParameters p = isb.buildParameters();
    assertEquals(2, p.size());
}
Also used : StatementParameters(com.ctrip.platform.dal.dao.StatementParameters) InsertSqlBuilder(com.ctrip.platform.dal.dao.sqlbuilder.InsertSqlBuilder) 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