github.com/dolthub/go-mysql-server@v0.18.0/sql/plan/showvariables.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  	"github.com/dolthub/go-mysql-server/sql/types"
    22  )
    23  
    24  // ShowVariables is a node that shows the global and session variables
    25  type ShowVariables struct {
    26  	Filter sql.Expression
    27  	Global bool
    28  }
    29  
    30  var _ sql.Node = (*ShowVariables)(nil)
    31  var _ sql.Expressioner = (*ShowVariables)(nil)
    32  var _ sql.CollationCoercible = (*ShowVariables)(nil)
    33  
    34  // NewShowVariables returns a new ShowVariables reference.
    35  func NewShowVariables(filter sql.Expression, isGlobal bool) *ShowVariables {
    36  	return &ShowVariables{
    37  		Filter: filter,
    38  		Global: isGlobal,
    39  	}
    40  }
    41  
    42  // Resolved implements sql.Node interface. The function always returns true.
    43  func (sv *ShowVariables) Resolved() bool {
    44  	return true
    45  }
    46  
    47  // WithChildren implements the Node interface.
    48  func (sv *ShowVariables) WithChildren(children ...sql.Node) (sql.Node, error) {
    49  	if len(children) != 0 {
    50  		return nil, sql.ErrInvalidChildrenNumber.New(sv, len(children), 0)
    51  	}
    52  
    53  	return sv, nil
    54  }
    55  
    56  // CheckPrivileges implements the interface sql.Node.
    57  func (sv *ShowVariables) CheckPrivileges(ctx *sql.Context, opChecker sql.PrivilegedOperationChecker) bool {
    58  	return true
    59  }
    60  
    61  // CollationCoercibility implements the interface sql.CollationCoercible.
    62  func (*ShowVariables) CollationCoercibility(ctx *sql.Context) (collation sql.CollationID, coercibility byte) {
    63  	return sql.Collation_binary, 7
    64  }
    65  
    66  // String implements the fmt.Stringer interface.
    67  func (sv *ShowVariables) String() string {
    68  	var f string
    69  	if sv.Filter != nil {
    70  		f = fmt.Sprintf(" WHERE %s", sv.Filter.String())
    71  	}
    72  
    73  	if sv.Global {
    74  		return fmt.Sprintf("SHOW GLOBAL VARIABLES%s", f)
    75  	}
    76  	return fmt.Sprintf("SHOW VARIABLES%s", f)
    77  }
    78  
    79  // Schema returns a new Schema reference for "SHOW VARIABLES" query.
    80  func (*ShowVariables) Schema() sql.Schema {
    81  	return sql.Schema{
    82  		&sql.Column{Name: "Variable_name", Type: types.LongText, Nullable: false},
    83  		&sql.Column{Name: "Value", Type: types.LongText, Nullable: true},
    84  	}
    85  }
    86  
    87  // Children implements sql.Node interface. The function always returns nil.
    88  func (*ShowVariables) Children() []sql.Node { return nil }
    89  
    90  func (sv *ShowVariables) Expressions() []sql.Expression {
    91  	return []sql.Expression{sv.Filter}
    92  }
    93  
    94  func (sv *ShowVariables) IsReadOnly() bool {
    95  	return true
    96  }
    97  
    98  func (sv *ShowVariables) WithExpressions(exprs ...sql.Expression) (sql.Node, error) {
    99  	if len(exprs) != 1 {
   100  		return nil, sql.ErrInvalidChildrenNumber.New(sv, len(exprs), 1)
   101  	}
   102  	ret := *sv
   103  	ret.Filter = exprs[0]
   104  	return &ret, nil
   105  }