github.com/dolthub/go-mysql-server@v0.18.0/sql/plan/drop_index.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  	"gopkg.in/src-d/go-errors.v1"
    19  
    20  	"github.com/dolthub/go-mysql-server/sql"
    21  )
    22  
    23  var (
    24  	// ErrIndexNotFound is returned when the index cannot be found.
    25  	ErrIndexNotFound = errors.NewKind("unable to find index %q on table %q of database %q")
    26  	// ErrTableNotValid is returned when the table is not valid
    27  	ErrTableNotValid = errors.NewKind("table is not valid")
    28  	// ErrTableNotNameable is returned when the table is not nameable.
    29  	ErrTableNotNameable = errors.NewKind("can't get name from table")
    30  	// ErrIndexNotAvailable is returned when trying to delete an index that is
    31  	// still not ready for usage.
    32  	ErrIndexNotAvailable = errors.NewKind("index %q is still not ready for usage and can't be deleted")
    33  )
    34  
    35  // DropIndex is a node to drop an index.
    36  type DropIndex struct {
    37  	Name            string
    38  	Table           sql.Node
    39  	Catalog         sql.Catalog
    40  	CurrentDatabase string
    41  }
    42  
    43  // NewDropIndex creates a new DropIndex node.
    44  func NewDropIndex(name string, table sql.Node) *DropIndex {
    45  	return &DropIndex{name, table, nil, ""}
    46  }
    47  
    48  var _ sql.Node = (*DropIndex)(nil)
    49  var _ sql.Databaseable = (*DropIndex)(nil)
    50  var _ sql.CollationCoercible = (*DropIndex)(nil)
    51  
    52  func (d *DropIndex) Database() string { return d.CurrentDatabase }
    53  
    54  // Resolved implements the Node interface.
    55  func (d *DropIndex) Resolved() bool { return d.Table.Resolved() }
    56  
    57  func (d *DropIndex) IsReadOnly() bool { return false }
    58  
    59  // Schema implements the Node interface.
    60  func (d *DropIndex) Schema() sql.Schema { return nil }
    61  
    62  // Children implements the Node interface.
    63  func (d *DropIndex) Children() []sql.Node { return []sql.Node{d.Table} }
    64  
    65  func (d *DropIndex) String() string {
    66  	pr := sql.NewTreePrinter()
    67  	_ = pr.WriteNode("DropIndex(%s)", d.Name)
    68  	_ = pr.WriteChildren(d.Table.String())
    69  	return pr.String()
    70  }
    71  
    72  // WithChildren implements the Node interface.
    73  func (d *DropIndex) WithChildren(children ...sql.Node) (sql.Node, error) {
    74  	if len(children) != 1 {
    75  		return nil, sql.ErrInvalidChildrenNumber.New(d, len(children), 1)
    76  	}
    77  
    78  	nd := *d
    79  	nd.Table = children[0]
    80  	return &nd, nil
    81  }
    82  
    83  // CheckPrivileges implements the interface sql.Node.
    84  func (d *DropIndex) CheckPrivileges(ctx *sql.Context, opChecker sql.PrivilegedOperationChecker) bool {
    85  	subject := sql.PrivilegeCheckSubject{
    86  		Database: GetDatabaseName(d.Table),
    87  		Table:    getTableName(d.Table),
    88  	}
    89  
    90  	return opChecker.UserHasPrivileges(ctx, sql.NewPrivilegedOperation(subject, sql.PrivilegeType_Index))
    91  }
    92  
    93  // CollationCoercibility implements the interface sql.CollationCoercible.
    94  func (*DropIndex) CollationCoercibility(ctx *sql.Context) (collation sql.CollationID, coercibility byte) {
    95  	return sql.Collation_binary, 7
    96  }