Skip to content

Commit 35919eb

Browse files
committed
Partially use plugin to replace builtin db library.
1 parent 47c16ae commit 35919eb

File tree

1 file changed

+52
-14
lines changed

1 file changed

+52
-14
lines changed

model.gb

Lines changed: 52 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,16 @@
1+
require "db"
2+
require "plugin"
3+
14
db_user = ENV["POSTGRES_USER"] || "postgres"
25
db_name = ENV["POSTGRES_DB"] || "goby_test"
36
db_host = ENV["POSTGRES_HOST"] || "0.0.0.0"
47

5-
require "plugin"
8+
PG = DB.open("postgres", String.fmt("user=%s dbname=%s host=%s sslmode=disable", db_user, db_name, db_host))
69

710
plugin = Plugin.generate("db") do |p|
8-
p.import_pkg("", "database/sql")
11+
p.import_pkg("", "github.com/jmoiron/sqlx")
912
p.import_pkg("_", "github.com/lib/pq")
10-
p.link_function("sql", "Open")
13+
p.link_function("sqlx", "Open")
1114
end
1215

1316
conn, err = plugin.go_func("Open", "postgres", String.fmt("user=%s dbname=%s host=%s sslmode=disable", db_user, db_name, db_host))
@@ -17,9 +20,43 @@ if err
1720
return nil
1821
end
1922

20-
PG = conn
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)
2157

22-
PG.run("create table if not exists list_items (
58+
59+
PluginPG.run("create table if not exists list_items (
2360
id serial primary key,
2461
title varchar(40),
2562
checked boolean
@@ -36,37 +73,38 @@ class ListItem
3673
end
3774

3875
def check
39-
ListItem.db.exec('UPDATE list_items SET checked = true WHERE id = $1', @id)
76+
self.class.plugin_db.exec('UPDATE list_items SET checked = true WHERE id = $1', @id)
4077
@checked = true
4178
end
4279

4380
def uncheck
44-
ListItem.db.exec('UPDATE list_items SET checked = false WHERE id = $1', @id)
81+
self.class.plugin_db.exec('UPDATE list_items SET checked = false WHERE id = $1', @id)
4582
@checked = false
4683
end
4784

4885
def update_title(title)
49-
ListItem.db.exec('UPDATE list_items SET title = $1 WHERE id = $2', title, @id)
86+
self.class.plugin_db.exec('UPDATE list_items SET title = $1 WHERE id = $2', title, @id)
5087
end
5188

5289
def destroy
53-
ListItem.db.exec('DELETE FROM list_items WHERE id = $1', @id)
90+
self.class.plugin_db.exec('DELETE FROM list_items WHERE id = $1', @id)
5491
end
5592

5693
def valid?
5794
@error.nil?
5895
end
5996

60-
def self.db
61-
PG
97+
98+
def self.plugin_db
99+
PluginPG
62100
end
63101

64102
def self.all
65-
self.db.query("SELECT * FROM list_items ORDER BY id DESC")
103+
PG.query("SELECT * FROM list_items ORDER BY id DESC")
66104
end
67105

68106
def self.find(id)
69-
result = self.db.query("SELECT * FROM list_items WHERE id = $1", id).first
107+
result = PG.query("SELECT * FROM list_items WHERE id = $1", id).first
70108
if result
71109
new({ id: result[:id], title: result[:title], checked: result[:checked] })
72110
end
@@ -77,7 +115,7 @@ class ListItem
77115
if result[:error].nil?
78116
title = params[:title]
79117
checked = params[:checked].to_i == 1
80-
resultID = self.db.exec("INSERT INTO list_items (title, checked) VALUES ($1, $2)", title, checked)
118+
resultID = self.plugin_db.exec("INSERT INTO list_items (title, checked) VALUES ($1, $2)", title, checked)
81119
new({ id: resultID, title: title, checked: checked })
82120
else
83121
new({ error: result[:error] })

0 commit comments

Comments
 (0)