1
+ require "db"
2
+ require "plugin"
3
+
1
4
db_user = ENV["POSTGRES_USER"] || "postgres"
2
5
db_name = ENV["POSTGRES_DB"] || "goby_test"
3
6
db_host = ENV["POSTGRES_HOST"] || "0.0.0.0"
4
7
5
- require "plugin"
8
+ PG = DB.open("postgres", String.fmt("user=%s dbname=%s host=%s sslmode=disable", db_user, db_name, db_host))
6
9
7
10
plugin = Plugin.generate("db") do |p|
8
- p.import_pkg("", "database/sql ")
11
+ p.import_pkg("", "github.com/jmoiron/sqlx ")
9
12
p.import_pkg("_", "github.com/lib/pq")
10
- p.link_function("sql ", "Open")
13
+ p.link_function("sqlx ", "Open")
11
14
end
12
15
13
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,9 +20,43 @@ if err
17
20
return nil
18
21
end
19
22
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)
21
57
22
- PG.run("create table if not exists list_items (
58
+
59
+ PluginPG.run("create table if not exists list_items (
23
60
id serial primary key,
24
61
title varchar(40),
25
62
checked boolean
@@ -36,37 +73,38 @@ class ListItem
36
73
end
37
74
38
75
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)
40
77
@checked = true
41
78
end
42
79
43
80
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)
45
82
@checked = false
46
83
end
47
84
48
85
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)
50
87
end
51
88
52
89
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)
54
91
end
55
92
56
93
def valid?
57
94
@error.nil?
58
95
end
59
96
60
- def self.db
61
- PG
97
+
98
+ def self.plugin_db
99
+ PluginPG
62
100
end
63
101
64
102
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")
66
104
end
67
105
68
106
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
70
108
if result
71
109
new({ id: result[:id], title: result[:title], checked: result[:checked] })
72
110
end
@@ -77,7 +115,7 @@ class ListItem
77
115
if result[:error].nil?
78
116
title = params[:title]
79
117
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)
81
119
new({ id: resultID, title: title, checked: checked })
82
120
else
83
121
new({ error: result[:error] })
0 commit comments