GuessConstraintInterfaceAndTable guess statement's constraint and it's table based on name nolint:cyclop
(stmt *gorm.Statement, name string)
| 703 | // GuessConstraintInterfaceAndTable guess statement's constraint and it's table based on name |
| 704 | // nolint:cyclop |
| 705 | func (m Migrator) GuessConstraintInterfaceAndTable(stmt *gorm.Statement, name string) (_ schema.ConstraintInterface, table string) { |
| 706 | if stmt.Schema == nil { |
| 707 | return nil, stmt.Table |
| 708 | } |
| 709 | |
| 710 | checkConstraints := stmt.Schema.ParseCheckConstraints() |
| 711 | if chk, ok := checkConstraints[name]; ok { |
| 712 | return &chk, stmt.Table |
| 713 | } |
| 714 | |
| 715 | uniqueConstraints := stmt.Schema.ParseUniqueConstraints() |
| 716 | if uni, ok := uniqueConstraints[name]; ok { |
| 717 | return &uni, stmt.Table |
| 718 | } |
| 719 | |
| 720 | getTable := func(rel *schema.Relationship) string { |
| 721 | switch rel.Type { |
| 722 | case schema.HasOne, schema.HasMany: |
| 723 | return rel.FieldSchema.Table |
| 724 | case schema.Many2Many: |
| 725 | return rel.JoinTable.Table |
| 726 | } |
| 727 | return stmt.Table |
| 728 | } |
| 729 | |
| 730 | for _, rel := range stmt.Schema.Relationships.Relations { |
| 731 | if constraint := rel.ParseConstraint(); constraint != nil && constraint.Name == name { |
| 732 | return constraint, getTable(rel) |
| 733 | } |
| 734 | } |
| 735 | |
| 736 | if field := stmt.Schema.LookUpField(name); field != nil { |
| 737 | for k := range checkConstraints { |
| 738 | if checkConstraints[k].Field == field { |
| 739 | v := checkConstraints[k] |
| 740 | return &v, stmt.Table |
| 741 | } |
| 742 | } |
| 743 | |
| 744 | for k := range uniqueConstraints { |
| 745 | if uniqueConstraints[k].Field == field { |
| 746 | v := uniqueConstraints[k] |
| 747 | return &v, stmt.Table |
| 748 | } |
| 749 | } |
| 750 | |
| 751 | for _, rel := range stmt.Schema.Relationships.Relations { |
| 752 | if constraint := rel.ParseConstraint(); constraint != nil && rel.Field == field { |
| 753 | return constraint, getTable(rel) |
| 754 | } |
| 755 | } |
| 756 | } |
| 757 | |
| 758 | return nil, stmt.Schema.Table |
| 759 | } |
| 760 | |
| 761 | // CreateConstraint create constraint |
| 762 | func (m Migrator) CreateConstraint(value interface{}, name string) error { |
no test coverage detected