package
0.5.0
Repository: https://github.com/vadv/gopher-lua-libs.git
Documentation: pkg.go.dev

# README

db GoDoc

Usage

local db = require("db")

local config = {
  shared = true, -- share connections between lua states
  max_connections = 1, -- max connection (if you open shared connection with different max_connections - first win)
  read_only = false,   -- must execute read-write query
}

local sqlite, err = db.open("sqlite3", "file:test.db?cache=shared&mode=memory", config)
if err then error(err) end

local result, err = sqlite:query("select 1")
if err then error(err) end
if not(result.rows[1][1] == 1) then error("sqlite error") end

local _, err = sqlite:exec("CREATE TABLE t (id int, name string);")
if err then error(err) end

for i = 1, 10 do
    local query = "INSERT INTO t VALUES ("..i..", \"name-"..i.."\");"
    if i % 2 == 0 then query = "INSERT INTO t VALUES ("..i..", NULL);" end
    local _, err = sqlite:exec(query)
    if err then error(err) end
end

local result, err = sqlite:query("select * from t;")
if err then error(err) end

for i, v in pairs(result.columns) do
    if i == 1 then if not(v == "id") then error("error") end end
    if i == 2 then if not(v == "name") then error("error") end end
end

for _, row in pairs(result.rows) do
    for id, name in pairs(result.columns) do
        print(name, row[id])
    end
end

local _, err = sqlite:exec("CREATE TABLE t_stmt (id int, name string);")
if err then error(err) end

-- stmt exec
local stmt, err = sqlite:stmt("insert into t_stmt (id, name) values (?, ?)")
if err then error(err) end
local result, err = stmt:exec(1, 'name-1')
if err then error(err) end
if not(result.rows_affected == 1) then error("affted: "..tostring(result.rows_affected)) end
local err = stmt:close()
if err then error(err) end

-- stmt query
local stmt, err = sqlite:stmt("select name from t_stmt where id = ?")
if err then error(err) end
local result, err = stmt:query(1)
if err then error(err) end
if not(result.rows[1][1] == 'name-1') then error("must be 'name-1': "..tostring(result.rows[1][1])) end
local err = stmt:close()
if err then error(err) end

-- command (outside transaction)
local _, err = sqlite:command("PRAGMA journal_mode = OFF;")
if err then error(err) end

local err = sqlite:close()
if err then error(err) end

Supported Drivers

# Functions

Close lua db_ud:close() returns err.
Command lua db_ud:command(query) returns ({rows = {}, columns = {}}, err).
Exec lua db_ud:exec(query) returns ({rows_affected=number, last_insert_id=number}, err).
Loader is the module loader function.
Open lua db.open(driver, connection_string, config) returns (db_ud, err) config table: { shared=false, max_connections=X, read_only=false }.
Preload adds db to the given Lua state's package.preload table.
Query lua db_ud:query(query) returns ({rows = {}, columns = {}}, err).
RegisterDriver register sql driver.
Stmt lua db_ud:stmt(query) returns (stmt_ud, err).
StmtClose lua stmt_ud:close() returns err.
StmtExec lua stmt_ud:exec(args) returns ({rows_affected=number, last_insert_id=number}, err).
StmtQuery lua stmt_ud:query(args) returns ({rows = {}, columns = {}}, err).

# Constants

max open connections.