Search in sources :

Example 91 with StatementParameters

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

the class BatchUpdateTaskTestStub method testNotUpdatableVersion.

@Test
public void testNotUpdatableVersion() throws SQLException {
    BatchUpdateTask<NonUpdatableVersionModel> test = new BatchUpdateTask<>();
    DalParser<NonUpdatableVersionModel> parser = new DalDefaultJpaParser<>(NonUpdatableVersionModel.class, getDbName());
    test.initialize(parser);
    DalTableDao<NonUpdatableVersionModel> dao = new DalTableDao<>(parser);
    DalHints hints = new DalHints();
    List<NonUpdatableVersionModel> pojos = dao.query("1=1", new StatementParameters(), new DalHints());
    for (NonUpdatableVersionModel model : pojos) {
        model.setAddress("1122334455");
    }
    int[] result = execute(test, hints, pojos);
    assertArrayEquals(new int[] { 1, 1, 1 }, result);
    pojos = dao.query("1=1", new StatementParameters(), new DalHints());
    for (NonUpdatableVersionModel model : pojos) assertEquals("1122334455", model.getAddress());
}
Also used : DalHints(com.ctrip.platform.dal.dao.DalHints) StatementParameters(com.ctrip.platform.dal.dao.StatementParameters) DalDefaultJpaParser(com.ctrip.platform.dal.dao.helper.DalDefaultJpaParser) BatchUpdateTask(com.ctrip.platform.dal.dao.task.BatchUpdateTask) DalTableDao(com.ctrip.platform.dal.dao.DalTableDao) Test(org.junit.Test)

Example 92 with StatementParameters

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

the class DalSqlTaskRequestTest method testCreateTasksByInParameters.

@Test
public void testCreateTasksByInParameters() {
    DalSqlTaskRequest<Integer> test = null;
    try {
        // Shard by in parameter
        DalHints hints = new DalHints();
        hints.shardBy("index");
        StatementParameters p = new StatementParameters();
        p.set(1, 1);
        List<Integer> l = new ArrayList<>();
        l.add(1);
        l.add(2);
        l.add(3);
        l.add(4);
        p.setInParameter(2, "index", Types.INTEGER, l);
        p.set(3, 1);
        test = new DalSqlTaskRequest<>("dao_test_sqlsvr_dbShard", new TestSqlBuilder("select * from tablea where id = ? and id in (?) and id = ?", p), hints, new TestSqlTask(1), new ResultMerger.IntSummary());
        Map<String, Callable<Integer>> tasks = test.createTasks();
        int i = 0;
        for (Callable<Integer> task : tasks.values()) {
            i += task.call().intValue();
        }
        assertEquals(2, i);
    } catch (Exception e) {
        fail();
    }
    assertNotNull(test.getMerger());
}
Also used : DalHints(com.ctrip.platform.dal.dao.DalHints) StatementParameters(com.ctrip.platform.dal.dao.StatementParameters) ArrayList(java.util.ArrayList) Callable(java.util.concurrent.Callable) SQLException(java.sql.SQLException) Test(org.junit.Test)

Example 93 with StatementParameters

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

the class BatchUpdateTaskTestStub method testDaoExcludeColumns.

@Test
public void testDaoExcludeColumns() throws SQLException {
    DalTableDao<UpdatableIntVersionModel> dao = new DalTableDao<>(UpdatableIntVersionModel.class, getDbName());
    DalHints hints = new DalHints().exclude("dbIndex");
    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 94 with StatementParameters

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

the class DalStatementCreator method createCallableStatement.

public CallableStatement createCallableStatement(Connection conn, String sql, StatementParameters[] parametersList, DalHints hints) throws Exception {
    CallableStatement statement = conn.prepareCall(sql);
    applyHints(statement, hints);
    for (StatementParameters parameters : parametersList) {
        setParameter(statement, parameters);
        statement.addBatch();
    }
    return statement;
}
Also used : CallableStatement(java.sql.CallableStatement) StatementParameters(com.ctrip.platform.dal.dao.StatementParameters)

Example 95 with StatementParameters

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

the class DalStatementCreator method createPreparedStatement.

public PreparedStatement createPreparedStatement(Connection conn, String sql, StatementParameters[] parametersList, DalHints hints) throws Exception {
    PreparedStatement statement = conn.prepareStatement(sql, getResultSetType(hints), getResultSetConcurrency(hints));
    applyHints(statement, hints);
    for (StatementParameters parameters : parametersList) {
        setParameter(statement, parameters);
        statement.addBatch();
    }
    return statement;
}
Also used : StatementParameters(com.ctrip.platform.dal.dao.StatementParameters) PreparedStatement(java.sql.PreparedStatement)

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