/
statements.go
119 lines (102 loc) · 3.05 KB
/
statements.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
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
/*
Copyright 2023 eatmoreapple
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
package juice
import (
"github.com/eatmoreapple/juice/driver"
"regexp"
)
type Statement interface {
ID() string
Name() string
Attribute(key string) string
Action() Action
Configuration() IConfiguration
ResultMap() (ResultMap, error)
Build(translator driver.Translator, param Param) (query string, args []any, err error)
}
var formatRegexp = regexp.MustCompile(`\$\{ *?([a-zA-Z0-9_\.]+) *?\}`)
// xmlSQLStatement defines a sql xmlSQLStatement.
type xmlSQLStatement struct {
mapper *Mapper
action Action
Nodes NodeGroup
attrs map[string]string
name string
id string
}
// Attribute returns the value of the attribute with the given key.
func (s *xmlSQLStatement) Attribute(key string) string {
value := s.attrs[key]
if value == "" {
value = s.mapper.Attribute(key)
}
return value
}
// setAttribute sets the attribute with the given key and value.
func (s *xmlSQLStatement) setAttribute(key, value string) {
if s.attrs == nil {
s.attrs = make(map[string]string)
}
s.attrs[key] = value
}
// ID returns the unique key of the namespace.
func (s *xmlSQLStatement) ID() string {
return s.id
}
func (s *xmlSQLStatement) lazyName() string {
var builder = getBuilder()
defer putBuilder(builder)
if prefix := s.mapper.mappers.Prefix(); prefix != "" {
builder.WriteString(prefix)
builder.WriteString(".")
}
builder.WriteString(s.mapper.namespace)
builder.WriteString(".")
builder.WriteString(s.id)
return builder.String()
}
// Name is a unique key of the whole xmlSQLStatement.
func (s *xmlSQLStatement) Name() string {
if s.name == "" {
s.name = s.lazyName()
}
return s.name
}
// Action returns the action of the xmlSQLStatement.
func (s *xmlSQLStatement) Action() Action {
return s.action
}
// Configuration returns the configuration of the xmlSQLStatement.
func (s *xmlSQLStatement) Configuration() IConfiguration {
return s.mapper.Configuration()
}
// ResultMap returns the ResultMap of the xmlSQLStatement.
func (s *xmlSQLStatement) ResultMap() (ResultMap, error) {
key := s.Attribute("resultMap")
if key == "" {
return nil, ErrResultMapNotSet
}
return s.mapper.GetResultMapByID(key)
}
// Build builds the xmlSQLStatement with the given parameter.
func (s *xmlSQLStatement) Build(translator driver.Translator, param Param) (query string, args []any, err error) {
value := newGenericParam(param, s.Attribute("paramName"))
query, args, err = s.Nodes.Accept(translator, value)
if err != nil {
return "", nil, err
}
if len(query) == 0 {
return "", nil, ErrEmptyQuery
}
return query, args, nil
}