-
Notifications
You must be signed in to change notification settings - Fork 2
/
gateway_engine.go
228 lines (195 loc) · 4.97 KB
/
gateway_engine.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
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
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
package orm
import (
"context"
"fmt"
"reflect"
"strings"
"github.com/go-openapi/inflect"
"github.com/phogolabs/orm/dialect"
"github.com/phogolabs/orm/dialect/sql"
"github.com/phogolabs/orm/dialect/sql/scan"
"github.com/phogolabs/prana/sqlexec"
)
var _ Querier = &engine{}
type engine struct {
provider *sqlexec.Provider
querier dialect.ExecQuerier
dialect string
}
// All executes the query and returns a list of entities.
func (g *engine) All(ctx context.Context, q sql.Querier, v interface{}) error {
rows, err := g.Query(ctx, q)
if err != nil {
return err
}
// close the rows
defer rows.Close()
// scan the rows into the target
if err := scan.Rows(rows, v); err != nil {
return g.wrap(err)
}
// if the query supports scannable interface
if scanner, ok := q.(scan.Readable); ok {
return scanner.Scan(v)
}
return nil
}
// Only returns the only entity in the query, returns an error if not
// exactly one entity was returned.
func (g *engine) Only(ctx context.Context, q sql.Querier, v interface{}) error {
rows, err := g.Query(ctx, q)
if err != nil {
return err
}
// close the rows
defer rows.Close()
// scan the rows into the target
err = scan.Row(rows, v)
switch {
case err == sql.ErrNoRows:
return &NotFoundError{nameOf(reflect.TypeOf(v))}
case err == scan.ErrOneRow:
return &NotSingularError{nameOf(reflect.TypeOf(v))}
default:
return g.wrap(err)
}
}
// First returns the first entity in the query. Returns *NotFoundError
// when no user was found.
func (g *engine) First(ctx context.Context, q sql.Querier, v interface{}) error {
rows, err := g.Query(ctx, q)
if err != nil {
return g.wrap(err)
}
// close the rows
defer rows.Close()
// scan the rows into the target
err = scan.Row(rows, v)
switch {
case err == sql.ErrNoRows:
return &NotFoundError{nameOf(reflect.TypeOf(v))}
case err == scan.ErrOneRow:
return nil
default:
return g.wrap(err)
}
}
// Query executes a query that returns rows, typically a SELECT in SQL.
// It scans the result into the pointer v. In SQL, you it's usually *sql.Rows.
func (g *engine) Query(ctx context.Context, q sql.Querier) (*sql.Rows, error) {
// compile the query prior to execution
query, params, err := g.compile(q)
if err != nil {
return nil, g.wrap(err)
}
rows := &sql.Rows{}
// execute the query into the rows
if err := g.querier.Query(ctx, query, params, rows); err != nil {
return nil, g.wrap(err)
}
return rows, nil
}
// Exec executes a query that doesn't return rows. For example, in SQL, INSERT
// or UPDATE. It scans the result into the pointer v. In SQL, you it's usually
// sql.Result.
func (g *engine) Exec(ctx context.Context, q sql.Querier) (sql.Result, error) {
// compile the query prior to execution
query, params, err := g.compile(q)
if err != nil {
return nil, g.wrap(err)
}
var result sql.Result
// execute the query into the reslt
if err := g.querier.Exec(ctx, query, params, &result); err != nil {
return nil, g.wrap(err)
}
return result, nil
}
func (g *engine) compile(stmt sql.Querier) (string, []interface{}, error) {
type QuerierRoutine interface {
Name() string
SetQuery(string)
}
// find the command if any
if routine, ok := stmt.(QuerierRoutine); ok {
// get the actual SQL query
query, err := g.provider.Query(routine.Name())
// if getting the query fails
if err != nil {
return "", nil, g.wrap(err)
}
// sets the routine's query
routine.SetQuery(query)
}
type QuerierDialect interface {
SetDialect(dialect string)
}
// set the dialect
if query, ok := stmt.(QuerierDialect); ok {
query.SetDialect(g.dialect)
}
// compile the query
query, params := stmt.Query()
type QuerierErr interface {
Err() error
}
// check for errors
if queryErr, ok := stmt.(QuerierErr); ok {
if err := queryErr.Err(); err != nil {
return "", nil, g.wrap(err)
}
}
return query, params, nil
}
func (g *engine) wrap(err error) error {
if err == nil {
return nil
}
var (
// error as string
errm = err.Error()
// known errors
errors = [...]string{
// MySQL 1062 error (ER_DUP_ENTRY).
"Error 1062",
// SQLite.
"UNIQUE constraint failed: %s",
// PostgreSQL.
"pq: duplicate key value violates unique constraint %q",
// check constraint
"pq: violates check constraint %q",
// new row check constraint
"pq: new row for relation %q violates check constraint %q",
}
)
for _, message := range errors {
// name of the constrain
var name string
// scane the name
fmt.Sscanf(errm, message, &name, &name)
// check
if len(name) > 0 || strings.Contains(errm, message) {
// return the constraint
return &ConstraintError{
name: name,
wrap: err,
}
}
}
return err
}
func nameOf(value reflect.Type) string {
switch value.Kind() {
case reflect.Ptr:
return nameOf(value.Elem())
case reflect.Slice:
return nameOf(value.Elem())
case reflect.Array:
return nameOf(value.Elem())
case reflect.Map:
return "map"
default:
name := inflect.Underscore(value.Name())
return strings.ToLower(name)
}
}