Search in sources :

Example 36 with StatementParameters

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

the class BaseDalTableDaoShardByDbTest method testBatchUpdateMultipleCallback.

public void testBatchUpdateMultipleCallback() throws SQLException {
    DalHints hints = new DalHints();
    List<ClientTestModel> entities = new ArrayList<>();
    for (int i = 0; i < 3; i++) {
        ClientTestModel model = new ClientTestModel();
        model.setId(i + 1);
        model.setAddress("CTRIP");
        entities.add(model);
    }
    int[] ress;
    try {
        ress = dao.batchUpdate(hints, entities);
        fail();
    } catch (Exception e) {
    }
    // By fields same shard
    // holder = createKeyHolder();
    entities.get(0).setTableIndex(0);
    entities.get(0).setAddress("1234");
    entities.get(1).setTableIndex(0);
    entities.get(1).setAddress("1234");
    entities.get(2).setTableIndex(0);
    entities.get(2).setAddress("1234");
    IntCallback callback = new IntCallback();
    hints = new DalHints().callbackWith(callback);
    ress = dao.batchUpdate(hints, entities);
    assertNull(ress);
    ress = callback.getIntArray();
    assertResEquals(3, ress);
    List<ClientTestModel> result = dao.query("1=1", new StatementParameters(), new DalHints().inShard(0));
    for (ClientTestModel m : result) assertEquals("1234", m.getAddress());
}
Also used : DalHints(com.ctrip.platform.dal.dao.DalHints) StatementParameters(com.ctrip.platform.dal.dao.StatementParameters) ArrayList(java.util.ArrayList) SQLException(java.sql.SQLException)

Example 37 with StatementParameters

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

the class AbstractSqlBuilder method buildParameters.

/**
	 * 获取StatementParameters
	 * @return
	 */
public StatementParameters buildParameters() {
    parameters = new StatementParameters();
    index = 1;
    for (FieldEntry entry : selectOrUpdataFieldEntrys) {
        parameters.add(new StatementParameter(index++, entry.getSqlType(), entry.getParamValue()).setSensitive(entry.isSensitive()).setName(entry.getFieldName()).setInParam(entry.isInParam()));
    }
    for (FieldEntry entry : whereFieldEntrys) {
        parameters.add(new StatementParameter(index++, entry.getSqlType(), entry.getParamValue()).setSensitive(entry.isSensitive()).setName(entry.getFieldName()).setInParam(entry.isInParam()));
    }
    return this.parameters;
}
Also used : StatementParameters(com.ctrip.platform.dal.dao.StatementParameters) StatementParameter(com.ctrip.platform.dal.dao.StatementParameter)

Example 38 with StatementParameters

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

the class InsertSqlBuilder method buildParameters.

public StatementParameters buildParameters() {
    StatementParameters parameters = new StatementParameters();
    int index = 1;
    for (FieldEntry entry : fieldEntrys) {
        if (entry.isSensitive())
            parameters.setSensitive(index++, entry.getFieldName(), entry.getSqlType(), entry.getParamValue());
        else
            parameters.set(index++, entry.getFieldName(), entry.getSqlType(), entry.getParamValue());
    }
    return parameters;
}
Also used : StatementParameters(com.ctrip.platform.dal.dao.StatementParameters)

Example 39 with StatementParameters

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

the class DalSqlTaskRequest method createTasks.

@Override
public Map<String, Callable<T>> createTasks() throws SQLException {
    Map<String, Callable<T>> tasks = new HashMap<>();
    if (parametersByShard == null) {
        // Create by given shards
        for (String shard : shards) {
            tasks.put(shard, create(parameters.duplicate(), hints.clone().inShard(shard)));
        }
    } else {
        // Create by sharded values
        for (Map.Entry<String, ?> shard : parametersByShard.entrySet()) {
            StatementParameters tempParameters = parameters.duplicateWith(hints.getShardBy(), (List) shard.getValue());
            tasks.put(shard.getKey(), create(tempParameters, hints.clone().inShard(shard.getKey())));
        }
    }
    return tasks;
}
Also used : HashMap(java.util.HashMap) StatementParameters(com.ctrip.platform.dal.dao.StatementParameters) HashMap(java.util.HashMap) Map(java.util.Map) Callable(java.util.concurrent.Callable)

Example 40 with StatementParameters

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

the class StatementParametersTest method testDuplicate.

@Test
public void testDuplicate() {
    StatementParameters test = new StatementParameters();
    test.set(1, "name1", Types.INTEGER, 1);
    test.set(2, "name2", Types.INTEGER, 2);
    StatementParameters test2 = test.duplicateWith("name1", 2);
    StatementParameters test3 = test.duplicateWith("name1", 3);
    assertEquals(2, test.size());
    assertEquals(2, test2.size());
    assertEquals(2, test3.size());
    assertEquals(1, test.get(0).getValue());
    assertEquals(2, test.get(1).getValue());
    assertEquals(2, test2.get(0).getValue());
    assertEquals(2, test2.get(1).getValue());
    assertEquals(3, test3.get(0).getValue());
    assertEquals(2, test3.get(1).getValue());
}
Also used : StatementParameters(com.ctrip.platform.dal.dao.StatementParameters) 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