github.com/dolthub/go-mysql-server@v0.18.0/sql/plan/show_create_database.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/types"
    22  )
    23  
    24  // ShowCreateDatabase returns the SQL for creating a database.
    25  type ShowCreateDatabase struct {
    26  	db          sql.Database
    27  	IfNotExists bool
    28  }
    29  
    30  var showCreateDatabaseSchema = sql.Schema{
    31  	{Name: "Database", Type: types.LongText},
    32  	{Name: "Create Database", Type: types.LongText},
    33  }
    34  
    35  // NewShowCreateDatabase creates a new ShowCreateDatabase node.
    36  func NewShowCreateDatabase(db sql.Database, ifNotExists bool) *ShowCreateDatabase {
    37  	return &ShowCreateDatabase{db, ifNotExists}
    38  }
    39  
    40  var _ sql.Node = (*ShowCreateDatabase)(nil)
    41  var _ sql.Databaser = (*ShowCreateDatabase)(nil)
    42  var _ sql.CollationCoercible = (*ShowCreateDatabase)(nil)
    43  
    44  // Database implements the sql.Databaser interface.
    45  func (s *ShowCreateDatabase) Database() sql.Database {
    46  	return s.db
    47  }
    48  
    49  func (s *ShowCreateDatabase) IsReadOnly() bool {
    50  	return true
    51  }
    52  
    53  // WithDatabase implements the sql.Databaser interface.
    54  func (s *ShowCreateDatabase) WithDatabase(db sql.Database) (sql.Node, error) {
    55  	nc := *s
    56  	nc.db = db
    57  	return &nc, nil
    58  }
    59  
    60  // Schema implements the sql.Node interface.
    61  func (s *ShowCreateDatabase) Schema() sql.Schema {
    62  	return showCreateDatabaseSchema
    63  }
    64  
    65  func (s *ShowCreateDatabase) String() string {
    66  	return fmt.Sprintf("SHOW CREATE DATABASE %s", s.db.Name())
    67  }
    68  
    69  // Children implements the sql.Node interface.
    70  func (s *ShowCreateDatabase) Children() []sql.Node { return nil }
    71  
    72  // Resolved implements the sql.Node interface.
    73  func (s *ShowCreateDatabase) Resolved() bool {
    74  	_, ok := s.db.(sql.UnresolvedDatabase)
    75  	return !ok
    76  }
    77  
    78  // WithChildren implements the Node interface.
    79  func (s *ShowCreateDatabase) WithChildren(children ...sql.Node) (sql.Node, error) {
    80  	if len(children) != 0 {
    81  		return nil, sql.ErrInvalidChildrenNumber.New(s, len(children), 0)
    82  	}
    83  
    84  	return s, nil
    85  }
    86  
    87  // CheckPrivileges implements the interface sql.Node.
    88  func (s *ShowCreateDatabase) CheckPrivileges(ctx *sql.Context, opChecker sql.PrivilegedOperationChecker) bool {
    89  	// The database won't be visible during the resolution step if the user doesn't have the correct privileges
    90  	return true
    91  }
    92  
    93  // CollationCoercibility implements the interface sql.CollationCoercible.
    94  func (*ShowCreateDatabase) CollationCoercibility(ctx *sql.Context) (collation sql.CollationID, coercibility byte) {
    95  	return sql.Collation_binary, 7
    96  }