1 package db
2
3 import (
4 "context"
5 "database/sql"
6 "errors"
7 "reflect"
8
9 "github.com/letsencrypt/borp"
10 )
11
12
13
14
15
16
17
18
19
20 type OneSelector interface {
21 SelectOne(context.Context, interface{}, string, ...interface{}) error
22 }
23
24
25 type Selector interface {
26 Select(context.Context, interface{}, string, ...interface{}) ([]interface{}, error)
27 }
28
29
30 type Inserter interface {
31 Insert(context.Context, ...interface{}) error
32 }
33
34
35 type Execer interface {
36 ExecContext(context.Context, string, ...interface{}) (sql.Result, error)
37 }
38
39
40
41 type SelectExecer interface {
42 Selector
43 Execer
44 }
45
46
47
48 type DatabaseMap interface {
49 OneSelector
50 Inserter
51 SelectExecer
52 BeginTx(context.Context) (Transaction, error)
53 }
54
55
56
57 type Executor interface {
58 OneSelector
59 Inserter
60 SelectExecer
61 Queryer
62 Delete(context.Context, ...interface{}) (int64, error)
63 Get(context.Context, interface{}, ...interface{}) (interface{}, error)
64 Update(context.Context, ...interface{}) (int64, error)
65 }
66
67
68
69
70
71 type Queryer interface {
72 QueryContext(context.Context, string, ...interface{}) (*sql.Rows, error)
73 }
74
75
76 type Transaction interface {
77 Executor
78 Rollback() error
79 Commit() error
80 }
81
82
83 type MappedExecutor interface {
84 TableFor(reflect.Type, bool) (*borp.TableMap, error)
85 QueryContext(ctx context.Context, clauses string, args ...interface{}) (*sql.Rows, error)
86 }
87
88
89
90 type MappedSelector[T any] interface {
91 QueryContext(ctx context.Context, clauses string, args ...interface{}) (Rows[T], error)
92 QueryFrom(ctx context.Context, tablename string, clauses string, args ...interface{}) (Rows[T], error)
93 }
94
95
96
97 type Rows[T any] interface {
98 Next() bool
99 Get() (*T, error)
100 Err() error
101 Close() error
102 }
103
104
105
106
107
108
109
110
111
112
113 type MockSqlExecutor struct{}
114
115 func (mse MockSqlExecutor) Get(ctx context.Context, i interface{}, keys ...interface{}) (interface{}, error) {
116 return nil, errors.New("unimplemented")
117 }
118 func (mse MockSqlExecutor) Insert(ctx context.Context, list ...interface{}) error {
119 return errors.New("unimplemented")
120 }
121 func (mse MockSqlExecutor) Update(ctx context.Context, list ...interface{}) (int64, error) {
122 return 0, errors.New("unimplemented")
123 }
124 func (mse MockSqlExecutor) Delete(ctx context.Context, list ...interface{}) (int64, error) {
125 return 0, errors.New("unimplemented")
126 }
127 func (mse MockSqlExecutor) ExecContext(ctx context.Context, query string, args ...interface{}) (sql.Result, error) {
128 return nil, errors.New("unimplemented")
129 }
130 func (mse MockSqlExecutor) Select(ctx context.Context, i interface{}, query string, args ...interface{}) ([]interface{}, error) {
131 return nil, errors.New("unimplemented")
132 }
133 func (mse MockSqlExecutor) SelectInt(ctx context.Context, query string, args ...interface{}) (int64, error) {
134 return 0, errors.New("unimplemented")
135 }
136 func (mse MockSqlExecutor) SelectNullInt(ctx context.Context, query string, args ...interface{}) (sql.NullInt64, error) {
137 return sql.NullInt64{}, errors.New("unimplemented")
138 }
139 func (mse MockSqlExecutor) SelectFloat(ctx context.Context, query string, args ...interface{}) (float64, error) {
140 return 0, errors.New("unimplemented")
141 }
142 func (mse MockSqlExecutor) SelectNullFloat(ctx context.Context, query string, args ...interface{}) (sql.NullFloat64, error) {
143 return sql.NullFloat64{}, errors.New("unimplemented")
144 }
145 func (mse MockSqlExecutor) SelectStr(ctx context.Context, query string, args ...interface{}) (string, error) {
146 return "", errors.New("unimplemented")
147 }
148 func (mse MockSqlExecutor) SelectNullStr(ctx context.Context, query string, args ...interface{}) (sql.NullString, error) {
149 return sql.NullString{}, errors.New("unimplemented")
150 }
151 func (mse MockSqlExecutor) SelectOne(ctx context.Context, holder interface{}, query string, args ...interface{}) error {
152 return errors.New("unimplemented")
153 }
154 func (mse MockSqlExecutor) QueryContext(ctx context.Context, query string, args ...interface{}) (*sql.Rows, error) {
155 return nil, errors.New("unimplemented")
156 }
157 func (mse MockSqlExecutor) QueryRowContext(ctx context.Context, query string, args ...interface{}) *sql.Row {
158 return nil
159 }
160
View as plain text