-
Notifications
You must be signed in to change notification settings - Fork 6
/
db.go
79 lines (66 loc) · 2.01 KB
/
db.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
package db
import (
"context"
"github.com/lib/pq"
"gorm.io/driver/postgres"
"gorm.io/gorm"
"gorm.io/gorm/logger"
)
type ExecuteQueryResult struct {
Rows []map[string]any
}
type ExecuteStatementResult struct {
RowsAffected int64
}
var (
PgNotNullConstraintViolation = "23502"
PgForeignKeyConstraintViolation = "23503"
PgUniqueConstraintViolation = "23505"
)
type DbError struct {
// if the error was associated with a specific table, the name of the table
Table string
// if the error was associated with specific table columns, the names of these columns
Columns []string
// the primary human-readable error message. This should be accurate but terse (typically one line). Always present
Message string
// the SQLSTATE code for the error - https://www.postgresql.org/docs/current/errcodes-appendix.html. Always present
PgErrCode string
// the underlying error
Err error
}
func (err *DbError) Error() string {
return err.Err.Error()
}
func (err *DbError) Unwrap() error {
return err.Err
}
type Database interface {
// Executes SQL query statement and returns rows.
ExecuteQuery(ctx context.Context, sql string, values ...any) (*ExecuteQueryResult, error)
// Executes SQL statement and returns number of rows affected.
ExecuteStatement(ctx context.Context, sql string, values ...any) (*ExecuteStatementResult, error)
// Runs fn inside a transaction which is commited if fn returns a nil error
Transaction(ctx context.Context, fn func(ctx context.Context) error) error
Close() error
GetDB() *gorm.DB
}
func New(ctx context.Context, connString string) (Database, error) {
db, err := gorm.Open(postgres.New(postgres.Config{
DSN: connString,
PreferSimpleProtocol: true,
}), &gorm.Config{
Logger: logger.Discard,
SkipDefaultTransaction: true,
})
if err != nil {
return nil, err
}
return &GormDB{db: db}, nil
}
func QuoteIdentifier(name string) string {
return pq.QuoteIdentifier(name)
}
func QuoteLiteral(literal string) string {
return pq.QuoteLiteral(literal)
}