github.com/hasnat/dolt/go@v0.0.0-20210628190320-9eb5d843fbb7/libraries/doltcore/sqle/sqlutil/schema.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 sqlutil 16 17 import ( 18 "context" 19 20 sqle "github.com/dolthub/go-mysql-server" 21 "github.com/dolthub/go-mysql-server/sql" 22 "github.com/dolthub/go-mysql-server/sql/parse" 23 "github.com/dolthub/vitess/go/vt/sqlparser" 24 25 "github.com/dolthub/dolt/go/libraries/doltcore/doltdb" 26 "github.com/dolthub/dolt/go/libraries/doltcore/row" 27 "github.com/dolthub/dolt/go/libraries/doltcore/schema" 28 "github.com/dolthub/dolt/go/store/types" 29 ) 30 31 // ApplyDefaults applies the default values to the given indices, returning the resulting row. 32 func ApplyDefaults(ctx context.Context, vrw types.ValueReadWriter, doltSchema schema.Schema, sqlSchema sql.Schema, indicesOfColumns []int, dRow row.Row) (row.Row, error) { 33 if len(indicesOfColumns) == 0 { 34 return dRow, nil 35 } 36 sqlCtx, ok := ctx.(*sql.Context) 37 if !ok { 38 sqlCtx = sql.NewContext(ctx) 39 } 40 doltCols := doltSchema.GetAllCols() 41 oldSqlRow := make(sql.Row, len(sqlSchema)) 42 for i, tag := range doltCols.Tags { 43 val, ok := dRow.GetColVal(tag) 44 if ok { 45 var err error 46 oldSqlRow[i], err = doltCols.TagToCol[tag].TypeInfo.ConvertNomsValueToValue(val) 47 if err != nil { 48 return nil, err 49 } 50 } else { 51 oldSqlRow[i] = nil 52 } 53 } 54 newSqlRow, err := sqle.ApplyDefaults(sqlCtx, sqlSchema, indicesOfColumns, oldSqlRow) 55 if err != nil { 56 return nil, err 57 } 58 newRow := make(row.TaggedValues) 59 for i, tag := range doltCols.Tags { 60 if newSqlRow[i] == nil { 61 continue 62 } 63 val, err := doltCols.TagToCol[tag].TypeInfo.ConvertValueToNomsValue(ctx, vrw, newSqlRow[i]) 64 if err != nil { 65 return nil, err 66 } 67 newRow[tag] = val 68 } 69 return row.New(dRow.Format(), doltSchema, newRow) 70 } 71 72 // ParseCreateTableStatement will parse a CREATE TABLE ddl statement and use it to create a Dolt Schema. A RootValue 73 // is used to generate unique tags for the Schema 74 func ParseCreateTableStatement(ctx context.Context, root *doltdb.RootValue, query string) (string, schema.Schema, error) { 75 // todo: verify create table statement 76 ddl, err := sqlparser.ParseStrictDDL(query) 77 78 if err != nil { 79 return "", nil, err 80 } 81 82 ts := ddl.(*sqlparser.DDL).TableSpec 83 s, err := parse.TableSpecToSchema(sql.NewContext(ctx), ts) 84 85 if err != nil { 86 return "", nil, err 87 } 88 89 tn := ddl.(*sqlparser.DDL).Table 90 buf := sqlparser.NewTrackedBuffer(nil) 91 tn.Format(buf) 92 tableName := buf.String() 93 sch, err := ToDoltSchema(ctx, root, tableName, s, nil) 94 95 if err != nil { 96 return "", nil, err 97 } 98 99 return tableName, sch, err 100 }