-
Notifications
You must be signed in to change notification settings - Fork 0
/
dml.go
65 lines (55 loc) · 1.57 KB
/
dml.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
package spnr
import (
"context"
"fmt"
)
const dmlLogTemplate = "executing dml... sql:%s, params:%s"
// DML offers ORM with DML.
// It also contains read operations (call Reader method.)
type DML struct {
table string
logger logger
logEnabled bool
}
// Options is for specifying the options for spnr.Mutation and spnr.DML.
type Options struct {
Logger logger
LogEnabled bool
}
// NewDML initializes ORM with DML.
// It also contains read operations (call Reader method of DML.)
// If you want to use Mutation API, use New() or NewMutation() instead.
func NewDML(tableName string) *DML {
return &DML{table: tableName}
}
// NewDMLWithOptions initializes DML with options.
// Check Options for the available options.
func NewDMLWithOptions(tableName string, op *Options) *DML {
dml := &DML{table: tableName, logger: op.Logger, logEnabled: op.LogEnabled}
if dml.logger == nil {
dml.logger = newDefaultLogger()
}
return dml
}
// Reader returns Reader struct to call read operations.
func (d *DML) Reader(ctx context.Context, tx Transaction) *Reader {
return &Reader{table: d.table, ctx: ctx, tx: tx, logger: d.logger, logEnabled: d.logEnabled}
}
// GetTableName returns table name
func (d *DML) GetTableName() string {
return d.table
}
func (d *DML) getTableName() string {
return quote(d.table)
}
func (d *DML) log(sql string, params map[string]any) {
if !d.logEnabled {
return
}
var paramsStr string
for k, v := range params {
paramsStr += fmt.Sprintf("%s=%+v,", k, v)
}
paramsStr = paramsStr[:len(paramsStr)-1]
d.logger.Printf(dmlLogTemplate, sql, paramsStr)
}