forked from carl-mastrangelo/pixur
-
Notifications
You must be signed in to change notification settings - Fork 1
/
postgres.go
134 lines (114 loc) · 2.69 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
package db
import (
"context"
"database/sql"
"fmt"
"runtime/trace"
"strconv"
"strings"
"github.com/lib/pq"
"pixur.org/pixur/be/status"
)
var _ DBAdapter = &postgresAdapter{}
type postgresAdapter struct{}
func (a *postgresAdapter) Open(ctx context.Context, dataSourceName string) (DB, error) {
return a.open(ctx, dataSourceName)
}
func (a *postgresAdapter) open(ctx context.Context, dataSourceName string) (*dbWrapper, status.S) {
if trace.IsEnabled() {
defer trace.StartRegion(ctx, "SqlOpen").End()
}
db, err := sql.Open(a.Name(), dataSourceName)
if err != nil {
return nil, status.Unknown(&sqlError{
wrapped: err,
adap: a,
}, "can't open db")
}
if err := db.Ping(); err != nil {
sts := status.Unknown(&sqlError{
wrapped: err,
adap: a,
}, "can't ping db")
if err2 := db.Close(); err2 != nil {
sts = status.WithSuppressed(sts, err2)
}
return nil, sts
}
// TODO: make this configurable
db.SetMaxOpenConns(20)
return &dbWrapper{
db: db,
adap: a,
pp: fixLibPqQuery,
}, nil
}
func (a *postgresAdapter) OpenForTest(ctx context.Context) (DB, error) {
return a.openForTest(ctx)
}
func (a *postgresAdapter) openForTest(ctx context.Context) (*dbWrapper, status.S) {
panic("not implemented")
}
func (_ *postgresAdapter) Name() string {
return "postgres"
}
func (_ *postgresAdapter) Quote(ident string) string {
if strings.ContainsAny(ident, "\"\x00") {
panic(fmt.Sprintf("Invalid identifier %#v", ident))
}
return `"` + ident + `"`
}
func (a *postgresAdapter) BlobIdxQuote(ident string) string {
return a.Quote(ident)
}
func (_ *postgresAdapter) BoolType() string {
return "bool"
}
func (_ *postgresAdapter) IntType() string {
return "integer"
}
func (_ *postgresAdapter) BigIntType() string {
return "bigint"
}
func (_ *postgresAdapter) BlobType() string {
return "bytea"
}
func (_ *postgresAdapter) LockStmt(buf *strings.Builder, lock Lock) {
switch lock {
case LockNone:
case LockRead:
buf.WriteString(" FOR SHARE")
case LockWrite:
buf.WriteString(" FOR UPDATE")
default:
panic(fmt.Errorf("Unknown lock %v", lock))
}
}
func (_ *postgresAdapter) RetryableErr(err error) bool {
if pqerr, ok := err.(*pq.Error); ok {
if pqerr.Code == codeSerializationFailureError {
return true
}
if pqerr.Code == codeDeadlockDetectedError {
return true
}
if pqerr.Code == codeUniqueViolationError {
return true
}
}
return false
}
func fixLibPqQuery(query string) string {
parts := strings.Split(query, "?")
var b strings.Builder
b.WriteString(parts[0])
for i := 1; i < len(parts); i++ {
b.WriteRune('$')
b.WriteString(strconv.Itoa(i))
b.WriteString(parts[i])
}
return b.String()
}
func init() {
RegisterAdapter(new(postgresAdapter))
}