github.com/cockroachdb/cockroach@v20.2.0-alpha.1+incompatible/pkg/sql/opt/cat/view.go (about)

     1  // Copyright 2018 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 cat
    12  
    13  import (
    14  	"bytes"
    15  
    16  	"github.com/cockroachdb/cockroach/pkg/sql/sem/tree"
    17  	"github.com/cockroachdb/cockroach/pkg/util/treeprinter"
    18  )
    19  
    20  // View is an interface to a database view, exposing only the information needed
    21  // by the query optimizer.
    22  type View interface {
    23  	DataSource
    24  
    25  	// Query returns the SQL text that specifies the SELECT query that constitutes
    26  	// this view.
    27  	Query() string
    28  
    29  	// ColumnNameCount returns the number of column names specified in the view.
    30  	// If zero, then the columns are not aliased. Otherwise, it will match the
    31  	// number of columns in the view.
    32  	ColumnNameCount() int
    33  
    34  	// ColumnNames returns the name of the column at the ith ordinal position
    35  	// within the view, where i < ColumnNameCount.
    36  	ColumnName(i int) tree.Name
    37  
    38  	// IsSystemView returns true if this view is a system view (like
    39  	// crdb_internal.ranges).
    40  	IsSystemView() bool
    41  }
    42  
    43  // FormatView nicely formats a catalog view using a treeprinter for debugging
    44  // and testing.
    45  func FormatView(view View, tp treeprinter.Node) {
    46  	var buf bytes.Buffer
    47  	if view.ColumnNameCount() > 0 {
    48  		buf.WriteString(" (")
    49  		for i := 0; i < view.ColumnNameCount(); i++ {
    50  			if i != 0 {
    51  				buf.WriteString(", ")
    52  			}
    53  			buf.WriteString(string(view.ColumnName(i)))
    54  		}
    55  		buf.WriteString(")")
    56  	}
    57  
    58  	child := tp.Childf("VIEW %s%s", view.Name(), buf.String())
    59  
    60  	child.Child(view.Query())
    61  }