github.com/dolthub/go-mysql-server@v0.18.0/sql/plan/show_grants.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  
    20  	"github.com/dolthub/go-mysql-server/sql/types"
    21  
    22  	"github.com/dolthub/go-mysql-server/sql"
    23  )
    24  
    25  // ShowGrants represents the statement SHOW GRANTS.
    26  type ShowGrants struct {
    27  	CurrentUser bool
    28  	For         *UserName
    29  	Using       []UserName
    30  	MySQLDb     sql.Database
    31  }
    32  
    33  var _ sql.Node = (*ShowGrants)(nil)
    34  var _ sql.Databaser = (*ShowGrants)(nil)
    35  var _ sql.CollationCoercible = (*ShowGrants)(nil)
    36  
    37  // Schema implements the interface sql.Node.
    38  func (n *ShowGrants) Schema() sql.Schema {
    39  	user := n.For
    40  	if user == nil {
    41  		user = &UserName{
    42  			Name:    "root",
    43  			Host:    "localhost",
    44  			AnyHost: true,
    45  		}
    46  	}
    47  	return sql.Schema{{
    48  		Name: fmt.Sprintf("Grants for %s", user.String("")),
    49  		Type: types.LongText,
    50  	}}
    51  }
    52  
    53  func (n *ShowGrants) IsReadOnly() bool {
    54  	return true
    55  }
    56  
    57  // String implements the interface sql.Node.
    58  func (n *ShowGrants) String() string {
    59  	user := n.For
    60  	if user == nil {
    61  		user = &UserName{
    62  			Name:    "root",
    63  			Host:    "localhost",
    64  			AnyHost: true,
    65  		}
    66  	}
    67  	return fmt.Sprintf("ShowGrants(%s)", user.String(""))
    68  }
    69  
    70  // Database implements the interface sql.Databaser.
    71  func (n *ShowGrants) Database() sql.Database {
    72  	return n.MySQLDb
    73  }
    74  
    75  // WithDatabase implements the interface sql.Databaser.
    76  func (n *ShowGrants) WithDatabase(db sql.Database) (sql.Node, error) {
    77  	nn := *n
    78  	nn.MySQLDb = db
    79  	return &nn, nil
    80  }
    81  
    82  // Resolved implements the interface sql.Node.
    83  func (n *ShowGrants) Resolved() bool {
    84  	_, ok := n.MySQLDb.(sql.UnresolvedDatabase)
    85  	return !ok
    86  }
    87  
    88  // Children implements the interface sql.Node.
    89  func (n *ShowGrants) Children() []sql.Node {
    90  	return nil
    91  }
    92  
    93  // WithChildren implements the interface sql.Node.
    94  func (n *ShowGrants) WithChildren(children ...sql.Node) (sql.Node, error) {
    95  	if len(children) != 0 {
    96  		return nil, sql.ErrInvalidChildrenNumber.New(n, len(children), 0)
    97  	}
    98  	return n, nil
    99  }
   100  
   101  // CheckPrivileges implements the interface sql.Node.
   102  func (n *ShowGrants) CheckPrivileges(ctx *sql.Context, opChecker sql.PrivilegedOperationChecker) bool {
   103  	if n.CurrentUser {
   104  		return true
   105  	}
   106  
   107  	subject := sql.PrivilegeCheckSubject{Database: "mysql"}
   108  	return opChecker.UserHasPrivileges(ctx, sql.NewPrivilegedOperation(subject, sql.PrivilegeType_Select))
   109  }
   110  
   111  // CollationCoercibility implements the interface sql.CollationCoercible.
   112  func (*ShowGrants) CollationCoercibility(ctx *sql.Context) (collation sql.CollationID, coercibility byte) {
   113  	return sql.Collation_binary, 7
   114  }