-
Notifications
You must be signed in to change notification settings - Fork 0
/
mapper.go
71 lines (66 loc) · 1.67 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
package preprocessor
import (
"fmt"
"github.com/jacobsimpson/mtsql/ast"
md "github.com/jacobsimpson/mtsql/metadata"
)
type mapper struct {
names map[string][]*md.Column
qualified map[string][]*md.Column
aliases map[string][]*md.Column
}
func newMapper(columns []*md.Column) *mapper {
result := &mapper{
names: map[string][]*md.Column{},
qualified: map[string][]*md.Column{},
aliases: map[string][]*md.Column{},
}
for _, c := range columns {
result.names[c.Name] = append(result.names[c.Name], c)
if c.Qualifier != "" {
result.qualified[c.QualifiedName()] = append(result.qualified[c.QualifiedName()], c)
}
if c.Alias != "" {
result.aliases[c.Alias] = append(result.aliases[c.Alias], c)
}
}
return result
}
func (m *mapper) findMatches(a *ast.Attribute) ([]*md.Column, error) {
if a.Alias != "" {
r := m.aliases[a.Alias]
if r == nil {
return nil, fmt.Errorf("no matching alias for %q", a.Alias)
}
if len(r) > 1 {
return nil, fmt.Errorf("too many matching aliases %q", a.Alias)
}
return r, nil
}
if a.Qualifier != "" {
qualifiedName := fmt.Sprintf("%s.%s", a.Qualifier, a.Name)
r := m.qualified[qualifiedName]
if r == nil {
return nil, fmt.Errorf("no matching qualified name %q", qualifiedName)
}
if len(r) > 1 {
return nil, fmt.Errorf("too many matching qualified names %q", qualifiedName)
}
return r, nil
}
if a.Name == "*" {
r := []*md.Column{}
for _, n := range m.names {
r = append(r, n...)
}
return r, nil
}
r := m.names[a.Name]
if r == nil {
return nil, fmt.Errorf("no matching name %q", a.Name)
}
if len(r) > 1 {
return nil, fmt.Errorf("too many matching names %q", a.Name)
}
return r, nil
}