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

     1  // Copyright 2022 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  // RoutineTable is a Table that depends on a procedures and functions.
    24  type RoutineTable interface {
    25  	sql.Table
    26  
    27  	// AssignProcedures assigns a map of db-procedures to the routines table.
    28  	AssignProcedures(p map[string][]*plan.Procedure) sql.Table
    29  	// TODO: also should assign FUNCTIONS
    30  }
    31  
    32  // assignRoutines sets the catalog in the required nodes.
    33  func assignRoutines(ctx *sql.Context, a *Analyzer, n sql.Node, scope *plan.Scope, sel RuleSelector) (sql.Node, transform.TreeIdentity, error) {
    34  	span, ctx := ctx.Span("assign_routines")
    35  	defer span.End()
    36  
    37  	return transform.Node(n, func(n sql.Node) (sql.Node, transform.TreeIdentity, error) {
    38  		if !n.Resolved() {
    39  			return n, transform.SameTree, nil
    40  		}
    41  
    42  		switch node := n.(type) {
    43  		case *plan.ResolvedTable:
    44  			nc := *node
    45  			ct, ok := nc.Table.(RoutineTable)
    46  
    47  			var err error
    48  			scope, err = loadStoredProcedures(ctx, a, n, scope, sel)
    49  			if err != nil {
    50  				return nil, false, err
    51  			}
    52  
    53  			dbs := a.Catalog.AllDatabases(ctx)
    54  			pm := make(map[string][]*plan.Procedure)
    55  			for _, db := range dbs {
    56  				if scope != nil && scope.Procedures != nil {
    57  					pm[db.Name()] = scope.Procedures.AllForDatabase(db.Name())
    58  				}
    59  			}
    60  
    61  			if ok {
    62  				nc.Table = ct.AssignProcedures(pm)
    63  				return &nc, transform.NewTree, nil
    64  			}
    65  			return node, transform.SameTree, nil
    66  		default:
    67  			return node, transform.SameTree, nil
    68  		}
    69  	})
    70  }