github.com/dolthub/go-mysql-server@v0.18.0/sql/plan/alter_auto_increment.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  )
    22  
    23  type AlterAutoIncrement struct {
    24  	ddlNode
    25  	Table   sql.Node
    26  	AutoVal uint64
    27  }
    28  
    29  var _ sql.Node = (*AlterAutoIncrement)(nil)
    30  var _ sql.CollationCoercible = (*AlterAutoIncrement)(nil)
    31  
    32  func NewAlterAutoIncrement(database sql.Database, table sql.Node, autoVal uint64) *AlterAutoIncrement {
    33  	return &AlterAutoIncrement{
    34  		ddlNode: ddlNode{Db: database},
    35  		Table:   table,
    36  		AutoVal: autoVal,
    37  	}
    38  }
    39  
    40  // WithChildren implements the Node interface.
    41  func (p *AlterAutoIncrement) WithChildren(children ...sql.Node) (sql.Node, error) {
    42  	if len(children) != 1 {
    43  		return nil, sql.ErrInvalidChildrenNumber.New(p, len(children), 1)
    44  	}
    45  	return NewAlterAutoIncrement(p.Database(), children[0], p.AutoVal), nil
    46  }
    47  
    48  // Children implements the sql.Node interface.
    49  func (p *AlterAutoIncrement) Children() []sql.Node {
    50  	return []sql.Node{p.Table}
    51  }
    52  
    53  // Resolved implements the sql.Node interface.
    54  func (p *AlterAutoIncrement) Resolved() bool {
    55  	return p.ddlNode.Resolved() && p.Table.Resolved()
    56  }
    57  
    58  func (p *AlterAutoIncrement) IsReadOnly() bool {
    59  	return false
    60  }
    61  
    62  // CheckPrivileges implements the interface sql.Node.
    63  func (p *AlterAutoIncrement) CheckPrivileges(ctx *sql.Context, opChecker sql.PrivilegedOperationChecker) bool {
    64  	subject := sql.PrivilegeCheckSubject{
    65  		Database: p.Database().Name(),
    66  		Table:    getTableName(p.Table),
    67  	}
    68  	return opChecker.UserHasPrivileges(ctx, sql.NewPrivilegedOperation(subject, sql.PrivilegeType_Alter))
    69  }
    70  
    71  // CollationCoercibility implements the interface sql.CollationCoercible.
    72  func (p *AlterAutoIncrement) CollationCoercibility(ctx *sql.Context) (collation sql.CollationID, coercibility byte) {
    73  	return sql.Collation_binary, 7
    74  }
    75  
    76  func (p *AlterAutoIncrement) Schema() sql.Schema { return nil }
    77  
    78  func (p AlterAutoIncrement) String() string {
    79  	pr := sql.NewTreePrinter()
    80  	_ = pr.WriteNode("AlterAutoIncrement(%d)", p.AutoVal)
    81  	_ = pr.WriteChildren(fmt.Sprintf("Table(%s)", p.Table.String()))
    82  	return pr.String()
    83  }
    84  
    85  // WithDatabase implements the sql.Databaser interface.
    86  func (p *AlterAutoIncrement) WithDatabase(db sql.Database) (sql.Node, error) {
    87  	nd := *p
    88  	nd.Db = db
    89  	return &nd, nil
    90  }