github.com/hasnat/dolt/go@v0.0.0-20210628190320-9eb5d843fbb7/libraries/doltcore/sqle/dfunctions/dolt_add.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 23 "github.com/dolthub/dolt/go/cmd/dolt/cli" 24 "github.com/dolthub/dolt/go/libraries/doltcore/env/actions" 25 "github.com/dolthub/dolt/go/libraries/doltcore/sqle" 26 ) 27 28 const DoltAddFuncName = "dolt_add" 29 30 type DoltAddFunc struct { 31 children []sql.Expression 32 } 33 34 func (d DoltAddFunc) Eval(ctx *sql.Context, row sql.Row) (interface{}, error) { 35 dbName := ctx.GetCurrentDatabase() 36 37 if len(dbName) == 0 { 38 return 1, fmt.Errorf("Empty database name.") 39 } 40 41 dSess := sqle.DSessFromSess(ctx.Session) 42 dbData, ok := dSess.GetDbData(dbName) 43 44 if !ok { 45 return 1, fmt.Errorf("Could not load database %s", dbName) 46 } 47 48 ap := cli.CreateAddArgParser() 49 args, err := getDoltArgs(ctx, row, d.Children()) 50 51 if err != nil { 52 return 1, err 53 } 54 55 apr, err := ap.Parse(args) 56 if err != nil { 57 return 1, err 58 } 59 60 allFlag := apr.Contains(cli.AllFlag) 61 62 if apr.NArg() == 0 && !allFlag { 63 return 1, fmt.Errorf("Nothing specified, nothing added. Maybe you wanted to say 'dolt add .'?") 64 } else if allFlag || apr.NArg() == 1 && apr.Arg(0) == "." { 65 err = actions.StageAllTables(ctx, dbData) 66 if err != nil { 67 return 1, err 68 } 69 70 hashString := dbData.Rsr.StagedHash().String() 71 if err != nil { 72 return 1, err 73 } 74 75 // Sets @@_working to staged. 76 err = setSessionRootExplicit(ctx, hashString, sqle.WorkingKeySuffix) 77 } else { 78 err = actions.StageTables(ctx, dbData, apr.Args()) 79 } 80 81 if err != nil { 82 return 1, err 83 } 84 85 return 0, nil 86 } 87 88 func (d DoltAddFunc) Resolved() bool { 89 for _, child := range d.Children() { 90 if !child.Resolved() { 91 return false 92 } 93 } 94 return true 95 } 96 97 func (d DoltAddFunc) String() string { 98 childrenStrings := make([]string, len(d.children)) 99 100 for i, child := range d.children { 101 childrenStrings[i] = child.String() 102 } 103 104 return fmt.Sprintf("DOLT_ADD(%s)", strings.Join(childrenStrings, ",")) 105 } 106 107 func (d DoltAddFunc) Type() sql.Type { 108 return sql.Int8 109 } 110 111 func (d DoltAddFunc) IsNullable() bool { 112 for _, child := range d.Children() { 113 if child.IsNullable() { 114 return true 115 } 116 } 117 return false 118 } 119 120 func (d DoltAddFunc) Children() []sql.Expression { 121 return d.children 122 } 123 124 func (d DoltAddFunc) WithChildren(ctx *sql.Context, children ...sql.Expression) (sql.Expression, error) { 125 return NewDoltAddFunc(ctx, children...) 126 } 127 128 // NewDoltAddFunc creates a new DoltAddFunc expression whose children represents the args passed in DOLT_ADD. 129 func NewDoltAddFunc(ctx *sql.Context, args ...sql.Expression) (sql.Expression, error) { 130 return &DoltAddFunc{children: args}, nil 131 }