github.com/matrixorigin/matrixone@v1.2.0/pkg/sql/parsers/tree/object_name.go (about)

     1  // Copyright 2021 Matrix Origin
     2  //
     3  // Licensed under the Apache License, Version 2.0 (the "License");
     4  // you may not use this file except in compliance with the License.
     5  // You may obtain a copy of the License at
     6  //
     7  //      http://www.apache.org/licenses/LICENSE-2.0
     8  //
     9  // Unless required by applicable law or agreed to in writing, software
    10  // distributed under the License is distributed on an "AS IS" BASIS,
    11  // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    12  // See the License for the specific language governing permissions and
    13  // limitations under the License.
    14  
    15  package tree
    16  
    17  import (
    18  	"context"
    19  	"github.com/matrixorigin/matrixone/pkg/common/moerr"
    20  )
    21  
    22  // the common interface for qualified object names
    23  type ObjectName interface {
    24  	NodeFormatter
    25  }
    26  
    27  // the internal type for a qualified object.
    28  type objName struct {
    29  	//the path to the object.
    30  	ObjectNamePrefix
    31  
    32  	//the unqualified name for the object
    33  	ObjectName Identifier
    34  }
    35  
    36  // the path prefix of an object name.
    37  type ObjectNamePrefix struct {
    38  	CatalogName Identifier
    39  	SchemaName  Identifier
    40  
    41  	//true iff the catalog was explicitly specified
    42  	ExplicitCatalog bool
    43  	//true iff the schema was explicitly specified
    44  	ExplicitSchema bool
    45  }
    46  
    47  // the unresolved qualified name for a database object (table, view, etc)
    48  type UnresolvedObjectName struct {
    49  	//the number of name parts; >= 1
    50  	NumParts int
    51  
    52  	//At most three components, in reverse order.
    53  	//object name, db/schema, catalog.
    54  	Parts [3]string
    55  }
    56  
    57  func (node *UnresolvedObjectName) Format(ctx *FmtCtx) {
    58  	prefix := ""
    59  	for i := node.NumParts - 1; i >= 0; i-- {
    60  		ctx.WriteString(prefix)
    61  		ctx.WriteString(node.Parts[i])
    62  		prefix = "."
    63  	}
    64  }
    65  
    66  func (node *UnresolvedObjectName) ToTableName() TableName {
    67  	return TableName{
    68  		objName: objName{
    69  			ObjectNamePrefix: ObjectNamePrefix{
    70  				SchemaName:      Identifier(node.Parts[1]),
    71  				CatalogName:     Identifier(node.Parts[2]),
    72  				ExplicitSchema:  node.NumParts >= 2,
    73  				ExplicitCatalog: node.NumParts >= 3,
    74  			},
    75  			ObjectName: Identifier(node.Parts[0]),
    76  		},
    77  	}
    78  }
    79  
    80  func NewUnresolvedObjectName(ctx context.Context, num int, parts [3]string) (*UnresolvedObjectName, error) {
    81  	if num < 1 || num > 3 {
    82  		return nil, moerr.NewInternalError(ctx, "invalid number of parts")
    83  	}
    84  	return &UnresolvedObjectName{
    85  		NumParts: num,
    86  		Parts:    parts,
    87  	}, nil
    88  }
    89  
    90  func SetUnresolvedObjectName(num int, parts [3]string) *UnresolvedObjectName {
    91  	return &UnresolvedObjectName{
    92  		NumParts: num,
    93  		Parts:    parts,
    94  	}
    95  }
    96  
    97  func (node *UnresolvedObjectName) GetDBName() string {
    98  	return node.Parts[1]
    99  }
   100  
   101  func (node *UnresolvedObjectName) GetTableName() string {
   102  	return node.Parts[0]
   103  }