func makeFKDeleteHelper( txn *client.Txn, table sqlbase.TableDescriptor, otherTables tableLookupsByID, colMap map[sqlbase.ColumnID]int, ) (fkDeleteHelper, error) { var fks fkDeleteHelper for _, idx := range table.AllNonDropIndexes() { for _, ref := range idx.ReferencedBy { if otherTables[ref.Table].isAdding { // We can assume that a table being added but not yet public is empty, // and thus does not need to be checked for FK violations. continue } fk, err := makeBaseFKHelper(txn, otherTables, idx, ref, colMap) if err == errSkipUnsedFK { continue } if err != nil { return fks, err } if fks == nil { fks = make(fkDeleteHelper) } fks[idx.ID] = append(fks[idx.ID], fk) } } return fks, nil }
func makeFKInsertHelper( txn *client.Txn, table *sqlbase.TableDescriptor, otherTables TablesByID, colMap map[sqlbase.ColumnID]int, ) (fkInsertHelper, error) { var fks fkInsertHelper for _, idx := range table.AllNonDropIndexes() { if idx.ForeignKey != nil { fk, err := makeBaseFKHelper(txn, otherTables, idx, idx.ForeignKey, colMap) if err != nil { return fks, err } if fks == nil { fks = make(fkInsertHelper) } fks[idx.ID] = append(fks[idx.ID], fk) } } return fks, nil }
func makeFKDeleteHelper( txn *client.Txn, table *sqlbase.TableDescriptor, otherTables TablesByID, colMap map[sqlbase.ColumnID]int, ) (fkDeleteHelper, error) { var fks fkDeleteHelper for _, idx := range table.AllNonDropIndexes() { for _, ref := range idx.ReferencedBy { fk, err := makeBaseFKHelper(txn, otherTables, idx, ref, colMap) if err != nil { return fks, err } if fks == nil { fks = make(fkDeleteHelper) } fks[idx.ID] = append(fks[idx.ID], fk) } } return fks, nil }
// tablesNeededForFKs calculates the IDs of the additional TableDescriptors that // will be needed for FK checking delete and/or insert operations on `table`. // // NB: the returned map's values are *not* set -- higher level calling code, eg // in planner, should fill the map's values by acquiring leases. This function // is essentially just returning a slice of IDs, but the empty map can be filled // in place and reused, avoiding a second allocation. func tablesNeededForFKs(table sqlbase.TableDescriptor, usage FKCheck) tableLookupsByID { var ret tableLookupsByID for _, idx := range table.AllNonDropIndexes() { if usage != CheckDeletes && idx.ForeignKey.IsSet() { if ret == nil { ret = make(tableLookupsByID) } ret[idx.ForeignKey.Table] = tableLookup{} } if usage != CheckInserts { for _, idx := range table.AllNonDropIndexes() { for _, ref := range idx.ReferencedBy { if ret == nil { ret = make(tableLookupsByID) } ret[ref.Table] = tableLookup{} } } } } return ret }