github.com/whtcorpsinc/milevadb-prod@v0.0.0-20211104133533-f57f4be3b597/soliton/berolinaSQL/ast.go (about)

     1  // Copyright 2020 WHTCORPS INC, Inc.
     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  // See the License for the specific language governing permissions and
    12  // limitations under the License.
    13  
    14  package BerolinaSQL
    15  
    16  import (
    17  	"github.com/whtcorpsinc/BerolinaSQL/ast"
    18  )
    19  
    20  // GetDefaultDB checks if all defCausumns in the AST have explicit DBName. If not, return specified DBName.
    21  func GetDefaultDB(sel ast.StmtNode, dbName string) string {
    22  	implicitDB := &implicitDatabase{}
    23  	sel.Accept(implicitDB)
    24  	if implicitDB.hasImplicit {
    25  		return dbName
    26  	}
    27  	return ""
    28  }
    29  
    30  type implicitDatabase struct {
    31  	hasImplicit bool
    32  }
    33  
    34  func (i *implicitDatabase) Enter(in ast.Node) (out ast.Node, skipChildren bool) {
    35  	switch x := in.(type) {
    36  	case *ast.BlockName:
    37  		if x.Schema.L == "" {
    38  			i.hasImplicit = true
    39  		}
    40  		return in, true
    41  	}
    42  	return in, i.hasImplicit
    43  }
    44  
    45  func (i *implicitDatabase) Leave(in ast.Node) (out ast.Node, ok bool) {
    46  	return in, true
    47  }