Search in sources :

Example 26 with StatementParameters

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

the class BaseDalTableDaoShardByDbTableTest method testQueryTopWithWhereClauseFailed.

private void testQueryTopWithWhereClauseFailed(DalHints hints) throws SQLException {
    String whereClause = "type=?";
    StatementParameters parameters = new StatementParameters();
    parameters.set(1, Types.SMALLINT, 10);
    List<ClientTestModel> models;
    try {
        hints = copy(hints);
        models = dao.queryTop(whereClause, parameters, hints, 2);
        models = assertModels(models, hints);
        Assert.fail();
    } catch (Exception e) {
    }
    models = dao.queryTop(whereClause, parameters, new DalHints().inTableShard(1).inShard(0), 2);
    Assert.assertTrue(null != models);
    Assert.assertEquals(0, models.size());
}
Also used : DalHints(com.ctrip.platform.dal.dao.DalHints) StatementParameters(com.ctrip.platform.dal.dao.StatementParameters) SQLException(java.sql.SQLException)

Example 27 with StatementParameters

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

the class BaseDalTableDaoShardByDbTableTest method testQueryWithWhereClause.

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

Example 28 with StatementParameters

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

the class BaseDalTableDaoShardByDbTableTest method testDeleteWithWhereClauseFail.

/**
	 * Test delete entities with where clause and parameters
	 * @throws SQLException
	 */
@Test
public void testDeleteWithWhereClauseFail() throws SQLException {
    String whereClause = "type=?";
    StatementParameters parameters = new StatementParameters();
    parameters.set(1, Types.SMALLINT, 1);
    int res;
    try {
        DalHints hints = new DalHints();
        res = dao.delete(whereClause, parameters, hints);
        Assert.fail();
    } catch (Exception e) {
    }
    //Async
    try {
        DalHints hints = asyncHints();
        res = dao.delete(whereClause, parameters, hints);
        res = assertInt(res, hints);
        Assert.fail();
    } catch (Exception e) {
    }
    //Callback
    try {
        DalHints hints = intHints();
        res = dao.delete(whereClause, parameters, hints);
        res = assertInt(res, hints);
        Assert.fail();
    } catch (Exception e) {
    }
}
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 29 with StatementParameters

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

the class DalConcurrentMysqlTest method testQueryById.

@Test
public void testQueryById() throws SQLException {
    int maxId = random.nextInt(INSERT_COUNT);
    List<ClientTestModel> models = dao.query("Id > " + generateIds.get(maxId).intValue(), new StatementParameters(), new DalHints());
    System.out.println(models.size());
}
Also used : DalHints(com.ctrip.platform.dal.dao.DalHints) StatementParameters(com.ctrip.platform.dal.dao.StatementParameters) Test(org.junit.Test)

Example 30 with StatementParameters

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

the class DalConcurrentMysqlTest method testUpdate.

@Test
public void testUpdate() throws SQLException {
    int id = random.nextInt(INSERT_COUNT);
    String updateSql = "UPDATE " + TABLE_NAME + " SET address = ? WHERE id = ?";
    StatementParameters param = new StatementParameters();
    if (id % 2 == 0)
        param.set(1, Types.VARCHAR, "BJ-" + id);
    else
        param.set(1, Types.VARCHAR, "SH-" + id);
    param.set(2, Types.INTEGER, id);
    int count = dao.update(updateSql, param, new DalHints());
    System.out.println("update souccess: " + id);
    Assert.assertEquals(1, count);
}
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)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