Skip to content

ScriptingQt

Paweł Salawa edited this page Jan 15, 2018 · 1 revision

Description

QtScript implements ECMAScript language, which is pretty much what JavaScript is. You can read more about QtScript language in Qt documentation.

This plugin executes any scripts by putting the script as a body of anonymous function that is immediately executed. Result of the function is captured and returned by the plugin to SQLiteStudio.

Scripts are pre-compiled and cached in memory, so executing same script multiple times benefits from it. Cache size is for 5 scripts at the time.

Script input arguments

All arguments passed to scripts are available through global "arguments" array-like variable (which is common for ECMAScript), with argument at position 0 being the first argument passed to the script.

Extra commands

The db command

Every script has access to the db object, which gives the script access to the database, that the script is executed for. This applies for example if the script is executed as implementation of Custom SQL function. If the function is not executed in any database context, then calling any of db functions will throw an exception.

Using db object from within custom SQL function implemented with QtScript, or custom collation (and so on...) should never do any INSERTs, UPDATEs, or DELETEs on the database, or the command might fail. Use it for SELECTs, or similar queries. Generally, use it to read data, not to change data.

Syntax

db.eval(sql)

Executes given sql on the database and returns results. The results type is preserved, but only for primitive types (integer, double, string). Arrays and objects returned from this function will be transformed into string representation as standard for those types.

db.eval(sql, argumentsArray)

Works almost the same as the function above, except it accepts array of arguments that will be passed to the database query. In this case the sql query should have argument placeholders (?, :name, @name, $name) that match arguments passed to the function.

db.eval(sql, argumentsObject)

Works almost the same as the function above, except it accepts object with arguments. Since arguments are passed in the object, each argument has a key. Therefore sql query should habe named placeholders (:name, @name, $name) that match arguments passed to the function.

db.eval(sql, argumentsArray, functionObject)

Executes given sql query with given array of arguments and then for each result row executes given function, passing row values as arguments to that function. This function works sequentially - it reads only single row, executes function, then reads next row, executes function and so on. This makes it good fit for working with huge result sets, cause it doesn't require high volumes of system resources. Example: we will return second column from query results if the first column equals "abc":

return db.eval(myQuery, myArguments, function()
{
    if (arguments[0] == 'abc')
        return arguments[1];
}
db.eval(sql, argumentsObject, functionObject)

Works pretty much the same as function above, except it takes query arguments as object, instead of array.

You can’t perform that action at this time.