package ui import "git.warky.dev/wdevs/relspecgo/pkg/models" // Relationship data operations - business logic for relationship management // CreateRelationship creates a new relationship and adds it to a table func (se *SchemaEditor) CreateRelationship(schemaIndex, tableIndex int, rel *models.Relationship) *models.Relationship { if schemaIndex < 0 || schemaIndex >= len(se.db.Schemas) { return nil } schema := se.db.Schemas[schemaIndex] if tableIndex < 0 || tableIndex >= len(schema.Tables) { return nil } table := schema.Tables[tableIndex] if table.Relationships == nil { table.Relationships = make(map[string]*models.Relationship) } table.Relationships[rel.Name] = rel table.UpdateDate() return rel } // UpdateRelationship updates an existing relationship func (se *SchemaEditor) UpdateRelationship(schemaIndex, tableIndex int, oldName string, rel *models.Relationship) bool { if schemaIndex < 0 || schemaIndex >= len(se.db.Schemas) { return false } schema := se.db.Schemas[schemaIndex] if tableIndex < 0 || tableIndex >= len(schema.Tables) { return false } table := schema.Tables[tableIndex] if table.Relationships == nil { return false } // Delete old entry if name changed if oldName != rel.Name { delete(table.Relationships, oldName) } table.Relationships[rel.Name] = rel table.UpdateDate() return true } // DeleteRelationship removes a relationship from a table func (se *SchemaEditor) DeleteRelationship(schemaIndex, tableIndex int, relName string) bool { if schemaIndex < 0 || schemaIndex >= len(se.db.Schemas) { return false } schema := se.db.Schemas[schemaIndex] if tableIndex < 0 || tableIndex >= len(schema.Tables) { return false } table := schema.Tables[tableIndex] if table.Relationships == nil { return false } delete(table.Relationships, relName) table.UpdateDate() return true } // GetRelationship returns a relationship by name func (se *SchemaEditor) GetRelationship(schemaIndex, tableIndex int, relName string) *models.Relationship { if schemaIndex < 0 || schemaIndex >= len(se.db.Schemas) { return nil } schema := se.db.Schemas[schemaIndex] if tableIndex < 0 || tableIndex >= len(schema.Tables) { return nil } table := schema.Tables[tableIndex] if table.Relationships == nil { return nil } return table.Relationships[relName] } // GetRelationshipNames returns all relationship names for a table func (se *SchemaEditor) GetRelationshipNames(schemaIndex, tableIndex int) []string { if schemaIndex < 0 || schemaIndex >= len(se.db.Schemas) { return nil } schema := se.db.Schemas[schemaIndex] if tableIndex < 0 || tableIndex >= len(schema.Tables) { return nil } table := schema.Tables[tableIndex] if table.Relationships == nil { return nil } names := make([]string, 0, len(table.Relationships)) for name := range table.Relationships { names = append(names, name) } return names }