github.com/cockroachdb/cockroach@v20.2.0-alpha.1+incompatible/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  MaterializeClause
    23  	Stmt Statement
    24  }
    25  
    26  // MaterializeClause represents a materialize clause inside of a WITH clause.
    27  type MaterializeClause struct {
    28  	// Set controls whether to use the Materialize bool instead of the default.
    29  	Set bool
    30  
    31  	// Materialize overrides the default materialization behavior.
    32  	Materialize bool
    33  }
    34  
    35  // Format implements the NodeFormatter interface.
    36  func (node *With) Format(ctx *FmtCtx) {
    37  	if node == nil {
    38  		return
    39  	}
    40  	ctx.WriteString("WITH ")
    41  	if node.Recursive {
    42  		ctx.WriteString("RECURSIVE ")
    43  	}
    44  	for i, cte := range node.CTEList {
    45  		if i != 0 {
    46  			ctx.WriteString(", ")
    47  		}
    48  		ctx.FormatNode(&cte.Name)
    49  		ctx.WriteString(" AS ")
    50  		if cte.Mtr.Set {
    51  			if !cte.Mtr.Materialize {
    52  				ctx.WriteString("NOT ")
    53  			}
    54  			ctx.WriteString("MATERIALIZED ")
    55  		}
    56  		ctx.WriteString("(")
    57  		ctx.FormatNode(cte.Stmt)
    58  		ctx.WriteString(")")
    59  	}
    60  	ctx.WriteByte(' ')
    61  }