forked from araddon/qlbridge
-
Notifications
You must be signed in to change notification settings - Fork 0
/
key.go
77 lines (67 loc) · 1.76 KB
/
key.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
package datasource
import (
"database/sql/driver"
u "github.com/araddon/gou"
"github.com/araddon/qlbridge/expr"
"github.com/araddon/qlbridge/rel"
"github.com/araddon/qlbridge/schema"
)
var (
_ schema.Key = (*KeyInt)(nil)
_ schema.Key = (*KeyInt64)(nil)
_ schema.Key = (*KeyCol)(nil)
)
// Variety of Key Types
type (
KeyInt struct {
Id int
}
KeyInt64 struct {
Id int64
}
KeyCol struct {
Name string
Val driver.Value
}
)
func NewKeyInt(key int) KeyInt { return KeyInt{key} }
func (m *KeyInt) Key() driver.Value { return driver.Value(m.Id) }
//func (m KeyInt) Less(than Item) bool { return m.Id < than.(KeyInt).Id }
func NewKeyInt64(key int64) KeyInt64 { return KeyInt64{key} }
func (m *KeyInt64) Key() driver.Value { return driver.Value(m.Id) }
func NewKeyCol(name string, val driver.Value) KeyCol { return KeyCol{name, val} }
func (m KeyCol) Key() driver.Value { return m.Val }
// Given a Where expression, lets try to create a key which
// requires form `idenity = "value"`
//
func KeyFromWhere(wh interface{}) schema.Key {
switch n := wh.(type) {
case *rel.SqlWhere:
return KeyFromWhere(n.Expr)
case *expr.BinaryNode:
if len(n.Args) != 2 {
u.Warnf("need more args? %#v", n.Args)
return nil
}
in, ok := n.Args[0].(*expr.IdentityNode)
if !ok {
u.Warnf("not identity? %T", n.Args[0])
return nil
}
// This only allows for identity = value
// NOT: identity = expr(identity, arg)
//
switch valT := n.Args[1].(type) {
case *expr.NumberNode:
return NewKeyCol(in.Text, valT.Float64)
case *expr.StringNode:
return NewKeyCol(in.Text, valT.Text)
//case *expr.FuncNode:
default:
u.Warnf("not supported arg? %#v", valT)
}
default:
u.Warnf("not supported node type? %#v", n)
}
return nil
}