-
Notifications
You must be signed in to change notification settings - Fork 10
/
Copy pathmapper.go
212 lines (186 loc) · 5.49 KB
/
mapper.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
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
package juice
import (
"errors"
"fmt"
"reflect"
"strings"
"github.com/go-juicedev/juice/internal/container"
)
// Mapper defines a set of statements.
type Mapper struct {
namespace string
mappers *Mappers
statements map[string]*xmlSQLStatement
sqlNodes map[string]*SQLNode
attrs map[string]string
}
// Namespace returns the namespace of the mapper.
func (m *Mapper) Namespace() string {
return m.namespace
}
// Mappers is an getter of mappers.
func (m *Mapper) Mappers() *Mappers {
return m.mappers
}
func (m *Mapper) setAttribute(key, value string) {
if m.attrs == nil {
m.attrs = make(map[string]string)
}
m.attrs[key] = value
}
func (m *Mapper) setSqlNode(node *SQLNode) error {
if m.sqlNodes == nil {
m.sqlNodes = make(map[string]*SQLNode)
}
if _, exists := m.sqlNodes[node.ID()]; exists {
return fmt.Errorf("sql node %s already exists", node.ID())
}
m.sqlNodes[node.ID()] = node
return nil
}
// Attribute returns the attribute value by key.
func (m *Mapper) Attribute(key string) string {
return m.attrs[key]
}
// Prefix returns the prefix of the mapper.
func (m *Mapper) Prefix() string {
return m.Attribute("prefix")
}
func (m *Mapper) GetSQLNodeByID(id string) (Node, error) {
// if the id is not cross-namespace
isCrossNamespace := strings.Contains(id, ".")
if !isCrossNamespace {
node, exists := m.sqlNodes[id]
if !exists {
return nil, &ErrSQLNodeNotFound{NodeName: id, MapperName: m.namespace}
}
return node, nil
} else {
return m.mappers.GetSQLNodeByID(id)
}
}
// ErrSQLNodeNotFound indicates that the SQL node was not found in the mapper
type ErrSQLNodeNotFound struct {
NodeName string
MapperName string
}
func (e ErrSQLNodeNotFound) Error() string {
return fmt.Sprintf("SQL node %q not found in mapper %q", e.NodeName, e.MapperName)
}
// Mappers is a container for all mappers.
type Mappers struct {
attrs map[string]string
cfg IConfiguration
// mappers uses Trie instead of map because mapper namespaces often share common prefixes
// (e.g., "com.example.user", "com.example.order"). Trie provides both memory efficiency
// by storing shared prefixes only once and fast prefix-based lookups
mappers *container.Trie[*Mapper]
}
func (m *Mappers) setMapper(key string, mapper *Mapper) error {
if prefix := m.Prefix(); prefix != "" {
key = fmt.Sprintf("%s.%s", prefix, key)
}
if m.mappers == nil {
m.mappers = container.NewTrie[*Mapper]()
}
if _, exists := m.mappers.Get(key); exists {
return fmt.Errorf("mapper %s already exists", key)
}
mapper.mappers = m
m.mappers.Insert(key, mapper)
return nil
}
func (m *Mappers) GetMapperByNamespace(namespace string) (*Mapper, bool) {
if m.mappers == nil {
return nil, false
}
return m.mappers.Get(namespace)
}
func (m *Mappers) getMapperAndKey(id string) (mapper *Mapper, key string, err error) {
lastDotIndex := strings.LastIndex(id, ".")
if lastDotIndex <= 0 {
return nil, "", ErrInvalidStatementID
}
namespace, key := id[:lastDotIndex], id[lastDotIndex+1:]
mapper, exists := m.GetMapperByNamespace(namespace)
if !exists {
return nil, "", ErrMapperNotFound(namespace)
}
return mapper, key, nil
}
// GetStatementByID returns a Statement by id.
// The id should be in the format of "namespace.statementName"
// For example: "main.UserMapper.SelectUser"
func (m *Mappers) GetStatementByID(id string) (Statement, error) {
if m == nil {
return nil, fmt.Errorf("%w: statement '%s' not found in mapper configuration", ErrNoStatementFound, id)
}
mapper, key, err := m.getMapperAndKey(id)
if err != nil {
return nil, err
}
stmt, exists := mapper.statements[key]
if !exists {
return nil, &ErrStatementNotFound{StatementName: key, MapperName: mapper.namespace}
}
return stmt, nil
}
func (m *Mappers) GetSQLNodeByID(id string) (Node, error) {
mapper, key, err := m.getMapperAndKey(id)
if err != nil {
return nil, err
}
node, exists := mapper.sqlNodes[key]
if !exists {
return nil, &ErrSQLNodeNotFound{NodeName: key, MapperName: mapper.namespace}
}
return node, nil
}
// GetStatement try to one the xmlSQLStatement from the Mappers with the given interface
func (m *Mappers) GetStatement(v any) (Statement, error) {
var id string
// if the interface is StatementIDGetter, use the StatementID() method to get the id
// or if the interface is a string type, use the string as the id
// otherwise, use the reflection to get the id
switch t := v.(type) {
case interface{ StatementID() string }:
id = t.StatementID()
case string:
id = t
default:
// else try to one the id from the interface
rv := reflect.Indirect(reflect.ValueOf(v))
switch rv.Kind() {
case reflect.Func:
id = cachedRuntimeFuncName(rv.Pointer())
case reflect.Struct:
id = rv.Type().PkgPath() + "." + rv.Type().Name()
default:
return nil, errors.New("invalid type of xmlSQLStatement id")
}
}
if len(id) == 0 {
return nil, errors.New("can not get the xmlSQLStatement id from the given interface")
}
return m.GetStatementByID(id)
}
// Configuration represents a configuration of juice.
func (m *Mappers) Configuration() IConfiguration {
return m.cfg
}
// setAttribute sets an attribute.
// same as setAttribute, but it is used for Mappers.
func (m *Mappers) setAttribute(key, value string) {
if m.attrs == nil {
m.attrs = make(map[string]string)
}
m.attrs[key] = value
}
// Attribute returns an attribute from the Mappers attributes.
func (m *Mappers) Attribute(key string) string {
return m.attrs[key]
}
// Prefix returns the prefix of the Mappers.
func (m *Mappers) Prefix() string {
return m.Attribute("prefix")
}