mwild1 / luadbi

Multi-backend SQL database library for Lua
MIT License
42 stars 15 forks source link

add SQLite3 connection flag support #46

Closed JorjBauer closed 5 years ago

JorjBauer commented 6 years ago

Adds optional support for SQLite3's open flags, supporting features like in-memory databases.

!/usr/bin/env lua

local DBI = require "DBI" local s = require "dbd.sqlite3"

local db = DBI.Connect("SQLite3", "memory", SQLITE_OPEN_MEMORY) assert(db)

assert(DBI.Do(db, [[ CREATE TABLE foo (bar integer, baz varchar(25)) ]] ))

local sth,err = db:prepare( [[ INSERT INTO foo (bar, baz) VALUES (?,?) ]] ) assert(sth,err) assert(sth:execute(1, "The Loneliest")) assert(sth:execute(42, "The Answer"))

sth,err = db:prepare( [[ SELECT * FROM foo ]] ) assert(sth,err) assert(sth:execute())

for row in sth:rows(true) do print ("Fetched row: " .. row['bar'] .. " == " .. row['baz']) end

JorjBauer commented 6 years ago

Working on a replacement. The constants for connection-time are exported after the connection is created, so my test script is actually passing in 'nil' as the flags. So there are at least three problems:

  1. How to export constants from DBI for each of the individual submodules before calling Connect().

The abstraction as-is doesn't provide any connectivity between DBI and the submodules until it calls New(), right after loading, in Connect(). So some new abstraction is necessary.

  1. I clearly don't understand what the test script is doing, since it's passing a nil as the second argument (the error that Jenkins flagged).

  2. Apparently my local sqlite3 is creating a temporary database somewhere rather than creating one in-memory, and my own test for in-memory and read-only opens isn't sufficient :)

JorjBauer commented 6 years ago

Problems sorted. Functioning test script, which forces a read-only file into the filesystem to prove it's not creating a new temporary database on disk. Obviously 5.3 (I'm using bitwise or here) but could just use '+' with 5.1/5.2 since they're bit flags.

!/usr/bin/env lua

require 'std.strict' local DBI = require "DBI" local s = require "dbd.sqlite3"

os.execute("touch memorytest") os.execute("chmod a-w memorytest") local db,_ = DBI.Connect("SQLite3", "memorytest", s.SQLITE_OPEN_MEMORY|s.SQLITE_OPEN_CREATE|s.SQLITE_OPENREADWRITE) assert(db,) os.execute("rm -f memorytest")

assert(DBI.Do(db, [[ CREATE TABLE foo (bar integer, baz varchar(25)) ]] ))

local sth,err = db:prepare( [[ INSERT INTO foo (bar, baz) VALUES (?,?) ]] ) assert(sth,err) assert(sth:execute(1, "The Loneliest")) assert(sth:execute(42, "The Answer"))

sth,err = db:prepare( [[ SELECT * FROM foo ]] ) assert(sth,err) assert(sth:execute())

for row in sth:rows(true) do print ("Fetched row: " .. row['bar'] .. " == " .. row['baz']) end

sparked435 commented 5 years ago

Looks great to me!