github.com/dolthub/go-mysql-server@v0.18.0/memory/foreign_key_collection.go (about) 1 // Copyright 2022 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 memory 16 17 import ( 18 "strings" 19 20 "github.com/dolthub/go-mysql-server/sql" 21 ) 22 23 // ForeignKeyCollection is a shareable container for a collection of foreign keys. 24 type ForeignKeyCollection struct { 25 fks []sql.ForeignKeyConstraint 26 } 27 28 // newForeignKeyCollection returns a new ForeignKeyCollection. 29 func newForeignKeyCollection() *ForeignKeyCollection { 30 return &ForeignKeyCollection{} 31 } 32 33 // AddFK adds the given foreign key to the internal slice. 34 func (fkc *ForeignKeyCollection) AddFK(fk sql.ForeignKeyConstraint) { 35 if fkc == nil { 36 return 37 } 38 fkc.fks = append(fkc.fks, fk) 39 } 40 41 // DropFK removes the given foreign key from the internal slice. Returns true if the foreign key was found. 42 func (fkc *ForeignKeyCollection) DropFK(fkName string) bool { 43 if fkc == nil { 44 return false 45 } 46 fkLowerName := strings.ToLower(fkName) 47 for i, existingFk := range fkc.fks { 48 if fkLowerName == strings.ToLower(existingFk.Name) { 49 fkc.fks = append(fkc.fks[:i], fkc.fks[i+1:]...) 50 return true 51 } 52 } 53 return false 54 } 55 56 // SetResolved sets the given foreign key as being resolved. 57 func (fkc *ForeignKeyCollection) SetResolved(fkName string) bool { 58 if fkc == nil { 59 return false 60 } 61 fkLowerName := strings.ToLower(fkName) 62 for i, existingFk := range fkc.fks { 63 if fkLowerName == strings.ToLower(existingFk.Name) { 64 fkc.fks[i].IsResolved = true 65 return true 66 } 67 } 68 return false 69 } 70 71 // Keys returns all of the foreign keys. 72 func (fkc *ForeignKeyCollection) Keys() []sql.ForeignKeyConstraint { 73 if fkc == nil { 74 return nil 75 } 76 return fkc.fks 77 }