github.com/dolthub/go-mysql-server@v0.18.0/sql/expression/function/database.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  	"fmt"
    19  
    20  	"github.com/dolthub/go-mysql-server/sql"
    21  	"github.com/dolthub/go-mysql-server/sql/types"
    22  )
    23  
    24  // Database implements the DATABASE() function
    25  type Database struct{}
    26  
    27  func (db *Database) IsNonDeterministic() bool {
    28  	return true
    29  }
    30  
    31  var _ sql.FunctionExpression = (*Database)(nil)
    32  var _ sql.CollationCoercible = (*Database)(nil)
    33  
    34  // NewDatabase returns a new Database function
    35  func NewDatabase() sql.Expression {
    36  	return &Database{}
    37  }
    38  
    39  // FunctionName implements sql.FunctionExpression
    40  func (db *Database) FunctionName() string {
    41  	return "database"
    42  }
    43  
    44  // Description implements sql.FunctionExpression
    45  func (db *Database) Description() string {
    46  	return "returns the default (current) database name."
    47  }
    48  
    49  // Type implements the sql.Expression (sql.LongText)
    50  func (db *Database) Type() sql.Type { return types.LongText }
    51  
    52  // CollationCoercibility implements the interface sql.CollationCoercible.
    53  func (*Database) CollationCoercibility(ctx *sql.Context) (collation sql.CollationID, coercibility byte) {
    54  	return sql.Collation_utf8mb3_general_ci, 3
    55  }
    56  
    57  // IsNullable implements the sql.Expression interface.
    58  // The function returns always true
    59  func (db *Database) IsNullable() bool {
    60  	return true
    61  }
    62  
    63  func (db *Database) String() string {
    64  	return fmt.Sprintf("%s()", db.FunctionName())
    65  }
    66  
    67  // WithChildren implements the Expression interface.
    68  func (db *Database) WithChildren(children ...sql.Expression) (sql.Expression, error) {
    69  	if len(children) != 0 {
    70  		return nil, sql.ErrInvalidChildrenNumber.New(db, len(children), 0)
    71  	}
    72  	return NewDatabase(), nil
    73  }
    74  
    75  // Resolved implements the sql.Expression interface.
    76  func (db *Database) Resolved() bool {
    77  	return true
    78  }
    79  
    80  // Children implements the sql.Expression interface.
    81  func (db *Database) Children() []sql.Expression { return nil }
    82  
    83  // Eval implements the sql.Expression interface.
    84  func (db *Database) Eval(ctx *sql.Context, row sql.Row) (interface{}, error) {
    85  	if ctx.GetCurrentDatabase() == "" {
    86  		return nil, nil
    87  	}
    88  	return ctx.GetCurrentDatabase(), nil
    89  }