github.com/dolthub/dolt/go@v0.40.5-0.20240520175717-68db7794bea6/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  	"github.com/dolthub/dolt/go/libraries/doltcore/env"
    22  	"github.com/dolthub/dolt/go/libraries/doltcore/sqle/dsess"
    23  	"github.com/dolthub/dolt/go/libraries/doltcore/table/editor"
    24  	"github.com/dolthub/dolt/go/libraries/utils/concurrentmap"
    25  )
    26  
    27  // UserSpaceDatabase in an implementation of sql.Database for root values. Does not expose any of the internal dolt tables.
    28  type UserSpaceDatabase struct {
    29  	doltdb.RootValue
    30  
    31  	editOpts editor.Options
    32  }
    33  
    34  var _ dsess.SqlDatabase = (*UserSpaceDatabase)(nil)
    35  
    36  func NewUserSpaceDatabase(root doltdb.RootValue, editOpts editor.Options) *UserSpaceDatabase {
    37  	return &UserSpaceDatabase{RootValue: root, editOpts: editOpts}
    38  }
    39  
    40  func (db *UserSpaceDatabase) Name() string {
    41  	return "dolt"
    42  }
    43  
    44  func (db *UserSpaceDatabase) Schema() string {
    45  	return ""
    46  }
    47  
    48  func (db *UserSpaceDatabase) GetTableInsensitive(ctx *sql.Context, tableName string) (sql.Table, bool, error) {
    49  	if doltdb.IsReadOnlySystemTable(tableName) {
    50  		return nil, false, nil
    51  	}
    52  	table, tableName, ok, err := doltdb.GetTableInsensitive(ctx, db.RootValue, tableName)
    53  	if err != nil {
    54  		return nil, false, err
    55  	}
    56  	if !ok {
    57  		return nil, false, nil
    58  	}
    59  	sch, err := table.GetSchema(ctx)
    60  	if err != nil {
    61  		return nil, false, err
    62  	}
    63  	dt, err := NewDoltTable(tableName, sch, table, db, db.editOpts)
    64  	if err != nil {
    65  		return nil, false, err
    66  	}
    67  	return dt, true, nil
    68  }
    69  
    70  func (db *UserSpaceDatabase) GetTableNames(ctx *sql.Context) ([]string, error) {
    71  	tableNames, err := db.RootValue.GetTableNames(ctx, doltdb.DefaultSchemaName)
    72  	if err != nil {
    73  		return nil, err
    74  	}
    75  	resultingTblNames := []string{}
    76  	for _, tbl := range tableNames {
    77  		if !doltdb.IsReadOnlySystemTable(tbl) {
    78  			resultingTblNames = append(resultingTblNames, tbl)
    79  		}
    80  	}
    81  	return resultingTblNames, nil
    82  }
    83  
    84  func (db *UserSpaceDatabase) InitialDBState(ctx *sql.Context) (dsess.InitialDbState, error) {
    85  	return dsess.InitialDbState{
    86  		Db:       db,
    87  		ReadOnly: true,
    88  		HeadRoot: db.RootValue,
    89  		DbData: env.DbData{
    90  			Rsw: noopRepoStateWriter{},
    91  		},
    92  		Remotes: concurrentmap.New[string, env.Remote](),
    93  	}, nil
    94  }
    95  
    96  func (db *UserSpaceDatabase) WithBranchRevision(requestedName string, branchSpec dsess.SessionDatabaseBranchSpec) (dsess.SqlDatabase, error) {
    97  	// Nothing to do here, we don't support changing branch revisions
    98  	return db, nil
    99  }
   100  
   101  func (db *UserSpaceDatabase) DoltDatabases() []*doltdb.DoltDB {
   102  	return nil
   103  }
   104  
   105  func (db *UserSpaceDatabase) GetRoot(*sql.Context) (doltdb.RootValue, error) {
   106  	return db.RootValue, nil
   107  }
   108  
   109  func (db *UserSpaceDatabase) GetTemporaryTablesRoot(*sql.Context) (doltdb.RootValue, bool) {
   110  	panic("UserSpaceDatabase should not contain any temporary tables")
   111  }
   112  
   113  func (db *UserSpaceDatabase) DbData() env.DbData {
   114  	return env.DbData{}
   115  }
   116  
   117  func (db *UserSpaceDatabase) EditOptions() editor.Options {
   118  	return db.editOpts
   119  }
   120  
   121  func (db *UserSpaceDatabase) Revision() string {
   122  	return ""
   123  }
   124  
   125  func (db *UserSpaceDatabase) Versioned() bool {
   126  	return false
   127  }
   128  
   129  func (db *UserSpaceDatabase) RevisionType() dsess.RevisionType {
   130  	return dsess.RevisionTypeNone
   131  }
   132  
   133  func (db *UserSpaceDatabase) RevisionQualifiedName() string {
   134  	return db.Name()
   135  }
   136  
   137  func (db *UserSpaceDatabase) RequestedName() string {
   138  	return db.Name()
   139  }