-
Notifications
You must be signed in to change notification settings - Fork 2
/
row_key.go
65 lines (52 loc) · 1.13 KB
/
row_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
package goen
import (
"sort"
sqr "github.com/Masterminds/squirrel"
"github.com/kamichidu/goen/dialect"
)
type RowKey interface {
sqr.Sqlizer
TableName() string
RowKey() ([]string, []interface{})
ToSqlizerWithDialect(dialect.Dialect) sqr.Sqlizer
}
type MapRowKey struct {
Table string
Key map[string]interface{}
}
func (key *MapRowKey) TableName() string {
return key.Table
}
func (key *MapRowKey) RowKey() ([]string, []interface{}) {
cols := make([]string, 0, len(key.Key))
for col := range key.Key {
cols = append(cols, col)
}
sort.Strings(cols)
vals := make([]interface{}, len(cols))
for i := range cols {
vals[i] = key.Key[cols[i]]
}
return cols, vals
}
func (key *MapRowKey) ToSql() (string, []interface{}, error) {
return key.toEq().ToSql()
}
func (key *MapRowKey) ToSqlizerWithDialect(dialect dialect.Dialect) sqr.Sqlizer {
eq := key.toEq()
for col := range eq {
val := eq[col]
delete(eq, col)
eq[dialect.Quote(col)] = val
}
return eq
}
// for testing
func (key *MapRowKey) toEq() sqr.Eq {
// copy to modify column name
expr := sqr.Eq{}
for col, val := range key.Key {
expr[col] = val
}
return expr
}