github.com/cockroachdb/cockroach@v20.2.0-alpha.1+incompatible/pkg/sql/delegate/show_var.go (about)

     1  // Copyright 2017 The Cockroach Authors.
     2  //
     3  // Use of this software is governed by the Business Source License
     4  // included in the file licenses/BSL.txt.
     5  //
     6  // As of the Change Date specified in that file, in accordance with
     7  // the Business Source License, use of this software will be governed
     8  // by the Apache License, Version 2.0, included in the file
     9  // licenses/APL.txt.
    10  
    11  package delegate
    12  
    13  import (
    14  	"fmt"
    15  	"strings"
    16  
    17  	"github.com/cockroachdb/cockroach/pkg/sql/lex"
    18  	"github.com/cockroachdb/cockroach/pkg/sql/pgwire/pgcode"
    19  	"github.com/cockroachdb/cockroach/pkg/sql/pgwire/pgerror"
    20  	"github.com/cockroachdb/cockroach/pkg/sql/sem/tree"
    21  	"github.com/cockroachdb/cockroach/pkg/sql/sqltelemetry"
    22  )
    23  
    24  // ValidVars contains the set of variable names; initialized from the SQL
    25  // package.
    26  var ValidVars = make(map[string]struct{})
    27  
    28  // Show a session-local variable name.
    29  func (d *delegator) delegateShowVar(n *tree.ShowVar) (tree.Statement, error) {
    30  	origName := n.Name
    31  	name := strings.ToLower(n.Name)
    32  
    33  	if name == "locality" {
    34  		sqltelemetry.IncrementShowCounter(sqltelemetry.Locality)
    35  	}
    36  
    37  	if name == "all" {
    38  		return parse(
    39  			"SELECT variable, value FROM crdb_internal.session_variables WHERE hidden = FALSE",
    40  		)
    41  	}
    42  
    43  	if _, ok := ValidVars[name]; !ok {
    44  		return nil, pgerror.Newf(pgcode.UndefinedObject,
    45  			"unrecognized configuration parameter %q", origName)
    46  	}
    47  
    48  	varName := lex.EscapeSQLString(name)
    49  	nm := tree.Name(name)
    50  	return parse(fmt.Sprintf(
    51  		`SELECT value AS %[1]s FROM crdb_internal.session_variables WHERE variable = %[2]s`,
    52  		nm.String(), varName,
    53  	))
    54  }