github.com/cockroachdb/cockroachdb-parser@v0.23.3-0.20240213214944-911057d40c9a/pkg/sql/sem/catid/ids.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 catid is a low-level package exporting ID types.
    12  package catid
    13  
    14  import (
    15  	"github.com/cockroachdb/cockroachdb-parser/pkg/sql/oidext"
    16  	"github.com/lib/pq/oid"
    17  )
    18  
    19  // DescID is a custom type for {Database,Table}Descriptor IDs.
    20  type DescID uint32
    21  
    22  // InvalidDescID is the uninitialised descriptor id.
    23  const InvalidDescID DescID = 0
    24  
    25  // SafeValue implements the redact.SafeValue interface.
    26  func (DescID) SafeValue() {}
    27  
    28  // TypeIDToOID converts a type descriptor ID into a type OID.
    29  func TypeIDToOID(id DescID) oid.Oid {
    30  	return idToUserDefinedOID(id)
    31  }
    32  
    33  // FuncIDToOID converts a function descriptor ID into a function OID.
    34  func FuncIDToOID(id DescID) oid.Oid {
    35  	return idToUserDefinedOID(id)
    36  }
    37  
    38  func idToUserDefinedOID(id DescID) oid.Oid {
    39  	return oid.Oid(id) + oidext.CockroachPredefinedOIDMax
    40  }
    41  
    42  // UserDefinedOIDToID converts the OID of a user-defined type or function
    43  // to a descriptor ID. Returns zero if the OID is not user-defined.
    44  func UserDefinedOIDToID(oid oid.Oid) DescID {
    45  	if !IsOIDUserDefined(oid) {
    46  		return InvalidDescID
    47  	}
    48  	return DescID(oid) - oidext.CockroachPredefinedOIDMax
    49  }
    50  
    51  // IsOIDUserDefined returns true if oid is greater than
    52  // CockroachPredefinedOIDMax, otherwise false.
    53  func IsOIDUserDefined(oid oid.Oid) bool {
    54  	return DescID(oid) > oidext.CockroachPredefinedOIDMax
    55  }
    56  
    57  // ColumnID is a custom type for Column IDs.
    58  type ColumnID uint32
    59  
    60  // SafeValue implements the redact.SafeValue interface.
    61  func (ColumnID) SafeValue() {}
    62  
    63  // FamilyID is a custom type for ColumnFamilyDescriptor IDs.
    64  type FamilyID uint32
    65  
    66  // SafeValue implements the redact.SafeValue interface.
    67  func (FamilyID) SafeValue() {}
    68  
    69  // IndexID is a custom type for IndexDescriptor IDs.
    70  type IndexID uint32
    71  
    72  // SafeValue implements the redact.SafeValue interface.
    73  func (IndexID) SafeValue() {}
    74  
    75  // ConstraintID is a custom type for TableDeascriptor constraint IDs.
    76  type ConstraintID uint32
    77  
    78  // SafeValue implements the redact.SafeValue interface.
    79  func (ConstraintID) SafeValue() {}
    80  
    81  // PGAttributeNum is a custom type for Column's logical order.
    82  type PGAttributeNum uint32
    83  
    84  // SafeValue implements the redact.SafeValue interface.
    85  func (PGAttributeNum) SafeValue() {}
    86  
    87  // RoleID is a custom type for a role id.
    88  type RoleID uint32