github.com/dolthub/go-mysql-server@v0.18.0/sql/plan/drop_view.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  	errors "gopkg.in/src-d/go-errors.v1"
    19  
    20  	"github.com/dolthub/go-mysql-server/sql/mysql_db"
    21  
    22  	"github.com/dolthub/go-mysql-server/sql"
    23  )
    24  
    25  var ErrDropViewChild = errors.NewKind("any child of DropView must be of type SingleDropView")
    26  
    27  type SingleDropView struct {
    28  	database sql.Database
    29  	ViewName string
    30  }
    31  
    32  var _ sql.Node = (*SingleDropView)(nil)
    33  var _ sql.CollationCoercible = (*SingleDropView)(nil)
    34  
    35  // NewSingleDropView creates a SingleDropView.
    36  func NewSingleDropView(
    37  	database sql.Database,
    38  	viewName string,
    39  ) *SingleDropView {
    40  	return &SingleDropView{database, viewName}
    41  }
    42  
    43  // Children implements the Node interface. It always returns nil.
    44  func (dv *SingleDropView) Children() []sql.Node {
    45  	return nil
    46  }
    47  
    48  // Resolved implements the Node interface. This node is resolved if and only if
    49  // its database is resolved.
    50  func (dv *SingleDropView) Resolved() bool {
    51  	_, ok := dv.database.(sql.UnresolvedDatabase)
    52  	return !ok
    53  }
    54  
    55  func (dv *SingleDropView) IsReadOnly() bool {
    56  	return false
    57  }
    58  
    59  // RowIter implements the Node interface. It always returns an empty iterator.
    60  func (dv *SingleDropView) RowIter(ctx *sql.Context, row sql.Row) (sql.RowIter, error) {
    61  	return sql.RowsToRowIter(), nil
    62  }
    63  
    64  // Schema implements the Node interface. It always returns nil.
    65  func (dv *SingleDropView) Schema() sql.Schema { return nil }
    66  
    67  // String implements the fmt.Stringer interface, using sql.TreePrinter to
    68  // generate the string.
    69  func (dv *SingleDropView) String() string {
    70  	pr := sql.NewTreePrinter()
    71  	_ = pr.WriteNode("SingleDropView(%s.%s)", dv.database.Name(), dv.ViewName)
    72  
    73  	return pr.String()
    74  }
    75  
    76  // WithChildren implements the Node interface. It only succeeds if the length
    77  // of the specified children equals 0.
    78  func (dv *SingleDropView) WithChildren(children ...sql.Node) (sql.Node, error) {
    79  	if len(children) != 0 {
    80  		return nil, sql.ErrInvalidChildrenNumber.New(dv, len(children), 0)
    81  	}
    82  
    83  	return dv, nil
    84  }
    85  
    86  // CheckPrivileges implements the interface sql.Node.
    87  func (dv *SingleDropView) CheckPrivileges(ctx *sql.Context, opChecker sql.PrivilegedOperationChecker) bool {
    88  	subject := sql.PrivilegeCheckSubject{
    89  		Database: dv.database.Name(),
    90  	}
    91  	return opChecker.UserHasPrivileges(ctx,
    92  		sql.NewPrivilegedOperation(subject, sql.PrivilegeType_Drop))
    93  }
    94  
    95  // CollationCoercibility implements the interface sql.CollationCoercible.
    96  func (*SingleDropView) CollationCoercibility(ctx *sql.Context) (collation sql.CollationID, coercibility byte) {
    97  	return sql.Collation_binary, 7
    98  }
    99  
   100  // Database implements the sql.Databaser interface. It returns the node's database.
   101  func (dv *SingleDropView) Database() sql.Database {
   102  	return dv.database
   103  }
   104  
   105  // WithDatabase implements the sql.Databaser interface, and it returns a copy of this
   106  // node with the specified database.
   107  func (dv *SingleDropView) WithDatabase(database sql.Database) (sql.Node, error) {
   108  	if privilegedDatabase, ok := database.(mysql_db.PrivilegedDatabase); ok {
   109  		database = privilegedDatabase.Unwrap()
   110  	}
   111  	newDrop := *dv
   112  	newDrop.database = database
   113  	return &newDrop, nil
   114  }
   115  
   116  // DropView is a node representing the removal of a list of views, defined by
   117  // the children member. The flag ifExists represents whether the user wants the
   118  // node to fail if any of the views in children does not exist.
   119  type DropView struct {
   120  	children []sql.Node
   121  	IfExists bool
   122  }
   123  
   124  var _ sql.Node = (*DropView)(nil)
   125  var _ sql.CollationCoercible = (*DropView)(nil)
   126  
   127  // NewDropView creates a DropView node with the specified parameters,
   128  // setting its catalog to nil.
   129  func NewDropView(children []sql.Node, ifExists bool) *DropView {
   130  	return &DropView{children: children, IfExists: ifExists}
   131  }
   132  
   133  // Children implements the Node interface. It returns the children of the
   134  // CreateView node; i.e., all the views that will be dropped.
   135  func (dvs *DropView) Children() []sql.Node {
   136  	return dvs.children
   137  }
   138  
   139  // Resolved implements the Node interface. This node is resolved if and only if
   140  // all of its children are resolved.
   141  func (dvs *DropView) Resolved() bool {
   142  	for _, child := range dvs.children {
   143  		if !child.Resolved() {
   144  			return false
   145  		}
   146  	}
   147  	return true
   148  }
   149  
   150  // Schema implements the Node interface. It always returns nil.
   151  func (dvs *DropView) Schema() sql.Schema { return nil }
   152  
   153  // String implements the fmt.Stringer interface, using sql.TreePrinter to
   154  // generate the string.
   155  func (dvs *DropView) String() string {
   156  	childrenStrings := make([]string, len(dvs.children))
   157  	for i, child := range dvs.children {
   158  		childrenStrings[i] = child.String()
   159  	}
   160  
   161  	pr := sql.NewTreePrinter()
   162  	_ = pr.WriteNode("DropView")
   163  	_ = pr.WriteChildren(childrenStrings...)
   164  
   165  	return pr.String()
   166  }
   167  
   168  // WithChildren implements the Node interface. It always suceeds, returning a
   169  // copy of this node with the new array of nodes as children.
   170  func (dvs *DropView) WithChildren(children ...sql.Node) (sql.Node, error) {
   171  	newDrop := dvs
   172  	newDrop.children = children
   173  	return newDrop, nil
   174  }
   175  
   176  // CheckPrivileges implements the interface sql.Node.
   177  func (dvs *DropView) CheckPrivileges(ctx *sql.Context, opChecker sql.PrivilegedOperationChecker) bool {
   178  	for _, child := range dvs.children {
   179  		if !child.CheckPrivileges(ctx, opChecker) {
   180  			return false
   181  		}
   182  	}
   183  	return true
   184  }
   185  
   186  func (dvs *DropView) IsReadOnly() bool {
   187  	return false
   188  }
   189  
   190  // CollationCoercibility implements the interface sql.CollationCoercible.
   191  func (*DropView) CollationCoercibility(ctx *sql.Context) (collation sql.CollationID, coercibility byte) {
   192  	return sql.Collation_binary, 7
   193  }