@@ -40,9 +40,7 @@ func applyForeignKeys(ctx *sql.Context, a *Analyzer, n sql.Node, scope *plan.Sco
4040// and caching of table editors.
4141func applyForeignKeysToNodes (ctx * sql.Context , a * Analyzer , n sql.Node , cache * foreignKeyCache ) (sql.Node , transform.TreeIdentity , error ) {
4242 var err error
43- fkChain := foreignKeyChain {
44- fkUpdate : make (map [foreignKeyTableName ]sql.ForeignKeyEditor ),
45- }
43+ fkChain := newForeignKeyChain ()
4644
4745 switch n := n .(type ) {
4846 case * plan.CreateTable :
@@ -209,7 +207,11 @@ func getForeignKeyEditor(ctx *sql.Context, a *Analyzer, tbl sql.ForeignKeyTable,
209207 if err != nil {
210208 return nil , err
211209 }
212- return getForeignKeyRefActions (ctx , a , tbl , cache , fkChain , fkEditor , checkRows )
210+ fkEditor , err = getForeignKeyRefActions (ctx , a , tbl , cache , fkChain , fkEditor , checkRows )
211+ if err != nil {
212+ return nil , err
213+ }
214+ return fkEditor , err
213215}
214216
215217// getForeignKeyReferences returns an editor containing only the references for the given table.
@@ -238,7 +240,7 @@ func getForeignKeyReferences(ctx *sql.Context, a *Analyzer, tbl sql.ForeignKeyTa
238240 if err != nil {
239241 return nil , err
240242 }
241- fkChain = fkChain .AddTable (fks [0 ].ParentDatabase , fks [0 ].ParentTable ).AddTableUpdater (fks [0 ].ParentDatabase , fks [0 ].ParentTable , updater )
243+ fkChain = fkChain .AddTable (fks [0 ].Database , fks [0 ].Table ).AddTableUpdater (fks [0 ].Database , fks [0 ].Table , updater )
242244
243245 tblSch := tbl .Schema ()
244246 fkEditor := & plan.ForeignKeyEditor {
@@ -426,6 +428,13 @@ type foreignKeyTableName struct {
426428 tblName string
427429}
428430
431+ func newForeignKeyTableName (dbName , tblName string ) foreignKeyTableName {
432+ return foreignKeyTableName {
433+ dbName : strings .ToLower (dbName ),
434+ tblName : strings .ToLower (tblName ),
435+ }
436+ }
437+
429438// foreignKeyTableUpdater is a foreign key table along with its updater.
430439type foreignKeyTableUpdater struct {
431440 tbl sql.ForeignKeyTable
@@ -539,9 +548,17 @@ func (cache *foreignKeyCache) GetEditor(fkEditor *plan.ForeignKeyEditor, dbName
539548// updaters that are not a part of this chain. In addition, any updaters that cannot be modified (such as those
540549// belonging to strictly RESTRICT referential actions) will not appear in the chain.
541550type foreignKeyChain struct {
542- fkNames map [string ]struct {}
543- fkTables map [foreignKeyTableName ]struct {}
544- fkUpdate map [foreignKeyTableName ]sql.ForeignKeyEditor
551+ fkNames map [string ]struct {}
552+ fkTables map [foreignKeyTableName ]struct {}
553+ fkUpdaters map [foreignKeyTableName ]sql.ForeignKeyEditor
554+ }
555+
556+ func newForeignKeyChain () foreignKeyChain {
557+ return foreignKeyChain {
558+ fkNames : make (map [string ]struct {}),
559+ fkTables : make (map [foreignKeyTableName ]struct {}),
560+ fkUpdaters : make (map [foreignKeyTableName ]sql.ForeignKeyEditor ),
561+ }
545562}
546563
547564// AddTable returns a new chain with the added table.
@@ -554,23 +571,17 @@ func (chain foreignKeyChain) AddTable(dbName string, tblName string) foreignKeyC
554571 for fkTable := range chain .fkTables {
555572 newFkTables [fkTable ] = struct {}{}
556573 }
557- newFkTables [foreignKeyTableName {
558- dbName : strings .ToLower (dbName ),
559- tblName : strings .ToLower (tblName ),
560- }] = struct {}{}
574+ newFkTables [newForeignKeyTableName (dbName , tblName )] = struct {}{}
561575 return foreignKeyChain {
562- fkNames : newFkNames ,
563- fkTables : newFkTables ,
564- fkUpdate : chain .fkUpdate ,
576+ fkNames : newFkNames ,
577+ fkTables : newFkTables ,
578+ fkUpdaters : chain .fkUpdaters ,
565579 }
566580}
567581
568582// AddTableUpdater returns a new chain with the added foreign key updater.
569583func (chain foreignKeyChain ) AddTableUpdater (dbName string , tblName string , fkUpdater sql.ForeignKeyEditor ) foreignKeyChain {
570- chain .fkUpdate [foreignKeyTableName {
571- dbName : strings .ToLower (dbName ),
572- tblName : strings .ToLower (tblName ),
573- }] = fkUpdater
584+ chain .fkUpdaters [newForeignKeyTableName (dbName , tblName )] = fkUpdater
574585 return chain
575586}
576587
@@ -586,35 +597,28 @@ func (chain foreignKeyChain) AddForeignKey(fkName string) foreignKeyChain {
586597 }
587598 newFkNames [strings .ToLower (fkName )] = struct {}{}
588599 return foreignKeyChain {
589- fkNames : newFkNames ,
590- fkTables : newFkTables ,
591- fkUpdate : chain .fkUpdate ,
600+ fkNames : newFkNames ,
601+ fkTables : newFkTables ,
602+ fkUpdaters : chain .fkUpdaters ,
592603 }
593604}
594605
595606// HasTable returns whether the chain contains the given table. Case-insensitive.
596607func (chain foreignKeyChain ) HasTable (dbName string , tblName string ) bool {
597- if _ , ok := chain .fkTables [foreignKeyTableName {
598- dbName : strings .ToLower (dbName ),
599- tblName : strings .ToLower (tblName ),
600- }]; ok {
601- return true
602- }
603- return false
608+ _ , ok := chain .fkTables [newForeignKeyTableName (dbName , tblName )]
609+ return ok
604610}
605611
606612// HasForeignKey returns whether the chain contains the given foreign key. Case-insensitive.
607613func (chain foreignKeyChain ) HasForeignKey (fkName string ) bool {
608- if _ , ok := chain .fkNames [strings .ToLower (fkName )]; ok {
609- return true
610- }
611- return false
614+ _ , ok := chain .fkNames [strings .ToLower (fkName )]
615+ return ok
612616}
613617
614618// GetUpdaters returns all foreign key updaters that have been added to the chain.
615619func (chain foreignKeyChain ) GetUpdaters () []sql.ForeignKeyEditor {
616- updaters := make ([]sql.ForeignKeyEditor , 0 , len (chain .fkUpdate ))
617- for _ , updater := range chain .fkUpdate {
620+ updaters := make ([]sql.ForeignKeyEditor , 0 , len (chain .fkUpdaters ))
621+ for _ , updater := range chain .fkUpdaters {
618622 updaters = append (updaters , updater )
619623 }
620624 return updaters
0 commit comments