Search in sources :

Example 51 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 52 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 53 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 54 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 55 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)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