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

     1  // Copyright 2017 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  // With represents a WITH statement.
    14  type With struct {
    15  	Recursive bool
    16  	CTEList   []*CTE
    17  }
    18  
    19  // CTE represents a common table expression inside of a WITH clause.
    20  type CTE struct {
    21  	Name AliasClause
    22  	Mtr  CTEMaterializeClause
    23  	Stmt Statement
    24  }
    25  
    26  // CTEMaterializeClause represents either MATERIALIZED, NOT MATERIALIZED, or an
    27  // empty materialization clause.
    28  type CTEMaterializeClause int8
    29  
    30  const (
    31  	// CTEMaterializeDefault represents an empty materialization clause.
    32  	CTEMaterializeDefault CTEMaterializeClause = iota
    33  	// CTEMaterializeAlways represents MATERIALIZED.
    34  	CTEMaterializeAlways
    35  	// CTEMaterializeNever represents NOT MATERIALIZED.
    36  	CTEMaterializeNever
    37  )
    38  
    39  // Format implements the NodeFormatter interface.
    40  func (node *With) Format(ctx *FmtCtx) {
    41  	if node == nil {
    42  		return
    43  	}
    44  	ctx.WriteString("WITH ")
    45  	if node.Recursive {
    46  		ctx.WriteString("RECURSIVE ")
    47  	}
    48  	for i, cte := range node.CTEList {
    49  		if i != 0 {
    50  			ctx.WriteString(", ")
    51  		}
    52  		ctx.FormatNode(&cte.Name)
    53  		ctx.WriteString(" AS ")
    54  		switch cte.Mtr {
    55  		case CTEMaterializeAlways:
    56  			ctx.WriteString("MATERIALIZED ")
    57  		case CTEMaterializeNever:
    58  			ctx.WriteString("NOT MATERIALIZED ")
    59  		}
    60  		ctx.WriteString("(")
    61  		ctx.FormatNode(cte.Stmt)
    62  		ctx.WriteString(")")
    63  	}
    64  	ctx.WriteByte(' ')
    65  }