Repository navigation
Expand file tree
/
Copy pathselectbuilder.go
More file actions
109 lines (86 loc) · 2.08 KB
/
Copy pathselectbuilder.go
File metadata and controls
109 lines (86 loc) · 2.08 KB
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
package cqlbuilder
import (
"bytes"
"errors"
"strconv"
)
// The select builder.
type SelectBuilder struct {
colums []string
whereConditions []conditionBuilder
limitNumber int
table string
allowFiltering bool
}
// Set a value
// If the value already been set, new value will overwrite old one.
func (c *SelectBuilder) AddColumn(name string) *SelectBuilder {
c.colums = append(c.colums, name)
return c
}
func (c *SelectBuilder) AddColumns(cols ...string) *SelectBuilder {
for _, col := range cols {
c.colums = append(c.colums, col)
}
return c
}
// Set the where condition.
func (c *SelectBuilder) Where(condition conditionBuilder) *SelectBuilder {
c.whereConditions = append(c.whereConditions, condition)
return c
}
//Set limit
func (c *SelectBuilder) SetLimit(n int) *SelectBuilder {
c.limitNumber = n
return c
}
//Set allow filtering
func (c *SelectBuilder) SetAllowFiltering(allow bool) *SelectBuilder {
c.allowFiltering = allow
return c
}
// Validate
func (c *SelectBuilder) validate() error {
if len(c.table) == 0 {
return errEmptyTable
}
if len(c.colums) == 0 {
return errEmptyColumn
}
if len(c.whereConditions) == 0 {
return errEmptyCondition
}
return nil
}
// Build the select query statement string and values.
// Example:
// SELECT col1,col2,Col3 FROM test WHERE Col4=? AND Col5=?
func (c *SelectBuilder) ToQuery() (string, []interface{}, error) {
if err := c.validate(); err != nil {
return "", nil, err
}
var buf bytes.Buffer
buf.WriteString(selectKW)
for i := 0; i < len(c.colums); i++ {
if len(c.colums[i]) == 0 {
return "", nil, errors.New("Column name can't be nil")
}
if i > 0 {
buf.WriteString(comma)
}
buf.WriteString(c.colums[i])
}
buf.WriteString(from)
buf.WriteString(c.table)
condition, conditionValues := buildCondition(c.whereConditions)
buf.WriteString(where)
buf.WriteString(condition)
if c.limitNumber > 0 {
buf.WriteString(limit)
buf.WriteString(strconv.Itoa(c.limitNumber))
}
if c.allowFiltering {
buf.WriteString(allowFiltering)
}
return buf.String(), conditionValues, nil
}