github.com/dolthub/go-mysql-server@v0.18.0/sql/plan/show_status.go (about)

     1  // Copyright 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  	"sort"
    20  
    21  	"github.com/dolthub/vitess/go/sqltypes"
    22  
    23  	"github.com/dolthub/go-mysql-server/sql"
    24  	"github.com/dolthub/go-mysql-server/sql/types"
    25  )
    26  
    27  const ShowStatusVariableCol = "Variable_name"
    28  const ShowStatusValueCol = "Value"
    29  
    30  // ShowStatus implements the SHOW STATUS MySQL command.
    31  // TODO: This is just a stub implementation that returns an empty set. The actual functionality needs to be implemented
    32  // in the future.
    33  type ShowStatus struct {
    34  	Modifier ShowStatusModifier
    35  }
    36  
    37  var _ sql.Node = (*ShowStatus)(nil)
    38  var _ sql.CollationCoercible = (*ShowStatus)(nil)
    39  
    40  type ShowStatusModifier byte
    41  
    42  const (
    43  	ShowStatusModifier_Session ShowStatusModifier = iota
    44  	ShowStatusModifier_Global
    45  )
    46  
    47  // NewShowStatus returns a new ShowStatus reference.
    48  func NewShowStatus(modifier ShowStatusModifier) *ShowStatus {
    49  	return &ShowStatus{Modifier: modifier}
    50  }
    51  
    52  // Resolved implements sql.Node interface.
    53  func (s *ShowStatus) Resolved() bool {
    54  	return true
    55  }
    56  
    57  func (s *ShowStatus) IsReadOnly() bool {
    58  	return true
    59  }
    60  
    61  // String implements sql.Node interface.
    62  func (s *ShowStatus) String() string {
    63  	return "SHOW STATUS"
    64  }
    65  
    66  // Schema implements sql.Node interface.
    67  func (s *ShowStatus) Schema() sql.Schema {
    68  	return sql.Schema{
    69  		{Name: ShowStatusVariableCol, Type: types.MustCreateStringWithDefaults(sqltypes.VarChar, 64), Default: nil, Nullable: false},
    70  		{Name: ShowStatusValueCol, Type: types.MustCreateStringWithDefaults(sqltypes.VarChar, 2048), Default: nil, Nullable: false},
    71  	}
    72  }
    73  
    74  // Children implements sql.Node interface.
    75  func (s *ShowStatus) Children() []sql.Node {
    76  	return nil
    77  }
    78  
    79  // RowIter implements sql.Node interface.
    80  func (s *ShowStatus) RowIter(ctx *sql.Context, row sql.Row) (sql.RowIter, error) {
    81  	var names []string
    82  	for name := range sql.SystemVariables.NewSessionMap() {
    83  		names = append(names, name)
    84  	}
    85  	sort.Strings(names)
    86  
    87  	var rows []sql.Row
    88  	for _, name := range names {
    89  		sysVar, val, ok := sql.SystemVariables.GetGlobal(name)
    90  		if !ok {
    91  			return nil, fmt.Errorf("missing system variable %s", name)
    92  		}
    93  
    94  		if s.Modifier == ShowStatusModifier_Session && sysVar.Scope == sql.SystemVariableScope_Global ||
    95  			s.Modifier == ShowStatusModifier_Global && sysVar.Scope == sql.SystemVariableScope_Session {
    96  			continue
    97  		}
    98  
    99  		rows = append(rows, sql.Row{name, val})
   100  	}
   101  
   102  	return sql.RowsToRowIter(rows...), nil
   103  }
   104  
   105  // WithChildren implements sql.Node interface.
   106  func (s *ShowStatus) WithChildren(node ...sql.Node) (sql.Node, error) {
   107  	return NewShowStatus(s.Modifier), nil
   108  }
   109  
   110  // CheckPrivileges implements the interface sql.Node.
   111  func (s *ShowStatus) CheckPrivileges(ctx *sql.Context, opChecker sql.PrivilegedOperationChecker) bool {
   112  	return true
   113  }
   114  
   115  // CollationCoercibility implements the interface sql.CollationCoercible.
   116  func (*ShowStatus) CollationCoercibility(ctx *sql.Context) (collation sql.CollationID, coercibility byte) {
   117  	return sql.Collation_binary, 7
   118  }