msys.db.execute
Name
msys.db.execute — Execute a query that is not expected to return data
Synopsis
require('msys.db')
require('msys.datasource')
success, errmsg = msys.db.execute(cachename, query, queryparams, options);
cachename: string query: string queryparams: table (optional) options: table (optional)
Description
Execute a query that is not expected to return data. The parameters are as follows:
-
cachename– Name of the datasource cache to be queried -
query– Query to be presented to the cacheThe query may use placeholder characters in the form
:name, ?, or$namedepending on the underlying driver. If the?placeholder is used, thenqueryparamsmust be a table with numeric keys, with index1corresponding to the first?in the query string. Otherwise, the keys must be string keys with names that match up to the defined parameters. For instance,$nameor:nameexpect to find a parameter in the table using the key name; the leading$or:is removed before looking up the value. -
queryparams– Lua table object with parameters to bind into the queryNote
The SQL standard requires the use of the "
IS [NOT] NULL"syntax in a predicate for matching againstNULL. A predicate "field_name= ?" would result in an error if a query parameter isNULL. A Luanilis equivalent to an SQLNULL. -
options– Lua table object containing additional options that affect this queryIf
optionsis specified, it must be a table. The following options are permitted:-
nocache– Boolean value. If set totrue, bypass the cache and force the query to be presented to the underlying datasource. -
raise_error– Boolean value. If set totrue, raise an exception (lua_error) on error containing the error string. Default value istrue.
-
Enable this function with the statement require('msys.db'); and also include the statement require('msys.datasource');.
This function returns the following:
-
If query succeeds – Returns
true. -
If query fails and
raise_erroris set tofalse– Returnsfalseindicating query faliure anderrmsgindicating what failed. -
If the query fails and
raise_erroris set totrue– Raises an exception.
The idiom for issuing a query and working with the results is:
require('msys.datasource'); require('msys.db'); local success, errmsg; local cache, query, raise_error; ... success, errmsg = msys.db.execute (cache, query, nil, { raise_error = false }); if (success == true) then print ("Query succeeded"); elseif (success == false) then print ("Query failed: " .. errmsg); else print ("Unknown state"); end ...
Note
The data cache used in the preceding example must be defined in your configuration file. For more information, see “ds_core - Datasource Query Core”.