Search in sources :

Example 1 with DalContextClient

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

the class AbstractSingleInsertTask method execute.

@Override
public int execute(DalHints hints, Map<String, ?> fields, T rawPojo, DalTaskContext taskContext) throws SQLException {
    List<Map<String, ?>> pojoList = new ArrayList<Map<String, ?>>();
    List<T> rawPojos = new ArrayList<>();
    pojoList.add(fields);
    rawPojos.add(rawPojo);
    Set<String> unqualifiedColumns = filterUnqualifiedColumns(hints, pojoList, rawPojos);
    removeUnqualifiedColumns(fields, unqualifiedColumns);
    // Put identityFields into context
    List<Map<String, Object>> identityFields = new ArrayList<>();
    Map<String, Object> identityField = getIdentityField(fields);
    if (identityField != null) {
        identityFields.add(identityField);
    }
    if (taskContext instanceof DefaultTaskContext) {
        ((DefaultTaskContext) taskContext).setIdentityFields(identityFields);
        ((DefaultTaskContext) taskContext).setPojosCount(1);
    }
    String tableName = getRawTableName(hints, fields);
    if (taskContext instanceof DalContextConfigure)
        ((DalContextConfigure) taskContext).addTables(tableName);
    /*
         * In case fields is empty, the final sql will be like "insert into tableName () values()".
         * We do not report error or simply return 0, but just let DB decide what to do.
         * For MS Sql server, sql like this is illegal, but for mysql, this works however
         */
    String insertSql = buildInsertSql(hints, fields, tableName);
    StatementParameters parameters = new StatementParameters();
    addParameters(parameters, fields);
    if (client instanceof DalContextClient)
        return ((DalContextClient) client).update(insertSql, 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 2 with DalContextClient

use of com.ctrip.platform.dal.dao.DalContextClient 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 3 with DalContextClient

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

the class CombinedDeleteTask 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();
    List<String> pkColumns = clonePkColumns();
    int startIndex = 1;
    for (Integer index : daoPojos.keySet()) {
        Map<String, ?> daoPojo = daoPojos.get(index);
        int paramCount = addParameters(startIndex, parameters, daoPojo, pkColumns);
        startIndex += paramCount;
        values.append(String.format(IN_TEMPLATE, combine("?", paramCount, ",")));
    }
    String tableName = getRawTableName(hints);
    if (taskContext instanceof DalContextConfigure) {
        ((DalContextConfigure) taskContext).addTables(tableName);
        ((DalContextConfigure) taskContext).setShardingCategory(shardingCategory);
    }
    String sql = String.format(TMPL_COMBINED_SQL_DELETE, quote(tableName), combineColumns(pkColumns, COLUMN_SEPARATOR), 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 : DalRuntimeException(com.ctrip.platform.dal.exceptions.DalRuntimeException) DalContextClient(com.ctrip.platform.dal.dao.DalContextClient) StatementParameters(com.ctrip.platform.dal.dao.StatementParameters)

Example 4 with DalContextClient

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

the class BatchDeleteTask method execute.

@Override
public int[] execute(DalHints hints, Map<Integer, Map<String, ?>> daoPojos, DalBulkTaskContext<T> taskContext) throws SQLException {
    StatementParameters[] parametersList = new StatementParameters[daoPojos.size()];
    List<String> pkNames = Arrays.asList(parser.getPrimaryKeyNames());
    int i = 0;
    for (Integer index : daoPojos.keySet()) {
        StatementParameters parameters = new StatementParameters();
        addParameters(1, parameters, daoPojos.get(index), pkNames);
        parametersList[i++] = parameters;
    }
    String tableName = getRawTableName(hints);
    if (taskContext instanceof DalContextConfigure)
        ((DalContextConfigure) taskContext).addTables(tableName);
    String deleteSql = buildDeleteSql(quote(tableName));
    int[] result;
    if (client instanceof DalContextClient)
        result = ((DalContextClient) client).batchUpdate(deleteSql, 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)

Example 5 with DalContextClient

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

the class SingleDeleteTask method execute.

@Override
public int execute(DalHints hints, Map<String, ?> fields, T rawPojo, DalTaskContext taskContext) throws SQLException {
    StatementParameters parameters = new StatementParameters();
    addParameters(parameters, fields, parser.getPrimaryKeyNames());
    String tableName = getRawTableName(hints, parameters, fields);
    if (taskContext instanceof DalContextConfigure)
        ((DalContextConfigure) taskContext).addTables(tableName);
    String deleteSql = buildDeleteSql(quote(tableName));
    if (client instanceof DalContextClient)
        return ((DalContextClient) client).update(deleteSql, 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) 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