github.com/hasnat/dolt/go@v0.0.0-20210628190320-9eb5d843fbb7/libraries/doltcore/diff/sql_diff.go (about) 1 // Copyright 2019 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 diff 16 17 import ( 18 "errors" 19 "io" 20 21 "github.com/dolthub/dolt/go/libraries/doltcore/row" 22 "github.com/dolthub/dolt/go/libraries/doltcore/schema" 23 "github.com/dolthub/dolt/go/libraries/doltcore/sqle/sqlfmt" 24 "github.com/dolthub/dolt/go/libraries/doltcore/table/pipeline" 25 "github.com/dolthub/dolt/go/libraries/utils/iohelp" 26 "github.com/dolthub/dolt/go/store/types" 27 ) 28 29 type SQLDiffSink struct { 30 wr io.WriteCloser 31 sch schema.Schema 32 tableName string 33 } 34 35 // NewSQLDiffSink creates a SQLDiffSink for a diff pipeline. 36 func NewSQLDiffSink(wr io.WriteCloser, sch schema.Schema, tableName string) (*SQLDiffSink, error) { 37 return &SQLDiffSink{wr, sch, tableName}, nil 38 } 39 40 // GetSchema gets the schema that the SQLDiffSink was created with. 41 func (sds *SQLDiffSink) GetSchema() schema.Schema { 42 return sds.sch 43 } 44 45 // ProcRowWithProps satisfies pipeline.SinkFunc; it writes SQL diff statements to output. 46 func (sds *SQLDiffSink) ProcRowWithProps(r row.Row, props pipeline.ReadableMap) error { 47 48 taggedVals := make(row.TaggedValues) 49 allCols := sds.sch.GetAllCols() 50 colDiffs := make(map[string]DiffChType) 51 52 if prop, ok := props.Get(CollChangesProp); ok { 53 if convertedVal, convertedOK := prop.(map[string]DiffChType); convertedOK { 54 colDiffs = convertedVal 55 } 56 } 57 58 err := allCols.Iter(func(tag uint64, col schema.Column) (stop bool, err error) { 59 if val, ok := r.GetColVal(tag); ok { 60 taggedVals[tag] = val 61 } 62 return false, nil 63 }) 64 65 if err != nil { 66 return err 67 } 68 69 r, err = row.New(r.Format(), sds.sch, taggedVals) 70 71 if err != nil { 72 return err 73 } 74 75 taggedVals[diffColTag] = types.String(" ") 76 if prop, ok := props.Get(DiffTypeProp); ok { 77 if dt, convertedOK := prop.(DiffChType); convertedOK { 78 switch dt { 79 case DiffAdded: 80 stmt, err := sqlfmt.RowAsInsertStmt(r, sds.tableName, sds.sch) 81 82 if err != nil { 83 return err 84 } 85 86 return iohelp.WriteLine(sds.wr, stmt) 87 case DiffRemoved: 88 stmt, err := sqlfmt.RowAsDeleteStmt(r, sds.tableName, sds.sch) 89 90 if err != nil { 91 return err 92 } 93 94 return iohelp.WriteLine(sds.wr, stmt) 95 case DiffModifiedOld: 96 return nil 97 case DiffModifiedNew: 98 // TODO: minimize update statement to modified rows 99 stmt, err := sqlfmt.RowAsUpdateStmt(r, sds.tableName, sds.sch) 100 101 if err != nil { 102 return err 103 } 104 105 return iohelp.WriteLine(sds.wr, stmt) 106 } 107 // Treat the diff indicator string as a diff of the same type 108 colDiffs[diffColName] = dt 109 } 110 } 111 112 return err 113 } 114 115 // ProcRowWithProps satisfies pipeline.SinkFunc; it writes rows as SQL statements. 116 func (sds *SQLDiffSink) ProcRowForExport(r row.Row, _ pipeline.ReadableMap) error { 117 stmt, err := sqlfmt.RowAsInsertStmt(r, sds.tableName, sds.sch) 118 119 if err != nil { 120 return err 121 } 122 123 return iohelp.WriteLine(sds.wr, stmt) 124 } 125 126 // Close should release resources being held 127 func (sds *SQLDiffSink) Close() error { 128 if sds.wr != nil { 129 err := sds.wr.Close() 130 if err != nil { 131 return err 132 } 133 sds.wr = nil 134 return nil 135 } else { 136 return errors.New("Already closed.") 137 } 138 }