forked from dolthub/go-mysql-server
/
repeat.go
102 lines (92 loc) · 2.83 KB
/
repeat.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
// Copyright 2023 Dolthub, Inc.
//
// 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 plan
import (
"github.com/gabereiser/go-mysql-server/sql"
"github.com/gabereiser/go-mysql-server/sql/expression"
)
// Repeat represents the REPEAT statement, which loops over a set of statements until the condition is true.
type Repeat struct {
*Loop
}
var _ sql.Node = (*Repeat)(nil)
var _ sql.DebugStringer = (*Repeat)(nil)
var _ sql.Expressioner = (*Repeat)(nil)
var _ RepresentsLabeledBlock = (*Repeat)(nil)
// NewRepeat returns a new *Repeat node.
func NewRepeat(label string, condition sql.Expression, block *Block) *Repeat {
return &Repeat{
&Loop{
Label: label,
Condition: expression.NewNot(condition), // Repeat stops when the condition is true, so we apply NOT to match loop's failure case
OnceBeforeEval: true,
Block: block,
},
}
}
// String implements the interface sql.Node.
func (r *Repeat) String() string {
label := ""
if len(r.Label) > 0 {
label = r.Label + ": "
}
p := sql.NewTreePrinter()
_ = p.WriteNode("%s: REPEAT(%s)", label, r.Condition.String())
var children []string
for _, s := range r.statements {
children = append(children, s.String())
}
_ = p.WriteChildren(children...)
return p.String()
}
// DebugString implements the interface sql.DebugStringer.
func (r *Repeat) DebugString() string {
label := ""
if len(r.Label) > 0 {
label = r.Label + ": "
}
p := sql.NewTreePrinter()
_ = p.WriteNode("%s: REPEAT(%s)", label, sql.DebugString(r.Condition))
var children []string
for _, s := range r.statements {
children = append(children, sql.DebugString(s))
}
_ = p.WriteChildren(children...)
return p.String()
}
// WithChildren implements the interface sql.Node.
func (r *Repeat) WithChildren(children ...sql.Node) (sql.Node, error) {
return &Repeat{
&Loop{
Label: r.Loop.Label,
Condition: r.Loop.Condition,
OnceBeforeEval: true,
Block: NewBlock(children),
},
}, nil
}
// WithExpressions implements the interface sql.Node.
func (r *Repeat) WithExpressions(exprs ...sql.Expression) (sql.Node, error) {
if len(exprs) != 1 {
return nil, sql.ErrInvalidChildrenNumber.New(r, len(exprs), 1)
}
return &Repeat{
&Loop{
Label: r.Loop.Label,
Condition: exprs[0],
OnceBeforeEval: true,
Block: r.Loop.Block,
},
}, nil
}