-
Notifications
You must be signed in to change notification settings - Fork 0
/
sql_generators.go
147 lines (139 loc) · 4.04 KB
/
sql_generators.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
package goquery
import (
"errors"
"fmt"
"reflect"
"strings"
)
func getSelectStatement(ds DataSet, key string, stmt string, suffix string, appends []interface{}, dest any) (string, error) {
var ok bool
if key != "" || stmt == "" {
if ds == nil {
return "", errors.New("Missing Dataset when referencing a statement key")
}
switch {
case key != "":
if stmt, ok = ds.Commands()[key]; !ok {
return "", fmt.Errorf("unable to find statement for %s: %s", ds.Entity(), key)
}
case stmt == "":
if stmt, ok = ds.Commands()[selectkey]; !ok {
if ds.Fields() != nil {
stmt = ToSelectStmtDepricated(ds)
} else if dest != nil {
stmt = ToSelectStmt(ds, dest)
} else {
return "", errors.New("unable to build query statement")
}
ds.PutCommand(selectkey, stmt)
}
}
}
stmt = fmt.Sprintf("%s %s", stmt, suffix)
return fmt.Sprintf(stmt, appends...), nil
}
func ToSelectStmt(ds DataSet, dest any) string {
var fieldsBuilder strings.Builder
fieldsBuilder.WriteString("select ")
typ := reflect.TypeOf(dest).Elem()
if typ.Kind() == reflect.Slice {
typ = typ.Elem()
}
fieldNum := typ.NumField()
field := 0
for i := 0; i < fieldNum; i++ {
if tagval, ok := typ.Field(i).Tag.Lookup("db"); ok && tagval != "_" {
if field > 0 {
fieldsBuilder.WriteRune(',')
}
fieldsBuilder.WriteString(tagval)
field++
}
}
fieldsBuilder.WriteString(fmt.Sprintf(" from %s", ds.Entity()))
return fieldsBuilder.String()
}
func ToSelectStmtDepricated(ds DataSet) string {
var fieldsBuilder strings.Builder
fieldsBuilder.WriteString("select ")
typ := reflect.TypeOf(ds.Fields())
fieldNum := typ.NumField()
field := 0
for i := 0; i < fieldNum; i++ {
if tagval, ok := typ.Field(i).Tag.Lookup("db"); ok && tagval != "_" {
if field > 0 {
fieldsBuilder.WriteRune(',')
}
fieldsBuilder.WriteString(tagval)
field++
}
}
fieldsBuilder.WriteString(fmt.Sprintf(" from %s", ds.Entity()))
return fieldsBuilder.String()
}
func ToInsert(ds DataSet, dialect DbDialect) (string, error) {
var fieldBuilder strings.Builder
var bindBuilder strings.Builder
typ := reflect.TypeOf(ds.Fields())
fieldNum := typ.NumField()
fieldcount := 0
paramcount := 0
for i := 0; i < fieldNum; i++ {
if dbfield, ok := typ.Field(i).Tag.Lookup("db"); ok && dbfield != "_" {
if fieldcount > 0 {
fieldBuilder.WriteRune(',')
bindBuilder.WriteRune(',')
}
if idtype, ok := typ.Field(i).Tag.Lookup("dbid"); ok {
if idtype != "AUTOINCREMENT" {
if idsequence, ok := typ.Field(i).Tag.Lookup("idsequence"); ok {
fieldBuilder.WriteString(dbfield)
bindBuilder.WriteString(dialect.Seq(idsequence))
fieldcount++
} else {
return "", errors.New("invalid id. sequence type must have an 'idsequence' tag")
}
}
} else {
fieldBuilder.WriteString(dbfield)
bindBuilder.WriteString(dialect.Bind(dbfield, paramcount))
fieldcount++
paramcount++
}
}
}
return fmt.Sprintf("insert into %s (%s) values (%s)", ds.Entity(), fieldBuilder.String(), bindBuilder.String()), nil
}
func ToUpdate(ds DataSet, bindTemplateFunction BindParamTemplateFunction) string {
var fieldsBuilder strings.Builder
var criteria string
typ := reflect.TypeOf(ds.Fields())
fieldNum := typ.NumField()
field := 0
for i := 0; i < fieldNum; i++ {
if tagval, ok := typ.Field(i).Tag.Lookup("db"); ok && tagval != "_" {
if field > 0 {
fieldsBuilder.WriteRune(',')
}
if _, ok := typ.Field(i).Tag.Lookup("dbid"); ok {
criteria = fmt.Sprintf(" where %s = %s", tagval, bindTemplateFunction(tagval, field))
continue //skip id field
}
fieldsBuilder.WriteString(fmt.Sprintf("%s = %s", tagval, bindTemplateFunction(tagval, field)))
field++
}
}
return fmt.Sprintf("update %s set %s %s", ds.Entity(), fieldsBuilder.String(), criteria)
}
func IdField(ds DataSet) string {
typ := reflect.TypeOf(ds.Fields())
fieldNum := typ.NumField()
for i := 0; i < fieldNum; i++ {
if tagval, ok := typ.Field(i).Tag.Lookup("db"); ok {
if _, ok := typ.Field(i).Tag.Lookup("dbid"); ok {
return tagval
}
}
}
return ""
}