github.com/cockroachdb/cockroach@v20.2.0-alpha.1+incompatible/pkg/sql/sem/tree/show.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  import "github.com/cockroachdb/cockroach/pkg/sql/lex"
    23  
    24  // ShowVar represents a SHOW statement.
    25  type ShowVar struct {
    26  	Name string
    27  }
    28  
    29  // Format implements the NodeFormatter interface.
    30  func (node *ShowVar) Format(ctx *FmtCtx) {
    31  	ctx.WriteString("SHOW ")
    32  	// Session var names never contain PII and should be distinguished
    33  	// for feature tracking purposes.
    34  	ctx.WithFlags(ctx.flags & ^FmtAnonymize, func() {
    35  		ctx.FormatNameP(&node.Name)
    36  	})
    37  }
    38  
    39  // ShowClusterSetting represents a SHOW CLUSTER SETTING statement.
    40  type ShowClusterSetting struct {
    41  	Name string
    42  }
    43  
    44  // Format implements the NodeFormatter interface.
    45  func (node *ShowClusterSetting) Format(ctx *FmtCtx) {
    46  	ctx.WriteString("SHOW CLUSTER SETTING ")
    47  	// Cluster setting names never contain PII and should be distinguished
    48  	// for feature tracking purposes.
    49  	ctx.WithFlags(ctx.flags & ^FmtAnonymize, func() {
    50  		ctx.FormatNameP(&node.Name)
    51  	})
    52  }
    53  
    54  // ShowClusterSettingList represents a SHOW [ALL|PUBLIC] CLUSTER SETTINGS statement.
    55  type ShowClusterSettingList struct {
    56  	// All indicates whether to include non-public settings in the output.
    57  	All bool
    58  }
    59  
    60  // Format implements the NodeFormatter interface.
    61  func (node *ShowClusterSettingList) Format(ctx *FmtCtx) {
    62  	ctx.WriteString("SHOW ")
    63  	qual := "PUBLIC"
    64  	if node.All {
    65  		qual = "ALL"
    66  	}
    67  	ctx.WriteString(qual)
    68  	ctx.WriteString(" CLUSTER SETTINGS")
    69  }
    70  
    71  // BackupDetails represents the type of details to display for a SHOW BACKUP
    72  // statement.
    73  type BackupDetails int
    74  
    75  const (
    76  	// BackupDefaultDetails identifies a bare SHOW BACKUP statement.
    77  	BackupDefaultDetails BackupDetails = iota
    78  	// BackupRangeDetails identifies a SHOW BACKUP RANGES statement.
    79  	BackupRangeDetails
    80  	// BackupFileDetails identifies a SHOW BACKUP FILES statement.
    81  	BackupFileDetails
    82  )
    83  
    84  // ShowBackup represents a SHOW BACKUP statement.
    85  type ShowBackup struct {
    86  	Path                 Expr
    87  	Details              BackupDetails
    88  	ShouldIncludeSchemas bool
    89  	Options              KVOptions
    90  }
    91  
    92  // Format implements the NodeFormatter interface.
    93  func (node *ShowBackup) Format(ctx *FmtCtx) {
    94  	ctx.WriteString("SHOW BACKUP ")
    95  	if node.Details == BackupRangeDetails {
    96  		ctx.WriteString("RANGES ")
    97  	} else if node.Details == BackupFileDetails {
    98  		ctx.WriteString("FILES ")
    99  	}
   100  	if node.ShouldIncludeSchemas {
   101  		ctx.WriteString("SCHEMAS ")
   102  	}
   103  	ctx.FormatNode(node.Path)
   104  	if len(node.Options) > 0 {
   105  		ctx.WriteString(" WITH ")
   106  		ctx.FormatNode(&node.Options)
   107  	}
   108  }
   109  
   110  // ShowColumns represents a SHOW COLUMNS statement.
   111  type ShowColumns struct {
   112  	Table       *UnresolvedObjectName
   113  	WithComment bool
   114  }
   115  
   116  // Format implements the NodeFormatter interface.
   117  func (node *ShowColumns) Format(ctx *FmtCtx) {
   118  	ctx.WriteString("SHOW COLUMNS FROM ")
   119  	ctx.FormatNode(node.Table)
   120  
   121  	if node.WithComment {
   122  		ctx.WriteString(" WITH COMMENT")
   123  	}
   124  }
   125  
   126  // ShowDatabases represents a SHOW DATABASES statement.
   127  type ShowDatabases struct {
   128  	WithComment bool
   129  }
   130  
   131  // Format implements the NodeFormatter interface.
   132  func (node *ShowDatabases) Format(ctx *FmtCtx) {
   133  	ctx.WriteString("SHOW DATABASES")
   134  
   135  	if node.WithComment {
   136  		ctx.WriteString(" WITH COMMENT")
   137  	}
   138  }
   139  
   140  // ShowTraceType is an enum of SHOW TRACE variants.
   141  type ShowTraceType string
   142  
   143  // A list of the SHOW TRACE variants.
   144  const (
   145  	ShowTraceRaw     ShowTraceType = "TRACE"
   146  	ShowTraceKV      ShowTraceType = "KV TRACE"
   147  	ShowTraceReplica ShowTraceType = "EXPERIMENTAL_REPLICA TRACE"
   148  )
   149  
   150  // ShowTraceForSession represents a SHOW TRACE FOR SESSION statement.
   151  type ShowTraceForSession struct {
   152  	TraceType ShowTraceType
   153  	Compact   bool
   154  }
   155  
   156  // Format implements the NodeFormatter interface.
   157  func (node *ShowTraceForSession) Format(ctx *FmtCtx) {
   158  	ctx.WriteString("SHOW ")
   159  	if node.Compact {
   160  		ctx.WriteString("COMPACT ")
   161  	}
   162  	ctx.WriteString(string(node.TraceType))
   163  	ctx.WriteString(" FOR SESSION")
   164  }
   165  
   166  // ShowIndexes represents a SHOW INDEX statement.
   167  type ShowIndexes struct {
   168  	Table       *UnresolvedObjectName
   169  	WithComment bool
   170  }
   171  
   172  // Format implements the NodeFormatter interface.
   173  func (node *ShowIndexes) Format(ctx *FmtCtx) {
   174  	ctx.WriteString("SHOW INDEXES FROM ")
   175  	ctx.FormatNode(node.Table)
   176  
   177  	if node.WithComment {
   178  		ctx.WriteString(" WITH COMMENT")
   179  	}
   180  }
   181  
   182  // ShowDatabaseIndexes represents a SHOW INDEXES FROM DATABASE statement.
   183  type ShowDatabaseIndexes struct {
   184  	Database    Name
   185  	WithComment bool
   186  }
   187  
   188  // Format implements the NodeFormatter interface.
   189  func (node *ShowDatabaseIndexes) Format(ctx *FmtCtx) {
   190  	ctx.WriteString("SHOW INDEXES FROM DATABASE ")
   191  	ctx.FormatNode(&node.Database)
   192  
   193  	if node.WithComment {
   194  		ctx.WriteString(" WITH COMMENT")
   195  	}
   196  }
   197  
   198  // ShowQueries represents a SHOW QUERIES statement.
   199  type ShowQueries struct {
   200  	All     bool
   201  	Cluster bool
   202  }
   203  
   204  // Format implements the NodeFormatter interface.
   205  func (node *ShowQueries) Format(ctx *FmtCtx) {
   206  	ctx.WriteString("SHOW ")
   207  	if node.All {
   208  		ctx.WriteString("ALL ")
   209  	}
   210  	if node.Cluster {
   211  		ctx.WriteString("CLUSTER QUERIES")
   212  	} else {
   213  		ctx.WriteString("LOCAL QUERIES")
   214  	}
   215  }
   216  
   217  // ShowJobs represents a SHOW JOBS statement
   218  type ShowJobs struct {
   219  	// If non-nil, a select statement that provides the job ids to be shown.
   220  	Jobs *Select
   221  
   222  	// If Automatic is true, show only automatically-generated jobs such
   223  	// as automatic CREATE STATISTICS jobs. If Automatic is false, show
   224  	// only non-automatically-generated jobs.
   225  	Automatic bool
   226  
   227  	// Whether to block and wait for completion of all running jobs to be displayed.
   228  	Block bool
   229  }
   230  
   231  // Format implements the NodeFormatter interface.
   232  func (node *ShowJobs) Format(ctx *FmtCtx) {
   233  	ctx.WriteString("SHOW ")
   234  	if node.Automatic {
   235  		ctx.WriteString("AUTOMATIC ")
   236  	}
   237  	ctx.WriteString("JOBS")
   238  	if node.Block {
   239  		ctx.WriteString(" WHEN COMPLETE")
   240  	}
   241  	if node.Jobs != nil {
   242  		ctx.WriteString(" ")
   243  		ctx.FormatNode(node.Jobs)
   244  	}
   245  }
   246  
   247  // ShowSessions represents a SHOW SESSIONS statement
   248  type ShowSessions struct {
   249  	All     bool
   250  	Cluster bool
   251  }
   252  
   253  // Format implements the NodeFormatter interface.
   254  func (node *ShowSessions) Format(ctx *FmtCtx) {
   255  	ctx.WriteString("SHOW ")
   256  	if node.All {
   257  		ctx.WriteString("ALL ")
   258  	}
   259  	if node.Cluster {
   260  		ctx.WriteString("CLUSTER SESSIONS")
   261  	} else {
   262  		ctx.WriteString("LOCAL SESSIONS")
   263  	}
   264  }
   265  
   266  // ShowSchemas represents a SHOW SCHEMAS statement.
   267  type ShowSchemas struct {
   268  	Database Name
   269  }
   270  
   271  // Format implements the NodeFormatter interface.
   272  func (node *ShowSchemas) Format(ctx *FmtCtx) {
   273  	ctx.WriteString("SHOW SCHEMAS")
   274  	if node.Database != "" {
   275  		ctx.WriteString(" FROM ")
   276  		ctx.FormatNode(&node.Database)
   277  	}
   278  }
   279  
   280  // ShowSequences represents a SHOW SEQUENCES statement.
   281  type ShowSequences struct {
   282  	Database Name
   283  }
   284  
   285  // Format implements the NodeFormatter interface.
   286  func (node *ShowSequences) Format(ctx *FmtCtx) {
   287  	ctx.WriteString("SHOW SEQUENCES")
   288  	if node.Database != "" {
   289  		ctx.WriteString(" FROM ")
   290  		ctx.FormatNode(&node.Database)
   291  	}
   292  }
   293  
   294  // ShowTables represents a SHOW TABLES statement.
   295  type ShowTables struct {
   296  	ObjectNamePrefix
   297  	WithComment bool
   298  }
   299  
   300  // Format implements the NodeFormatter interface.
   301  func (node *ShowTables) Format(ctx *FmtCtx) {
   302  	ctx.WriteString("SHOW TABLES")
   303  	if node.ExplicitSchema {
   304  		ctx.WriteString(" FROM ")
   305  		ctx.FormatNode(&node.ObjectNamePrefix)
   306  	}
   307  
   308  	if node.WithComment {
   309  		ctx.WriteString(" WITH COMMENT")
   310  	}
   311  }
   312  
   313  // ShowConstraints represents a SHOW CONSTRAINTS statement.
   314  type ShowConstraints struct {
   315  	Table *UnresolvedObjectName
   316  }
   317  
   318  // Format implements the NodeFormatter interface.
   319  func (node *ShowConstraints) Format(ctx *FmtCtx) {
   320  	ctx.WriteString("SHOW CONSTRAINTS FROM ")
   321  	ctx.FormatNode(node.Table)
   322  }
   323  
   324  // ShowGrants represents a SHOW GRANTS statement.
   325  // TargetList is defined in grant.go.
   326  type ShowGrants struct {
   327  	Targets  *TargetList
   328  	Grantees NameList
   329  }
   330  
   331  // Format implements the NodeFormatter interface.
   332  func (node *ShowGrants) Format(ctx *FmtCtx) {
   333  	ctx.WriteString("SHOW GRANTS")
   334  	if node.Targets != nil {
   335  		ctx.WriteString(" ON ")
   336  		ctx.FormatNode(node.Targets)
   337  	}
   338  	if node.Grantees != nil {
   339  		ctx.WriteString(" FOR ")
   340  		ctx.FormatNode(&node.Grantees)
   341  	}
   342  }
   343  
   344  // ShowRoleGrants represents a SHOW GRANTS ON ROLE statement.
   345  type ShowRoleGrants struct {
   346  	Roles    NameList
   347  	Grantees NameList
   348  }
   349  
   350  // Format implements the NodeFormatter interface.
   351  func (node *ShowRoleGrants) Format(ctx *FmtCtx) {
   352  	ctx.WriteString("SHOW GRANTS ON ROLE")
   353  	if node.Roles != nil {
   354  		ctx.WriteString(" ")
   355  		ctx.FormatNode(&node.Roles)
   356  	}
   357  	if node.Grantees != nil {
   358  		ctx.WriteString(" FOR ")
   359  		ctx.FormatNode(&node.Grantees)
   360  	}
   361  }
   362  
   363  // ShowCreate represents a SHOW CREATE statement.
   364  type ShowCreate struct {
   365  	Name *UnresolvedObjectName
   366  }
   367  
   368  // Format implements the NodeFormatter interface.
   369  func (node *ShowCreate) Format(ctx *FmtCtx) {
   370  	ctx.WriteString("SHOW CREATE ")
   371  	ctx.FormatNode(node.Name)
   372  }
   373  
   374  // ShowSyntax represents a SHOW SYNTAX statement.
   375  // This the most lightweight thing that can be done on a statement
   376  // server-side: just report the statement that was entered without
   377  // any processing. Meant for use for syntax checking on clients,
   378  // when the client version might differ from the server.
   379  type ShowSyntax struct {
   380  	Statement string
   381  }
   382  
   383  // Format implements the NodeFormatter interface.
   384  func (node *ShowSyntax) Format(ctx *FmtCtx) {
   385  	ctx.WriteString("SHOW SYNTAX ")
   386  	ctx.WriteString(lex.EscapeSQLString(node.Statement))
   387  }
   388  
   389  // ShowTransactionStatus represents a SHOW TRANSACTION STATUS statement.
   390  type ShowTransactionStatus struct {
   391  }
   392  
   393  // Format implements the NodeFormatter interface.
   394  func (node *ShowTransactionStatus) Format(ctx *FmtCtx) {
   395  	ctx.WriteString("SHOW TRANSACTION STATUS")
   396  }
   397  
   398  // ShowSavepointStatus represents a SHOW SAVEPOINT STATUS statement.
   399  type ShowSavepointStatus struct {
   400  }
   401  
   402  // Format implements the NodeFormatter interface.
   403  func (node *ShowSavepointStatus) Format(ctx *FmtCtx) {
   404  	ctx.WriteString("SHOW SAVEPOINT STATUS")
   405  }
   406  
   407  // ShowUsers represents a SHOW USERS statement.
   408  type ShowUsers struct {
   409  }
   410  
   411  // Format implements the NodeFormatter interface.
   412  func (node *ShowUsers) Format(ctx *FmtCtx) {
   413  	ctx.WriteString("SHOW USERS")
   414  }
   415  
   416  // ShowRoles represents a SHOW ROLES statement.
   417  type ShowRoles struct {
   418  }
   419  
   420  // Format implements the NodeFormatter interface.
   421  func (node *ShowRoles) Format(ctx *FmtCtx) {
   422  	ctx.WriteString("SHOW ROLES")
   423  }
   424  
   425  // ShowRanges represents a SHOW RANGES statement.
   426  type ShowRanges struct {
   427  	TableOrIndex TableIndexName
   428  	DatabaseName Name
   429  }
   430  
   431  // Format implements the NodeFormatter interface.
   432  func (node *ShowRanges) Format(ctx *FmtCtx) {
   433  	ctx.WriteString("SHOW RANGES FROM ")
   434  	if node.DatabaseName != "" {
   435  		ctx.WriteString("DATABASE ")
   436  		ctx.FormatNode(&node.DatabaseName)
   437  	} else if node.TableOrIndex.Index != "" {
   438  		ctx.WriteString("INDEX ")
   439  		ctx.FormatNode(&node.TableOrIndex)
   440  	} else {
   441  		ctx.WriteString("TABLE ")
   442  		ctx.FormatNode(&node.TableOrIndex)
   443  	}
   444  }
   445  
   446  // ShowRangeForRow represents a SHOW RANGE FOR ROW statement.
   447  type ShowRangeForRow struct {
   448  	TableOrIndex TableIndexName
   449  	Row          Exprs
   450  }
   451  
   452  // Format implements the NodeFormatter interface.
   453  func (node *ShowRangeForRow) Format(ctx *FmtCtx) {
   454  	ctx.WriteString("SHOW RANGE FROM ")
   455  	if node.TableOrIndex.Index != "" {
   456  		ctx.WriteString("INDEX ")
   457  	} else {
   458  		ctx.WriteString("TABLE ")
   459  	}
   460  	ctx.FormatNode(&node.TableOrIndex)
   461  	ctx.WriteString(" FOR ROW (")
   462  	ctx.FormatNode(&node.Row)
   463  	ctx.WriteString(")")
   464  }
   465  
   466  // ShowFingerprints represents a SHOW EXPERIMENTAL_FINGERPRINTS statement.
   467  type ShowFingerprints struct {
   468  	Table *UnresolvedObjectName
   469  }
   470  
   471  // Format implements the NodeFormatter interface.
   472  func (node *ShowFingerprints) Format(ctx *FmtCtx) {
   473  	ctx.WriteString("SHOW EXPERIMENTAL_FINGERPRINTS FROM TABLE ")
   474  	ctx.FormatNode(node.Table)
   475  }
   476  
   477  // ShowTableStats represents a SHOW STATISTICS FOR TABLE statement.
   478  type ShowTableStats struct {
   479  	Table     *UnresolvedObjectName
   480  	UsingJSON bool
   481  }
   482  
   483  // Format implements the NodeFormatter interface.
   484  func (node *ShowTableStats) Format(ctx *FmtCtx) {
   485  	ctx.WriteString("SHOW STATISTICS ")
   486  	if node.UsingJSON {
   487  		ctx.WriteString("USING JSON ")
   488  	}
   489  	ctx.WriteString("FOR TABLE ")
   490  	ctx.FormatNode(node.Table)
   491  }
   492  
   493  // ShowHistogram represents a SHOW HISTOGRAM statement.
   494  type ShowHistogram struct {
   495  	HistogramID int64
   496  }
   497  
   498  // Format implements the NodeFormatter interface.
   499  func (node *ShowHistogram) Format(ctx *FmtCtx) {
   500  	ctx.Printf("SHOW HISTOGRAM %d", node.HistogramID)
   501  }
   502  
   503  // ShowPartitions represents a SHOW PARTITIONS statement.
   504  type ShowPartitions struct {
   505  	IsDB     bool
   506  	Database Name
   507  
   508  	IsIndex bool
   509  	Index   TableIndexName
   510  
   511  	IsTable bool
   512  	Table   *UnresolvedObjectName
   513  }
   514  
   515  // Format implements the NodeFormatter interface.
   516  func (node *ShowPartitions) Format(ctx *FmtCtx) {
   517  	if node.IsDB {
   518  		ctx.Printf("SHOW PARTITIONS FROM DATABASE ")
   519  		ctx.FormatNode(&node.Database)
   520  	} else if node.IsIndex {
   521  		ctx.Printf("SHOW PARTITIONS FROM INDEX ")
   522  		ctx.FormatNode(&node.Index)
   523  	} else {
   524  		ctx.Printf("SHOW PARTITIONS FROM TABLE ")
   525  		ctx.FormatNode(node.Table)
   526  	}
   527  }