|
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" |
58 | 2 |
|
59 | 3 | PluginPG.run("create table if not exists list_items (
|
60 | 4 | id serial primary key,
|
@@ -100,11 +44,11 @@ class ListItem
|
100 | 44 | end
|
101 | 45 |
|
102 | 46 | 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") |
104 | 48 | end
|
105 | 49 |
|
106 | 50 | 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 |
108 | 52 | if result
|
109 | 53 | new({ id: result[:id], title: result[:title], checked: result[:checked] })
|
110 | 54 | end
|
|
0 commit comments