github.com/dolthub/go-mysql-server@v0.18.0/enginetest/mysqlshim/table_editor.go (about) 1 // Copyright 2021 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 mysqlshim 16 17 import ( 18 "fmt" 19 "strconv" 20 "strings" 21 "time" 22 23 "github.com/dolthub/go-mysql-server/sql" 24 ) 25 26 // tableEditor is used as a sql.TableEditor. 27 type tableEditor struct { 28 table Table 29 sch sql.Schema 30 } 31 32 func (t *tableEditor) IndexedAccess(lookup sql.IndexLookup) sql.IndexedTable { 33 //TODO implement me 34 panic("implement me") 35 } 36 37 func (t *tableEditor) PreciseMatch() bool { 38 return true 39 } 40 41 func (t *tableEditor) GetIndexes(ctx *sql.Context) ([]sql.Index, error) { 42 //TODO implement me 43 panic("implement me") 44 } 45 46 var _ sql.TableEditor = (*tableEditor)(nil) 47 var _ sql.ForeignKeyEditor = (*tableEditor)(nil) 48 49 // StatementBegin implements the interface sql.TableEditor. 50 func (t *tableEditor) StatementBegin(ctx *sql.Context) { 51 err := t.table.db.shim.Exec(t.table.db.name, "BEGIN;") 52 if err != nil { 53 panic(err) 54 } 55 } 56 57 // DiscardChanges implements the interface sql.TableEditor. 58 func (t *tableEditor) DiscardChanges(ctx *sql.Context, errorEncountered error) error { 59 return t.table.db.shim.Exec(t.table.db.name, "ROLLBACK;") 60 } 61 62 // StatementComplete implements the interface sql.TableEditor. 63 func (t *tableEditor) StatementComplete(ctx *sql.Context) error { 64 return t.table.db.shim.Exec(t.table.db.name, "COMMIT;") 65 } 66 67 // Insert implements the interface sql.RowInserter. 68 func (t *tableEditor) Insert(ctx *sql.Context, row sql.Row) error { 69 sb := strings.Builder{} 70 for i, val := range row { 71 if i != 0 { 72 sb.WriteByte(',') 73 } 74 sb.WriteString(t.rowValToString(val)) 75 } 76 return t.table.db.shim.Exec(t.table.db.name, fmt.Sprintf("INSERT INTO `%s` VALUES (%s);", t.table.name, sb.String())) 77 } 78 79 // Update implements the interface sql.RowUpdater. 80 func (t *tableEditor) Update(ctx *sql.Context, old sql.Row, new sql.Row) error { 81 err := t.Delete(ctx, old) 82 if err != nil { 83 return err 84 } 85 return t.Insert(ctx, new) 86 } 87 88 // Delete implements the interface sql.RowDeleter. 89 func (t *tableEditor) Delete(ctx *sql.Context, row sql.Row) error { 90 if len(row) != len(t.sch) { 91 return fmt.Errorf("expected `%d` values but got `%d` for DELETE", len(t.sch), len(row)) 92 } 93 sb := strings.Builder{} 94 for i, val := range row { 95 if i != 0 { 96 sb.WriteString(" AND") 97 } 98 sb.WriteString(fmt.Sprintf(" `%s` = %s", t.sch[i].Name, t.rowValToString(val))) 99 } 100 return t.table.db.shim.Exec(t.table.db.name, fmt.Sprintf("DELETE FROM `%s` WHERE%s;", t.table.name, sb.String())) 101 } 102 103 // Close implements the interface sql.TableEditor. 104 func (t *tableEditor) Close(ctx *sql.Context) error { 105 return nil 106 } 107 108 // rowValToString converts the given value from a sql.Row into a string representation. 109 func (t *tableEditor) rowValToString(val interface{}) string { 110 switch val := val.(type) { 111 case bool: 112 if val { 113 return "true" 114 } 115 return "false" 116 case int: 117 return strconv.FormatInt(int64(val), 10) 118 case int8: 119 return strconv.FormatInt(int64(val), 10) 120 case int16: 121 return strconv.FormatInt(int64(val), 10) 122 case int32: 123 return strconv.FormatInt(int64(val), 10) 124 case int64: 125 return strconv.FormatInt(val, 10) 126 case uint: 127 return strconv.FormatUint(uint64(val), 10) 128 case uint8: 129 return strconv.FormatUint(uint64(val), 10) 130 case uint16: 131 return strconv.FormatUint(uint64(val), 10) 132 case uint32: 133 return strconv.FormatUint(uint64(val), 10) 134 case uint64: 135 return strconv.FormatUint(val, 10) 136 case float32: 137 return strconv.FormatFloat(float64(val), 'g', -1, 32) 138 case float64: 139 return strconv.FormatFloat(val, 'g', -1, 64) 140 case string: 141 return "'" + strings.ReplaceAll(val, "'", `\'`) + "'" 142 case []byte: 143 return "'" + strings.ReplaceAll(string(val), "'", `\'`) + "'" 144 case time.Time: 145 return "'" + val.Format("2006-01-02T15:04:05") + "'" 146 case nil: 147 return "NULL" 148 default: 149 panic(fmt.Errorf("unknown type: %T", val)) 150 } 151 }