forked from gobuffalo/pop
-
Notifications
You must be signed in to change notification settings - Fork 0
/
dialect_common.go
221 lines (186 loc) · 5.45 KB
/
dialect_common.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
package pop
import (
"bytes"
"database/sql"
"encoding/gob"
"fmt"
"io"
"io/ioutil"
"os"
"os/exec"
"strings"
"github.com/gobuffalo/pop/v6/columns"
"github.com/gobuffalo/pop/v6/logging"
"github.com/gofrs/uuid"
"github.com/jmoiron/sqlx"
)
func init() {
gob.Register(uuid.UUID{})
}
type commonDialect struct {
ConnectionDetails *ConnectionDetails
}
func (commonDialect) Lock(fn func() error) error {
return fn()
}
func (commonDialect) Quote(key string) string {
parts := strings.Split(key, ".")
for i, part := range parts {
part = strings.Trim(part, `"`)
part = strings.TrimSpace(part)
parts[i] = fmt.Sprintf(`"%v"`, part)
}
return strings.Join(parts, ".")
}
func genericCreate(c *Connection, model *Model, cols columns.Columns, quoter quotable) error {
keyType, err := model.PrimaryKeyType()
if err != nil {
return err
}
switch keyType {
case "int", "int64":
var id int64
cols.Remove(model.IDField())
w := cols.Writeable()
query := fmt.Sprintf("INSERT INTO %s (%s) VALUES (%s)", quoter.Quote(model.TableName()), w.QuotedString(quoter), w.SymbolizedString())
txlog(logging.SQL, c, query, model.Value)
res, err := c.Store.NamedExecContext(model.ctx, query, model.Value)
if err != nil {
return err
}
id, err = res.LastInsertId()
if err == nil {
model.setID(id)
}
if err != nil {
return err
}
return nil
case "UUID", "string":
if keyType == "UUID" {
if model.ID() == emptyUUID {
u, err := uuid.NewV4()
if err != nil {
return err
}
model.setID(u)
}
} else if model.ID() == "" {
return fmt.Errorf("missing ID value")
}
w := cols.Writeable()
w.Add(model.IDField())
query := fmt.Sprintf("INSERT INTO %s (%s) VALUES (%s)", quoter.Quote(model.TableName()), w.QuotedString(quoter), w.SymbolizedString())
txlog(logging.SQL, c, query, model.Value)
if _, err := c.Store.NamedExecContext(model.ctx, query, model.Value); err != nil {
return fmt.Errorf("named insert: %w", err)
}
return nil
}
return fmt.Errorf("can not use %s as a primary key type!", keyType)
}
func genericUpdate(c *Connection, model *Model, cols columns.Columns, quoter quotable) error {
stmt := fmt.Sprintf("UPDATE %s AS %s SET %s WHERE %s", quoter.Quote(model.TableName()), model.Alias(), cols.Writeable().QuotedUpdateString(quoter), model.WhereNamedID())
txlog(logging.SQL, c, stmt, model.ID())
_, err := c.Store.NamedExecContext(model.ctx, stmt, model.Value)
if err != nil {
return err
}
return nil
}
func genericUpdateQuery(c *Connection, model *Model, cols columns.Columns, quoter quotable, query Query, bindType int) (int64, error) {
q := fmt.Sprintf("UPDATE %s AS %s SET %s", quoter.Quote(model.TableName()), model.Alias(), cols.Writeable().QuotedUpdateString(quoter))
q, updateArgs, err := sqlx.Named(q, model.Value)
if err != nil {
return 0, err
}
sb := query.toSQLBuilder(model)
q = sb.buildWhereClauses(q)
q = sqlx.Rebind(bindType, q)
result, err := genericExec(c, q, append(updateArgs, sb.args...)...)
if err != nil {
return 0, err
}
n, err := result.RowsAffected()
if err != nil {
return 0, err
}
return n, err
}
func genericDestroy(c *Connection, model *Model, quoter quotable) error {
stmt := fmt.Sprintf("DELETE FROM %s AS %s WHERE %s", quoter.Quote(model.TableName()), model.Alias(), model.WhereID())
_, err := genericExec(c, stmt, model.ID())
if err != nil {
return err
}
return nil
}
func genericDelete(c *Connection, model *Model, query Query) error {
sqlQuery, args := query.ToSQL(model)
_, err := genericExec(c, sqlQuery, args...)
return err
}
func genericExec(c *Connection, stmt string, args ...interface{}) (sql.Result, error) {
txlog(logging.SQL, c, stmt, args...)
res, err := c.Store.ExecContext(c.Context(), stmt, args...)
return res, err
}
func genericSelectOne(c *Connection, model *Model, query Query) error {
sqlQuery, args := query.ToSQL(model)
txlog(logging.SQL, query.Connection, sqlQuery, args...)
err := c.Store.GetContext(model.ctx, model.Value, sqlQuery, args...)
if err != nil {
return err
}
return nil
}
func genericSelectMany(c *Connection, models *Model, query Query) error {
sqlQuery, args := query.ToSQL(models)
txlog(logging.SQL, query.Connection, sqlQuery, args...)
err := c.Store.SelectContext(models.ctx, models.Value, sqlQuery, args...)
if err != nil {
return err
}
return nil
}
func genericLoadSchema(d dialect, r io.Reader) error {
deets := d.Details()
// Open DB connection on the target DB
db, err := openPotentiallyInstrumentedConnection(d, d.MigrationURL())
if err != nil {
return fmt.Errorf("unable to load schema for %s: %w", deets.Database, err)
}
defer db.Close()
// Get reader contents
contents, err := ioutil.ReadAll(r)
if err != nil {
return err
}
if len(contents) == 0 {
log(logging.Info, "schema is empty for %s, skipping", deets.Database)
return nil
}
_, err = db.Exec(string(contents))
if err != nil {
return fmt.Errorf("unable to load schema for %s: %w", deets.Database, err)
}
log(logging.Info, "loaded schema for %s", deets.Database)
return nil
}
func genericDumpSchema(deets *ConnectionDetails, cmd *exec.Cmd, w io.Writer) error {
log(logging.SQL, strings.Join(cmd.Args, " "))
bb := &bytes.Buffer{}
mw := io.MultiWriter(w, bb)
cmd.Stdout = mw
cmd.Stderr = os.Stderr
err := cmd.Run()
if err != nil {
return err
}
x := bytes.TrimSpace(bb.Bytes())
if len(x) == 0 {
return fmt.Errorf("unable to dump schema for %s", deets.Database)
}
log(logging.Info, "dumped schema for %s", deets.Database)
return nil
}