github.com/cockroachdb/cockroachdb-parser@v0.23.3-0.20240213214944-911057d40c9a/pkg/sql/sem/plpgsqltree/exception.go (about)

     1  // Copyright 2023 The Cockroach Authors.
     2  //
     3  // Use of this software is governed by the Business Source License
     4  // included in the file licenses/BSL.txt.
     5  //
     6  // As of the Change Date specified in that file, in accordance with
     7  // the Business Source License, use of this software will be governed
     8  // by the Apache License, Version 2.0, included in the file
     9  // licenses/APL.txt.
    10  
    11  package plpgsqltree
    12  
    13  import "github.com/cockroachdb/cockroachdb-parser/pkg/sql/sem/tree"
    14  
    15  type Exception struct {
    16  	StatementImpl
    17  	Conditions []Condition
    18  	Action     []Statement
    19  }
    20  
    21  func (s *Exception) CopyNode() *Exception {
    22  	copyNode := *s
    23  	copyNode.Conditions = append([]Condition(nil), copyNode.Conditions...)
    24  	copyNode.Action = append([]Statement(nil), copyNode.Action...)
    25  	return &copyNode
    26  }
    27  
    28  func (s *Exception) Format(ctx *tree.FmtCtx) {
    29  	ctx.WriteString("WHEN ")
    30  	for i, cond := range s.Conditions {
    31  		if i > 0 {
    32  			ctx.WriteString(" OR ")
    33  		}
    34  		if cond.SqlErrState != "" {
    35  			ctx.WriteString("SQLSTATE ")
    36  			formatStringQuotes(ctx, cond.SqlErrState)
    37  		} else {
    38  			formatString(ctx, cond.SqlErrName)
    39  		}
    40  	}
    41  	ctx.WriteString(" THEN\n")
    42  	for _, stmt := range s.Action {
    43  		ctx.FormatNode(stmt)
    44  	}
    45  }
    46  
    47  func (s *Exception) PlpgSQLStatementTag() string {
    48  	return "proc_exception"
    49  }
    50  
    51  func (s *Exception) WalkStmt(visitor StatementVisitor) (newStmt Statement, changed bool) {
    52  	newStmt, changed = visitor.Visit(s)
    53  	for i, stmt := range s.Action {
    54  		ns, ch := stmt.WalkStmt(visitor)
    55  		if ch {
    56  			changed = true
    57  			if newStmt == s {
    58  				newStmt = s.CopyNode()
    59  			}
    60  			newStmt.(*Exception).Action[i] = ns
    61  		}
    62  	}
    63  	return newStmt, changed
    64  }
    65  
    66  type Condition struct {
    67  	SqlErrState string
    68  	SqlErrName  string
    69  }