Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add execute(Object[] values) method to StoredProcedure [SPR-5696] #10366

Closed
spring-projects-issues opened this issue Apr 23, 2009 · 0 comments
Closed
Labels
in: data Issues in data modules (jdbc, orm, oxm, tx) type: enhancement A general enhancement
Milestone

Comments

@spring-projects-issues
Copy link
Collaborator

Celal Ziftci opened SPR-5696 and commented

Currently there are methods that take in "Object[]" for the execution of operations in most of the jdbc.object classes, such as:

  • SqlUpdate.update(Object[])
  • MappingSqlQuery.execute(Object[])

It would be very beneficial to have a similar method in StoredProcedure as "execute(Object[])".

I understand that there is a distinction in terms of update or query, and stored procedures in that stored procs can have a mixture of input and output params. However, this can be handled quite easily by using the already existing metadata provided to construct the StoredProcedure.

Below is a sample implementation:

/**
     * Avoids having to use a Map, hence to duplicate the keys of the map, which are the parameter names already
     * provided while registering the parameters to the {@link StoredProcedure}.
     * 
     * @param values
     *            the values for the <b>input</b> parameters defined on this stored procedure, in the order of
     *            registration. It is legal for map entries to be null, and this will produce the correct behavior using
     *            a NULL argument to the stored procedure.
     * @return map of output params, keyed by name as in parameter declarations. Output parameters will appear here,
     *         with their values after the stored procedure has been called.
     */
    public Map execute(Object[] values) {
        if (values == null) {
            return execute(Collections.EMPTY_MAP);
        } else {
            List<SqlParameter> params = this.getDeclaredParameters();
            List<SqlParameter> inputParams = new ArrayList<SqlParameter>();
            // filter the input params
            for (SqlParameter sqlParameter : params) {
                if (sqlParameter.isInputValueProvided()) {
                    inputParams.add(sqlParameter);
                }
            }

            // Make sure exactly correct number of arguments are provided
            if (inputParams.size() != values.length) {
                throw new IllegalArgumentException("There are " + inputParams.size() + " input arguments, but "
                        + values.length + " values are provided");
            }

            // Create a map, and call existing execute method
            Map<String, Object> valuesToReturn = new HashMap<String, Object>();
            for (int i = 0; i < inputParams.size(); i++) {
                valuesToReturn.put(inputParams.get(i).getName(), values[i]);
            }
            return execute(valuesToReturn);
        }
    }

The use-case for this is: this avoids having to duplicate the input parameter names twice:

  • once during the registration of the input parameter with name to StoredProcedure
  • second, when we construct the Map for values to pass values to StoredProcedure.execute(Map) method

Affects: 3.0 M1

Referenced from: commits 7c05312

@spring-projects-issues spring-projects-issues added in: data Issues in data modules (jdbc, orm, oxm, tx) type: enhancement A general enhancement labels Jan 11, 2019
@spring-projects-issues spring-projects-issues added this to the 3.0 M4 milestone Jan 11, 2019
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
in: data Issues in data modules (jdbc, orm, oxm, tx) type: enhancement A general enhancement
Projects
None yet
Development

No branches or pull requests

1 participant