forked from gobuffalo/pop
-
Notifications
You must be signed in to change notification settings - Fork 0
/
dialect_postgresql.go
387 lines (333 loc) · 9.55 KB
/
dialect_postgresql.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
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
package pop
import (
"database/sql"
"fmt"
"io"
"os/exec"
"strings"
"sync"
"unicode"
"github.com/gobuffalo/fizz"
"github.com/gobuffalo/fizz/translators"
"github.com/gobuffalo/pop/columns"
"github.com/gobuffalo/pop/internal/defaults"
"github.com/gobuffalo/pop/logging"
"github.com/jmoiron/sqlx"
pg "github.com/lib/pq"
"github.com/pkg/errors"
)
const namePostgreSQL = "postgres"
const portPostgreSQL = "5432"
func init() {
AvailableDialects = append(AvailableDialects, namePostgreSQL)
dialectSynonyms["postgresql"] = namePostgreSQL
dialectSynonyms["pg"] = namePostgreSQL
urlParser[namePostgreSQL] = urlParserPostgreSQL
finalizer[namePostgreSQL] = finalizerPostgreSQL
newConnection[namePostgreSQL] = newPostgreSQL
}
var _ dialect = &postgresql{}
type postgresql struct {
commonDialect
translateCache map[string]string
mu sync.Mutex
}
func (p *postgresql) Name() string {
return namePostgreSQL
}
func (p *postgresql) Details() *ConnectionDetails {
return p.ConnectionDetails
}
func (p *postgresql) Create(s store, model *Model, cols columns.Columns) error {
keyType := model.PrimaryKeyType()
switch keyType {
case "int", "int64":
cols.Remove("id")
id := struct {
ID int `db:"id"`
}{}
w := cols.Writeable()
var query string
if len(w.Cols) > 0 {
query = fmt.Sprintf("INSERT INTO %s (%s) VALUES (%s) returning id", p.Quote(model.TableName()), w.QuotedString(p), w.SymbolizedString())
} else {
query = fmt.Sprintf("INSERT INTO %s DEFAULT VALUES returning id", p.Quote(model.TableName()))
}
log(logging.SQL, query)
stmt, err := s.PrepareNamed(query)
if err != nil {
return err
}
err = stmt.Get(&id, model.Value)
if err != nil {
if err := stmt.Close(); err != nil {
return errors.WithMessage(err, "failed to close statement")
}
return err
}
model.setID(id.ID)
return errors.WithMessage(stmt.Close(), "failed to close statement")
}
return genericCreate(s, model, cols, p)
}
func (p *postgresql) Update(s store, model *Model, cols columns.Columns) error {
return genericUpdate(s, model, cols, p)
}
func (p *postgresql) Destroy(s store, model *Model) error {
stmt := p.TranslateSQL(fmt.Sprintf("DELETE FROM %s WHERE %s", p.Quote(model.TableName()), model.whereID()))
_, err := genericExec(s, stmt, model.ID())
if err != nil {
return err
}
return nil
}
func (p *postgresql) SelectOne(s store, model *Model, query Query) error {
return genericSelectOne(s, model, query)
}
func (p *postgresql) SelectMany(s store, models *Model, query Query) error {
return genericSelectMany(s, models, query)
}
func (p *postgresql) CreateDB() error {
// createdb -h db -p 5432 -U postgres enterprise_development
deets := p.ConnectionDetails
db, err := sql.Open(deets.Dialect, p.urlWithoutDb())
if err != nil {
return errors.Wrapf(err, "error creating PostgreSQL database %s", deets.Database)
}
defer db.Close()
query := fmt.Sprintf("CREATE DATABASE %s", p.Quote(deets.Database))
log(logging.SQL, query)
_, err = db.Exec(query)
if err != nil {
return errors.Wrapf(err, "error creating PostgreSQL database %s", deets.Database)
}
log(logging.Info, "created database %s", deets.Database)
return nil
}
func (p *postgresql) DropDB() error {
deets := p.ConnectionDetails
db, err := sql.Open(deets.Dialect, p.urlWithoutDb())
if err != nil {
return errors.Wrapf(err, "error dropping PostgreSQL database %s", deets.Database)
}
defer db.Close()
query := fmt.Sprintf("DROP DATABASE %s", p.Quote(deets.Database))
log(logging.SQL, query)
_, err = db.Exec(query)
if err != nil {
return errors.Wrapf(err, "error dropping PostgreSQL database %s", deets.Database)
}
log(logging.Info, "dropped database %s", deets.Database)
return nil
}
func (p *postgresql) URL() string {
c := p.ConnectionDetails
if c.URL != "" {
return c.URL
}
s := "postgres://%s:%s@%s:%s/%s?%s"
return fmt.Sprintf(s, c.User, c.Password, c.Host, c.Port, c.Database, c.OptionsString(""))
}
func (p *postgresql) urlWithoutDb() string {
c := p.ConnectionDetails
// https://github.com/gobuffalo/buffalo/issues/836
// If the db is not precised, postgresql takes the username as the database to connect on.
// To avoid a connection problem if the user db is not here, we use the default "postgres"
// db, just like the other client tools do.
s := "postgres://%s:%s@%s:%s/postgres?%s"
return fmt.Sprintf(s, c.User, c.Password, c.Host, c.Port, c.OptionsString(""))
}
func (p *postgresql) MigrationURL() string {
return p.URL()
}
func (p *postgresql) TranslateSQL(sql string) string {
defer p.mu.Unlock()
p.mu.Lock()
if csql, ok := p.translateCache[sql]; ok {
return csql
}
csql := sqlx.Rebind(sqlx.DOLLAR, sql)
p.translateCache[sql] = csql
return csql
}
func (p *postgresql) FizzTranslator() fizz.Translator {
return translators.NewPostgres()
}
func (p *postgresql) DumpSchema(w io.Writer) error {
cmd := exec.Command("pg_dump", "-s", fmt.Sprintf("--dbname=%s", p.URL()))
return genericDumpSchema(p.Details(), cmd, w)
}
// LoadSchema executes a schema sql file against the configured database.
func (p *postgresql) LoadSchema(r io.Reader) error {
return genericLoadSchema(p.ConnectionDetails, p.MigrationURL(), r)
}
// TruncateAll truncates all tables for the given connection.
func (p *postgresql) TruncateAll(tx *Connection) error {
return tx.RawQuery(fmt.Sprintf(pgTruncate, tx.MigrationTableName())).Exec()
}
func newPostgreSQL(deets *ConnectionDetails) (dialect, error) {
cd := &postgresql{
commonDialect: commonDialect{ConnectionDetails: deets},
translateCache: map[string]string{},
mu: sync.Mutex{},
}
return cd, nil
}
// urlParserPostgreSQL parses the options the same way official lib/pg does:
// https://godoc.org/github.com/lib/pq#hdr-Connection_String_Parameters
// After parsed, they are set to ConnectionDetails instance
func urlParserPostgreSQL(cd *ConnectionDetails) error {
var err error
name := cd.URL
if strings.HasPrefix(name, "postgres://") || strings.HasPrefix(name, "postgresql://") {
name, err = pg.ParseURL(name)
if err != nil {
return err
}
}
o := make(values)
if err := parseOpts(name, o); err != nil {
return err
}
if dbname, ok := o["dbname"]; ok {
cd.Database = dbname
}
if host, ok := o["host"]; ok {
cd.Host = host
}
if password, ok := o["password"]; ok {
cd.Password = password
}
if user, ok := o["user"]; ok {
cd.User = user
}
if port, ok := o["port"]; ok {
cd.Port = port
}
options := []string{"sslmode", "fallback_application_name", "connect_timeout", "sslcert", "sslkey", "sslrootcert"}
for i := range options {
if opt, ok := o[options[i]]; ok {
cd.Options[options[i]] = opt
}
}
return nil
}
func finalizerPostgreSQL(cd *ConnectionDetails) {
cd.Options["sslmode"] = defaults.String(cd.Options["sslmode"], "disable")
cd.Port = defaults.String(cd.Port, portPostgreSQL)
}
const pgTruncate = `DO
$func$
DECLARE
_tbl text;
_sch text;
BEGIN
FOR _sch, _tbl IN
SELECT schemaname, tablename
FROM pg_tables
WHERE tablename <> '%s' AND schemaname NOT IN ('pg_catalog', 'information_schema') AND tableowner = current_user
LOOP
--RAISE ERROR '%%',
EXECUTE -- dangerous, test before you execute!
format('TRUNCATE TABLE %%I.%%I CASCADE', _sch, _tbl);
END LOOP;
END
$func$;`
// Code below is ported from: https://github.com/lib/pq/blob/master/conn.go
type values map[string]string
// scanner implements a tokenizer for libpq-style option strings.
type scanner struct {
s []rune
i int
}
// newScanner returns a new scanner initialized with the option string s.
func newScanner(s string) *scanner {
return &scanner{[]rune(s), 0}
}
// Next returns the next rune.
// It returns 0, false if the end of the text has been reached.
func (s *scanner) Next() (rune, bool) {
if s.i >= len(s.s) {
return 0, false
}
r := s.s[s.i]
s.i++
return r, true
}
// SkipSpaces returns the next non-whitespace rune.
// It returns 0, false if the end of the text has been reached.
func (s *scanner) SkipSpaces() (rune, bool) {
r, ok := s.Next()
for unicode.IsSpace(r) && ok {
r, ok = s.Next()
}
return r, ok
}
// parseOpts parses the options from name and adds them to the values.
//
// The parsing code is based on conninfo_parse from libpq's fe-connect.c
func parseOpts(name string, o values) error {
s := newScanner(name)
for {
var (
keyRunes, valRunes []rune
r rune
ok bool
)
if r, ok = s.SkipSpaces(); !ok {
break
}
// Scan the key
for !unicode.IsSpace(r) && r != '=' {
keyRunes = append(keyRunes, r)
if r, ok = s.Next(); !ok {
break
}
}
// Skip any whitespace if we're not at the = yet
if r != '=' {
r, ok = s.SkipSpaces()
}
// The current character should be =
if r != '=' || !ok {
return fmt.Errorf(`missing "=" after %q in connection info string"`, string(keyRunes))
}
// Skip any whitespace after the =
if r, ok = s.SkipSpaces(); !ok {
// If we reach the end here, the last value is just an empty string as per libpq.
o[string(keyRunes)] = ""
break
}
if r != '\'' {
for !unicode.IsSpace(r) {
if r == '\\' {
if r, ok = s.Next(); !ok {
return fmt.Errorf(`missing character after backslash`)
}
}
valRunes = append(valRunes, r)
if r, ok = s.Next(); !ok {
break
}
}
} else {
quote:
for {
if r, ok = s.Next(); !ok {
return fmt.Errorf(`unterminated quoted string literal in connection string`)
}
switch r {
case '\'':
break quote
case '\\':
r, _ = s.Next()
fallthrough
default:
valRunes = append(valRunes, r)
}
}
}
o[string(keyRunes)] = string(valRunes)
}
return nil
}