mirror of
https://github.com/mailscope/kumomta.git
synced 2026-09-10 04:22:13 +00:00
A bit of a big commit because I wanted sqlite queries to run async and there isn't a sane way to tell in mlua whether a given context is safe to yield for an await, which meant making every case where we call into lua be fully async. This adds a simple but powerful binding to sqlite that performs its IO in a blocking tokio thread pool, and returns the results back to lua. There is no implicit caching of queries, and no implicit connection pooling.
67 lines
2.1 KiB
Markdown
67 lines
2.1 KiB
Markdown
# `sqlite.open(PATH, [BUSY_TIMEOUT=500])`
|
|
|
|
Opens the sqlite database at `PATH` and returns a connection handle.
|
|
|
|
`BUSY_TIMEOUT` specifies the time in milliseconds over which sqlite
|
|
should retry operations when it is unable to get an exclusive lock.
|
|
The default is 500ms.
|
|
|
|
The returned connection object has a single method, `execute` which can
|
|
be used to execute a query.
|
|
|
|
```admonish
|
|
when using the special path `:memory:`, sqlite will create an in-memory
|
|
database which is great for this contrived example, but not a great
|
|
deal of use in a real worl usage inside KumoMTA.
|
|
```
|
|
|
|
```lua
|
|
local db = sqlite.open ':memory:'
|
|
|
|
-- For the sake of this example, populate with some simple data
|
|
db:execute 'CREATE TABLE people (name, age)'
|
|
|
|
-- You can use prepared statements with positional arguments like this:
|
|
db:execute('INSERT INTO people (name, age) values (?, ?)', 'john', 42)
|
|
|
|
-- and with named arguments like this:
|
|
db:execute(
|
|
'INSERT INTO people (name, age) values (:name, :age)',
|
|
{ name = 'fred', age = 32 }
|
|
)
|
|
|
|
-- Lets print out just the ages from the database; when the query
|
|
-- returns only a single column, the returned value will be an
|
|
-- array style lua table consisting of just the values. In this case,
|
|
-- it is equivalent to `{32, 42}`
|
|
print 'query ages'
|
|
local ages = db:execute 'select age from people order by age'
|
|
for k, v in ipairs(ages) do
|
|
print(k, v)
|
|
end
|
|
|
|
-- When multiple columns are returned, they are presented as
|
|
-- an array style table of lua object style tables will be returned.
|
|
-- In this case it is equivalent to:
|
|
-- `{ {name="john", age=42 }, {name="fred", age=32}}`
|
|
print 'query all'
|
|
local ages = db:execute 'select * from people order by age'
|
|
for k, v in ipairs(ages) do
|
|
print('row', k)
|
|
for name, value in pairs(v) do
|
|
print(name, value)
|
|
end
|
|
end
|
|
|
|
-- When no rows are returned by the query, the return value is
|
|
-- the number of rows affected by the query. In this case, because
|
|
-- 2 records are being deleted, this will print 2
|
|
print('deleted rows:', db:execute 'delete from people')
|
|
```
|
|
|
|
sqlite queries are executed via a thread pool so that the query won't
|
|
block important IO scheduling.
|
|
|
|
Queries and query results are not implicitly cached.
|
|
|