Search in sources :

Example 1 with StatementParameter

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

the class DalDirectClient method call.

@Override
public Map<String, ?> call(String callString, StatementParameters parameters, final DalHints hints) throws SQLException {
    ConnectionAction<Map<String, ?>> action = new ConnectionAction<Map<String, ?>>() {

        @Override
        public Map<String, ?> execute() throws Exception {
            List<StatementParameter> resultParameters = new ArrayList<StatementParameter>();
            List<StatementParameter> callParameters = new ArrayList<StatementParameter>();
            for (StatementParameter parameter : parameters.values()) {
                if (parameter.isResultsParameter()) {
                    resultParameters.add(parameter);
                } else if (parameter.isOutParameter()) {
                    callParameters.add(parameter);
                }
            }
            if (hints.is(DalHintEnum.retrieveAllSpResults) && resultParameters.size() > 0)
                throw new DalException("Dal hint 'autoRetrieveAllResults' should only be used when there is no special result parameter specified");
            conn = getConnection(hints, this);
            callableStatement = createCallableStatement(conn, callString, parameters, hints);
            DalWatcher.beginExecute();
            boolean retVal = callableStatement.execute();
            int updateCount = callableStatement.getUpdateCount();
            DalWatcher.endExectue();
            Map<String, Object> returnedResults = new LinkedHashMap<String, Object>();
            if (retVal || updateCount != -1) {
                returnedResults.putAll(extractReturnedResults(callableStatement, resultParameters, updateCount, hints));
            }
            returnedResults.putAll(extractOutputParameters(callableStatement, callParameters));
            return returnedResults;
        }
    };
    action.populateSp(callString, parameters);
    return doInConnection(action, hints);
}
Also used : ArrayList(java.util.ArrayList) StatementParameter(com.ctrip.platform.dal.dao.StatementParameter) LinkedHashMap(java.util.LinkedHashMap) DalException(com.ctrip.platform.dal.exceptions.DalException) LinkedHashMap(java.util.LinkedHashMap) Map(java.util.Map)

Example 2 with StatementParameter

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

the class DalDirectClient method extractOutputParameters.

private Map<String, Object> extractOutputParameters(CallableStatement statement, List<StatementParameter> callParameters) throws SQLException {
    Map<String, Object> returnedResults = new LinkedHashMap<String, Object>();
    for (StatementParameter parameter : callParameters) {
        Object value = parameter.getName() == null ? statement.getObject(parameter.getIndex()) : statement.getObject(parameter.getName());
        parameter.setValue(value);
        if (value instanceof ResultSet) {
            value = parameter.getResultSetExtractor().extract(statement.getResultSet());
        }
        returnedResults.put(parameter.getName(), value);
    }
    return returnedResults;
}
Also used : ResultSet(java.sql.ResultSet) StatementParameter(com.ctrip.platform.dal.dao.StatementParameter) LinkedHashMap(java.util.LinkedHashMap)

Example 3 with StatementParameter

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

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

the class DalSqlTaskRequest method getShards.

private Set<String> getShards() throws SQLException {
    Set<String> shards = null;
    if (!DalShardingHelper.isShardingEnabled(logicDbName))
        return null;
    if (hints.isAllShards()) {
        shards = DalClientFactory.getDalConfigure().getDatabaseSet(logicDbName).getAllShards();
    } else if (hints.isInShards()) {
        shards = (Set<String>) hints.get(DalHintEnum.shards);
    } else if (hints.isShardBy()) {
        // The new code gen will set hints shardBy to indicate this is a potential cross shard operation
        // Check parameters. It can only surpport DB shard at this level
        StatementParameter parameter = parameters.get(hints.getShardBy(), ParameterDirection.Input);
        parametersByShard = DalShardingHelper.shuffle(logicDbName, (List) parameter.getValue());
        shards = parametersByShard.keySet();
    }
    if (shards != null && shards.size() > 1)
        logger.warn("Execute on multiple shards detected: " + builder.build());
    return shards;
}
Also used : Set(java.util.Set) StatementParameter(com.ctrip.platform.dal.dao.StatementParameter) List(java.util.List)

Aggregations

StatementParameter (com.ctrip.platform.dal.dao.StatementParameter)4 LinkedHashMap (java.util.LinkedHashMap)2 StatementParameters (com.ctrip.platform.dal.dao.StatementParameters)1 DalException (com.ctrip.platform.dal.exceptions.DalException)1 ResultSet (java.sql.ResultSet)1 ArrayList (java.util.ArrayList)1 List (java.util.List)1 Map (java.util.Map)1 Set (java.util.Set)1