Search in sources :

Example 6 with DalContextClient

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

the class SingleUpdateTask method execute.

@Override
public int execute(DalHints hints, Map<String, ?> fields, T rawPojo, DalTaskContext taskContext) throws SQLException {
    if (fields.size() == 0)
        throw new DalException(ErrorCode.ValidateFieldCount);
    Map<String, ?> pks = getPrimaryKeys(fields);
    Object version = getVersion(fields);
    Map<String, ?> filted = null;
    if (rawPojo instanceof UpdatableEntity)
        filted = filterUpdatableEntity(hints, fields, getUpdatedColumns(rawPojo));
    else
        filted = filterNullColumns(hints, fields);
    // If there is no columns changed, we will not perform update
    if (filted.size() == 0)
        return 0;
    String tableName = getRawTableName(hints, fields);
    if (taskContext instanceof DalContextConfigure)
        ((DalContextConfigure) taskContext).addTables(tableName);
    String updateSql = buildUpdateSql(quote(tableName), filted, hints);
    StatementParameters parameters = new StatementParameters();
    addParameters(parameters, filted);
    addParameters(parameters, pks);
    addVersion(parameters, version);
    if (client instanceof DalContextClient)
        return ((DalContextClient) client).update(updateSql, parameters, hints.setFields(fields), taskContext);
    else
        throw new DalRuntimeException("The client is not instance of DalClient");
}
Also used : DalRuntimeException(com.ctrip.platform.dal.exceptions.DalRuntimeException) DalContextClient(com.ctrip.platform.dal.dao.DalContextClient) DalException(com.ctrip.platform.dal.exceptions.DalException) UpdatableEntity(com.ctrip.platform.dal.dao.UpdatableEntity) StatementParameters(com.ctrip.platform.dal.dao.StatementParameters)

Example 7 with DalContextClient

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

the class AbstractCombinedInsertTask method execute.

@Override
public Integer execute(DalHints hints, Map<Integer, Map<String, ?>> daoPojos, DalBulkTaskContext<T> taskContext) throws SQLException {
    StatementParameters parameters = new StatementParameters();
    StringBuilder values = new StringBuilder();
    Set<String> unqualifiedColumns = taskContext.getUnqualifiedColumns();
    List<String> finalInsertableColumns = buildValidColumnsForInsert(unqualifiedColumns);
    String insertColumns = combineColumns(finalInsertableColumns, COLUMN_SEPARATOR);
    List<Map<String, Object>> identityFields = new ArrayList<>();
    int startIndex = 1;
    for (Integer index : daoPojos.keySet()) {
        Map<String, ?> pojo = daoPojos.get(index);
        removeUnqualifiedColumns(pojo, unqualifiedColumns);
        Map<String, Object> identityField = getIdentityField(pojo);
        if (identityField != null) {
            identityFields.add(identityField);
        }
        int paramCount = addParameters(startIndex, parameters, pojo, finalInsertableColumns);
        startIndex += paramCount;
        values.append(String.format("(%s),", combine("?", paramCount, ",")));
    }
    // Put identityFields and pojos count into context
    if (taskContext instanceof DefaultTaskContext) {
        ((DefaultTaskContext) taskContext).setIdentityFields(identityFields);
        ((DefaultTaskContext) taskContext).setPojosCount(daoPojos.size());
    }
    String tableName = getRawTableName(hints);
    if (taskContext instanceof DalContextConfigure) {
        ((DalContextConfigure) taskContext).addTables(tableName);
        ((DalContextConfigure) taskContext).setShardingCategory(shardingCategory);
    }
    String sql = String.format(getSqlTpl(), quote(tableName), insertColumns, values.substring(0, values.length() - 2) + ")");
    if (client instanceof DalContextClient)
        return ((DalContextClient) client).update(sql, parameters, hints, taskContext);
    else
        throw new DalRuntimeException("The client is not instance of DalClient");
}
Also used : DalContextClient(com.ctrip.platform.dal.dao.DalContextClient) StatementParameters(com.ctrip.platform.dal.dao.StatementParameters) ArrayList(java.util.ArrayList) DalRuntimeException(com.ctrip.platform.dal.exceptions.DalRuntimeException) Map(java.util.Map)

Example 8 with DalContextClient

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

the class AbstractBatchInsertTask method execute.

@Override
public int[] execute(DalHints hints, Map<Integer, Map<String, ?>> daoPojos, DalBulkTaskContext<T> taskContext) throws SQLException {
    StatementParameters[] parametersList = new StatementParameters[daoPojos.size()];
    int i = 0;
    Set<String> unqualifiedColumns = taskContext.getUnqualifiedColumns();
    for (Integer index : daoPojos.keySet()) {
        Map<String, ?> pojo = daoPojos.get(index);
        removeUnqualifiedColumns(pojo, unqualifiedColumns);
        StatementParameters parameters = new StatementParameters();
        addParameters(parameters, pojo);
        parametersList[i++] = parameters;
    }
    String tableName = getRawTableName(hints);
    if (taskContext instanceof DalContextConfigure) {
        ((DalContextConfigure) taskContext).addTables(tableName);
        ((DalContextConfigure) taskContext).setShardingCategory(shardingCategory);
    }
    String batchInsertSql = buildBatchInsertSql(hints, unqualifiedColumns, tableName);
    int[] result;
    if (client instanceof DalContextClient)
        result = ((DalContextClient) client).batchUpdate(batchInsertSql, parametersList, hints, taskContext);
    else
        throw new DalRuntimeException("The client is not instance of DalClient");
    return result;
}
Also used : DalRuntimeException(com.ctrip.platform.dal.exceptions.DalRuntimeException) DalContextClient(com.ctrip.platform.dal.dao.DalContextClient) StatementParameters(com.ctrip.platform.dal.dao.StatementParameters)

Aggregations

DalContextClient (com.ctrip.platform.dal.dao.DalContextClient)8 StatementParameters (com.ctrip.platform.dal.dao.StatementParameters)8 DalRuntimeException (com.ctrip.platform.dal.exceptions.DalRuntimeException)8 UpdatableEntity (com.ctrip.platform.dal.dao.UpdatableEntity)2 ArrayList (java.util.ArrayList)2 Map (java.util.Map)2 DalException (com.ctrip.platform.dal.exceptions.DalException)1