github.com/hasnat/dolt/go@v0.0.0-20210628190320-9eb5d843fbb7/libraries/doltcore/sqle/user_space_database.go (about)

     1  // Copyright 2020 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 sqle
    16  
    17  import (
    18  	"github.com/dolthub/go-mysql-server/sql"
    19  
    20  	"github.com/dolthub/dolt/go/libraries/doltcore/doltdb"
    21  )
    22  
    23  // UserSpaceDatabase in an implementation of sql.Database for root values. Does not expose any of the internal dolt tables.
    24  type UserSpaceDatabase struct {
    25  	*doltdb.RootValue
    26  }
    27  
    28  var _ SqlDatabase = (*UserSpaceDatabase)(nil)
    29  
    30  func NewUserSpaceDatabase(root *doltdb.RootValue) *UserSpaceDatabase {
    31  	return &UserSpaceDatabase{RootValue: root}
    32  }
    33  
    34  func (db *UserSpaceDatabase) Name() string {
    35  	return "dolt"
    36  }
    37  
    38  func (db *UserSpaceDatabase) GetTableInsensitive(ctx *sql.Context, tableName string) (sql.Table, bool, error) {
    39  	if doltdb.IsReadOnlySystemTable(tableName) {
    40  		return nil, false, nil
    41  	}
    42  	table, tableName, ok, err := db.RootValue.GetTableInsensitive(ctx, tableName)
    43  	if err != nil {
    44  		return nil, false, err
    45  	}
    46  	if !ok {
    47  		return nil, false, nil
    48  	}
    49  	sch, err := table.GetSchema(ctx)
    50  	if err != nil {
    51  		return nil, false, err
    52  	}
    53  	dt := NewDoltTable(tableName, sch, table, db, false)
    54  	return dt, true, nil
    55  }
    56  
    57  func (db *UserSpaceDatabase) GetTableNames(ctx *sql.Context) ([]string, error) {
    58  	tableNames, err := db.RootValue.GetTableNames(ctx)
    59  	if err != nil {
    60  		return nil, err
    61  	}
    62  	resultingTblNames := []string{}
    63  	for _, tbl := range tableNames {
    64  		if !doltdb.IsReadOnlySystemTable(tbl) {
    65  			resultingTblNames = append(resultingTblNames, tbl)
    66  		}
    67  	}
    68  	return resultingTblNames, nil
    69  }
    70  
    71  func (db *UserSpaceDatabase) GetRoot(*sql.Context) (*doltdb.RootValue, error) {
    72  	return db.RootValue, nil
    73  }
    74  
    75  func (db *UserSpaceDatabase) GetTemporaryTablesRoot(*sql.Context) (*doltdb.RootValue, bool) {
    76  	panic("UserSpaceDatabase should not contain any temporary tables")
    77  }