-
Notifications
You must be signed in to change notification settings - Fork 3
/
where.go
53 lines (43 loc) · 1.2 KB
/
where.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
package sqlbuilder
import (
"bytes"
)
//
// Author: 陈永佳 [email protected], [email protected]
//
type WhereBuilder struct {
ctx *SQLContext
buffer *bytes.Buffer
conditions SQLStatement
}
func (slf *WhereBuilder) Compile() string {
slf.buffer.WriteString(slf.conditions.Compile())
return slf.buffer.String()
}
func (slf *WhereBuilder) ToSQL() string {
slf.buffer.WriteString(slf.conditions.Compile())
return sqlEndpoint(slf.buffer)
}
//
func (slf *WhereBuilder) Limit(limit int) *LimitBuilder {
return newLimitBuilder(slf.ctx, slf.Compile(), limit)
}
func (slf *WhereBuilder) OrderBy(columns ...string) *OrderByBuilder {
return newOrderByBuilder(slf.ctx, slf.Compile(), columns...)
}
func (slf *WhereBuilder) GroupBy(columns ...string) *GroupByBuilder {
return newGroupByBuilder(slf.ctx, slf.Compile(), columns...)
}
func (slf *WhereBuilder) Execute() *Executor {
return newExecute(slf.ToSQL(), slf.ctx.prepare)
}
func newWhereBuilder(ctx *SQLContext, pre string, conditions SQLStatement) *WhereBuilder {
wb := &WhereBuilder{
ctx: ctx,
buffer: new(bytes.Buffer),
conditions: conditions,
}
wb.buffer.WriteString(pre)
wb.buffer.WriteString(" WHERE ")
return wb
}