github.com/cockroachdb/cockroachdb-parser@v0.23.3-0.20240213214944-911057d40c9a/pkg/sql/sem/tree/tenant.go (about)

     1  // Copyright 2022 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 "strconv"
    14  
    15  // TenantID represents a tenant ID that can be pretty-printed.
    16  type TenantID struct {
    17  	ID uint64
    18  
    19  	// Specified is set to true when the TENANT clause was specified in
    20  	// the backup target input syntax. We need this, instead of relying
    21  	// on ID != 0, because we need a special marker for
    22  	// the case when the value was anonymized. In other places, the
    23  	// value used for anonymized integer literals is 0, but we can't use
    24  	// 0 for TenantID as this is refused during parsing, nor can we use
    25  	// any other value since all non-zero integers are valid tenant IDs.
    26  	Specified bool
    27  }
    28  
    29  var _ NodeFormatter = (*TenantID)(nil)
    30  
    31  // IsSet returns whether the TenantID is set.
    32  func (t *TenantID) IsSet() bool {
    33  	return t.Specified && t.ID != 0
    34  }
    35  
    36  // Format implements the NodeFormatter interface.
    37  func (t *TenantID) Format(ctx *FmtCtx) {
    38  	if ctx.flags.HasFlags(FmtHideConstants) || !t.IsSet() {
    39  		// The second part of the condition above is conceptually
    40  		// redundant, but is sadly needed here because we need to be able
    41  		// to re-print a TENANT clause after it has been anonymized,
    42  		// and we can't emit the value zero which is unparseable.
    43  		ctx.WriteByte('_')
    44  	} else {
    45  		ctx.WriteString(strconv.FormatUint(t.ID, 10))
    46  	}
    47  }