github.com/dolthub/go-mysql-server@v0.18.0/sql/plan/empty_table.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  	"io"
    19  
    20  	"github.com/dolthub/go-mysql-server/sql"
    21  )
    22  
    23  func IsEmptyTable(n sql.Node) bool {
    24  	_, ok := n.(*EmptyTable)
    25  	return ok
    26  }
    27  func NewEmptyTableWithSchema(schema sql.Schema) sql.Node {
    28  	return &EmptyTable{schema: schema}
    29  }
    30  
    31  var _ sql.Node = (*EmptyTable)(nil)
    32  var _ sql.CollationCoercible = (*EmptyTable)(nil)
    33  var _ sql.UpdatableTable = (*EmptyTable)(nil)
    34  var _ sql.DeletableTable = (*EmptyTable)(nil)
    35  var _ sql.RenameableNode = (*EmptyTable)(nil)
    36  
    37  type EmptyTable struct {
    38  	schema sql.Schema
    39  	id     sql.TableId
    40  	cols   sql.ColSet
    41  }
    42  
    43  // WithId implements sql.TableIdNode
    44  func (e *EmptyTable) WithId(id sql.TableId) TableIdNode {
    45  	ret := *e
    46  	ret.id = id
    47  	return &ret
    48  }
    49  
    50  // Id implements sql.TableIdNode
    51  func (e *EmptyTable) Id() sql.TableId {
    52  	return e.id
    53  }
    54  
    55  // WithColumns implements sql.TableIdNode
    56  func (e *EmptyTable) WithColumns(set sql.ColSet) TableIdNode {
    57  	ret := *e
    58  	ret.cols = set
    59  	return &ret
    60  }
    61  
    62  // Columns implements sql.TableIdNode
    63  func (e *EmptyTable) Columns() sql.ColSet {
    64  	return e.cols
    65  }
    66  
    67  func (e *EmptyTable) WithName(s string) sql.Node {
    68  	ret := *e
    69  	newSch := make(sql.Schema, len(ret.schema))
    70  	for i, c := range ret.schema {
    71  		newC := c.Copy()
    72  		newC.Source = s
    73  		newSch[i] = newC
    74  	}
    75  	ret.schema = newSch
    76  	return &ret
    77  }
    78  
    79  func (e *EmptyTable) Name() string {
    80  	if len(e.schema) == 0 {
    81  		return "__emptytable"
    82  	}
    83  	return e.schema[0].Source
    84  }
    85  
    86  func (e *EmptyTable) Schema() sql.Schema { return e.schema }
    87  func (*EmptyTable) Children() []sql.Node { return nil }
    88  func (*EmptyTable) Resolved() bool       { return true }
    89  func (*EmptyTable) IsReadOnly() bool     { return true }
    90  func (e *EmptyTable) String() string     { return "EmptyTable" }
    91  
    92  // RowIter implements the sql.Node interface.
    93  func (*EmptyTable) RowIter(ctx *sql.Context, row sql.Row) (sql.RowIter, error) {
    94  	return sql.RowsToRowIter(), nil
    95  }
    96  
    97  // WithChildren implements the sql.Node interface.
    98  func (e *EmptyTable) WithChildren(children ...sql.Node) (sql.Node, error) {
    99  	if len(children) != 0 {
   100  		return nil, sql.ErrInvalidChildrenNumber.New(e, len(children), 0)
   101  	}
   102  
   103  	return e, nil
   104  }
   105  
   106  // CheckPrivileges implements the interface sql.Node.
   107  func (e *EmptyTable) CheckPrivileges(ctx *sql.Context, opChecker sql.PrivilegedOperationChecker) bool {
   108  	return true
   109  }
   110  
   111  // CollationCoercibility implements the interface sql.CollationCoercible.
   112  func (*EmptyTable) CollationCoercibility(ctx *sql.Context) (collation sql.CollationID, coercibility byte) {
   113  	return sql.Collation_binary, 7
   114  }
   115  
   116  // Updater implements the sql.UpdatableTable interface.
   117  func (e *EmptyTable) Updater(ctx *sql.Context) sql.RowUpdater {
   118  	return &emptyTableUpdater{}
   119  }
   120  
   121  // Collation implements the sql.UpdatableTable interface.
   122  func (e *EmptyTable) Collation() sql.CollationID {
   123  	return sql.Collation_Default
   124  }
   125  
   126  // Partitions implements the sql.UpdatableTable interface.
   127  func (e *EmptyTable) Partitions(_ *sql.Context) (sql.PartitionIter, error) {
   128  	return &emptyTablePartitionIter{}, nil
   129  }
   130  
   131  // PartitionRows implements the sql.UpdatableTable interface.
   132  func (e *EmptyTable) PartitionRows(_ *sql.Context, _ sql.Partition) (sql.RowIter, error) {
   133  	return &emptyTableIter{}, nil
   134  }
   135  
   136  // Deleter implements the sql.DeletableTable interface.
   137  func (e *EmptyTable) Deleter(context *sql.Context) sql.RowDeleter {
   138  	return &emptyTableDeleter{}
   139  }
   140  
   141  type emptyTableUpdater struct{}
   142  
   143  var _ sql.RowUpdater = (*emptyTableUpdater)(nil)
   144  
   145  // StatementBegin implements the sql.EditOpenerCloser interface
   146  func (e *emptyTableUpdater) StatementBegin(_ *sql.Context) {}
   147  
   148  // DiscardChanges implements the sql.EditOpenerCloser interface
   149  func (e *emptyTableUpdater) DiscardChanges(_ *sql.Context, _ error) error {
   150  	return nil
   151  }
   152  
   153  // StatementComplete implements the sql.EditOpenerCloser interface
   154  func (e *emptyTableUpdater) StatementComplete(_ *sql.Context) error {
   155  	return nil
   156  }
   157  
   158  // Update implements the sql.RowUpdater interface
   159  func (e *emptyTableUpdater) Update(_ *sql.Context, _ sql.Row, _ sql.Row) error {
   160  	return nil
   161  }
   162  
   163  // Close implements the sql.Closer interface
   164  func (e *emptyTableUpdater) Close(_ *sql.Context) error {
   165  	return nil
   166  }
   167  
   168  type emptyTableIter struct{}
   169  
   170  var _ sql.RowIter = (*emptyTableIter)(nil)
   171  
   172  // Next implements the sql.RowIter interface.
   173  func (e *emptyTableIter) Next(_ *sql.Context) (sql.Row, error) {
   174  	return nil, io.EOF
   175  }
   176  
   177  // Close implements the sql.RowIter interface.
   178  func (e *emptyTableIter) Close(_ *sql.Context) error {
   179  	return nil
   180  }
   181  
   182  type emptyTablePartitionIter struct{}
   183  
   184  var _ sql.PartitionIter = (*emptyTablePartitionIter)(nil)
   185  
   186  // Close implements the sql.PartitionIter interface.
   187  func (e *emptyTablePartitionIter) Close(_ *sql.Context) error {
   188  	return nil
   189  }
   190  
   191  // Next implements the sql.PartitionIter interface.
   192  func (e *emptyTablePartitionIter) Next(_ *sql.Context) (sql.Partition, error) {
   193  	return nil, io.EOF
   194  }
   195  
   196  type emptyTableDeleter struct{}
   197  
   198  var _ sql.RowDeleter = (*emptyTableDeleter)(nil)
   199  
   200  func (e *emptyTableDeleter) StatementBegin(_ *sql.Context) {}
   201  
   202  func (e *emptyTableDeleter) DiscardChanges(_ *sql.Context, _ error) error {
   203  	return nil
   204  }
   205  
   206  func (e *emptyTableDeleter) StatementComplete(_ *sql.Context) error {
   207  	return nil
   208  }
   209  
   210  func (e *emptyTableDeleter) Delete(_ *sql.Context, _ sql.Row) error {
   211  	return nil
   212  }
   213  
   214  func (e *emptyTableDeleter) Close(_ *sql.Context) error {
   215  	return nil
   216  }