-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathexample.lua
More file actions
58 lines (49 loc) · 1.3 KB
/
Copy pathexample.lua
File metadata and controls
58 lines (49 loc) · 1.3 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
local cqueues = require "cqueues"
local pgsql = require "cqueues_pgsql"
-- Simple function that prints out a result object
local function pr(res)
print("STATUS: ", res:status())
for j=1, res:nfields() do
print(res:fname(j))
end
for i=1, res:ntuples() do
for j=1, res:nfields() do
print(res:getvalue(i, j))
end
end
end
-- Works like normal in a standard lua context:
do
local conn = pgsql.connectdb ""
if conn:status() ~= pgsql.CONNECTION_OK then
error(conn:errorMessage(), nil)
end
local res = conn:exec("SELECT * FROM sometable")
if not res then error(conn:errorMessage(), nil) end
pr(res)
end
-- But when inside a cqueues coroutine, it's non blocking!
local loop = cqueues.new()
loop:wrap(function()
local conn = pgsql.connectdb ""
if conn:status() ~= pgsql.CONNECTION_OK then
error(conn:errorMessage(), nil)
end
do
local res = conn:exec("SELECT * FROM sometable")
if not res then error(conn:errorMessage(), nil) end
pr(res)
end
do
local prepared = conn:prepare("sel", [[SELECT $1 AS "heh" FROM sometable]], "asd")
if not prepared or prepared:status() ~= pgsql.PGRES_COMMAND_OK then
error(prepared:errorMessage(), nil)
end
end
do
local res = conn:execPrepared("sel", "id")
if not res then error(conn:errorMessage(), nil) end
pr(res)
end
end)
assert(loop:loop())