-
Notifications
You must be signed in to change notification settings - Fork 2
/
model.go
136 lines (127 loc) · 2.94 KB
/
model.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
package gosql
import (
"reflect"
"strings"
)
type model struct {
name string
table string
typ reflect.Type
fields []string
primaryFieldIndecies []int
}
func isIntIn(i int, arr []int) bool {
for _, arrInt := range arr {
if arrInt == i {
return true
}
}
return false
}
func (m *model) getInsertQuery(v reflect.Value) string {
var query strings.Builder
var values strings.Builder
query.WriteString("insert into ")
query.WriteString(m.table)
query.WriteString(" (")
for i := 0; i < len(m.fields); i++ {
if isIntIn(i, m.primaryFieldIndecies) && v.Field(i).IsZero() {
if i == len(m.fields)-1 {
query.WriteString(") ")
values.WriteString(")")
}
continue
}
query.WriteString(m.fields[i])
values.WriteString("?")
if i == len(m.fields)-1 {
query.WriteString(") ")
values.WriteString(")")
continue
}
if !isIntIn(i+1, m.primaryFieldIndecies) || !v.Field(i+1).IsZero() {
query.WriteString(", ")
values.WriteString(", ")
}
}
query.WriteString("values (")
query.WriteString(values.String())
return query.String()
}
func (m *model) getDeleteQuery() string {
var query strings.Builder
query.WriteString("delete from ")
query.WriteString(m.table)
query.WriteString(" where ")
for i, index := range m.primaryFieldIndecies {
query.WriteString(m.fields[index])
query.WriteString(" = ?")
if i < len(m.primaryFieldIndecies)-1 {
query.WriteString(" and ")
}
}
return query.String()
}
func (m *model) getUpdateQuery() string {
var query strings.Builder
query.WriteString("update ")
query.WriteString(m.table)
query.WriteString(" set ")
for i := 0; i < len(m.fields); i++ {
if isIntIn(i, m.primaryFieldIndecies) {
continue
}
query.WriteString(m.fields[i])
query.WriteString(" = ?")
if i < len(m.fields)-1 && !isIntIn(i+1, m.primaryFieldIndecies) {
query.WriteString(", ")
}
}
query.WriteString(" where ")
for i, index := range m.primaryFieldIndecies {
query.WriteString(m.fields[index])
query.WriteString(" = ?")
if i < len(m.primaryFieldIndecies)-1 {
query.WriteString(" and ")
}
}
return query.String()
}
func (m *model) getFieldIndexByName(name string) int {
for i, f := range m.fields {
if name == f || strings.HasSuffix(name, "."+f) {
return i
}
}
return -1
}
func (m *model) getArgs(v reflect.Value) []interface{} {
var args []interface{}
for i := 0; i < len(m.fields); i++ {
f := v.Field(i)
if isIntIn(i, m.primaryFieldIndecies) && f.IsZero() {
continue
}
args = append(args, f.Interface())
}
return args
}
func (m *model) getArgsPrimaryLast(v reflect.Value) []interface{} {
var args []interface{}
var primaryArgs []interface{}
i := 0
for {
if i == len(m.fields) {
break
}
arg := v.Field(i).Interface()
if isIntIn(i, m.primaryFieldIndecies) {
primaryArgs = append(primaryArgs, arg)
} else {
args = append(args, arg)
}
i++
}
args = append(args, primaryArgs...)
return args
}