-
Notifications
You must be signed in to change notification settings - Fork 1
/
query.go
108 lines (95 loc) · 2.79 KB
/
query.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
package struct2sql
import (
"reflect"
"strings"
)
//
//
// Generate Query
//
//
func CreateTableQuery(table SQLTable) string {
tableName := strings.ToLower(table.Name)
query := "CREATE TABLE " + tableName + " ("
detail := []string{}
// Fields
for _, f := range table.Fields {
str := strings.ToLower(f.Name) + " "
if f.CustomType != "" {
str += f.CustomType
} else {
str += GoType2SQLType(f.Type)
}
if f.Keyword != "" {
str += " " + f.Keyword
}
detail = append(detail, str)
}
// Keys
if table.PrimaryKey != -1 {
detail = append(detail, "PRIMARY KEY ("+strings.ToLower(table.Fields[table.PrimaryKey].Name)+")")
}
for _, f := range table.ForeignKey {
str := "FOREIGN KEY (" + strings.ToLower(table.Fields[f.FieldIndex].Name) + ") REFERENCES " + f.Reference
detail = append(detail, str)
}
// Combine the query
query = query + strings.Join(detail, ", ")
query += ");"
return query
}
func DropTableQuery(tables ...SQLTable) string {
tablesName := make([]string, len(tables))
for i, table := range tables {
tablesName[i] = strings.ToLower(table.Name)
}
query := "DROP TABLE " + strings.Join(tablesName, ", ") + ";"
return query
}
func InsertQuery(model interface{}) string {
t := reflect.Indirect(reflect.ValueOf(model)).Type()
table := getTable(t)
fields := []string{}
questionMark := []string{}
for i, f := range table.Fields {
if i != table.PrimaryKey {
fields = append(fields, strings.ToLower(f.Name))
questionMark = append(questionMark, "?")
}
}
query := "INSERT INTO " + strings.ToLower(table.Name) + "(" + strings.Join(fields, ", ") + ") VALUES(" + strings.Join(questionMark, ", ") + ");"
return query
}
func SelectQuery(model interface{}, condition string) string {
t := reflect.Indirect(reflect.ValueOf(model)).Type()
var table SQLTable
if t.Kind() == reflect.Slice {
table = getTable(t.Elem())
} else {
table = getTable(t)
}
fields := []string{}
for _, f := range table.Fields {
fields = append(fields, strings.ToLower(f.Name))
}
return "SELECT " + strings.Join(fields, ", ") + " FROM " + strings.ToLower(table.Name) + " WHERE " + condition + ";"
}
func UpdateQuery(model interface{}, condition string) string {
t := reflect.Indirect(reflect.ValueOf(model)).Type()
table := getTable(t)
fields := []string{}
for i, f := range table.Fields {
if i != table.PrimaryKey {
fields = append(fields, strings.ToLower(f.Name)+"=?")
}
}
return "UPDATE " + strings.ToLower(table.Name) + " SET " + strings.Join(fields, ", ") + " WHERE " + condition + ";"
}
func DeleteQuery(model interface{}, condition string) string {
t := reflect.Indirect(reflect.ValueOf(model)).Type()
table := getTable(t)
return "DELETE FROM " + strings.ToLower(table.Name) + " WHERE " + condition + ";"
}
func Escape(str string) string {
return strings.Replace(str, "'", "''", -1)
}