github.com/team-ide/go-dialect@v1.9.20/vitess/sqlparser/parse_table.go (about)

     1  /*
     2  Copyright 2020 The Vitess Authors.
     3  
     4  Licensed under the Apache License, Version 2.0 (the "License");
     5  you may not use this file except in compliance with the License.
     6  You may obtain a copy of the License at
     7  
     8      http://www.apache.org/licenses/LICENSE-2.0
     9  
    10  Unless required by applicable law or agreed to in writing, software
    11  distributed under the License is distributed on an "AS IS" BASIS,
    12  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    13  See the License for the specific language governing permissions and
    14  limitations under the License.
    15  */
    16  
    17  package sqlparser
    18  
    19  import (
    20  	"github.com/team-ide/go-dialect/vitess/vterrors"
    21  	vtrpcpb "github.com/team-ide/go-dialect/vitess/vtrpc"
    22  )
    23  
    24  // ParseTable parses the input as a qualified table name.
    25  // It handles all valid literal escaping.
    26  func ParseTable(input string) (keyspace, table string, err error) {
    27  	tokenizer := NewStringTokenizer(input)
    28  
    29  	// Start, want ID
    30  	token, value := tokenizer.Scan()
    31  	switch token {
    32  	case ID:
    33  		table = string(value)
    34  	default:
    35  		table = KeywordString(token)
    36  		if table == "" {
    37  			return "", "", vterrors.Errorf(vtrpcpb.Code_INVALID_ARGUMENT, "invalid table name: %s", input)
    38  		}
    39  	}
    40  
    41  	// Seen first ID, want '.' or 0
    42  	token, _ = tokenizer.Scan()
    43  	switch token {
    44  	case '.':
    45  		keyspace = table
    46  	case 0:
    47  		return keyspace, table, nil
    48  	default:
    49  		return "", "", vterrors.Errorf(vtrpcpb.Code_INVALID_ARGUMENT, "invalid table name: %s", input)
    50  	}
    51  
    52  	// Seen '.', want ID
    53  	token, value = tokenizer.Scan()
    54  	switch token {
    55  	case ID:
    56  		table = string(value)
    57  	default:
    58  		table = KeywordString(token)
    59  		if table == "" {
    60  			return "", "", vterrors.Errorf(vtrpcpb.Code_INVALID_ARGUMENT, "invalid table name: %s", input)
    61  		}
    62  	}
    63  
    64  	// Seen second ID, want 0
    65  	token, _ = tokenizer.Scan()
    66  	switch token {
    67  	case 0:
    68  		return keyspace, table, nil
    69  	default:
    70  		return "", "", vterrors.Errorf(vtrpcpb.Code_INVALID_ARGUMENT, "invalid table name: %s", input)
    71  	}
    72  }