/
postgres.go
195 lines (161 loc) · 4.87 KB
/
postgres.go
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
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
// Quicknote stores and searches tens of thousands of short notes.
//
// Copyright (C) 2017 Andrew Miller <amiller@amilx.com>
//
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program. If not, see <http://www.gnu.org/licenses/>.
package postgres
import (
"database/sql"
"errors"
"fmt"
"reflect"
"strings"
// pq must be imported for initialization
_ "github.com/lib/pq"
)
var schema = `
CREATE TABLE IF NOT EXISTS books (
id SERIAL PRIMARY KEY,
created TIMESTAMPTZ NOT NULL,
modified TIMESTAMPTZ NOT NULL,
name TEXT UNIQUE
);
CREATE TABLE IF NOT EXISTS notes (
id SERIAL PRIMARY KEY,
created TIMESTAMPTZ NOT NULL,
modified TIMESTAMPTZ NOT NULL,
bk_id INTEGER NOT NULL REFERENCES books(id) ON DELETE CASCADE,
type TEXT NOT NULL,
title TEXT,
body TEXT
);
CREATE INDEX IF NOT EXISTS idx_notes_bk_id ON notes (bk_id);
CREATE INDEX IF NOT EXISTS idx_notes_bk_type_title_body ON notes (bk_id, type, title, body);
CREATE TABLE IF NOT EXISTS tags (
id SERIAL PRIMARY KEY,
created TIMESTAMPTZ NOT NULL,
modified TIMESTAMPTZ NOT NULL,
name TEXT UNIQUE
);
CREATE TABLE IF NOT EXISTS note_tag (
note_id INTEGER REFERENCES notes(id) ON DELETE CASCADE,
tag_id INTEGER REFERENCES tags(id) ON DELETE CASCADE,
PRIMARY KEY (note_id, tag_id)
);
CREATE TABLE IF NOT EXISTS note_book_tag (
note_id INTEGER REFERENCES notes(id) ON DELETE CASCADE,
bk_id INTEGER REFERENCES books(id) ON DELETE CASCADE,
tag_id INTEGER REFERENCES tags(id) ON DELETE CASCADE,
PRIMARY KEY (note_id, bk_id, tag_id)
);`
var dropAllTables = `
DROP INDEX IF EXISTS idx_notes_bk_id;
DROP TABLE IF EXISTS note_book_tag;
DROP TABLE IF EXISTS note_tag;
DROP TABLE IF EXISTS tags;
DROP TABLE IF EXISTS notes;
DROP TABLE IF EXISTS books;
`
// ErrInvalidArguments invalid arguments were given
var ErrInvalidArguments = errors.New("Invalid arguments given to PostgreSQL database")
var maxWideCards = 1000
// Database provides an interface to PostgreSQL
type Database struct {
db *sql.DB
}
// NewDatabase returns a data Database
func NewDatabase(options ...string) (*Database, error) {
if len(options) != 6 {
return nil, ErrInvalidArguments
}
strParams := fmt.Sprintf("dbname=%s host=%s port=%s user=%s password=%s sslmode=%s",
options[0], options[1], options[2], options[3],
strings.Replace(options[4], "'", "\\'", -1),
options[5])
db, err := sql.Open("postgres", strParams)
if err != nil {
return nil, err
}
_, err = db.Exec(schema)
if err != nil {
return nil, err
}
return &Database{db: db}, nil
}
// GetTableNames returns a list of all table names
func (d *Database) GetTableNames() ([]string, error) {
sqlStr := "SELECT table_name FROM information_schema.tables WHERE table_schema='public' AND table_type='BASE TABLE';"
rows, err := d.db.Query(sqlStr)
if err != nil {
return nil, err
}
defer rows.Close()
tables := make([]string, 0)
for rows.Next() {
var name string
err := rows.Scan(&name)
if err != nil {
return nil, err
}
tables = append(tables, name)
}
return tables, nil
}
// Close closes the database
func (d *Database) Close() error {
return d.db.Close()
}
func (d *Database) getTxStmt(sqlStmt string) (*sql.Tx, *sql.Stmt, error) {
tx, err := d.db.Begin()
if err != nil {
return nil, nil, err
}
stmt, err := tx.Prepare(sqlStmt)
if err != nil {
return nil, nil, err
}
return tx, stmt, nil
}
func (d *Database) ResetTables() error {
if _, err := d.db.Exec(dropAllTables); err != nil {
return err
}
if _, err := d.db.Exec(schema); err != nil {
return err
}
return nil
}
// splitSliceToChuck slice s into chucks containing the maximum number of
// objects we can use in a statement.
//
// wcPreRecord is the number of wild cards each object requires
func splitSliceToChuck(s interface{}, wcPreRecord int) ([]interface{}, error) {
// I'm not fund of using reflect as I believe it hurts performance, but I don't
// know any other way of doing this and still be usable by multiple types
slice := reflect.ValueOf(s)
if slice.Kind() != reflect.Slice {
return nil, errors.New("arg is not a slice")
}
maxChuck := maxWideCards / wcPreRecord
chuck := make([]interface{}, 0)
numRecords := slice.Len()
for i := 0; i < numRecords; i = i + maxChuck {
end := i + maxChuck
if end > numRecords {
end = numRecords
}
chuck = append(chuck, slice.Slice(i, end).Interface())
}
return chuck, nil
}