-
Notifications
You must be signed in to change notification settings - Fork 1
/
gql.go
141 lines (107 loc) · 2.44 KB
/
gql.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 go_orm
import (
"fmt"
)
/**
sql 生成器
*/
type Gql struct {
andSql string
andCount int64
orSql string
//orCount int64
orderBySql string
para []interface{}
fields[] string
isCount bool
selectSql string
t interface{}
tableName string
QueryBody *QueryBody
}
func (gql *Gql) New() *Gql {
return &Gql{}
}
func (gql *Gql) Where(andsql string) *Gql {
if gql.andCount==0 {
gql.andSql+=" where " +andsql+" "
}else {
gql.andSql+=" and " +andsql+" "
}
gql.andCount++
return gql
}
func (gql *Gql) Or(orSql string) *Gql {
gql.orSql+=" or " +orSql +" "
return gql
}
func (gql *Gql) Order(orderSql string) *Gql {
gql.orderBySql="order by "+orderSql +" "
return gql
}
func (gql *Gql) Count() *Gql {
gql.isCount=true
return gql
}
func (gql *Gql)Fields(field ...string) {
gql.fields=field
}
func (gql *Gql) GetGql(dbSetting *DbSetting) string {
if gql.t == nil {
panic("需要绑定一个结构体")
}
ss:=""
if 0 == len(gql.fields) {
gql.selectSql = " * "
}else {
for _,fd := range gql.fields{
ss+= fd +" ,"
}
gql.selectSql=ss[:len(ss)-1]
}
if gql.isCount {
return fmt.Sprintf("%s count(%s) form %s %s %s %s","select",gql.selectSql,gql.QueryBody.TableName,gql.andSql,gql.orSql,gql.orderBySql)
}
return fmt.Sprintf("%s %s from %s %s %s %s","select",gql.selectSql,gql.QueryBody.TableName,gql.andSql,gql.orSql,gql.orderBySql)
}
func (gql *Gql) SetPara(para ...interface{}) *Gql {
gql.para=para
return gql
}
func (gql *Gql) GetPara() *[]interface{} {
return &(gql.para)
}
func (gql *Gql) GetBind() interface{} {
return gql.t
}
func (gql *Gql) Bind(bind interface{}) *Gql {
gql.t=bind
return gql
}
func (gql *Gql) GetFields() ([] string) {
return gql.fields
}
func process(queryBody *QueryBody) {
/*queryBody.Ttype=reflect.TypeOf(queryBody.T)
queryBody.Tvalue=reflect.ValueOf(queryBody.T)
if queryBody.Ttype.Kind() == reflect.Ptr{
queryBody.Ttype = queryBody.Ttype.Elem()
queryBody.Tvalue = queryBody.Tvalue.Elem()
}else{
panic("请传递指针类型")
}
//判断是否是分片类型
if queryBody.Ttype.Kind()==reflect.Slice{
queryBody.IsSlice=true
}
if queryBody.IsSlice{
queryBody.Ttype = queryBody.Tvalue.Type().Elem()
tableNames := strings.Split(queryBody.Ttype.String(),".")
if len(tableNames)>0 {
queryBody.TableName = tableNames[len(tableNames)-1]
}
queryBody.Tvalue = reflect.New(queryBody.Ttype).Elem()
}else{
queryBody.TableName = queryBody.Ttype.Name()
}*/
}