-
Notifications
You must be signed in to change notification settings - Fork 0
/
context.go
40 lines (35 loc) · 888 Bytes
/
context.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
package sqlf
import (
"github.com/qjebbs/go-sqlf/v2/syntax"
)
// Context is the global context shared between all fragments building.
type Context struct {
bindVarStyle syntax.BindVarStyle
argStore argStore
parent *Context
funcs map[string]*funcInfo
frag *FragmentContext
}
// NewContext returns a new context.
func NewContext(bindVarStyle syntax.BindVarStyle) *Context {
ctx := newEmptyContext(bindVarStyle)
ctx.bindVarStyle = bindVarStyle
err := addValueFuncs(ctx.funcs, builtInFuncs)
if err != nil {
// should never happen for builtInFuncs
panic(err)
}
return ctx
}
func newEmptyContext(bindVarStyle syntax.BindVarStyle) *Context {
var argStore argStore
if bindVarStyle == syntax.Dollar {
argStore = newDollarArgStore()
} else {
argStore = newQuestionArgStore()
}
return &Context{
funcs: make(map[string]*funcInfo),
argStore: argStore,
}
}