forked from si3nloong/sqlike
-
Notifications
You must be signed in to change notification settings - Fork 0
/
select.go
133 lines (119 loc) · 2.7 KB
/
select.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
package sql
import (
"reflect"
"github.com/RevenueMonster/sqlike/reflext"
"github.com/RevenueMonster/sqlike/sql/expr"
"github.com/RevenueMonster/sqlike/sqlike/primitive"
)
// SelectStmt :
type SelectStmt struct {
DistinctOn bool
Tables []interface{}
Projections []interface{}
Joins []interface{}
IndexHints string
Conditions primitive.Group
Havings primitive.Group
Groups []interface{}
Sorts []interface{}
Max uint
Skip uint
}
// Select :
func Select(fields ...interface{}) *SelectStmt {
stmt := new(SelectStmt)
return stmt.Select(fields...)
}
// Select :
func (stmt *SelectStmt) Select(fields ...interface{}) *SelectStmt {
if len(fields) == 1 {
switch fields[0].(type) {
case primitive.As, *SelectStmt:
grp := primitive.Group{}
grp.Values = append(grp.Values, primitive.Raw{Value: "("})
grp.Values = append(grp.Values, fields...)
grp.Values = append(grp.Values, primitive.Raw{Value: ")"})
stmt.Projections = append(stmt.Projections, grp)
default:
stmt.Projections = append(stmt.Projections, fields...)
}
return stmt
}
stmt.Projections = fields
return stmt
}
// From :
func (stmt *SelectStmt) From(values ...interface{}) *SelectStmt {
length := len(values)
if length == 0 {
panic("empty table name")
}
switch length {
case 1:
stmt.Tables = append(stmt.Tables, values[0])
case 2:
stmt.Tables = append(stmt.Tables,
primitive.Column{
Table: mustString(values[0]),
Name: mustString(values[1]),
},
)
case 3:
stmt.Tables = append(stmt.Tables,
primitive.Column{
Table: mustString(values[0]),
Name: mustString(values[1]),
},
values[2],
)
default:
panic("invalid length of arguments")
}
return stmt
}
// Distinct :
func (stmt *SelectStmt) Distinct() *SelectStmt {
stmt.DistinctOn = true
return stmt
}
// Where :
func (stmt *SelectStmt) Where(fields ...interface{}) *SelectStmt {
stmt.Conditions = expr.And(fields...)
return stmt
}
// Having :
func (stmt *SelectStmt) Having(fields ...interface{}) *SelectStmt {
stmt.Havings = expr.And(fields...)
return stmt
}
// OrderBy :
func (stmt *SelectStmt) OrderBy(fields ...interface{}) *SelectStmt {
stmt.Sorts = fields
return stmt
}
// GroupBy :
func (stmt *SelectStmt) GroupBy(fields ...interface{}) *SelectStmt {
stmt.Groups = fields
return stmt
}
// Limit :
func (stmt *SelectStmt) Limit(num uint) *SelectStmt {
if num > 0 {
stmt.Max = num
}
return stmt
}
// Offset :
func (stmt *SelectStmt) Offset(num uint) *SelectStmt {
if num > 0 {
stmt.Skip = num
}
return stmt
}
func mustString(it interface{}) string {
v := reflext.Indirect(reflext.ValueOf(it))
if v.Kind() != reflect.String {
panic("unsupported data type")
}
return v.String()
}