github.com/dolthub/go-mysql-server@v0.18.0/sql/plan/create_user.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 plan
    16  
    17  import (
    18  	"fmt"
    19  	"strings"
    20  
    21  	"github.com/dolthub/go-mysql-server/sql"
    22  	"github.com/dolthub/go-mysql-server/sql/types"
    23  )
    24  
    25  // CreateUser represents the statement CREATE USER.
    26  type CreateUser struct {
    27  	IfNotExists     bool
    28  	Users           []AuthenticatedUser
    29  	DefaultRoles    []UserName
    30  	TLSOptions      *TLSOptions
    31  	AccountLimits   *AccountLimits
    32  	PasswordOptions *PasswordOptions
    33  	Locked          bool
    34  	Attribute       string
    35  	MySQLDb         sql.Database
    36  }
    37  
    38  var _ sql.Node = (*CreateUser)(nil)
    39  var _ sql.Databaser = (*CreateUser)(nil)
    40  var _ sql.CollationCoercible = (*CreateUser)(nil)
    41  
    42  // Schema implements the interface sql.Node.
    43  func (n *CreateUser) Schema() sql.Schema {
    44  	return types.OkResultSchema
    45  }
    46  
    47  // String implements the interface sql.Node.
    48  func (n *CreateUser) String() string {
    49  	users := make([]string, len(n.Users))
    50  	for i, user := range n.Users {
    51  		users[i] = user.UserName.String("")
    52  	}
    53  	ifNotExists := ""
    54  	if n.IfNotExists {
    55  		ifNotExists = "IfNotExists: "
    56  	}
    57  	return fmt.Sprintf("CreateUser(%s%s)", ifNotExists, strings.Join(users, ", "))
    58  }
    59  
    60  // Database implements the interface sql.Databaser.
    61  func (n *CreateUser) Database() sql.Database {
    62  	return n.MySQLDb
    63  }
    64  
    65  // WithDatabase implements the interface sql.Databaser.
    66  func (n *CreateUser) WithDatabase(db sql.Database) (sql.Node, error) {
    67  	nn := *n
    68  	nn.MySQLDb = db
    69  	return &nn, nil
    70  }
    71  
    72  // Resolved implements the interface sql.Node.
    73  func (n *CreateUser) Resolved() bool {
    74  	_, ok := n.MySQLDb.(sql.UnresolvedDatabase)
    75  	return !ok
    76  }
    77  
    78  func (n *CreateUser) IsReadOnly() bool {
    79  	return false
    80  }
    81  
    82  // Children implements the interface sql.Node.
    83  func (n *CreateUser) Children() []sql.Node {
    84  	return nil
    85  }
    86  
    87  // WithChildren implements the interface sql.Node.
    88  func (n *CreateUser) WithChildren(children ...sql.Node) (sql.Node, error) {
    89  	if len(children) != 0 {
    90  		return nil, sql.ErrInvalidChildrenNumber.New(n, len(children), 0)
    91  	}
    92  	return n, nil
    93  }
    94  
    95  // CheckPrivileges implements the interface sql.Node.
    96  func (n *CreateUser) CheckPrivileges(ctx *sql.Context, opChecker sql.PrivilegedOperationChecker) bool {
    97  	return opChecker.UserHasPrivileges(ctx,
    98  		sql.NewPrivilegedOperation(sql.PrivilegeCheckSubject{}, sql.PrivilegeType_CreateUser))
    99  }
   100  
   101  // CollationCoercibility implements the interface sql.CollationCoercible.
   102  func (*CreateUser) CollationCoercibility(ctx *sql.Context) (collation sql.CollationID, coercibility byte) {
   103  	return sql.Collation_binary, 7
   104  }