-
-
Notifications
You must be signed in to change notification settings - Fork 2
/
select.go
68 lines (56 loc) · 1.17 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
package mogi
import (
"fmt"
"reflect"
"strings"
"github.com/guregu/mogi/internal/sqlparser"
)
type selectCond struct {
cols []string
}
func (sc selectCond) matches(in input) bool {
_, ok := in.statement.(*sqlparser.Select)
if !ok {
return false
}
// zero parameters means anything
if len(sc.cols) == 0 {
return true
}
return reflect.DeepEqual(lowercase(sc.cols), lowercase(in.cols()))
}
func (sc selectCond) priority() int {
if len(sc.cols) > 0 {
return 2
}
return 1
}
func (sc selectCond) String() string {
cols := "(any)" // TODO support star select
if len(sc.cols) > 0 {
cols = strings.Join(sc.cols, ", ")
}
return fmt.Sprintf("SELECT %s", cols)
}
type fromCond struct {
tables []string
}
func (fc fromCond) matches(in input) bool {
var inTables []string
switch x := in.statement.(type) {
case *sqlparser.Select:
for _, tex := range x.From {
extractTableNames(&inTables, tex)
}
}
return reflect.DeepEqual(lowercase(fc.tables), lowercase(inTables))
}
func (fc fromCond) priority() int {
if len(fc.tables) > 0 {
return 1
}
return 0
}
func (fc fromCond) String() string {
return fmt.Sprintf("FROM %s", strings.Join(fc.tables, ", "))
}