Add func and bats

This commit is contained in:
Max Hoffman
2021-03-09 20:38:00 -08:00
parent d718aa0364
commit 615a757d08
3 changed files with 134 additions and 0 deletions
+37
View File
@@ -0,0 +1,37 @@
#!/usr/bin/env bats
load $BATS_TEST_DIRNAME/helper/common.bash
setup() {
setup_common
dolt sql <<SQL
CREATE TABLE test (
pk int primary key
);
CREATE TABLE test2 (
pk int primary key
);
INSERT INTO test VALUES (0),(1),(2);
SQL
}
teardown() {
assert_feature_version
teardown_common
}
@test "active_branch() func" {
run dolt sql -q 'select active_branch()' -r csv
[ $status -eq 0 ]
[[ "$output" =~ "active_branch()" ]] || false
[[ "$output" =~ "master" ]] || false
}
@test "active_branch() func on feature branch" {
run dolt branch tmp_br
run dolt checkout tmp_br
run dolt sql -q 'select active_branch()' -r csv
[ $status -eq 0 ]
[[ "$output" =~ "active_branch()" ]] || false
[[ "$output" =~ "tmp_br" ]] || false
}
@@ -0,0 +1,96 @@
// Copyright 2021 Dolthub, Inc.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package dfunctions
import (
"fmt"
"github.com/dolthub/dolt/go/libraries/doltcore/ref"
"github.com/dolthub/go-mysql-server/sql"
"github.com/dolthub/dolt/go/libraries/doltcore/sqle"
)
const ActiveBranchFuncName = "active_branch"
type ActiveBranchFunc struct {
}
// NewCommitFunc creates a new CommitFunc expression.
func NewActiveBranchFunc() sql.Expression {
return &ActiveBranchFunc{}
}
// Eval implements the Expression interface.
func (cf *ActiveBranchFunc) Eval(ctx *sql.Context, row sql.Row) (interface{}, error) {
dbName := ctx.GetCurrentDatabase()
dSess := sqle.DSessFromSess(ctx.Session)
ddb, ok := dSess.GetDoltDB(dbName)
if !ok {
return nil, sql.ErrDatabaseNotFound.New(dbName)
}
rsr, ok := dSess.GetDoltDBRepoStateReader(dbName)
if !ok {
return nil, sql.ErrDatabaseNotFound.New(dbName)
}
currentBranch := rsr.CWBHeadRef()
branches, err := ddb.GetBranches(ctx)
if err != nil {
return nil, err
}
for _, br := range(branches) {
if ref.Equals(br, currentBranch) {
return br.GetPath(), nil
}
}
return nil, fmt.Errorf("active branch not found")
}
// String implements the Stringer interface.
func (cf *ActiveBranchFunc) String() string {
return fmt.Sprint("ACTIVE_BRANCH()")
}
// IsNullable implements the Expression interface.
func (cf *ActiveBranchFunc) IsNullable() bool {
return false
}
// Resolved implements the Expression interface.
func (*ActiveBranchFunc) Resolved() bool {
return true
}
func (cf *ActiveBranchFunc) Type() sql.Type {
return sql.Boolean
}
// Children implements the Expression interface.
func (*ActiveBranchFunc) Children() []sql.Expression {
return nil
}
// WithChildren implements the Expression interface.
func (v *ActiveBranchFunc) WithChildren(children ...sql.Expression) (sql.Expression, error) {
if len(children) != 0 {
return nil, sql.ErrInvalidChildrenNumber.New(v, len(children), 0)
}
return NewActiveBranchFunc(), nil
}
@@ -27,6 +27,7 @@ var DoltFunctions = []sql.Function{
sql.FunctionN{Name: DoltResetFuncName, Fn: NewDoltResetFunc},
sql.FunctionN{Name: DoltCheckoutFuncName, Fn: NewDoltCheckoutFunc},
sql.FunctionN{Name: DoltMergeFuncName, Fn: NewDoltMergeFunc},
sql.Function0{Name: ActiveBranchFuncName, Fn: NewActiveBranchFunc},
}
// These are the DoltFunctions that get exposed to Dolthub Api.