github.com/dolthub/go-mysql-server@v0.18.0/sql/expression/function/system.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 function
    16  
    17  import (
    18  	"github.com/dolthub/go-mysql-server/sql"
    19  	"github.com/dolthub/go-mysql-server/sql/types"
    20  )
    21  
    22  type ConnectionID struct {
    23  	NoArgFunc
    24  }
    25  
    26  func (c ConnectionID) IsNonDeterministic() bool {
    27  	return true
    28  }
    29  
    30  func connIDFuncLogic(ctx *sql.Context, _ sql.Row) (interface{}, error) {
    31  	return ctx.ID(), nil
    32  }
    33  
    34  var _ sql.FunctionExpression = ConnectionID{}
    35  var _ sql.CollationCoercible = ConnectionID{}
    36  
    37  func NewConnectionID() sql.Expression {
    38  	return ConnectionID{
    39  		NoArgFunc: NoArgFunc{"connection_id", types.Uint32},
    40  	}
    41  }
    42  
    43  // FunctionName implements sql.FunctionExpression
    44  func (c ConnectionID) FunctionName() string {
    45  	return "connection_id"
    46  }
    47  
    48  // Description implements sql.FunctionExpression
    49  func (c ConnectionID) Description() string {
    50  	return "returns the current connection ID."
    51  }
    52  
    53  // CollationCoercibility implements the interface sql.CollationCoercible.
    54  func (ConnectionID) CollationCoercibility(ctx *sql.Context) (collation sql.CollationID, coercibility byte) {
    55  	return sql.Collation_utf8mb3_general_ci, 3
    56  }
    57  
    58  // Eval implements sql.Expression
    59  func (c ConnectionID) Eval(ctx *sql.Context, row sql.Row) (interface{}, error) {
    60  	return connIDFuncLogic(ctx, row)
    61  }
    62  
    63  // WithChildren implements sql.Expression
    64  func (c ConnectionID) WithChildren(children ...sql.Expression) (sql.Expression, error) {
    65  	return NoArgFuncWithChildren(c, children)
    66  }
    67  
    68  type User struct {
    69  	NoArgFunc
    70  }
    71  
    72  func (c User) IsNonDeterministic() bool {
    73  	return true
    74  }
    75  
    76  func userFuncLogic(ctx *sql.Context, _ sql.Row) (interface{}, error) {
    77  	if ctx.Client().User == "" && ctx.Client().Address == "" {
    78  		return "", nil
    79  	}
    80  
    81  	return ctx.Client().User + "@" + ctx.Client().Address, nil
    82  }
    83  
    84  var _ sql.FunctionExpression = User{}
    85  var _ sql.CollationCoercible = User{}
    86  
    87  // Description implements sql.FunctionExpression
    88  func (c User) Description() string {
    89  	return "returns the authenticated user name and host name."
    90  }
    91  
    92  // CollationCoercibility implements the interface sql.CollationCoercible.
    93  func (User) CollationCoercibility(ctx *sql.Context) (collation sql.CollationID, coercibility byte) {
    94  	return sql.Collation_utf8mb3_general_ci, 3
    95  }
    96  
    97  func NewUser() sql.Expression {
    98  	return User{
    99  		NoArgFunc: NoArgFunc{"user", types.LongText},
   100  	}
   101  }
   102  
   103  func NewCurrentUser() sql.Expression {
   104  	return User{
   105  		NoArgFunc: NoArgFunc{"current_user", types.LongText},
   106  	}
   107  }
   108  
   109  // Eval implements sql.Expression
   110  func (c User) Eval(ctx *sql.Context, row sql.Row) (interface{}, error) {
   111  	return userFuncLogic(ctx, row)
   112  }
   113  
   114  // WithChildren implements sql.Expression
   115  func (c User) WithChildren(children ...sql.Expression) (sql.Expression, error) {
   116  	return NoArgFuncWithChildren(c, children)
   117  }