Lua Scripting

Lua scripts can be used as client code for eXtremeSQL A Lua script will typically include code to load the eXtremeDB module and initialize the runtime, create/load and connect a database, then execute SQL queries and retrieve a result set. For example:

First load the eXtremeDB module:

 
    require ('exdb')
     

Next load and initialize the eXtremeDB runtime. Note that this function call is made using braces "{}". This is Lua syntax for passing a table as a parameter to a function. For example:

 
    initRuntime{disk=is_disk, debug=is_debug, shm=is_shm, tmgr=tmgr}
     

Then create/load the database with a list of devices and specified parameters:

 
    devices = {
        { 
            type='conv',
            assignment= 'database',
            size = 64*1024*1024
        }
    }
    dbparams = {ddl_dict_size=64*1024, max_classes=100, max_indexes=100}
    db = create("luadb", devices, dbparams)
     

And connect to the database:

 
    local c = connect(db)
     

Now the script can perform queries and process result sets:

 
    c:statement("create table t(i int)")
    print ("Inserting value")
    c:statement("insert into t values([1,2,3])")
 
    -- querying, cursor is a resut of the query
    local curs = c:query("select count(*) from t")
 
    -- traversing result set
    local elem = curs:next()
    while elem do
        for k,v in pairs(elem) do
            print(k,v)
        end
        elem = curs:next()
    end
    c:close()
    close(db)
    close(dbname)
     
    -- Explicitly free database memory with LUA GC
    devices[1].ptr = nil
     

Example

The Lua SDK provides samples "samples/lua/insert" and "samples/lua/open" which can be run from the associated command files. For example on a Windows system the following command file run.bat can be executed from the directory sampes/lua/insert:

 
    set LUA_PATH=..\..\..\target\lua\exdb.lua
    set PATH=%PATH%;..\..\..\target\bin.so
     
    ..\..\..\target\bin\luajit insert.lua