• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

stephenafamo / bob / 14939098091

09 May 2025 10:59PM UTC coverage: 36.868% (-4.6%) from 41.506%
14939098091

push

github

stephenafamo
Move reusable sqlite parser components to antlrhelpers package

0 of 758 new or added lines in 12 files covered. (0.0%)

547 existing lines in 8 files now uncovered.

6533 of 17720 relevant lines covered (36.87%)

212.48 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

0.0
/gen/bobgen-helpers/parser/antlrhelpers/visitor.go
1
package antlrhelpers
2

3
import (
4
        "fmt"
5
        "slices"
6
        "sync/atomic"
7

8
        "github.com/aarondl/opt/omit"
9
        "github.com/stephenafamo/bob/gen/bobgen-helpers/parser"
10
        "github.com/stephenafamo/bob/gen/drivers"
11
        "github.com/stephenafamo/bob/internal"
12
)
13

14
type Visitor[C, I any] struct {
15
        Err       error
16
        DB        drivers.Tables[C, I]
17
        Names     map[NodeKey]string
18
        Infos     map[NodeKey]NodeInfo
19
        Sources   []QuerySource
20
        Functions Functions
21
        BaseRules []internal.EditRule
22

23
        // Refresh these for each statement
24
        StmtRules []internal.EditRule
25
        Atom      *atomic.Int64
26
}
27

NEW
28
func (v *Visitor[C, I]) UpdateInfo(info NodeInfo) {
×
NEW
29
        key := Key(info.Node)
×
NEW
30

×
NEW
31
        currentExpr, ok := v.Infos[key]
×
NEW
32
        if !ok {
×
NEW
33
                v.Infos[key] = info
×
NEW
34
                return
×
NEW
35
        }
×
36

NEW
37
        currentExpr.Node = info.Node
×
NEW
38
        currentExpr.IsGroup = currentExpr.IsGroup || info.IsGroup
×
NEW
39
        currentExpr.CanBeMultiple = currentExpr.CanBeMultiple || info.CanBeMultiple
×
NEW
40

×
NEW
41
        if info.EditedPosition != [2]int{} {
×
NEW
42
                currentExpr.EditedPosition = info.EditedPosition
×
NEW
43
        }
×
44

NEW
45
        if info.ExprDescription != "" {
×
NEW
46
                currentExpr.ExprDescription += ","
×
NEW
47
                currentExpr.ExprDescription += info.ExprDescription
×
NEW
48
        }
×
49

NEW
50
        if info.ExprRef != nil {
×
NEW
51
                currentExpr.ExprRef = info.ExprRef
×
NEW
52
                currentExpr.IgnoreRefNullability = info.IgnoreRefNullability
×
NEW
53
        }
×
54

NEW
55
        if info.Type == nil {
×
NEW
56
                v.Infos[key] = currentExpr
×
NEW
57
                return
×
NEW
58
        }
×
59

NEW
60
        if currentExpr.Type == nil {
×
NEW
61
                currentExpr.Type = info.Type
×
NEW
62
                v.Infos[key] = currentExpr
×
NEW
63
                return
×
NEW
64
        }
×
65

NEW
66
        matchingDBTypes := currentExpr.Type.Match(info.Type)
×
NEW
67
        if len(matchingDBTypes) == 0 {
×
NEW
68
                panic(fmt.Sprintf(
×
NEW
69
                        "No matching DBType found for %s: \n%v\n%v",
×
NEW
70
                        info.Node.GetText(),
×
NEW
71
                        currentExpr.Type.List(),
×
NEW
72
                        info.Type.List(),
×
NEW
73
                ))
×
74
        }
75

NEW
76
        currentExpr.Type = matchingDBTypes
×
NEW
77
        v.Infos[key] = currentExpr
×
78
}
79

NEW
80
func (v Visitor[C, I]) GetName(expr Node) string {
×
NEW
81
        exprKey := Key(expr)
×
NEW
82
        return v.Names[exprKey]
×
NEW
83
}
×
84

