-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathgransak.go
67 lines (50 loc) · 1.26 KB
/
gransak.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
package gransak
import (
"net/http"
"net/url"
"strings"
"github.com/crowdint/gransak/core"
)
var (
Gransak *GransakFilter
)
type GransakFilter struct {
core *core.GransakCore
tableName string
engine string
}
func init() {
if Gransak == nil {
core := core.NewGransak()
Gransak = &GransakFilter{
core: core,
}
}
}
func (this *GransakFilter) ToSql(input string, param interface{}) (string, []interface{}) {
statement, parsedParams := this.core.Parse(input, param)
result := this.appendSelectStatement(statement)
result = ReplaceByEngineHolders(result, parsedParams)
this.tableName = ""
return result, parsedParams
}
func (this *GransakFilter) Table(tableName string) *GransakFilter {
this.tableName = tableName
return this
}
func (this *GransakFilter) appendSelectStatement(statement string) string {
table := strings.Trim(this.tableName, " ")
if table != "" {
return "SELECT * FROM " + table + " WHERE " + statement
}
return statement
}
func (this *GransakFilter) FromRequest(r *http.Request) (string, []interface{}) {
return parseRequest(r)
}
func (this *GransakFilter) FromUrlValues(v url.Values) (string, []interface{}) {
return parseUrlValues(v)
}
func (this *GransakFilter) SetEngine(engine string) {
core.ENGINE = engine
}