-
Notifications
You must be signed in to change notification settings - Fork 17
/
context.go
66 lines (54 loc) · 1.92 KB
/
context.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
package crud
import (
"context"
stdsql "database/sql"
"github.com/azer/logger"
)
type WithContext struct {
Context context.Context
DB *stdsql.DB
Id string
IdKey string
}
// Execute any SQL query on the context client. Returns sql.Result.
func (ctx *WithContext) Exec(sql string, params ...interface{}) (stdsql.Result, error) {
timer := log.Timer()
result, err := ctx.DB.ExecContext(ctx.Context, sql, params...)
timer.End("Executed SQL query.", logger.Attrs{
ctx.IdKey: ctx.Id,
"sql": sql,
})
return result, err
}
// Execute any SQL query on the context client. Returns sql.Rows.
func (ctx *WithContext) Query(sql string, params ...interface{}) (*stdsql.Rows, error) {
timer := log.Timer()
result, err := ctx.DB.QueryContext(ctx.Context, sql, params...)
timer.End("Run SQL query.", logger.Attrs{
ctx.IdKey: ctx.Id,
"sql": sql,
})
return result, err
}
// Insert given record to the database.
func (ctx *WithContext) Create(record interface{}) error {
return create(ctx.Exec, record)
}
// Inserts given record and scans the inserted row back to the given row.
func (ctx *WithContext) CreateAndRead(record interface{}) error {
return createAndRead(ctx.Exec, ctx.Query, record)
}
// Run a select query on the databaase (w/ given parameters optionally) and scan the result(s) to the
// target interface specified as the first parameter.
func (ctx *WithContext) Read(scanTo interface{}, params ...interface{}) error {
return read(ctx.Query, scanTo, params)
}
// Run an update query on the transaction, finding out the primary-key field of the given row.
func (ctx *WithContext) Update(record interface{}) error {
return mustUpdate(ctx.Exec, record)
}
// Executes a DELETE query on the transaction for given struct record. It matches
// the database row by finding out the primary key field defined in the table schema.
func (ctx *WithContext) Delete(record interface{}) error {
return mustDelete(ctx.Exec, record)
}