github.com/cockroachdb/cockroach@v20.2.0-alpha.1+incompatible/pkg/sql/sem/tree/rename.go (about)

     1  // Copyright 2012, Google Inc. All rights reserved.
     2  // Use of this source code is governed by a BSD-style
     3  // license that can be found in licenses/BSD-vitess.txt.
     4  
     5  // Portions of this file are additionally subject to the following
     6  // license and copyright.
     7  //
     8  // Copyright 2015 The Cockroach Authors.
     9  //
    10  // Use of this software is governed by the Business Source License
    11  // included in the file licenses/BSL.txt.
    12  //
    13  // As of the Change Date specified in that file, in accordance with
    14  // the Business Source License, use of this software will be governed
    15  // by the Apache License, Version 2.0, included in the file
    16  // licenses/APL.txt.
    17  
    18  // This code was derived from https://github.com/youtube/vitess.
    19  
    20  package tree
    21  
    22  // RenameDatabase represents a RENAME DATABASE statement.
    23  type RenameDatabase struct {
    24  	Name    Name
    25  	NewName Name
    26  }
    27  
    28  // Format implements the NodeFormatter interface.
    29  func (node *RenameDatabase) Format(ctx *FmtCtx) {
    30  	ctx.WriteString("ALTER DATABASE ")
    31  	ctx.FormatNode(&node.Name)
    32  	ctx.WriteString(" RENAME TO ")
    33  	ctx.FormatNode(&node.NewName)
    34  }
    35  
    36  // RenameTable represents a RENAME TABLE or RENAME VIEW or RENAME SEQUENCE
    37  // statement. Whether the user has asked to rename a view or a sequence
    38  // is indicated by the IsView and IsSequence fields.
    39  type RenameTable struct {
    40  	Name       *UnresolvedObjectName
    41  	NewName    *UnresolvedObjectName
    42  	IfExists   bool
    43  	IsView     bool
    44  	IsSequence bool
    45  }
    46  
    47  // Format implements the NodeFormatter interface.
    48  func (node *RenameTable) Format(ctx *FmtCtx) {
    49  	ctx.WriteString("ALTER ")
    50  	if node.IsView {
    51  		ctx.WriteString("VIEW ")
    52  	} else if node.IsSequence {
    53  		ctx.WriteString("SEQUENCE ")
    54  	} else {
    55  		ctx.WriteString("TABLE ")
    56  	}
    57  	if node.IfExists {
    58  		ctx.WriteString("IF EXISTS ")
    59  	}
    60  	ctx.FormatNode(node.Name)
    61  	ctx.WriteString(" RENAME TO ")
    62  	ctx.FormatNode(node.NewName)
    63  }
    64  
    65  // RenameIndex represents a RENAME INDEX statement.
    66  type RenameIndex struct {
    67  	Index    *TableIndexName
    68  	NewName  UnrestrictedName
    69  	IfExists bool
    70  }
    71  
    72  // Format implements the NodeFormatter interface.
    73  func (node *RenameIndex) Format(ctx *FmtCtx) {
    74  	ctx.WriteString("ALTER INDEX ")
    75  	if node.IfExists {
    76  		ctx.WriteString("IF EXISTS ")
    77  	}
    78  	ctx.FormatNode(node.Index)
    79  	ctx.WriteString(" RENAME TO ")
    80  	ctx.FormatNode(&node.NewName)
    81  }
    82  
    83  // RenameColumn represents a RENAME COLUMN statement.
    84  type RenameColumn struct {
    85  	Table   TableName
    86  	Name    Name
    87  	NewName Name
    88  	// IfExists refers to the table, not the column.
    89  	IfExists bool
    90  }
    91  
    92  // Format implements the NodeFormatter interface.
    93  func (node *RenameColumn) Format(ctx *FmtCtx) {
    94  	ctx.WriteString("ALTER TABLE ")
    95  	if node.IfExists {
    96  		ctx.WriteString("IF EXISTS ")
    97  	}
    98  	ctx.FormatNode(&node.Table)
    99  	ctx.WriteString(" RENAME COLUMN ")
   100  	ctx.FormatNode(&node.Name)
   101  	ctx.WriteString(" TO ")
   102  	ctx.FormatNode(&node.NewName)
   103  }