-
Notifications
You must be signed in to change notification settings - Fork 3.7k
/
exception.go
72 lines (63 loc) · 1.63 KB
/
exception.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
// Copyright 2023 The Cockroach Authors.
//
// Use of this software is governed by the Business Source License
// included in the file licenses/BSL.txt.
//
// As of the Change Date specified in that file, in accordance with
// the Business Source License, use of this software will be governed
// by the Apache License, Version 2.0, included in the file
// licenses/APL.txt.
package plpgsqltree
import (
"fmt"
"github.com/cockroachdb/cockroach/pkg/sql/sem/tree"
)
type Exception struct {
StatementImpl
Conditions []Condition
Action []Statement
}
func (s *Exception) CopyNode() *Exception {
copyNode := *s
copyNode.Conditions = append([]Condition(nil), copyNode.Conditions...)
copyNode.Action = append([]Statement(nil), copyNode.Action...)
return ©Node
}
func (s *Exception) Format(ctx *tree.FmtCtx) {
ctx.WriteString("WHEN ")
for i, cond := range s.Conditions {
if i > 0 {
ctx.WriteString(" OR ")
}
if cond.SqlErrState != "" {
ctx.WriteString(fmt.Sprintf("SQLSTATE '%s'", cond.SqlErrState))
} else {
ctx.WriteString(cond.SqlErrName)
}
}
ctx.WriteString(" THEN\n")
for _, stmt := range s.Action {
stmt.Format(ctx)
}
}
func (s *Exception) PlpgSQLStatementTag() string {
return "proc_exception"
}
func (s *Exception) WalkStmt(visitor StatementVisitor) (newStmt Statement, changed bool) {
newStmt, changed = visitor.Visit(s)
for i, stmt := range s.Action {
ns, ch := stmt.WalkStmt(visitor)
if ch {
changed = true
if newStmt != stmt {
newStmt = s.CopyNode()
}
newStmt.(*Exception).Action[i] = ns
}
}
return newStmt, changed
}
type Condition struct {
SqlErrState string
SqlErrName string
}