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

     1  // Copyright 2016 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 tree
    12  
    13  // ReturningClause represents the returning clause on a statement.
    14  type ReturningClause interface {
    15  	NodeFormatter
    16  	// statementReturnType returns the StatementReturnType of statements that include
    17  	// the implementors variant of a RETURNING clause.
    18  	statementReturnType() StatementReturnType
    19  	returningClause()
    20  }
    21  
    22  var _ ReturningClause = &ReturningExprs{}
    23  var _ ReturningClause = &ReturningNothing{}
    24  var _ ReturningClause = &NoReturningClause{}
    25  
    26  // ReturningExprs represents RETURNING expressions.
    27  type ReturningExprs SelectExprs
    28  
    29  // Format implements the NodeFormatter interface.
    30  func (r *ReturningExprs) Format(ctx *FmtCtx) {
    31  	ctx.WriteString("RETURNING ")
    32  	ctx.FormatNode((*SelectExprs)(r))
    33  }
    34  
    35  // ReturningNothingClause is a shared instance to avoid unnecessary allocations.
    36  var ReturningNothingClause = &ReturningNothing{}
    37  
    38  // ReturningNothing represents RETURNING NOTHING.
    39  type ReturningNothing struct{}
    40  
    41  // Format implements the NodeFormatter interface.
    42  func (*ReturningNothing) Format(ctx *FmtCtx) {
    43  	ctx.WriteString("RETURNING NOTHING")
    44  }
    45  
    46  // AbsentReturningClause is a ReturningClause variant representing the absence of
    47  // a RETURNING clause.
    48  var AbsentReturningClause = &NoReturningClause{}
    49  
    50  // NoReturningClause represents the absence of a RETURNING clause.
    51  type NoReturningClause struct{}
    52  
    53  // Format implements the NodeFormatter interface.
    54  func (*NoReturningClause) Format(_ *FmtCtx) {}
    55  
    56  // used by parent statements to determine their own StatementReturnType.
    57  func (*ReturningExprs) statementReturnType() StatementReturnType    { return Rows }
    58  func (*ReturningNothing) statementReturnType() StatementReturnType  { return RowsAffected }
    59  func (*NoReturningClause) statementReturnType() StatementReturnType { return RowsAffected }
    60  
    61  func (*ReturningExprs) returningClause()    {}
    62  func (*ReturningNothing) returningClause()  {}
    63  func (*NoReturningClause) returningClause() {}
    64  
    65  // HasReturningClause determines if a ReturningClause is present, given a
    66  // variant of the ReturningClause interface.
    67  func HasReturningClause(clause ReturningClause) bool {
    68  	_, ok := clause.(*NoReturningClause)
    69  	return !ok
    70  }