-
Notifications
You must be signed in to change notification settings - Fork 2
/
main.go
104 lines (84 loc) · 2.31 KB
/
main.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
package pgqb // import "git.ultraware.nl/NiseVoid/qb/driver/pgqb"
import (
"database/sql"
"strconv"
"strings"
"git.ultraware.nl/NiseVoid/qb"
"git.ultraware.nl/NiseVoid/qb/driver/pgqb/pgqc"
"git.ultraware.nl/NiseVoid/qb/driver/pgqb/pgqf"
"git.ultraware.nl/NiseVoid/qb/qbdb"
"git.ultraware.nl/NiseVoid/qb/qc"
"git.ultraware.nl/NiseVoid/qb/qf"
)
// Driver implements PostgreSQL-specific features
type Driver struct{}
// New returns the driver
func New(db *sql.DB) *qbdb.DB {
return qbdb.New(Driver{}, db)
}
// ValueString returns a the SQL for a parameter value
func (d Driver) ValueString(c int) string {
return `$` + strconv.Itoa(c)
}
// BoolString formats a boolean in a format supported by PostgreSQL
func (d Driver) BoolString(v bool) string {
return strconv.FormatBool(v)
}
// UpsertSQL implements qb.Driver
func (d Driver) UpsertSQL(t *qb.Table, conflict []qb.Field, q qb.Query) (string, []interface{}) {
c := qb.NewContext(d, qb.NoAlias())
sql := ``
for k, v := range conflict {
if k > 0 {
sql += qb.COMMA
}
sql += v.QueryString(c)
}
usql, values := q.SQL(qb.NewSQLBuilder(d))
if !strings.HasPrefix(usql, `UPDATE `+t.Name) {
panic(`Update does not update the correct table`)
}
usql = strings.Replace(usql, `UPDATE `+t.Name, `UPDATE`, -1)
return `ON CONFLICT (` + sql + `) DO ` + usql, values
}
// Limit implements qb.Driver
func (d Driver) Limit(sql qb.SQL, limit int) {
sql.WriteLine(`LIMIT ` + strconv.Itoa(limit))
}
// Returning implements qb.Driver
func (d Driver) Returning(q qb.Query, f []qb.Field) (string, []interface{}) {
b := qb.NewSQLBuilder(d)
s, v := q.SQL(b)
line := ``
for k, field := range f {
if k > 0 {
line += `, `
}
line += field.QueryString(b.Context)
}
return s + `RETURNING ` + line + qb.NEWLINE, append(v, *b.Context.Values...)
}
var types = map[qb.DataType]string{
qb.Int: `int`,
qb.String: `text`,
qb.Bool: `boolean`,
qb.Float: `float`,
qb.Date: `date`,
qb.Time: `timestamptz`,
}
// TypeName implements qb.Driver
func (d Driver) TypeName(t qb.DataType) string {
if s, ok := types[t]; ok {
return s
}
panic(`Unknown type`)
}
var override = qb.OverrideMap{}
func init() {
override.Add(qf.Excluded, pgqf.Excluded)
override.Add(qc.Like, pgqc.ILike)
}
// Override implements qb.Driver
func (d Driver) Override() qb.OverrideMap {
return override
}