-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpsql.go
67 lines (50 loc) · 1.38 KB
/
psql.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
// Copyright 2022 The Go Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
package serialkey
import (
"bytes"
_ "embed"
"fmt"
"html/template"
)
//go:embed psql_next.sql
var postgreSQLNext []byte
func (db PostgreSQL) next() (string, error) {
return db.generate(string(postgreSQLNext))
}
//go:embed psql_next_n.sql
var postgreSQLNextN []byte
func (db PostgreSQL) nextN() (string, error) {
return db.generate(string(postgreSQLNextN))
}
//go:embed psql_last.sql
var postgreSQLLast []byte
func (db PostgreSQL) last() (string, error) {
return db.generate(string(postgreSQLLast))
}
//go:embed psql_forward.sql
var postgreSQLForward []byte
func (db PostgreSQL) forward() (string, error) {
return db.generate(string(postgreSQLForward))
}
//go:embed psql_create_table.sql
var PostgreSQLCreateTable []byte
func (db PostgreSQL) createTable() (string, error) {
return db.generate(string(PostgreSQLCreateTable))
}
func (db PostgreSQL) generate(query string) (string, error) {
tmpl, err := template.New("postgresql").Parse(query)
if err != nil {
return "", fmt.Errorf("parse template: %w", err)
}
var buf bytes.Buffer
err = tmpl.ExecuteTemplate(&buf, "postgresql", db)
if err != nil {
return "", fmt.Errorf("execute template: %w", err)
}
return buf.String(), nil
}
type PostgreSQL struct {
Table string
}