-
Notifications
You must be signed in to change notification settings - Fork 0
/
sql.go
189 lines (131 loc) · 3.94 KB
/
sql.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
package minidal
import (
"fmt"
"strings"
)
type Driver string
const (
SQLite Driver = "sqlite3" // tested with "github.com/mattn/go-sqlite3"
MySQL Driver = "mysql" // tested with "github.com/go-sql-driver/mysql"
)
type SortOrder string
const (
ASC SortOrder = "ASC"
DESC SortOrder = "DESC"
)
type GlueOperator string
const (
AND GlueOperator = "AND"
OR GlueOperator = "OR"
)
func __where(where Object, values []any, startIndex int) ([]string, []any, int, error) {
setWhere := make([]string, len(where))
j := 0
for k := range where {
if where[k] == nil {
setWhere[j] = k + " IS ?"
} else {
setWhere[j] = k + " = ?"
}
values[startIndex] = where[k]
startIndex += 1
j += 1
}
return setWhere, values, startIndex, nil
}
func __orderby(clause Object) ([]string, error) {
setOrderBy := make([]string, len(clause))
j := 0
for k := range clause {
setOrderBy[j] = k + " " + string(clause[k].(SortOrder))
j += 1
}
return setOrderBy, nil
}
func __insert(table string, data Object) (string, []any, error) {
columns := make([]string, len(data))
placeholders := make([]string, len(data))
values := make([]any, len(data))
i := 0
for k := range data {
columns[i] = k
placeholders[i] = "?"
values[i] = data[k]
i += 1
}
return fmt.Sprintf("INSERT INTO %s (%s) VALUES (%s)", table, strings.Join(columns, ","), strings.Join(placeholders, ",")), values, nil
}
func __insertBulk(table string, data []Object) (string, []any, error) {
columns := []string{}
placeholders := [][]string{}
values := make([]any, 0)
for j, idata := range data {
i := 0
placeholder := []string{}
for k := range idata {
placeholder = []string{}
if j == 0 {
columns = append(columns, k)
}
placeholder = append(placeholder, "?")
values = append(values, idata[k])
i += 1
}
placeholders = append(placeholders, placeholder)
}
setValues := []string{}
for j := range placeholders {
setValues = append(setValues, "("+strings.Join(placeholders[j], ",")+")")
}
return fmt.Sprintf("INSERT INTO %s (%s) VALUES %s", table, strings.Join(columns, ","), strings.Join(setValues, ",")), values, nil
}
func __update(table string, glue string, where Object, data Object) (string, []any, error) {
set := make([]string, len(data))
values := make([]any, len(data)+len(where))
i := 0
for k := range data {
set[i] = k + " = ?"
values[i] = data[k]
i += 1
}
setWhere, values, _, err := __where(where, values, i)
if err != nil {
panic(fmt.Errorf("invalid Where Clause %v", where))
}
return fmt.Sprintf("UPDATE %s SET %s WHERE %s", table, strings.Join(set, ","), strings.Join(setWhere, glue)), values, nil
}
func __first(table string, glue string, where Object) (string, []any, error) {
i := 0
values := make([]any, len(where))
setWhere, values, _, err := __where(where, values, i)
if err != nil {
panic(fmt.Errorf("invalid Where Clause %v", where))
}
return fmt.Sprintf("SELECT * FROM %s WHERE %s LIMIT 1", table, strings.Join(setWhere, glue)), values, nil
}
func __find(table string, glue string, where Object, orderby Object) (string, []any, error) {
i := 0
values := make([]any, len(where))
setWhere, values, _, err := __where(where, values, i)
if err != nil {
panic(fmt.Errorf("invalid Where Clause %v", where))
}
whereClause := ""
if len(values) > 0 {
whereClause = "WHERE "
}
setOrderBy, err := __orderby(orderby)
if err != nil {
panic(fmt.Errorf("invalid OrderBy Clause %v", orderby))
}
return fmt.Sprintf("SELECT * FROM %s %s%s ORDER BY %s", table, whereClause, strings.Join(setWhere, " "+glue+" "), strings.Join(setOrderBy, ", ")), values, nil
}
func __delete(table string, glue string, where Object) (string, []any, error) {
values := make([]any, len(where))
i := 0
deleteWhere, values, _, err := __where(where, values, i)
if err != nil {
panic(fmt.Errorf("invalid Where Clause %v", where))
}
return fmt.Sprintf("DELETE FROM %s WHERE %s", table, strings.Join(deleteWhere, glue)), values, nil
}