-
-
Notifications
You must be signed in to change notification settings - Fork 6
/
builder.go
53 lines (43 loc) · 1.11 KB
/
builder.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
package sqlstmt
import (
"fmt"
"sync"
"reflect"
"github.com/si3nloong/sqlike/reflext"
)
// BuildStatementFunc :
type BuildStatementFunc func(stmt Stmt, it interface{}) error
// StatementBuilder :
type StatementBuilder struct {
mutex *sync.Mutex
builders map[interface{}]BuildStatementFunc
}
// NewStatementBuilder :
func NewStatementBuilder() *StatementBuilder {
return &StatementBuilder{
mutex: new(sync.Mutex),
builders: make(map[interface{}]BuildStatementFunc),
}
}
// SetBuilder :
func (sb *StatementBuilder) SetBuilder(it interface{}, p BuildStatementFunc) {
sb.mutex.Lock()
defer sb.mutex.Unlock()
sb.builders[it] = p
}
// LookupBuilder :
func (sb *StatementBuilder) LookupBuilder(t reflect.Type) (blr BuildStatementFunc, ok bool) {
blr, ok = sb.builders[t]
return
}
// BuildStatement :
func (sb *StatementBuilder) BuildStatement(stmt Stmt, it interface{}) error {
v := reflext.ValueOf(it)
if x, ok := sb.builders[v.Type()]; ok {
return x(stmt, it)
}
if x, ok := sb.builders[v.Kind()]; ok {
return x(stmt, it)
}
return fmt.Errorf("sqlstmt: invalid data type support %v", v.Type())
}