Skip to content

ScriptingQt

Paweł Salawa edited this page Apr 16, 2021 · 3 revisions

Description

QtScript implements ECMAScript language, which is pretty much what JavaScript is.

It is historically named "QtScript", because in versions 3.2.x and older, the the Qt module providing this implementation was names QtScript. Nevertheless, it provides JavaScript support.

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 functions and objects

The db object

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.

The debug() function

The debug() function is global and can be used for quick debugging. It accept 1 argument and will print it to SQLiteStudio status panel.

If you need more sophisticated debugging functions, use the usual console object and its log() functions family. Be aware, that the console will not print to the status panel, but instead to the debug console, so to see output, you need to run SQLiteStudio with -d option (and under Windows you need to hit F12 to see debug console).