github.com/hasnat/dolt/go@v0.0.0-20210628190320-9eb5d843fbb7/libraries/doltcore/sqle/dfunctions/reset.go (about) 1 // Copyright 2020 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 dfunctions 16 17 import ( 18 "fmt" 19 "strings" 20 21 "github.com/dolthub/go-mysql-server/sql" 22 "github.com/dolthub/go-mysql-server/sql/expression" 23 24 "github.com/dolthub/dolt/go/libraries/doltcore/sqle" 25 "github.com/dolthub/dolt/go/store/hash" 26 ) 27 28 const ( 29 resetFuncName = "reset" 30 31 resetHardParameter = "hard" 32 ) 33 34 type ResetFunc struct { 35 expression.UnaryExpression 36 } 37 38 // NewDoltResetFunc creates a new ResetFunc expression. 39 func NewResetFunc(ctx *sql.Context, e sql.Expression) sql.Expression { 40 return ResetFunc{expression.UnaryExpression{Child: e}} 41 } 42 43 // Eval implements the Expression interface. 44 func (rf ResetFunc) Eval(ctx *sql.Context, row sql.Row) (interface{}, error) { 45 val, err := rf.Child.Eval(ctx, row) 46 if err != nil { 47 return nil, err 48 } 49 arg, ok := val.(string) 50 if !ok { 51 return nil, sql.ErrInvalidChildType.New(rf.Child.String(), rf.Child.Type(), sql.Text.Type()) 52 } 53 54 dbName := ctx.GetCurrentDatabase() 55 dSess := sqle.DSessFromSess(ctx.Session) 56 57 var h hash.Hash 58 if strings.ToLower(arg) != resetHardParameter { 59 return nil, fmt.Errorf("invalid arugument to %s(): %s", resetFuncName, arg) 60 } 61 62 parent, _, err := dSess.GetHeadCommit(ctx, dbName) 63 if err != nil { 64 return nil, err 65 } 66 67 h, err = parent.HashOf() 68 if err != nil { 69 return nil, err 70 } 71 72 return h.String(), nil 73 } 74 75 // Resolved implements the Expression interface. 76 func (rf ResetFunc) Resolved() bool { 77 return rf.Child.Resolved() 78 } 79 80 // String implements the Stringer interface. 81 func (rf ResetFunc) String() string { 82 return fmt.Sprintf("RESET_HARD(%s)", rf.Child.String()) 83 } 84 85 // IsNullable implements the Expression interface. 86 func (rf ResetFunc) IsNullable() bool { 87 return false 88 } 89 90 // Children implements the Expression interface. 91 func (rf ResetFunc) Children() []sql.Expression { 92 return []sql.Expression{rf.Child} 93 } 94 95 // WithChildren implements the Expression interface. 96 func (rf ResetFunc) WithChildren(ctx *sql.Context, children ...sql.Expression) (sql.Expression, error) { 97 if len(children) != 1 { 98 return nil, sql.ErrInvalidChildrenNumber.New(rf, len(children), 1) 99 } 100 return NewResetFunc(ctx, children[0]), nil 101 } 102 103 // Type implements the Expression interface. 104 func (rf ResetFunc) Type() sql.Type { 105 return sql.Text 106 }