SQLite database operations

I am performing several dozen SQLite CRUD operations in my app. I am curious if I can send custom function calls as parameters in the following sample. I will explain.

The code below shows how I query a “customer” table and then fill an input control datalist.
I call this function to kick it off:

function GetContractListByCustomerId(customer_id) {
arg = [customer_id];
 var s = new Array(['SELECT id, contract_name, customer_id FROM contract WHERE customer_id=?;', arg, ResultsToHomeContractInput]);
 Sql(DB, s);
}

Then you can see in the Sql function I have a success callback as shown below. This callback receives the transaction object and the results array of the select query.

function ResultsToHomeContractInput(transaction, results)
  {
    DBRecords = results;
    var arrContracts = [];
    for(i=0; i<=DBRecords.rows.length-1; i++){
      arrContracts.push(DBRecords.rows.item(i)['contract_name']);
    }
    inputContract.datalist = arrContracts;
    imageBusy.hidden=true;
  }

This code works great but what I am finding is that I want to add some dynamic function calls to the callback results. So lets say that I want the above sample to look like below. If I add a function variable as a parameter, can I send that variable as a parameter to the Sql success callback function?

function GetContractListByCustomerId(customer_id, **functionNameVar**) {
    arg = [customer_id];
     var s = new Array(['SELECT id, contract_name, customer_id FROM contract WHERE customer_id=?;', arg, ResultsToHomeContractInput(**functionNameVar**)]);
     Sql(DB, s);
    }

What would that look like in the callback itself? The parameters “transaction” and “results” are added.

function ResultsToHomeContractInput(transaction, results, **functionNameVar**)
      {
        DBRecords = results;
        var arrContracts = [];
        for(i=0; i<=DBRecords.rows.length-1; i++){
          arrContracts.push(DBRecords.rows.item(i)['contract_name']);
        }
        inputContract.datalist = arrContracts;
        imageBusy.hidden=true;
       -->>> Call the function variable **functionNameVar** here???
    
      }

You could use the function name var in a switch statement then call the real function based on the passed value.

I like that idea.

1 Like

Well I like that you liked the idea so I liked your response :stuck_out_tongue: lol

Hope it works out for you.

From a proper javascript standpoint is it legal to use a function variable like the way I described?

Yes - there is no problem passing function references as parameters.