Question

I am able to create a worklight adapter, deploy it and invoke it by passing the parameter manually, but i need to create a login page where when the user enters the credentials, the application should get the his/her data from the tables by passing his username in the where clause of the query SELECT * FROM USER_ACC_TABLE WHERE USER = ?

How do I programmatically pass the parameters for a query in worklight adapters?

Was it helpful?

Solution

If I understand you correctly, you'd like to get the values that the user will input in the login form's text fields and use them in your adapter request?

Maybe the answer the following question will help: Passing parameters through HTTP Adapter?
While it explains it for HTTP adapter, the JavaScript part of getting the values should be the same.

Basically, you use the following the get the value from the input field. You could save it in a variable just as well:

parameters : [$('#element-name').val()]

Full example is available in the linked question.

For passing the value to your SQL query in a SQL adapter, the following answer by jnortey should help: IBM Worklight - Error while using variable in a SQL Adapter query, which is basically the same.

That's it. Now the '?' in the SQL query should get the parameter's value.

OTHER TIPS

If you look at the docs, you will see that the invocation process includes a parameters key. In your case, I would use a JSON object as shown.

http://www-01.ibm.com/support/knowledgecenter/SSZH4A_6.2.0/com.ibm.worklight.apiref.doc/html/refjavascript-client/html/WL.Client.html%23invokeProcedure

function invokeAdapter(USERNAME) {

var USERINFO = {username: USERNAME};

var invocationData = {
        adapter: "USER_ACCT_TABLE",
        procedure: "getUserData",
        parameters:[USERINFO]
};
// DEFINE THE CALL BACK FUNCTIONS
var options = {
        onSuccess: onSuccess,
        onFailure: onFailure 
};

WL.Client.invokeProcedure(invocationData, options); 
}

In your adapter code, you would have something like the following:

var selectStatement = WL.Server.createSQLStatement("SELECT * FROM USER_ACC_TABLE WHERE USER = ?");

function getUserData(data) {
    var USERNAME = data.username;
    return WL.Server.invokeSQLStatement({
      preparedStatement : selectStatement,
      parameters : [USERNAME]
});
}     
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top