Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
365 views
in Technique[技术] by (71.8m points)

mysql - IBM Worklight - How to get OUT parameter when invoking a stored procedure?

I want to know is it possible to get OUT parameter when invoking a stored mysql procedure using worklight SQL adapter.

For example i have an employee table:-

enter image description here

and stored procedure for above table is:-

enter image description here

and then i query like this:-

enter image description here

It gives the following result :-

enter image description here

Which is working fine.

But when I invoke same procedure using Worklight SQL adapter instead on query browser like: -

WL.Server.invokeSQLStoredProcedure({
         procedure : "usp_GetEmployeeName",
    parameters : [103,"@name"]
});

it only return me the result set and does not return OUT parameter value.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

The ability to call java from the adapters offers a great deal of flexibility, including the ability to call your own JDBC stored proc implementation. Using this we are able to call the stored procs with OUT SYS_REFCURSOR defined and get the result set back as JSON. A jdbc datasource needs to created on your app server (Liberty, Websphere, etc..) See the methods referenced in the StoredProcedure java class to execute the stored proc and get the data back as JSON. Uses Apache dbutils and Jackson for the JSON stuff.

CREATE PACKAGE "HR"."HR_DATA" IS  -- Declare types, variables, constants, exceptions, cursors, 
  -- and subprograms that can be referenced from outside the package.

  PROCEDURE "GETCURSORS" (
    "DEP_ID" IN NUMBER, 
    "EMPLOYEES_C" OUT SYS_REFCURSOR, 
    "DEPENDENTS_C" OUT SYS_REFCURSOR);
END "HR_DATA";

Adapter code:

function getEmployeesByDep(departmentId){

var storedProcedure = new Packages.com.samnunnally.dao.StoredProcedure();

storedProcedure.setDataSource("jdbc/hr_datasource");
storedProcedure.setStoredProcName("HR.HR_DATA");
storedProcedure.addParameter(0, departmentId, false);
storedProcedure.addOutParameter(1, Packages.oracle.jdbc.OracleTypes.CURSOR);
storedProcedure.addOutParameter(2, Packages.oracle.jdbc.OracleTypes.CURSOR);

var rsHandler = new Packages.org.apache.commons.dbutils.handlers.MapListHandler();
storedProcedure.execute(rsHandler);

return {
        result : 
            {   
                EMPLOYEES : storedProcedure.getJsonParameterValue(1),
                DEPENDENTS : storedProcedure.getJsonParameterValue(2)
            } 
    };


}

Java JDBC class:

package com.samnunnally.dao;

import java.io.IOException;
import java.sql.CallableStatement;
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Types;
import java.util.ArrayList;

import javax.naming.Context;
import javax.naming.InitialContext;
import javax.naming.NamingException;
import javax.sql.DataSource;

import org.apache.commons.dbutils.ResultSetHandler;
import org.apache.commons.dbutils.handlers.KeyedHandler;
import org.codehaus.jackson.JsonGenerationException;
import org.codehaus.jackson.annotate.JsonProperty;
import org.codehaus.jackson.annotate.JsonRawValue;
import org.codehaus.jackson.map.JsonMappingException;
import org.codehaus.jackson.map.ObjectMapper;

/**
 * @author Sam Nunnally
 *
 */
public class StoredProcedure {

    private String dataSource = "";
    private String storedProcName = "";
    private ArrayList<Parameter> parameters = null;

    private Object resultSet = null;

    private boolean autoCommit = true;

    private final static String CALL_STRING = "{call "; 
    private final static String CALL_OPEN_PAREN = "(";
    private final static String CALL_PARM = "?";
    private final static String CALL_COMMA = ",";
    private final static String CALL_CLOSE = ")}";

    private class Parameter{
        private int index = -1;

        private boolean outParamter = false;

        private Object value = null;

        private Integer type = null;

        /**
         * @return the index
         */
        public int getIndex() {
            return index;
        }
        /**
         * @param index the index to set
         */
        public void setIndex(int index) {
            this.index = index;
        }
        /**
         * @return the outParamter
         */
        public boolean isOutParamter() {
            return outParamter;
        }
        /**
         * @param outParamter the outParamter to set
         */
        public void setOutParamter(boolean outParamter) {
            this.outParamter = outParamter;
        }
        /**
         * @return the value
         */
        public Object getValue() {
            return value;
        }
        /**
         * @param value the value to set
         */
        public void setValue(Object value) {
            this.value = value;
        }
        /**
         * @return the type
         */
        public int getType() {
            return type;
        }
        /**
         * @param type the type to set
         */
        public void setType(int type) {
            this.type = type;
        }


    }

    public void execute() throws NamingException, SQLException{
        this.execute(new KeyedHandler<Object>());
    }

