db

package
v0.1.6 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: May 6, 2022 License: BSD-3-Clause Imports: 9 Imported by: 0

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

Documentation

Overview

Package db implements golang package db functionality for lua.

Index

Constants

View Source
const (
	// max open connections
	MaxOpenConns = 1
)

Variables

This section is empty.

Functions

func Close

func Close(L *lua.LState) int

Close lua db_ud:close() returns err

func Command

func Command(L *lua.LState) int

Command lua db_ud:command(query) returns ({rows = {}, columns = {}}, err)

func Exec

func Exec(L *lua.LState) int

Exec lua db_ud:exec(query) returns ({rows_affected=number, last_insert_id=number}, err)

func Loader

func Loader(L *lua.LState) int

Loader is the module loader function.

func Open

func Open(L *lua.LState) int

Open lua db.open(driver, connection_string, config) returns (db_ud, err) config table:

{
  shared=false,
  max_connections=X,
  read_only=false
}

func Preload

func Preload(L *lua.LState)

Preload adds db to the given Lua state's package.preload table. After it has been preloaded, it can be loaded using require:

local db = require("db")

func Query

func Query(L *lua.LState) int

Query lua db_ud:query(query) returns ({rows = {}, columns = {}}, err)

func RegisterDriver

func RegisterDriver(driver string, i luaDB)

RegisterDriver register sql driver

func Stmt

func Stmt(L *lua.LState) int

Stmt lua db_ud:stmt(query) returns (stmt_ud, err)

func StmtClose

func StmtClose(L *lua.LState) int

StmtClose lua stmt_ud:close() returns err

func StmtExec

func StmtExec(L *lua.LState) int

StmtExec lua stmt_ud:exec(args) returns ({rows_affected=number, last_insert_id=number}, err)

func StmtQuery

func StmtQuery(L *lua.LState) int

StmtQuery lua stmt_ud:query(args) returns ({rows = {}, columns = {}}, err)

Types

This section is empty.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL