Search in sources :

Example 1 with UpdatableEntity

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

the class BatchUpdateTask method execute.

@Override
public int[] execute(DalHints hints, Map<Integer, Map<String, ?>> daoPojos, DalBulkTaskContext<T> taskContext) throws SQLException {
    List<T> rawPojos = taskContext.getRawPojos();
    boolean isUpdatableEntity = taskContext.isUpdatableEntity();
    Map<String, Boolean> pojoFieldStatus = taskContext.getPojoFieldStatus();
    StatementParameters[] parametersList = new StatementParameters[daoPojos.size()];
    int i = 0;
    String[] updateColumnNames = pojoFieldStatus.keySet().toArray(new String[pojoFieldStatus.size()]);
    for (Integer index : daoPojos.keySet()) {
        Map<String, ?> pojo = daoPojos.get(index);
        StatementParameters parameters = new StatementParameters();
        if (isUpdatableEntity && !hints.isUpdateUnchangedField())
            addParameters(parameters, pojo, updateColumnNames, ((UpdatableEntity) rawPojos.get(index)).getUpdatedColumns());
        else
            addParameters(parameters, pojo, updateColumnNames);
        addParameters(parameters, pojo, parser.getPrimaryKeyNames());
        addVersion(parameters, pojo);
        parametersList[i++] = parameters;
    }
    String tableName = getRawTableName(hints);
    if (taskContext instanceof DalContextConfigure) {
        ((DalContextConfigure) taskContext).addTables(tableName);
        ((DalContextConfigure) taskContext).setShardingCategory(shardingCategory);
    }
    String batchUpdateSql = buildBatchUpdateSql(quote(tableName), pojoFieldStatus);
    int[] result;
    if (client instanceof DalContextClient)
        result = ((DalContextClient) client).batchUpdate(batchUpdateSql, parametersList, hints, taskContext);
    else
        throw new DalRuntimeException("The client is not instance of DalClient");
    return result;
}
Also used : DalContextClient(com.ctrip.platform.dal.dao.DalContextClient) UpdatableEntity(com.ctrip.platform.dal.dao.UpdatableEntity) StatementParameters(com.ctrip.platform.dal.dao.StatementParameters) DalRuntimeException(com.ctrip.platform.dal.exceptions.DalRuntimeException)

Example 2 with UpdatableEntity

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

the class DalDefaultJpaMapper method map.

@Override
public T map(ResultSet rs, int rowNum) throws SQLException {
    try {
        T instance = this.clazz.newInstance();
        for (int i = 0; i < columnNames.length; i++) {
            Field field = fieldsMap.get(columnNames[i]);
            if (field == null)
                if (ignorMissingFields)
                    continue;
                else
                    throw new DalException(ErrorCode.FieldNotExists, clazz.getName(), columnNames[i]);
            setValue(field, instance, rs, i);
        }
        if (instance instanceof UpdatableEntity)
            ((UpdatableEntity) instance).reset();
        return instance;
    } catch (Throwable e) {
        throw DalException.wrap(ErrorCode.ResultMappingError, e);
    }
}
Also used : Field(java.lang.reflect.Field) DalException(com.ctrip.platform.dal.exceptions.DalException) UpdatableEntity(com.ctrip.platform.dal.dao.UpdatableEntity)

Example 3 with UpdatableEntity

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

the class BatchUpdateTask method execute.

@Override
public int[] execute(DalHints hints, Map<Integer, Map<String, ?>> daoPojos, BulkTaskContext<T> taskContext) throws SQLException {
    List<T> rawPojos = taskContext.getRawPojos();
    boolean isUpdatableEntity = taskContext.isUpdatableEntity();
    Map<String, Boolean> pojoFieldStatus = taskContext.getPojoFieldStatus();
    StatementParameters[] parametersList = new StatementParameters[daoPojos.size()];
    int i = 0;
    String[] updateColumnNames = pojoFieldStatus.keySet().toArray(new String[pojoFieldStatus.size()]);
    for (Integer index : daoPojos.keySet()) {
        Map<String, ?> pojo = daoPojos.get(index);
        StatementParameters parameters = new StatementParameters();
        if (isUpdatableEntity && !hints.isUpdateUnchangedField())
            addParameters(parameters, pojo, updateColumnNames, ((UpdatableEntity) rawPojos.get(index)).getUpdatedColumns());
        else
            addParameters(parameters, pojo, updateColumnNames);
        addParameters(parameters, pojo, parser.getPrimaryKeyNames());
        addVersion(parameters, pojo);
        parametersList[i++] = parameters;
    }
    String batchUpdateSql = buildBatchUpdateSql(getTableName(hints), pojoFieldStatus);
    int[] result = client.batchUpdate(batchUpdateSql, parametersList, hints);
    return result;
}
Also used : UpdatableEntity(com.ctrip.platform.dal.dao.UpdatableEntity) StatementParameters(com.ctrip.platform.dal.dao.StatementParameters)

Example 4 with UpdatableEntity

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

the class SingleUpdateTask method execute.

@Override
public int execute(DalHints hints, Map<String, ?> fields, T rawPojo) 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 updateSql = buildUpdateSql(getTableName(hints, fields), filted, hints);
    StatementParameters parameters = new StatementParameters();
    addParameters(parameters, filted);
    addParameters(parameters, pks);
    addVersion(parameters, version);
    return client.update(updateSql, parameters, hints.setFields(fields));
}
Also used : DalException(com.ctrip.platform.dal.exceptions.DalException) UpdatableEntity(com.ctrip.platform.dal.dao.UpdatableEntity) StatementParameters(com.ctrip.platform.dal.dao.StatementParameters)

Example 5 with UpdatableEntity

use of com.ctrip.platform.dal.dao.UpdatableEntity 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)

Aggregations

UpdatableEntity (com.ctrip.platform.dal.dao.UpdatableEntity)5 StatementParameters (com.ctrip.platform.dal.dao.StatementParameters)4 DalException (com.ctrip.platform.dal.exceptions.DalException)3 DalContextClient (com.ctrip.platform.dal.dao.DalContextClient)2 DalRuntimeException (com.ctrip.platform.dal.exceptions.DalRuntimeException)2 Field (java.lang.reflect.Field)1