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

     1  // Copyright 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 sql
    16  
    17  import (
    18  	"fmt"
    19  	"strings"
    20  )
    21  
    22  type Catalog interface {
    23  	DatabaseProvider
    24  	FunctionProvider
    25  	TableFunctionProvider
    26  	ExternalStoredProcedureProvider
    27  	StatsProvider
    28  
    29  	// CreateDatabase creates a new database, or returns an error if the operation isn't supported or fails.
    30  	CreateDatabase(ctx *Context, dbName string, collation CollationID) error
    31  
    32  	// RemoveDatabase removes the  database named, or returns an error if the operation isn't supported or fails.
    33  	RemoveDatabase(ctx *Context, dbName string) error
    34  
    35  	// Table returns the table with the name given in the db with the name given
    36  	Table(ctx *Context, dbName, tableName string) (Table, Database, error)
    37  
    38  	// DatabaseTable returns the table with the name given in the db given
    39  	DatabaseTable(ctx *Context, db Database, tableName string) (Table, Database, error)
    40  
    41  	// TableAsOf returns the table with the name given in the db with the name given, as of the given marker
    42  	TableAsOf(ctx *Context, dbName, tableName string, asOf interface{}) (Table, Database, error)
    43  
    44  	// DatabaseTableAsOf returns the table with the name given in the db given, as of the given marker
    45  	DatabaseTableAsOf(ctx *Context, db Database, tableName string, asOf interface{}) (Table, Database, error)
    46  
    47  	// Function returns the function with the name given, or sql.ErrFunctionNotFound if it doesn't exist
    48  	Function(ctx *Context, name string) (Function, error)
    49  
    50  	// RegisterFunction registers the functions given, adding them to the built-in functions.
    51  	// Integrators with custom functions should typically use the FunctionProvider interface to register their functions.
    52  	RegisterFunction(ctx *Context, fns ...Function)
    53  
    54  	// LockTable locks the table named
    55  	LockTable(ctx *Context, table string)
    56  
    57  	// UnlockTables unlocks all tables locked by the session id given
    58  	UnlockTables(ctx *Context, id uint32) error
    59  }
    60  
    61  // CatalogTable is a Table that depends on a Catalog.
    62  type CatalogTable interface {
    63  	Table
    64  
    65  	// AssignCatalog assigns a Catalog to the table.
    66  	AssignCatalog(cat Catalog) Table
    67  }
    68  
    69  func NewDbTable(db, table string) DbTable {
    70  	return DbTable{Db: strings.ToLower(db), Table: strings.ToLower(table)}
    71  }
    72  
    73  type DbTable struct {
    74  	Db    string
    75  	Table string
    76  }
    77  
    78  func (dt *DbTable) String() string {
    79  	if dt.Db == "" {
    80  		return dt.Table
    81  	}
    82  	return fmt.Sprintf("%s.%s", dt.Db, dt.Table)
    83  }