github.com/cockroachdb/cockroachdb-parser@v0.23.3-0.20240213214944-911057d40c9a/pkg/sql/sem/tree/alter_index.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  import "fmt"
    14  
    15  // AlterIndex represents an ALTER INDEX statement.
    16  type AlterIndex struct {
    17  	IfExists bool
    18  	Index    TableIndexName
    19  	Cmds     AlterIndexCmds
    20  }
    21  
    22  var _ Statement = &AlterIndex{}
    23  
    24  // Format implements the NodeFormatter interface.
    25  func (node *AlterIndex) Format(ctx *FmtCtx) {
    26  	ctx.WriteString("ALTER INDEX ")
    27  	if node.IfExists {
    28  		ctx.WriteString("IF EXISTS ")
    29  	}
    30  	ctx.FormatNode(&node.Index)
    31  	ctx.FormatNode(&node.Cmds)
    32  }
    33  
    34  // AlterIndexCmds represents a list of index alterations.
    35  type AlterIndexCmds []AlterIndexCmd
    36  
    37  // Format implements the NodeFormatter interface.
    38  func (node *AlterIndexCmds) Format(ctx *FmtCtx) {
    39  	for i, n := range *node {
    40  		if i > 0 {
    41  			ctx.WriteString(",")
    42  		}
    43  		ctx.FormatNode(n)
    44  	}
    45  }
    46  
    47  // AlterIndexCmd represents an index modification operation.
    48  type AlterIndexCmd interface {
    49  	NodeFormatter
    50  	// Placeholder function to ensure that only desired types
    51  	// (AlterIndex*) conform to the AlterIndexCmd interface.
    52  	alterIndexCmd()
    53  }
    54  
    55  func (*AlterIndexPartitionBy) alterIndexCmd() {}
    56  
    57  var _ AlterIndexCmd = &AlterIndexPartitionBy{}
    58  
    59  // AlterIndexPartitionBy represents an ALTER INDEX PARTITION BY
    60  // command.
    61  type AlterIndexPartitionBy struct {
    62  	*PartitionByIndex
    63  }
    64  
    65  // Format implements the NodeFormatter interface.
    66  func (node *AlterIndexPartitionBy) Format(ctx *FmtCtx) {
    67  	ctx.FormatNode(node.PartitionByIndex)
    68  }
    69  
    70  // AlterIndexVisible represents a ALTER INDEX ... [VISIBLE | NOT VISIBLE] statement.
    71  type AlterIndexVisible struct {
    72  	Index        TableIndexName
    73  	Invisibility IndexInvisibility
    74  	IfExists     bool
    75  }
    76  
    77  var _ Statement = &AlterIndexVisible{}
    78  
    79  // Format implements the NodeFormatter interface.
    80  func (node *AlterIndexVisible) Format(ctx *FmtCtx) {
    81  	ctx.WriteString("ALTER INDEX ")
    82  	if node.IfExists {
    83  		ctx.WriteString("IF EXISTS ")
    84  	}
    85  	ctx.FormatNode(&node.Index)
    86  	switch {
    87  	case node.Invisibility.FloatProvided:
    88  		ctx.WriteString(" VISIBILITY " + fmt.Sprintf("%.2f", 1-node.Invisibility.Value))
    89  	case node.Invisibility.Value == 1.0:
    90  		ctx.WriteString(" NOT VISIBLE")
    91  	default:
    92  		ctx.WriteString(" VISIBLE")
    93  	}
    94  }