github.com/dolthub/go-mysql-server@v0.18.0/sql/analyzer/resolve_database.go (about)

     1  // Copyright 2020-2021 Dolthub, 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  // 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 analyzer
    16  
    17  import (
    18  	"github.com/dolthub/go-mysql-server/sql"
    19  	"github.com/dolthub/go-mysql-server/sql/plan"
    20  	"github.com/dolthub/go-mysql-server/sql/transform"
    21  )
    22  
    23  // validateDatabaseSet returns an error if any database node that requires a database doesn't have one
    24  func validateDatabaseSet(ctx *sql.Context, a *Analyzer, n sql.Node, scope *plan.Scope, sel RuleSelector) (sql.Node, transform.TreeIdentity, error) {
    25  	var err error
    26  	transform.Inspect(n, func(node sql.Node) bool {
    27  		switch n.(type) {
    28  		// TODO: there are probably other kinds of nodes that need this too
    29  		case *plan.ShowTables, *plan.ShowTriggers, *plan.CreateTable:
    30  			n := n.(sql.Databaser)
    31  			if _, ok := n.Database().(sql.UnresolvedDatabase); ok {
    32  				err = sql.ErrNoDatabaseSelected.New()
    33  				return false
    34  			}
    35  		}
    36  		return true
    37  	})
    38  	if err != nil {
    39  		return nil, transform.SameTree, err
    40  	}
    41  
    42  	return n, transform.SameTree, nil
    43  }