Repository navigation
Expand file tree
/
Copy path05_db_postgres.lua
More file actions
164 lines (144 loc) · 4.9 KB
/
Copy path05_db_postgres.lua
File metadata and controls
164 lines (144 loc) · 4.9 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
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
--[[
PostgreSQL Database Demo for lunet
QUICK START:
# Option 1: Set environment variables
export LUNET_DB_USER=$(whoami) # or your PostgreSQL username
LUNET_BIN=$(find build -path '*/release/lunet-run' -type f 2>/dev/null | head -1)
"$LUNET_BIN" examples/05_db_postgres.lua
# Option 2: Edit the db.open() call below to match your setup
Prerequisites:
1. Build core + postgres driver:
xmake f -m release -y
xmake build
xmake build lunet-postgres
2. Create the demo database in PostgreSQL:
psql
CREATE DATABASE lunet_demo;
Schema (created by this demo):
CREATE TABLE users (
id SERIAL PRIMARY KEY,
name VARCHAR(255) NOT NULL,
email VARCHAR(255) NOT NULL,
age INTEGER
);
Environment Variables:
LUNET_DB_HOST - PostgreSQL host (default: localhost)
LUNET_DB_PORT - PostgreSQL port (default: 5432)
LUNET_DB_USER - PostgreSQL user (default: current system user)
LUNET_DB_PASS - PostgreSQL password (default: empty)
LUNET_DB_NAME - Database name (default: lunet_demo)
]]
local lunet = require('lunet')
local db = require('lunet.postgres')
lunet.spawn(function()
print("=== PostgreSQL Database Demo ===")
print()
print("NOTE: This demo requires a running PostgreSQL server.")
print(" Configure the connection parameters below.")
print()
local conn, err = db.open({
host = os.getenv("LUNET_DB_HOST") or "localhost",
port = tonumber(os.getenv("LUNET_DB_PORT")) or 5432,
user = os.getenv("LUNET_DB_USER") or os.getenv("USER") or "postgres",
password = os.getenv("LUNET_DB_PASS") or "",
database = os.getenv("LUNET_DB_NAME") or "lunet_demo"
})
if not conn then
print("Failed to connect to PostgreSQL:", err)
print()
print("To run this demo:")
print(" 1. Start PostgreSQL server")
print(" 2. Create the database: CREATE DATABASE lunet_demo;")
print(" 3. Update connection parameters above if needed")
return
end
print("Connected to PostgreSQL database: lunet_demo")
local _, result, rows
_, err = db.exec(conn, "DROP TABLE IF EXISTS users")
if err then
print("Warning: Could not drop existing table:", err)
end
_, err = db.exec(conn, [[
CREATE TABLE users (
id SERIAL PRIMARY KEY,
name VARCHAR(255) NOT NULL,
email VARCHAR(255) NOT NULL,
age INTEGER
)
]])
if err then
print("Failed to create table:", err)
db.close(conn)
return
end
print("Created users table")
local users = {
{ name = "Alice", email = "alice@example.com", age = 28 },
{ name = "Bob", email = "bob@example.com", age = 35 },
{ name = "O'Brien", email = "obrien@example.com", age = 42 }
}
for _, user in ipairs(users) do
local ins_rows, ins_err = db.query(
conn,
"INSERT INTO users (name, email, age) VALUES ($1, $2, $3) RETURNING id",
user.name,
user.email,
user.age
)
if ins_err then
print("Failed to insert user:", ins_err)
else
local id = ins_rows[1] and ins_rows[1].id or "?"
print(("Inserted %s (id=%s)"):format(user.name, tostring(id)))
end
end
print()
print("Querying all users:")
rows, err = db.query(conn, "SELECT id, name, email, age FROM users ORDER BY id")
if err then
print("Query failed:", err)
else
for _, row in ipairs(rows) do
print((" [%d] %s <%s> age=%d"):format(row.id, row.name, row.email, row.age))
end
end
print()
print("Querying one user (with parameters):")
rows, err = db.query(conn, "SELECT id, name, email FROM users WHERE name = $1", "Alice")
if err then
print("Query failed:", err)
else
for _, row in ipairs(rows) do
print((" [%d] %s <%s>"):format(row.id, row.name, row.email))
end
end
print()
print("Updating Bob's age to 36...")
result, err = db.exec(conn, "UPDATE users SET age = $1 WHERE name = $2", 36, "Bob")
if err then
print("Update failed:", err)
else
print((" Affected rows: %d"):format(result.affected_rows))
end
print()
print("Deleting O'Brien...")
result, err = db.exec(conn, "DELETE FROM users WHERE name = $1", "O'Brien")
if err then
print("Delete failed:", err)
else
print((" Affected rows: %d"):format(result.affected_rows))
end
print()
print("Final user list:")
rows, err = db.query(conn, "SELECT id, name, email, age FROM users ORDER BY id")
if err then
print("Query failed:", err)
else
for _, row in ipairs(rows) do
print((" [%d] %s <%s> age=%d"):format(row.id, row.name, row.email, row.age))
end
end
db.close(conn)
print()
print("Database closed. Demo complete!")
end)