github.com/insionng/yougam@v0.0.0-20170714101924-2bc18d833463/libraries/pingcap/tidb/evaluator/builtin_info.go (about)

     1  // Copyright 2013 The ql Authors. All rights reserved.
     2  // Use of this source code is governed by a BSD-style
     3  // license that can be found in the LICENSES/QL-LICENSE file.
     4  
     5  // Copyright 2015 PingCAP, Inc.
     6  //
     7  // Licensed under the Apache License, Version 2.0 (the "License");
     8  // you may not use this file except in compliance with the License.
     9  // You may obtain a copy of the License at
    10  //
    11  //     http://www.apache.org/licenses/LICENSE-2.0
    12  //
    13  // Unless required by applicable law or agreed to in writing, software
    14  // distributed under the License is distributed on an "AS IS" BASIS,
    15  // See the License for the specific language governing permissions and
    16  // limitations under the License.
    17  
    18  package evaluator
    19  
    20  import (
    21  	"github.com/insionng/yougam/libraries/juju/errors"
    22  	"github.com/insionng/yougam/libraries/pingcap/tidb/context"
    23  	"github.com/insionng/yougam/libraries/pingcap/tidb/mysql"
    24  	"github.com/insionng/yougam/libraries/pingcap/tidb/sessionctx/db"
    25  	"github.com/insionng/yougam/libraries/pingcap/tidb/sessionctx/variable"
    26  	"github.com/insionng/yougam/libraries/pingcap/tidb/util/types"
    27  )
    28  
    29  // See: https://dev.mysql.com/doc/refman/5.7/en/information-functions.html
    30  
    31  func builtinDatabase(args []types.Datum, ctx context.Context) (d types.Datum, err error) {
    32  	s := db.GetCurrentSchema(ctx)
    33  	if s == "" {
    34  		return d, nil
    35  	}
    36  	d.SetString(s)
    37  	return d, nil
    38  }
    39  
    40  func builtinFoundRows(arg []types.Datum, ctx context.Context) (d types.Datum, err error) {
    41  	data := variable.GetSessionVars(ctx)
    42  	if data == nil {
    43  		return d, errors.Errorf("Missing session variable when evalue builtin")
    44  	}
    45  
    46  	d.SetUint64(data.FoundRows)
    47  	return d, nil
    48  }
    49  
    50  // See: https://dev.mysql.com/doc/refman/5.7/en/information-functions.html#function_current-user
    51  // TODO: The value of CURRENT_USER() can differ from the value of USER(). We will finish this after we support grant tables.
    52  func builtinCurrentUser(args []types.Datum, ctx context.Context) (d types.Datum, err error) {
    53  	data := variable.GetSessionVars(ctx)
    54  	if data == nil {
    55  		return d, errors.Errorf("Missing session variable when evalue builtin")
    56  	}
    57  
    58  	d.SetString(data.User)
    59  	return d, nil
    60  }
    61  
    62  func builtinUser(args []types.Datum, ctx context.Context) (d types.Datum, err error) {
    63  	data := variable.GetSessionVars(ctx)
    64  	if data == nil {
    65  		return d, errors.Errorf("Missing session variable when evalue builtin")
    66  	}
    67  
    68  	d.SetString(data.User)
    69  	return d, nil
    70  }
    71  
    72  func builtinConnectionID(args []types.Datum, ctx context.Context) (d types.Datum, err error) {
    73  	data := variable.GetSessionVars(ctx)
    74  	if data == nil {
    75  		return d, errors.Errorf("Missing session variable when evalue builtin")
    76  	}
    77  
    78  	d.SetUint64(data.ConnectionID)
    79  	return d, nil
    80  }
    81  
    82  func builtinVersion(args []types.Datum, ctx context.Context) (d types.Datum, err error) {
    83  	d.SetString(mysql.ServerVersion)
    84  	return d, nil
    85  }