7.12.1. Stored SQL Statement

The runSql() function

A detailed explanation of the stored SQL statement implementation in IntegraXor can be read in the back-end configuration chapter of the stored SQL configuration.

After defining the name of the stored statement, the SQL statement to be executed, and SQL parameters (if any) in Project Editor, we can immediately start accessing it from the front end. We access the stored SQL statements using the following expression:

runSql(name, parameters, option, callback-function);

There are 4 parameters being passed based on the above expression.

Table below explains what each of the parameters in the runSql() function does.

name

Name is a string value corresponding to the Name of the stored SQL statement specified in Project Editor

[Important] Important

As you can probably guess, name is a compulsory parameter and should NOT be left empty!


parameters

Parameters is where you specify the value of the parameters that you defined for a particular statement in the Project Editor

Parameters is a Javascript object and should follow JSON syntax

[Note] Note

The '@' symbol used to designate parameter(s) in Project Editor should NOT be included


Let's say the defined statement has 2 parameters, @table and @val, the param object should be constructed as below:

var param = {
	"table": "table value",
	"val": "val value"
};
					

option

Option is also a Javascript object and should follow the JSON syntax

For the time being, option has two entry, outfmt, which stands for output format, and dbgroup, the database name

outfmt is the data transport format that the user wants the server to give back for the resultant data of any SQL statement

outfmt can take any of the 4 values, of json, csv, csvh or xml

If not specified, outfmt value will default to csv

dbgroup allows you to specify the database name where the SQL statement should be executed

The name should match the name of the database that you've defined in the Database section of the Project Editor

If dbgroup is not specified in the option, a default value of -1 will be applied which tells the server to run the SQL statement on all databases defined in Project Editor

To define option, you can either define it as below:

var opt = {
	"outfmt": "json",
	"dbgroup": "dbname"
};
						

OR you can also do it like the following:

var opt = {};
opt["outfmt"] = "json";
opt["dbgroup"] = "dbname";
						

callback-function

Callback function will be the function responsible for processing the result that the server returns when the SQL statement is executed

Callback function is also responsible for user notification if the server doesn't return any result

You can declare a function beforehand and pass the function name as the parameter:

var cbfn = function(result){
	// This is the callback function
	// result is the response from the server
};

runSql(name, parameters, options, cbfn);
						

OR you can declare an anonymous function when specifying the parameter for callback-function

runSql(name, parameters, options, function(result) {
	// This is the callback function
	// result is the response from the server
});