102 lines
4.2 KiB
Go
102 lines
4.2 KiB
Go
package models
|
|
|
|
// Summary/Compact Views
|
|
//
|
|
// This file provides lightweight summary structures with essential fields
|
|
// and aggregated counts for quick database schema overviews without loading
|
|
// full object graphs.
|
|
|
|
// DatabaseSummary provides a compact overview of a database with aggregated statistics.
|
|
type DatabaseSummary struct {
|
|
Name string `json:"name" yaml:"name" xml:"name"`
|
|
Description string `json:"description,omitempty" yaml:"description,omitempty" xml:"description,omitempty"`
|
|
DatabaseType DatabaseType `json:"database_type,omitempty" yaml:"database_type,omitempty" xml:"database_type,omitempty"`
|
|
DatabaseVersion string `json:"database_version,omitempty" yaml:"database_version,omitempty" xml:"database_version,omitempty"`
|
|
SchemaCount int `json:"schema_count" yaml:"schema_count" xml:"schema_count"`
|
|
TotalTables int `json:"total_tables" yaml:"total_tables" xml:"total_tables"`
|
|
TotalColumns int `json:"total_columns" yaml:"total_columns" xml:"total_columns"`
|
|
}
|
|
|
|
// ToSummary converts a Database to a DatabaseSummary with calculated counts.
|
|
func (d *Database) ToSummary() *DatabaseSummary {
|
|
summary := &DatabaseSummary{
|
|
Name: d.Name,
|
|
Description: d.Description,
|
|
DatabaseType: d.DatabaseType,
|
|
DatabaseVersion: d.DatabaseVersion,
|
|
SchemaCount: len(d.Schemas),
|
|
}
|
|
|
|
// Count total tables and columns
|
|
for _, schema := range d.Schemas {
|
|
summary.TotalTables += len(schema.Tables)
|
|
for _, table := range schema.Tables {
|
|
summary.TotalColumns += len(table.Columns)
|
|
}
|
|
}
|
|
|
|
return summary
|
|
}
|
|
|
|
// SchemaSummary provides a compact overview of a schema with aggregated statistics.
|
|
type SchemaSummary struct {
|
|
Name string `json:"name" yaml:"name" xml:"name"`
|
|
Description string `json:"description,omitempty" yaml:"description,omitempty" xml:"description,omitempty"`
|
|
Owner string `json:"owner" yaml:"owner" xml:"owner"`
|
|
TableCount int `json:"table_count" yaml:"table_count" xml:"table_count"`
|
|
ScriptCount int `json:"script_count" yaml:"script_count" xml:"script_count"`
|
|
TotalColumns int `json:"total_columns" yaml:"total_columns" xml:"total_columns"`
|
|
TotalConstraints int `json:"total_constraints" yaml:"total_constraints" xml:"total_constraints"`
|
|
}
|
|
|
|
// ToSummary converts a Schema to a SchemaSummary with calculated counts.
|
|
func (s *Schema) ToSummary() *SchemaSummary {
|
|
summary := &SchemaSummary{
|
|
Name: s.Name,
|
|
Description: s.Description,
|
|
Owner: s.Owner,
|
|
TableCount: len(s.Tables),
|
|
ScriptCount: len(s.Scripts),
|
|
}
|
|
|
|
// Count columns and constraints
|
|
for _, table := range s.Tables {
|
|
summary.TotalColumns += len(table.Columns)
|
|
summary.TotalConstraints += len(table.Constraints)
|
|
}
|
|
|
|
return summary
|
|
}
|
|
|
|
// TableSummary provides a compact overview of a table with aggregated statistics.
|
|
type TableSummary struct {
|
|
Name string `json:"name" yaml:"name" xml:"name"`
|
|
Schema string `json:"schema" yaml:"schema" xml:"schema"`
|
|
Description string `json:"description,omitempty" yaml:"description,omitempty" xml:"description,omitempty"`
|
|
ColumnCount int `json:"column_count" yaml:"column_count" xml:"column_count"`
|
|
ConstraintCount int `json:"constraint_count" yaml:"constraint_count" xml:"constraint_count"`
|
|
IndexCount int `json:"index_count" yaml:"index_count" xml:"index_count"`
|
|
RelationshipCount int `json:"relationship_count" yaml:"relationship_count" xml:"relationship_count"`
|
|
HasPrimaryKey bool `json:"has_primary_key" yaml:"has_primary_key" xml:"has_primary_key"`
|
|
ForeignKeyCount int `json:"foreign_key_count" yaml:"foreign_key_count" xml:"foreign_key_count"`
|
|
}
|
|
|
|
// ToSummary converts a Table to a TableSummary with calculated counts.
|
|
func (t *Table) ToSummary() *TableSummary {
|
|
summary := &TableSummary{
|
|
Name: t.Name,
|
|
Schema: t.Schema,
|
|
Description: t.Description,
|
|
ColumnCount: len(t.Columns),
|
|
ConstraintCount: len(t.Constraints),
|
|
IndexCount: len(t.Indexes),
|
|
RelationshipCount: len(t.Relationships),
|
|
HasPrimaryKey: t.GetPrimaryKey() != nil,
|
|
}
|
|
|
|
// Count foreign keys
|
|
summary.ForeignKeyCount = len(t.GetForeignKeys())
|
|
|
|
return summary
|
|
}
|