github.com/dolthub/go-mysql-server@v0.18.0/sql/plan/use.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 plan
    16  
    17  import (
    18  	"fmt"
    19  
    20  	"github.com/dolthub/go-mysql-server/sql"
    21  	"github.com/dolthub/go-mysql-server/sql/mysql_db"
    22  )
    23  
    24  // Use changes the current database.
    25  type Use struct {
    26  	db      sql.Database
    27  	Catalog sql.Catalog
    28  }
    29  
    30  // NewUse creates a new Use node.
    31  func NewUse(db sql.Database) *Use {
    32  	return &Use{db: db}
    33  }
    34  
    35  var _ sql.Node = (*Use)(nil)
    36  var _ sql.Databaser = (*Use)(nil)
    37  var _ sql.CollationCoercible = (*Use)(nil)
    38  
    39  // Database implements the sql.Databaser interface.
    40  func (u *Use) Database() sql.Database {
    41  	return u.db
    42  }
    43  
    44  // WithDatabase implements the sql.Databaser interface.
    45  func (u *Use) WithDatabase(db sql.Database) (sql.Node, error) {
    46  	nc := *u
    47  	nc.db = db
    48  	return &nc, nil
    49  }
    50  
    51  // Children implements the sql.Node interface.
    52  func (Use) Children() []sql.Node { return nil }
    53  
    54  // Resolved implements the sql.Node interface.
    55  func (u *Use) Resolved() bool {
    56  	_, ok := u.db.(sql.UnresolvedDatabase)
    57  	return !ok
    58  }
    59  
    60  func (u *Use) IsReadOnly() bool {
    61  	return true
    62  }
    63  
    64  // Schema implements the sql.Node interface.
    65  func (Use) Schema() sql.Schema { return nil }
    66  
    67  // RowIter implements the sql.Node interface.
    68  func (u *Use) RowIter(ctx *sql.Context, row sql.Row) (sql.RowIter, error) {
    69  	// We want to return to the session interface the same database instance they gave us, unwrap it if necessary
    70  	db := u.db
    71  	if pdb, ok := db.(mysql_db.PrivilegedDatabase); ok {
    72  		db = pdb.Unwrap()
    73  	}
    74  
    75  	err := ctx.Session.UseDatabase(ctx, db)
    76  	if err != nil {
    77  		return nil, err
    78  	}
    79  
    80  	ctx.SetCurrentDatabase(u.db.Name())
    81  	return sql.RowsToRowIter(), nil
    82  }
    83  
    84  // WithChildren implements the Node interface.
    85  func (u *Use) WithChildren(children ...sql.Node) (sql.Node, error) {
    86  	if len(children) != 0 {
    87  		return nil, sql.ErrInvalidChildrenNumber.New(u, len(children), 1)
    88  	}
    89  
    90  	return u, nil
    91  }
    92  
    93  // CheckPrivileges implements the interface sql.Node.
    94  func (u *Use) CheckPrivileges(ctx *sql.Context, opChecker sql.PrivilegedOperationChecker) bool {
    95  	// The given database will not be visible if the user does not have the appropriate privileges, so we can just
    96  	// return true here.
    97  	return true
    98  }
    99  
   100  // CollationCoercibility implements the interface sql.CollationCoercible.
   101  func (*Use) CollationCoercibility(ctx *sql.Context) (collation sql.CollationID, coercibility byte) {
   102  	return sql.Collation_binary, 7
   103  }
   104  
   105  // String implements the sql.Node interface.
   106  func (u *Use) String() string {
   107  	return fmt.Sprintf("USE(%s)", u.db.Name())
   108  }