-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathmodifier.go
More file actions
119 lines (106 loc) · 2.57 KB
/
modifier.go
File metadata and controls
119 lines (106 loc) · 2.57 KB
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
// A quickly mysql access component.
//
// Copyright 2023 The daog Authors. All rights reserved.
package daog
import (
"strings"
)
const (
selfAdd = 1
selfMinus = 2
)
type pair struct {
column string
value any
self int
}
func (p *pair) isSelf() bool {
return p.self == selfAdd || p.self == selfMinus
}
// NewModifier 创建 Modifier 对象
func NewModifier() Modifier {
return &internalModifier{
preventRepeat: map[string]*pair{},
}
}
// Modifier 描述 update 语义中set cause的生成,通过 Modifier 来避免自己拼接sql片段,降低出错概率,
// 最终生成 update tab set xx=?,bb=? 的 sql 片段
type Modifier interface {
// Add 增加一个字段的修改,比如 id = 100
Add(column string, value any) Modifier
SelfAdd(column string, value any) Modifier
SelfMinus(column string, value any) Modifier
toSQL(tableName string) (string, []any)
getPureChangePairs() ([]string, []any)
}
type internalModifier struct {
preventRepeat map[string]*pair
modifies []*pair
}
func (m *internalModifier) Add(column string, value any) Modifier {
old, ok := m.preventRepeat[column]
if ok {
old.value = value
old.self = 0
return m
}
p := &pair{column, value, 0}
m.preventRepeat[column] = p
m.modifies = append(m.modifies, p)
return m
}
func (m *internalModifier) SelfAdd(column string, value any) Modifier {
old, ok := m.preventRepeat[column]
if ok {
old.value = value
old.self = 1
return m
}
p := &pair{column, value, 1}
m.preventRepeat[column] = p
m.modifies = append(m.modifies, p)
return m
}
func (m *internalModifier) SelfMinus(column string, value any) Modifier {
old, ok := m.preventRepeat[column]
if ok {
old.value = value
old.self = 2
return m
}
p := &pair{column, value, 2}
m.preventRepeat[column] = p
m.modifies = append(m.modifies, p)
return m
}
func (m *internalModifier) toSQL(tableName string) (string, []any) {
l := len(m.modifies)
if l == 0 {
return "", nil
}
modStmt := make([]string, l)
args := make([]any, l)
for i, p := range m.modifies {
if p.self == selfAdd {
modStmt[i] = p.column + "=" + p.column + "+?"
} else if p.self == selfMinus {
modStmt[i] = p.column + "=" + p.column + "-?"
} else {
modStmt[i] = p.column + "=?"
}
args[i] = p.value
}
return "update " + tableName + " set " + strings.Join(modStmt, ","), args
}
func (m *internalModifier) getPureChangePairs() ([]string, []any) {
var columns []string
var values []any
for _, p := range m.modifies {
if p.isSelf() {
continue
}
columns = append(columns, p.column)
values = append(values, p.value)
}
return columns, values
}