Skip to content

Commit 1e190c0

Browse files
committed
Use generated PG plugin to replace builtin db library.
1 parent a4e0ab6 commit 1e190c0

File tree

2 files changed

+65
-59
lines changed

2 files changed

+65
-59
lines changed

model.gb

Lines changed: 3 additions & 59 deletions
Original file line numberDiff line numberDiff line change
@@ -1,60 +1,4 @@
1-
require "db"
2-
require "plugin"
3-
4-
db_user = ENV["POSTGRES_USER"] || "postgres"
5-
db_name = ENV["POSTGRES_DB"] || "goby_test"
6-
db_host = ENV["POSTGRES_HOST"] || "0.0.0.0"
7-
8-
PG = DB.open("postgres", String.fmt("user=%s dbname=%s host=%s sslmode=disable", db_user, db_name, db_host))
9-
10-
plugin = Plugin.generate("db") do |p|
11-
p.import_pkg("", "github.com/jmoiron/sqlx")
12-
p.import_pkg("_", "github.com/lib/pq")
13-
p.link_function("sqlx", "Open")
14-
end
15-
16-
conn, err = plugin.go_func("Open", "postgres", String.fmt("user=%s dbname=%s host=%s sslmode=disable", db_user, db_name, db_host))
17-
18-
if err
19-
puts(err.go_func("Error"))
20-
return nil
21-
end
22-
23-
class PluginDB
24-
def initialize(conn)
25-
@conn = conn
26-
end
27-
28-
def run(query, *args)
29-
r, err = @conn.go_func("Exec", query, *args)
30-
31-
if err != nil
32-
puts(err.go_func("Error"))
33-
end
34-
end
35-
36-
def exec(query, *args)
37-
id = 0
38-
err = @conn.go_func("QueryRow", String.fmt("%s RETURNING id", query), *args).go_func("Scan", id.ptr)
39-
40-
if err != nil
41-
puts(err.go_func("Error"))
42-
else
43-
id
44-
end
45-
end
46-
47-
# def query(query, *args)
48-
# rows, err = conn.go_func("Queryx", query, *args)
49-
50-
# while rows.go_func("Next") do
51-
52-
# end
53-
# end
54-
end
55-
56-
PluginPG = PluginDB.new(conn)
57-
1+
require_relative "./plugin_pg"
582

593
PluginPG.run("create table if not exists list_items (
604
id serial primary key,
@@ -100,11 +44,11 @@ class ListItem
10044
end
10145

10246
def self.all
103-
PG.query("SELECT * FROM list_items ORDER BY id DESC")
47+
plugin_db.query("SELECT * FROM list_items ORDER BY id DESC")
10448
end
10549

10650
def self.find(id)
107-
result = PG.query("SELECT * FROM list_items WHERE id = $1", id).first
51+
result = plugin_db.query("SELECT * FROM list_items WHERE id = $1", id).first
10852
if result
10953
new({ id: result[:id], title: result[:title], checked: result[:checked] })
11054
end

plugin_pg.gb

Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
require "plugin"
2+
3+
# Create the plugin
4+
plugin = Plugin.generate("db") do |p|
5+
p.import_pkg("", "github.com/jmoiron/sqlx")
6+
p.import_pkg("_", "github.com/lib/pq")
7+
p.link_function("sqlx", "Open")
8+
end
9+
10+
# Some PG settings
11+
db_user = ENV["POSTGRES_USER"] || "postgres"
12+
db_name = ENV["POSTGRES_DB"] || "goby_test"
13+
db_host = ENV["POSTGRES_HOST"] || "0.0.0.0"
14+
15+
# Open PG connection using sqlx package's Open function
16+
conn, err = plugin.go_func("Open", "postgres", String.fmt("user=%s dbname=%s host=%s sslmode=disable", db_user, db_name, db_host))
17+
18+
if err
19+
puts(err.go_func("Error"))
20+
return nil
21+
end
22+
23+
class PluginDB
24+
def initialize(conn)
25+
@conn = conn
26+
end
27+
28+
def run(query, *args)
29+
r, err = @conn.go_func("Exec", query, *args)
30+
31+
if err != nil
32+
puts(err.go_func("Error"))
33+
end
34+
end
35+
36+
def exec(query, *args)
37+
id = 0
38+
err = @conn.go_func("QueryRow", String.fmt("%s RETURNING id", query), *args).go_func("Scan", id.ptr)
39+
40+
if err != nil
41+
puts(err.go_func("Error"))
42+
else
43+
id
44+
end
45+
end
46+
47+
def query(query, *args)
48+
rows, err = @conn.go_func("Queryx", query, *args)
49+
50+
results = []
51+
52+
while rows.go_func("Next") do
53+
row = GoMap.new
54+
rows.go_func("MapScan", row)
55+
results.push(row.to_hash)
56+
end
57+
58+
results
59+
end
60+
end
61+
62+
PluginPG = PluginDB.new(conn)

0 commit comments

Comments
 (0)