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
|
// GoToSocial
// Copyright (C) GoToSocial Authors admin@gotosocial.org
// SPDX-License-Identifier: AGPL-3.0-or-later
//
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU Affero 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 Affero General Public License for more details.
//
// You should have received a copy of the GNU Affero General Public License
// along with this program. If not, see <http://www.gnu.org/licenses/>.
package postgres
import (
"context"
"database/sql/driver"
pgx "github.com/jackc/pgx/v5/stdlib"
"github.com/superseriousbusiness/gotosocial/internal/db"
)
var (
// global PostgreSQL driver instances.
postgresDriver = pgx.GetDefaultDriver().(*pgx.Driver)
// check the postgres driver types
// conforms to our interface types.
// (note SQLite doesn't export their
// driver types, and gets checked in
// tests very regularly anywho).
_ connIface = (*pgx.Conn)(nil)
_ stmtIface = (*pgx.Stmt)(nil)
_ rowsIface = (*pgx.Rows)(nil)
)
// Driver is our own wrapper around the
// pgx/stdlib.Driver{} type in order to wrap further
// SQL driver types with our own err processing.
type Driver struct{}
func (d *Driver) Open(name string) (driver.Conn, error) {
conn, err := postgresDriver.Open(name)
if err != nil {
err = processPostgresError(err)
return nil, err
}
return &postgresConn{conn.(connIface)}, nil
}
func (d *Driver) OpenConnector(name string) (driver.Connector, error) {
cc, err := postgresDriver.OpenConnector(name)
if err != nil {
err = processPostgresError(err)
return nil, err
}
return &postgresConnector{driver: d, Connector: cc}, nil
}
type postgresConnector struct {
driver *Driver
driver.Connector
}
func (c *postgresConnector) Driver() driver.Driver { return c.driver }
func (c *postgresConnector) Connect(ctx context.Context) (driver.Conn, error) {
conn, err := c.Connector.Connect(ctx)
if err != nil {
err = processPostgresError(err)
return nil, err
}
return &postgresConn{conn.(connIface)}, nil
}
type postgresConn struct{ connIface }
func (c *postgresConn) Begin() (driver.Tx, error) {
return c.BeginTx(context.Background(), driver.TxOptions{})
}
func (c *postgresConn) BeginTx(ctx context.Context, opts driver.TxOptions) (driver.Tx, error) {
tx, err := c.connIface.BeginTx(ctx, opts)
err = processPostgresError(err)
if err != nil {
return nil, err
}
return &postgresTx{tx}, nil
}
func (c *postgresConn) Prepare(query string) (driver.Stmt, error) {
return c.PrepareContext(context.Background(), query)
}
func (c *postgresConn) PrepareContext(ctx context.Context, query string) (driver.Stmt, error) {
st, err := c.connIface.PrepareContext(ctx, query)
err = processPostgresError(err)
if err != nil {
return nil, err
}
return &postgresStmt{st.(stmtIface)}, nil
}
func (c *postgresConn) Exec(query string, args []driver.Value) (driver.Result, error) {
return c.ExecContext(context.Background(), query, db.ToNamedValues(args))
}
func (c *postgresConn) ExecContext(ctx context.Context, query string, args []driver.NamedValue) (driver.Result, error) {
result, err := c.connIface.ExecContext(ctx, query, args)
err = processPostgresError(err)
return result, err
}
func (c *postgresConn) Query(query string, args []driver.Value) (driver.Rows, error) {
return c.QueryContext(context.Background(), query, db.ToNamedValues(args))
}
func (c *postgresConn) QueryContext(ctx context.Context, query string, args []driver.NamedValue) (driver.Rows, error) {
rows, err := c.connIface.QueryContext(ctx, query, args)
err = processPostgresError(err)
if err != nil {
return nil, err
}
return &postgresRows{rows.(rowsIface)}, nil
}
func (c *postgresConn) Close() error {
err := c.connIface.Close()
return processPostgresError(err)
}
type postgresTx struct{ driver.Tx }
func (tx *postgresTx) Commit() error {
err := tx.Tx.Commit()
return processPostgresError(err)
}
func (tx *postgresTx) Rollback() error {
err := tx.Tx.Rollback()
return processPostgresError(err)
}
type postgresStmt struct{ stmtIface }
func (stmt *postgresStmt) Exec(args []driver.Value) (driver.Result, error) {
return stmt.ExecContext(context.Background(), db.ToNamedValues(args))
}
func (stmt *postgresStmt) ExecContext(ctx context.Context, args []driver.NamedValue) (driver.Result, error) {
res, err := stmt.stmtIface.ExecContext(ctx, args)
err = processPostgresError(err)
return res, err
}
func (stmt *postgresStmt) Query(args []driver.Value) (driver.Rows, error) {
return stmt.QueryContext(context.Background(), db.ToNamedValues(args))
}
func (stmt *postgresStmt) QueryContext(ctx context.Context, args []driver.NamedValue) (driver.Rows, error) {
rows, err := stmt.stmtIface.QueryContext(ctx, args)
err = processPostgresError(err)
if err != nil {
return nil, err
}
return &postgresRows{rows.(rowsIface)}, nil
}
type postgresRows struct{ rowsIface }
func (r *postgresRows) Next(dest []driver.Value) error {
err := r.rowsIface.Next(dest)
err = processPostgresError(err)
return err
}
func (r *postgresRows) Close() error {
err := r.rowsIface.Close()
err = processPostgresError(err)
return err
}
type connIface interface {
driver.Conn
driver.ConnPrepareContext
driver.ExecerContext
driver.QueryerContext
driver.ConnBeginTx
}
type stmtIface interface {
driver.Stmt
driver.StmtExecContext
driver.StmtQueryContext
}
type rowsIface interface {
driver.Rows
driver.RowsColumnTypeDatabaseTypeName
driver.RowsColumnTypeLength
driver.RowsColumnTypePrecisionScale
driver.RowsColumnTypeScanType
driver.RowsColumnTypeScanType
}
|