    public void execute(ResultSetHandler<?> rsHandler) throws NamingException, SQLException{

        Connection connection = null;
        try {
            Context ctx = new InitialContext();

            DataSource ds = (DataSource)ctx.lookup(dataSource);

            connection = ds.getConnection();

            connection.setAutoCommit(autoCommit);

            String call = createCallString();

            CallableStatement cs = connection.prepareCall(call);

            int i=1;
            for (Parameter p : this.getParameters()) {
                if(p.isOutParamter()){
                    cs.registerOutParameter(i, p.getType());
                }
                else{
                    cs.setObject(i, p.getValue());
                }

                i++;
            }

            cs.execute();

            i=1;
            for (Parameter p : this.getParameters()) {
                if(p.isOutParamter()){

                    if(p.getType() == -10/*oracle.jdbc.OracleTypes.CURSOR*/){
                        p.setValue(rsHandler.handle((ResultSet)cs.getObject(i)));
                    }
                    else{
                        p.setValue(cs.getObject(i));
                    }
                }
                i++;
            }

            /*
             * Could do some BeanHandler implementation for <T> T
             * 
            Class Summary 
            AbstractKeyedHandler<K,V> ResultSetHandler implementation that returns a Map. 
            AbstractListHandler<T> Abstract class that simplify development of ResultSetHandler classes that convert ResultSet into List. 
            ArrayHandler ResultSetHandler implementation that converts a ResultSet into an Object[]. 
            ArrayListHandler ResultSetHandler implementation that converts the ResultSet into a List of Object[]s. 
            BeanHandler<T> ResultSetHandler implementation that converts the first ResultSet row into a JavaBean. 
            BeanListHandler<T> ResultSetHandler implementation that converts a ResultSet into a List of beans. 
            BeanMapHandler<K,V> ResultSetHandler implementation that returns a Map of Beans. 
            ColumnListHandler<T> ResultSetHandler implementation that converts one ResultSet column into a List of Objects. 
            KeyedHandler<K> ResultSetHandler implementation that returns a Map of Maps. 
            MapHandler ResultSetHandler implementation that converts the first ResultSet row into a Map. 
            MapListHandler ResultSetHandler implementation that converts a ResultSet into a List of Maps. 
            ScalarHandler<T> ResultSetHandler implementation that converts one ResultSet column into an Object. 
                 */
            ResultSet rs = cs.getResultSet();
            if(rs != null){
                resultSet = rsHandler.handle(rs);
            }

        } 
        finally{
            if(connection != null){
                connection.close();
            }
        }
    }

    private String createCallString(){

        StringBuffer sb = new StringBuffer();

        int parmCnt = this.getParameters().size();

        sb.append(CALL_STRING)
            .append(storedProcName)
            .append(CALL_OPEN_PAREN);

        for(int i = 0;i<parmCnt;i++){
            sb.append(CALL_PARM);
            if(parmCnt > i + 1){
                sb.append(CALL_COMMA);
            }
        }

        sb.append(CALL_CLOSE);
        return sb.toString();
    }

    /**
     * @return the dataSource
     */
    public String getDataSource() {
        return dataSource;
    }

    /**
     * @param dataSource the dataSource to set
     */
    public void setDataSource(final String dataSource) {
        this.dataSource = dataSource;
    }

    /**
     * @return the storedProc
     */
    public String getStoredProcName() {
        return storedProcName;
    }

    /**
     * @param storedProc the storedProc to set
     */
    public void setStoredProcName(final String storedProcName) {
        this.storedProcName = storedProcName;
    }

    /**
     * @return the autoCommit
     */
    public boolean isAutoCommit() {
        return autoCommit;
    }

    /**
     * @param autoCommit the autoCommit to set
     */
    public void setAutoCommit(boolean autoCommit) {
        this.autoCommit = autoCommit;
    }

    /**
     * @return the resultSet
     */
    public Object getResultSet() {
        return resultSet;
    }

    /**
     * @return the parameters
     */
    private ArrayList<Parameter> getParameters() {
        if(parameters == null){
            parameters = new ArrayList<StoredProcedure.Parameter>();
        }
        return parameters;
    }



    /**
     * @param index - the first parameter is 0, the second is 1, and so on
     * @param value
     * @param isOutParam
     */
    public void addParameter(int index, Object value, boolean isOutParam){
        Parameter p = new Parameter();

        p.setIndex(index);
        p.setValue(value);
        p.setOutParamter(isOutParam);
        p.setType(determineSqlType(value));

        this.getParameters().add(index, p);
    }


    /**
     * @param index - the first parameter is 0, the second is 1, and so on
     * @param type - java.sql.Types int value of the parameter
     */
    public void addOutParameter(int index, int type){
        Parameter p = new Parameter();

        p.setIndex(index);
        p.setType(type);
        p.setOutParamter(true);

        this.getParameters().add(index, p);
    }

    /**
     * @param index - the first parameter is 0, the second is 1, and so on
     * @param type - java.sql.Types value of the parameter
     */
    public void addOutParameter(int index, Object type){
        Parameter p = new Parameter();

        p.setIndex(index);
        p.setType((int)type);
        p.setOutParamter(true);

        this.getParameters().add(index, p);
    }

    public Object getParameter(int index){
        return this.getParameters().get(index);
    }

    @JsonProperty("raw")
    @JsonRawValue
    public String getJsonResultSet() throws JsonGenerationException, JsonMappingException, IOException{


        String result = "{}";

        if(this.getResultSet() != null){
            ObjectMapper mapper = new ObjectMapper();

            result = mapper.writeValueAsString(this.getResultSet());
        }

        return result;
    }

    @JsonProperty("raw")
    @JsonRawValue
    public String getJsonParameterValue(int index) throws JsonGenerationException, JsonMappingException, IOException{

        String result = "{}";

        Parameter parm = this.getParameters().get(index);
        if(parm != null && parm.getValue() != null){
            ObjectMapper mapper = new ObjectMapper();

            result = mapper.writeValueAsString(parm.getValue());
        }

        return result;
    }


    /**
     * 
     * Determines SqlType of object. throws exception if unknown
     * 
     * NOTE: this is not meant to be a complete list, just picked most
     * common types to start with.
     * 
     * @param obj
     *

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...