package ui import "git.warky.dev/wdevs/relspecgo/pkg/models" // Table data operations - business logic for table management // CreateTable creates a new table and adds it to a schema func (se *SchemaEditor) CreateTable(schemaIndex int, name, description string) *models.Table { if schemaIndex < 0 || schemaIndex >= len(se.db.Schemas) { return nil } schema := se.db.Schemas[schemaIndex] newTable := &models.Table{ Name: name, Schema: schema.Name, Description: description, Columns: make(map[string]*models.Column), Constraints: make(map[string]*models.Constraint), Indexes: make(map[string]*models.Index), } schema.UpdateDate() schema.Tables = append(schema.Tables, newTable) return newTable } // UpdateTable updates an existing table's properties func (se *SchemaEditor) UpdateTable(schemaIndex, tableIndex int, name, description string) { if schemaIndex < 0 || schemaIndex >= len(se.db.Schemas) { return } schema := se.db.Schemas[schemaIndex] if tableIndex < 0 || tableIndex >= len(schema.Tables) { return } schema.UpdateDate() table := schema.Tables[tableIndex] table.Name = name table.Description = description table.UpdateDate() } // DeleteTable removes a table from a schema func (se *SchemaEditor) DeleteTable(schemaIndex, tableIndex int) 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 } schema.UpdateDate() schema.Tables = append(schema.Tables[:tableIndex], schema.Tables[tableIndex+1:]...) return true } // GetTable returns a table by schema and table index func (se *SchemaEditor) GetTable(schemaIndex, tableIndex int) *models.Table { if schemaIndex < 0 || schemaIndex >= len(se.db.Schemas) { return nil } schema := se.db.Schemas[schemaIndex] if tableIndex < 0 || tableIndex >= len(schema.Tables) { return nil } return schema.Tables[tableIndex] } // GetAllTables returns all tables across all schemas func (se *SchemaEditor) GetAllTables() []*models.Table { var tables []*models.Table for _, schema := range se.db.Schemas { tables = append(tables, schema.Tables...) } return tables } // GetTablesInSchema returns all tables in a specific schema func (se *SchemaEditor) GetTablesInSchema(schemaIndex int) []*models.Table { if schemaIndex < 0 || schemaIndex >= len(se.db.Schemas) { return nil } return se.db.Schemas[schemaIndex].Tables }