NEW
85
func (v *Visitor[C, I]) MaybeSetName(ctx Node, name string) {
×
NEW
86
        if name == "" {
×
NEW
87
                return
×
NEW
88
        }
×
89

NEW
90
        key := Key(ctx)
×
NEW
91
        _, ok := v.Names[key]
×
NEW
92
        if ok {
×
NEW
93
                return
×
NEW
94
        }
×
95

NEW
96
        v.Names[key] = name
×
97
}
98

NEW
99
func (w *Visitor[C, I]) MatchNames(p1 Node, p2 Node) {
×
NEW
100
        w.MaybeSetName(p1, w.Names[Key(p2)])
×
NEW
101
        w.MaybeSetName(p2, w.Names[Key(p1)])
×
NEW
102
}
×
103

NEW
104
func (v Visitor[C, I]) GetArgs(args, groups []NodeInfo, translate func(string) string) []drivers.QueryArg {
×
NEW
105
        bindArgs := make([]drivers.QueryArg, len(args))
×
NEW
106
        keys := make(map[string]int, len(bindArgs))
×
NEW
107
        for i, arg := range args {
×
NEW
108
                key := arg.ArgKey
×
NEW
109
                if oldIndex, ok := keys[key]; ok && key != "" {
×
NEW
110
                        bindArgs[oldIndex].Positions = append(
×
NEW
111
                                bindArgs[oldIndex].Positions, arg.EditedPosition,
×
NEW
112
                        )
×
NEW
113
                        // if an arg is used multiple times, it can't be multiple
×
NEW
114
                        bindArgs[oldIndex].CanBeMultiple = false
×
NEW
115
                        // Merge the config
×
NEW
116
                        bindArgs[oldIndex].Col = bindArgs[oldIndex].Col.Merge(arg.Config)
×
NEW
117
                        continue
×
118
                }
NEW
119
                keys[arg.ArgKey] = i
×
NEW
120

×
NEW
121
                name := v.Names[Key(arg.Node)]
×
NEW
122
                if name == "" {
×
NEW
123
                        name = "arg"
×
NEW
124
                }
×
125

NEW
126
                bindArgs[i] = drivers.QueryArg{
×
NEW
127
                        Col: drivers.QueryCol{
×
NEW
128
                                Name:     name,
×
NEW
129
                                Nullable: omit.From(arg.Type.Nullable()),
×
NEW
130
                                TypeName: translate(GetDBType(v.Infos, arg).ConfirmedDBType()),
×
NEW
131
                        }.Merge(arg.Config),
×
NEW
132
                        Positions:     [][2]int{arg.EditedPosition},
×
NEW
133
                        CanBeMultiple: arg.CanBeMultiple,
×
NEW
134
                }
×
135
        }
NEW
136
        bindArgs = slices.DeleteFunc(bindArgs, func(q drivers.QueryArg) bool {
×
NEW
137
                return len(q.Positions) == 0
×
NEW
138
        })
×
139

NEW
140
        groupArgs := make([]drivers.QueryArg, len(groups))
×
NEW
141
        for groupIndex, group := range groups {
×
NEW
142
                name := v.Names[Key(group.Node)]
×
NEW
143
                if name == "" {
×
NEW
144
                        name = "group"
×
NEW
145
                }
×
146

NEW
147
                groupArgs[groupIndex] = drivers.QueryArg{
×
NEW
148
                        Col: drivers.QueryCol{
×
NEW
149
                                Name:     name,
×
NEW
150
                                Nullable: omit.From(group.Type.Nullable()),
×
NEW
151
                                TypeName: translate(GetDBType(v.Infos, group).ConfirmedDBType()),
×
NEW
152
                        }.Merge(group.Config),
×
NEW
153
                        Positions:     [][2]int{group.EditedPosition},
×
NEW
154
                        CanBeMultiple: group.CanBeMultiple,
×
NEW
155
                }
×
156
        }
157

NEW
158
        return parser.GetArgs(bindArgs, groupArgs)
×
159
}
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2025 Coveralls, Inc