models

package
v1.0.5 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Jan 4, 2026 License: Apache-2.0 Imports: 6 Imported by: 0

Documentation

Overview

Package models provides the core data structures for representing database schemas. It defines types for databases, schemas, tables, columns, relationships, constraints, indexes, views, sequences, and other database objects. These models serve as the intermediate representation for converting between various database schema formats.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func SortColumnsByName added in v1.0.4

func SortColumnsByName(columns []*Column, desc bool) error

SortColumnsByName sorts columns by name

func SortColumnsBySequence added in v1.0.4

func SortColumnsBySequence(columns []*Column, desc bool) error

SortColumnsBySequence sorts columns by sequence number

func SortConstraintsByName added in v1.0.4

func SortConstraintsByName(constraints []*Constraint, desc bool) error

SortConstraintsByName sorts constraints by name

func SortEnumsByName added in v1.0.4

func SortEnumsByName(enums []*Enum, desc bool) error

SortEnumsByName sorts enums by name

func SortIndexesByName added in v1.0.4

func SortIndexesByName(indexes []*Index, desc bool) error

SortIndexesByName sorts indexes by name

func SortIndexesBySequence added in v1.0.4

func SortIndexesBySequence(indexes []*Index, desc bool) error

SortIndexesBySequence sorts indexes by sequence number

func SortRelationshipsByName added in v1.0.4

func SortRelationshipsByName(relationships []*Relationship, desc bool) error

SortRelationshipsByName sorts relationships by name

func SortSchemasByName added in v1.0.4

func SortSchemasByName(schemas []*Schema, desc bool) error

SortSchemasByName sorts schemas by name

func SortSchemasBySequence added in v1.0.4

func SortSchemasBySequence(schemas []*Schema, desc bool) error

SortSchemasBySequence sorts schemas by sequence number

func SortScriptsByName added in v1.0.4

func SortScriptsByName(scripts []*Script, desc bool) error

SortScriptsByName sorts scripts by name

func SortSequencesByName added in v1.0.4

func SortSequencesByName(sequences []*Sequence, desc bool) error

SortSequencesByName sorts sequences by name

func SortSequencesBySequence added in v1.0.4

func SortSequencesBySequence(sequences []*Sequence, desc bool) error

SortSequencesBySequence sorts sequences by sequence number

func SortTablesByName added in v1.0.4

func SortTablesByName(tables []*Table, desc bool) error

SortTablesByName sorts tables by name

func SortTablesBySequence added in v1.0.4

func SortTablesBySequence(tables []*Table, desc bool) error

SortTablesBySequence sorts tables by sequence number

func SortViewsByName added in v1.0.4

func SortViewsByName(views []*View, desc bool) error

SortViewsByName sorts views by name

func SortViewsBySequence added in v1.0.4

func SortViewsBySequence(views []*View, desc bool) error

SortViewsBySequence sorts views by sequence number

Types

type Column

type Column struct {
	Name          string `json:"name" yaml:"name" xml:"name"`
	Description   string `json:"description,omitempty" yaml:"description,omitempty" xml:"description,omitempty"`
	Table         string `json:"table" yaml:"table" xml:"table"`
	Schema        string `json:"schema" yaml:"schema" xml:"schema"`
	Type          string `json:"type" yaml:"type" xml:"type"`
	Length        int    `json:"length,omitempty" yaml:"length,omitempty" xml:"length,omitempty"`
	Precision     int    `json:"precision,omitempty" yaml:"precision,omitempty" xml:"precision,omitempty"`
	Scale         int    `json:"scale,omitempty" yaml:"scale,omitempty" xml:"scale,omitempty"`
	NotNull       bool   `json:"not_null" yaml:"not_null" xml:"not_null"`
	Default       any    `json:"default,omitempty" yaml:"default,omitempty" xml:"default,omitempty"`
	AutoIncrement bool   `json:"auto_increment" yaml:"auto_increment" xml:"auto_increment"`
	IsPrimaryKey  bool   `json:"is_primary_key" yaml:"is_primary_key" xml:"is_primary_key"`
	Comment       string `json:"comment,omitempty" yaml:"comment,omitempty" xml:"comment,omitempty"`
	Collation     string `json:"collation,omitempty" yaml:"collation,omitempty" xml:"collation,omitempty"`
	Sequence      uint   `json:"sequence,omitempty" yaml:"sequence,omitempty" xml:"sequence,omitempty"`
	GUID          string `json:"guid" yaml:"guid" xml:"guid"`
}

Column represents a table column

func InitColumn

func InitColumn(name, table, schema string) *Column

InitColumn initializes a new Column

func SortColumnsMapByName added in v1.0.4

func SortColumnsMapByName(columns map[string]*Column, desc bool) []*Column

SortColumnsMapByName converts column map to sorted slice by name

func SortColumnsMapBySequence added in v1.0.4

func SortColumnsMapBySequence(columns map[string]*Column, desc bool) []*Column

SortColumnsMapBySequence converts column map to sorted slice by sequence

func (*Column) SQLName

func (d *Column) SQLName() string

SQLName returns the column name in lowercase for SQL compatibility.

type Constraint

type Constraint struct {
	Name              string         `json:"name" yaml:"name" xml:"name"`
	Type              ConstraintType `json:"type" yaml:"type" xml:"type"`
	Columns           []string       `json:"columns" yaml:"columns" xml:"columns"`
	Expression        string         `json:"expression,omitempty" yaml:"expression,omitempty" xml:"expression,omitempty"`
	Schema            string         `json:"schema,omitempty" yaml:"schema,omitempty" xml:"schema,omitempty"`
	Table             string         `json:"table,omitempty" yaml:"table,omitempty" xml:"table,omitempty"`
	ReferencedTable   string         `json:"referenced_table" yaml:"referenced_table" xml:"referenced_table"`
	ReferencedSchema  string         `json:"referenced_schema" yaml:"referenced_schema" xml:"referenced_schema"`
	ReferencedColumns []string       `json:"referenced_columns" yaml:"referenced_columns" xml:"referenced_columns"`
	OnDelete          string         `json:"on_delete" yaml:"on_delete" xml:"on_delete"` // CASCADE, SET NULL, RESTRICT, etc.
	OnUpdate          string         `json:"on_update" yaml:"on_update" xml:"on_update"`
	Deferrable        bool           `json:"deferrable,omitempty" yaml:"deferrable,omitempty" xml:"deferrable,omitempty"`
	InitiallyDeferred bool           `json:"initially_deferred,omitempty" yaml:"initially_deferred,omitempty" xml:"initially_deferred,omitempty"`
	Sequence          uint           `json:"sequence,omitempty" yaml:"sequence,omitempty" xml:"sequence,omitempty"`
	GUID              string         `json:"guid" yaml:"guid" xml:"guid"`
}

Constraint represents a database constraint that enforces data integrity rules. Constraints can be primary keys, foreign keys, unique constraints, check constraints, or not-null constraints.

func InitConstraint

func InitConstraint(name string, constraintType ConstraintType) *Constraint

InitConstraint initializes a new Constraint with empty slices

func SortConstraintsMapByName added in v1.0.4

func SortConstraintsMapByName(constraints map[string]*Constraint, desc bool) []*Constraint

SortConstraintsMapByName converts constraint map to sorted slice by name

func (*Constraint) SQLName

func (d *Constraint) SQLName() string

SQLName returns the constraint name in lowercase for SQL compatibility.

type ConstraintType

type ConstraintType string

ConstraintType represents the type of database constraint.

const (
	PrimaryKeyConstraint ConstraintType = "primary_key" // Primary key uniquely identifies each record
	ForeignKeyConstraint ConstraintType = "foreign_key" // Foreign key references another table
	UniqueConstraint     ConstraintType = "unique"      // Unique constraint ensures all values are different
	CheckConstraint      ConstraintType = "check"       // Check constraint validates data against an expression
	NotNullConstraint    ConstraintType = "not_null"    // Not null constraint requires a value
)

Supported constraint types.

type DCTXComponent

type DCTXComponent struct {
	Guid    string `xml:"Guid,attr"`
	FieldId string `xml:"FieldId,attr,omitempty"`
	Order   int    `xml:"Order,attr"`
	Ascend  bool   `xml:"Ascend,attr,omitempty"`
}

DCTXComponent represents a component of a key, referencing a field in the index.

type DCTXDictionary

type DCTXDictionary struct {
	XMLName   xml.Name       `xml:"Dictionary"`
	Name      string         `xml:"Name,attr"`
	Version   string         `xml:"Version,attr"`
	Tables    []DCTXTable    `xml:"Table"`
	Relations []DCTXRelation `xml:"Relation,omitempty"`
}

DCTXDictionary represents the root element of a DCTX file.

type DCTXField

type DCTXField struct {
	Guid       string       `xml:"Guid,attr"`
	Name       string       `xml:"Name,attr"`
	DataType   string       `xml:"DataType,attr"`
	Size       int          `xml:"Size,attr,omitempty"`
	NoPopulate bool         `xml:"NoPopulate,attr,omitempty"`
	Thread     bool         `xml:"Thread,attr,omitempty"`
	Fields     []DCTXField  `xml:"Field,omitempty"` // For GROUP fields (nested structures)
	Options    []DCTXOption `xml:"Option,omitempty"`
}

DCTXField represents a field/column definition in DCTX format. Fields can be nested for GROUP structures.

type DCTXFieldMapping

type DCTXFieldMapping struct {
	Guid  string `xml:"Guid,attr"`
	Field string `xml:"Field,attr"`
}

DCTXFieldMapping represents a field mapping in a relation for multi-column foreign keys.

type DCTXKey

type DCTXKey struct {
	Guid        string          `xml:"Guid,attr"`
	Name        string          `xml:"Name,attr"`
	KeyType     string          `xml:"KeyType,attr,omitempty"`
	Primary     bool            `xml:"Primary,attr,omitempty"`
	Unique      bool            `xml:"Unique,attr,omitempty"`
	Order       int             `xml:"Order,attr,omitempty"`
	Description string          `xml:"Description,attr,omitempty"`
	Components  []DCTXComponent `xml:"Component"`
}

DCTXKey represents an index or key definition in DCTX format.

type DCTXOption

type DCTXOption struct {
	Property      string `xml:"Property,attr"`
	PropertyType  string `xml:"PropertyType,attr,omitempty"`
	PropertyValue string `xml:"PropertyValue,attr"`
}

DCTXOption represents a property option in DCTX format for metadata storage.

type DCTXRelation

type DCTXRelation struct {
	Guid            string             `xml:"Guid,attr"`
	PrimaryTable    string             `xml:"PrimaryTable,attr"`
	ForeignTable    string             `xml:"ForeignTable,attr"`
	PrimaryKey      string             `xml:"PrimaryKey,attr,omitempty"`
	ForeignKey      string             `xml:"ForeignKey,attr,omitempty"`
	Delete          string             `xml:"Delete,attr,omitempty"`
	Update          string             `xml:"Update,attr,omitempty"`
	ForeignMappings []DCTXFieldMapping `xml:"ForeignMapping,omitempty"`
	PrimaryMappings []DCTXFieldMapping `xml:"PrimaryMapping,omitempty"`
}

DCTXRelation represents a relationship/foreign key in DCTX format.

type DCTXTable

type DCTXTable struct {
	Guid        string       `xml:"Guid,attr"`
	Name        string       `xml:"Name,attr"`
	Prefix      string       `xml:"Prefix,attr"`
	Driver      string       `xml:"Driver,attr,omitempty"`
	Owner       string       `xml:"Owner,attr,omitempty"`
	Path        string       `xml:"Path,attr,omitempty"`
	Description string       `xml:"Description,attr,omitempty"`
	Fields      []DCTXField  `xml:"Field"`
	Keys        []DCTXKey    `xml:"Key,omitempty"`
	Options     []DCTXOption `xml:"Option,omitempty"`
}

DCTXTable represents a table definition in DCTX format.

type Database

type Database struct {
	Name            string       `json:"name" yaml:"name"`
	Description     string       `json:"description,omitempty" yaml:"description,omitempty" xml:"description,omitempty"`
	Schemas         []*Schema    `json:"schemas" yaml:"schemas" xml:"schemas"`
	Domains         []*Domain    `json:"domains,omitempty" yaml:"domains,omitempty" xml:"domains,omitempty"`
	Comment         string       `json:"comment,omitempty" yaml:"comment,omitempty" xml:"comment,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"`
	SourceFormat    string       `json:"source_format,omitempty" yaml:"source_format,omitempty" xml:"source_format,omitempty"` // Source Format of the database.
	UpdatedAt       string       `json:"updatedat,omitempty" yaml:"updatedat,omitempty" xml:"updatedat,omitempty"`
	GUID            string       `json:"guid" yaml:"guid" xml:"guid"`
}

Database represents the complete database schema

func InitDatabase

func InitDatabase(name string) *Database

InitDatabase initializes a new Database with empty slices

func (*Database) SQLName

func (d *Database) SQLName() string

SQLName returns the database name in lowercase for SQL compatibility.

func (*Database) ToFlatColumns

func (d *Database) ToFlatColumns() []*FlatColumn

ToFlatColumns converts a Database to a slice of FlatColumns for denormalized access to all columns.

func (*Database) ToFlatConstraints

func (d *Database) ToFlatConstraints() []*FlatConstraint

ToFlatConstraints converts a Database to a slice of FlatConstraints for denormalized access to all constraints.

func (*Database) ToFlatRelationships

func (d *Database) ToFlatRelationships() []*FlatRelationship

ToFlatRelationships converts a Database to a slice of FlatRelationships for denormalized access to all relationships.

func (*Database) ToFlatTables

func (d *Database) ToFlatTables() []*FlatTable

ToFlatTables converts a Database to a slice of FlatTables for denormalized access to all tables.

func (*Database) ToSummary

func (d *Database) ToSummary() *DatabaseSummary

ToSummary converts a Database to a DatabaseSummary with calculated counts.

func (*Database) UpdateDate added in v1.0.5

func (d *Database) UpdateDate()

UpdateDate sets the UpdatedAt field to the current time in RFC3339 format.

type DatabaseSummary

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"`
}

DatabaseSummary provides a compact overview of a database with aggregated statistics.

type DatabaseType

type DatabaseType string

DatabaseType represents the type of database system.

const (
	PostgresqlDatabaseType DatabaseType = "pgsql"  // PostgreSQL database
	MSSQLDatabaseType      DatabaseType = "mssql"  // Microsoft SQL Server database
	SqlLiteDatabaseType    DatabaseType = "sqlite" // SQLite database
)

Supported database types.

type Domain added in v1.0.5

type Domain struct {
	Name        string         `json:"name" yaml:"name" xml:"name"`
	Description string         `json:"description,omitempty" yaml:"description,omitempty" xml:"description,omitempty"`
	Tables      []*DomainTable `json:"tables" yaml:"tables" xml:"tables"`
	Comment     string         `json:"comment,omitempty" yaml:"comment,omitempty" xml:"comment,omitempty"`
	Metadata    map[string]any `json:"metadata,omitempty" yaml:"metadata,omitempty" xml:"-"`
	Sequence    uint           `json:"sequence,omitempty" yaml:"sequence,omitempty" xml:"sequence,omitempty"`
	GUID        string         `json:"guid" yaml:"guid" xml:"guid"`
}

Domain represents a logical business domain grouping multiple tables from potentially different schemas. Domains allow for organizing database tables by functional areas (e.g., authentication, user data, financial).

func InitDomain added in v1.0.5

func InitDomain(name string) *Domain

InitDomain initializes a new Domain with empty slices and maps

func (*Domain) SQLName added in v1.0.5

func (d *Domain) SQLName() string

SQLName returns the domain name in lowercase for SQL compatibility.

type DomainTable added in v1.0.5

type DomainTable struct {
	TableName  string `json:"table_name" yaml:"table_name" xml:"table_name"`
	SchemaName string `json:"schema_name" yaml:"schema_name" xml:"schema_name"`
	Sequence   uint   `json:"sequence,omitempty" yaml:"sequence,omitempty" xml:"sequence,omitempty"`
	RefTable   *Table `json:"-" yaml:"-" xml:"-"` // Excluded to prevent circular references
	GUID       string `json:"guid" yaml:"guid" xml:"guid"`
}

DomainTable represents a reference to a specific table within a domain. It identifies the table by name and schema, allowing a single domain to include tables from multiple schemas.

func InitDomainTable added in v1.0.5

func InitDomainTable(tableName, schemaName string) *DomainTable

InitDomainTable initializes a new DomainTable reference

type Enum added in v1.0.2

type Enum struct {
	Name   string   `json:"name" yaml:"name" xml:"name"`
	Values []string `json:"values" yaml:"values" xml:"values"`
	Schema string   `json:"schema,omitempty" yaml:"schema,omitempty" xml:"schema,omitempty"`
	GUID   string   `json:"guid" yaml:"guid" xml:"guid"`
}

Enum represents a database enumeration type with a set of allowed values.

func InitEnum added in v1.0.5

func InitEnum(name, schema string) *Enum

InitEnum initializes a new Enum with empty values slice

func (*Enum) SQLName added in v1.0.2

func (d *Enum) SQLName() string

SQLName returns the enum name in lowercase for SQL compatibility.

type FlatColumn

type FlatColumn struct {
	DatabaseName       string `json:"database_name" yaml:"database_name" xml:"database_name"`
	SchemaName         string `json:"schema_name" yaml:"schema_name" xml:"schema_name"`
	TableName          string `json:"table_name" yaml:"table_name" xml:"table_name"`
	ColumnName         string `json:"column_name" yaml:"column_name" xml:"column_name"`
	FullyQualifiedName string `json:"fully_qualified_name" yaml:"fully_qualified_name" xml:"fully_qualified_name"` // database.schema.table.column
	Type               string `json:"type" yaml:"type" xml:"type"`
	Length             int    `json:"length,omitempty" yaml:"length,omitempty" xml:"length,omitempty"`
	Precision          int    `json:"precision,omitempty" yaml:"precision,omitempty" xml:"precision,omitempty"`
	Scale              int    `json:"scale,omitempty" yaml:"scale,omitempty" xml:"scale,omitempty"`
	NotNull            bool   `json:"not_null" yaml:"not_null" xml:"not_null"`
	Default            any    `json:"default,omitempty" yaml:"default,omitempty" xml:"default,omitempty"`
	AutoIncrement      bool   `json:"auto_increment" yaml:"auto_increment" xml:"auto_increment"`
	IsPrimaryKey       bool   `json:"is_primary_key" yaml:"is_primary_key" xml:"is_primary_key"`
	Description        string `json:"description,omitempty" yaml:"description,omitempty" xml:"description,omitempty"`
	Comment            string `json:"comment,omitempty" yaml:"comment,omitempty" xml:"comment,omitempty"`
}

FlatColumn represents a column with full database context in a single structure. It includes fully qualified names for easy identification and querying.

type FlatConstraint

type FlatConstraint struct {
	DatabaseName       string         `json:"database_name" yaml:"database_name" xml:"database_name"`
	SchemaName         string         `json:"schema_name" yaml:"schema_name" xml:"schema_name"`
	TableName          string         `json:"table_name" yaml:"table_name" xml:"table_name"`
	ConstraintName     string         `json:"constraint_name" yaml:"constraint_name" xml:"constraint_name"`
	FullyQualifiedName string         `json:"fully_qualified_name" yaml:"fully_qualified_name" xml:"fully_qualified_name"` // database.schema.table.constraint
	Type               ConstraintType `json:"type" yaml:"type" xml:"type"`
	Columns            []string       `json:"columns" yaml:"columns" xml:"columns"`
	Expression         string         `json:"expression,omitempty" yaml:"expression,omitempty" xml:"expression,omitempty"`
	ReferencedTable    string         `json:"referenced_table,omitempty" yaml:"referenced_table,omitempty" xml:"referenced_table,omitempty"`
	ReferencedSchema   string         `json:"referenced_schema,omitempty" yaml:"referenced_schema,omitempty" xml:"referenced_schema,omitempty"`
	ReferencedColumns  []string       `json:"referenced_columns,omitempty" yaml:"referenced_columns,omitempty" xml:"referenced_columns,omitempty"`
	ReferencedFQN      string         `json:"referenced_fqn,omitempty" yaml:"referenced_fqn,omitempty" xml:"referenced_fqn,omitempty"` // Fully qualified reference
	OnDelete           string         `json:"on_delete,omitempty" yaml:"on_delete,omitempty" xml:"on_delete,omitempty"`
	OnUpdate           string         `json:"on_update,omitempty" yaml:"on_update,omitempty" xml:"on_update,omitempty"`
}

FlatConstraint represents a constraint with full database context and resolved references.

type FlatRelationship

type FlatRelationship struct {
	DatabaseName     string       `json:"database_name" yaml:"database_name" xml:"database_name"`
	RelationshipName string       `json:"relationship_name" yaml:"relationship_name" xml:"relationship_name"`
	Type             RelationType `json:"type" yaml:"type" xml:"type"`
	FromFQN          string       `json:"from_fqn" yaml:"from_fqn" xml:"from_fqn"` // database.schema.table
	ToFQN            string       `json:"to_fqn" yaml:"to_fqn" xml:"to_fqn"`       // database.schema.table
	FromTable        string       `json:"from_table" yaml:"from_table" xml:"from_table"`
	FromSchema       string       `json:"from_schema" yaml:"from_schema" xml:"from_schema"`
	ToTable          string       `json:"to_table" yaml:"to_table" xml:"to_table"`
	ToSchema         string       `json:"to_schema" yaml:"to_schema" xml:"to_schema"`
	ForeignKey       string       `json:"foreign_key" yaml:"foreign_key" xml:"foreign_key"`
	ThroughTableFQN  string       `json:"through_table_fqn,omitempty" yaml:"through_table_fqn,omitempty" xml:"through_table_fqn,omitempty"`
	Description      string       `json:"description,omitempty" yaml:"description,omitempty" xml:"description,omitempty"`
}

FlatRelationship represents a relationship with full database context and fully qualified table names.

type FlatTable

type FlatTable struct {
	DatabaseName       string `json:"database_name" yaml:"database_name" xml:"database_name"`
	SchemaName         string `json:"schema_name" yaml:"schema_name" xml:"schema_name"`
	TableName          string `json:"table_name" yaml:"table_name" xml:"table_name"`
	FullyQualifiedName string `json:"fully_qualified_name" yaml:"fully_qualified_name" xml:"fully_qualified_name"` // database.schema.table
	Description        string `json:"description,omitempty" yaml:"description,omitempty" xml:"description,omitempty"`
	Comment            string `json:"comment,omitempty" yaml:"comment,omitempty" xml:"comment,omitempty"`
	Tablespace         string `json:"tablespace,omitempty" yaml:"tablespace,omitempty" xml:"tablespace,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"`
}

FlatTable represents a table with full database context and aggregated counts.

type Index

type Index struct {
	Name        string   `json:"name" yaml:"name" xml:"name"`
	Description string   `json:"description,omitempty" yaml:"description,omitempty" xml:"description,omitempty"`
	Table       string   `json:"table,omitempty" yaml:"table,omitempty" xml:"table,omitempty"`
	Schema      string   `json:"schema,omitempty" yaml:"schema,omitempty" xml:"schema,omitempty"`
	Columns     []string `json:"columns" yaml:"columns" xml:"columns"`
	Unique      bool     `json:"unique" yaml:"unique" xml:"unique"`
	Type        string   `json:"type" yaml:"type" xml:"type"`                                  // btree, hash, gin, gist, etc.
	Where       string   `json:"where,omitempty" yaml:"where,omitempty" xml:"where,omitempty"` // partial index condition
	Concurrent  bool     `json:"concurrent,omitempty" yaml:"concurrent,omitempty" xml:"concurrent,omitempty"`
	Include     []string `json:"include,omitempty" yaml:"include,omitempty" xml:"include,omitempty"` // INCLUDE columns
	Comment     string   `json:"comment,omitempty" yaml:"comment,omitempty" xml:"comment,omitempty"`
	Sequence    uint     `json:"sequence,omitempty" yaml:"sequence,omitempty" xml:"sequence,omitempty"`
	GUID        string   `json:"guid" yaml:"guid" xml:"guid"`
}

Index represents a database index for optimizing query performance. Indexes can be unique, partial, or include additional columns.

func InitIndex

func InitIndex(name, table, schema string) *Index

InitIndex initializes a new Index with empty slices

func SortIndexesMapByName added in v1.0.4

func SortIndexesMapByName(indexes map[string]*Index, desc bool) []*Index

SortIndexesMapByName converts index map to sorted slice by name

func SortIndexesMapBySequence added in v1.0.4

func SortIndexesMapBySequence(indexes map[string]*Index, desc bool) []*Index

SortIndexesMapBySequence converts index map to sorted slice by sequence

func (*Index) SQLName

func (d *Index) SQLName() string

SQLName returns the index name in lowercase for SQL compatibility.

type RelationType

type RelationType string

RelationType represents the type of relationship between database tables.

const (
	OneToOne   RelationType = "one_to_one"   // One record in table A relates to one record in table B
	OneToMany  RelationType = "one_to_many"  // One record in table A relates to many records in table B
	ManyToMany RelationType = "many_to_many" // Many records in table A relate to many records in table B
)

Supported relationship types.

type Relationship

type Relationship struct {
	Name          string            `json:"name" yaml:"name" xml:"name"`
	Type          RelationType      `json:"type" yaml:"type" xml:"type"`
	FromTable     string            `json:"from_table" yaml:"from_table" xml:"from_table"`
	FromSchema    string            `json:"from_schema" yaml:"from_schema" xml:"from_schema"`
	FromColumns   []string          `json:"from_columns" yaml:"from_columns" xml:"from_columns"`
	ToTable       string            `json:"to_table" yaml:"to_table" xml:"to_table"`
	ToSchema      string            `json:"to_schema" yaml:"to_schema" xml:"to_schema"`
	ToColumns     []string          `json:"to_columns" yaml:"to_columns" xml:"to_columns"`
	ForeignKey    string            `json:"foreign_key" yaml:"foreign_key" xml:"foreign_key"`
	Properties    map[string]string `json:"properties" yaml:"properties" xml:"-"`
	ThroughTable  string            `json:"through_table,omitempty" yaml:"through_table,omitempty" xml:"through_table,omitempty"` // For many-to-many
	ThroughSchema string            `json:"through_schema,omitempty" yaml:"through_schema,omitempty" xml:"through_schema,omitempty"`
	Description   string            `json:"description,omitempty" yaml:"description,omitempty" xml:"description,omitempty"`
	Sequence      uint              `json:"sequence,omitempty" yaml:"sequence,omitempty" xml:"sequence,omitempty"`
	GUID          string            `json:"guid" yaml:"guid" xml:"guid"`
}

Relationship represents a relationship between two database tables. Relationships can be one-to-one, one-to-many, or many-to-many.

func InitRelation

func InitRelation(name, schema string) *Relationship

InitRelation initializes a new Relationship with empty slices

func InitRelationship

func InitRelationship(name string, relType RelationType) *Relationship

InitRelationship initializes a new Relationship with empty maps

func SortRelationshipsMapByName added in v1.0.4

func SortRelationshipsMapByName(relationships map[string]*Relationship, desc bool) []*Relationship

SortRelationshipsMapByName converts relationship map to sorted slice by name

func (*Relationship) SQLName

func (d *Relationship) SQLName() string

SQLName returns the relationship name in lowercase for SQL compatibility.

type SQLNamer

type SQLNamer interface {
	SQLName() string
}

SQLNamer interface for types that can provide a normalized sql name

type Schema

type Schema struct {
	Name        string            `json:"name" yaml:"name" xml:"name"`
	Description string            `json:"description,omitempty" yaml:"description,omitempty" xml:"description,omitempty"`
	Tables      []*Table          `json:"tables" yaml:"tables" xml:"-"`
	Views       []*View           `json:"views,omitempty" yaml:"views,omitempty" xml:"-"`
	Sequences   []*Sequence       `json:"sequences,omitempty" yaml:"sequences,omitempty" xml:"-"`
	Owner       string            `json:"owner" yaml:"owner" xml:"owner"`
	Permissions map[string]string `json:"permissions,omitempty" yaml:"permissions,omitempty" xml:"-"`
	Comment     string            `json:"comment,omitempty" yaml:"comment,omitempty" xml:"comment,omitempty"`
	Metadata    map[string]any    `json:"metadata,omitempty" yaml:"metadata,omitempty" xml:"-"`
	Scripts     []*Script         `json:"scripts,omitempty" yaml:"scripts,omitempty" xml:"scripts,omitempty"`
	Sequence    uint              `json:"sequence,omitempty" yaml:"sequence,omitempty" xml:"sequence,omitempty"`
	RefDatabase *Database         `json:"-" yaml:"-" xml:"-"` // Excluded to prevent circular references
	Relations   []*Relationship   `json:"relations,omitempty" yaml:"relations,omitempty" xml:"-"`
	Enums       []*Enum           `json:"enums,omitempty" yaml:"enums,omitempty" xml:"enums"`
	UpdatedAt   string            `json:"updatedat,omitempty" yaml:"updatedat,omitempty" xml:"updatedat,omitempty"`
	GUID        string            `json:"guid" yaml:"guid" xml:"guid"`
}

Schema represents a database schema, which is a logical grouping of database objects such as tables, views, sequences, and relationships within a database.

func InitSchema

func InitSchema(name string) *Schema

InitSchema initializes a new Schema with empty slices and maps

func (*Schema) SQLName

func (d *Schema) SQLName() string

SQLName returns the schema name in lowercase for SQL compatibility.

func (*Schema) ToSummary

func (s *Schema) ToSummary() *SchemaSummary

ToSummary converts a Schema to a SchemaSummary with calculated counts.

func (*Schema) UpdateDate added in v1.0.5

func (d *Schema) UpdateDate()

UpdaUpdateDateted sets the UpdatedAt field to the current time in RFC3339 format.

type SchemaSummary

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"`
}

SchemaSummary provides a compact overview of a schema with aggregated statistics.

type Script

type Script struct {
	Name        string   `json:"name" yaml:"name" xml:"name"`
	Description string   `json:"description" yaml:"description" xml:"description"`
	SQL         string   `json:"sql" yaml:"sql" xml:"sql"`
	Rollback    string   `json:"rollback,omitempty" yaml:"rollback,omitempty" xml:"rollback,omitempty"`
	RunAfter    []string `json:"run_after,omitempty" yaml:"run_after,omitempty" xml:"run_after,omitempty"`
	Schema      string   `json:"schema,omitempty" yaml:"schema,omitempty" xml:"schema,omitempty"`
	Version     string   `json:"version,omitempty" yaml:"version,omitempty" xml:"version,omitempty"`
	Priority    int      `json:"priority,omitempty" yaml:"priority,omitempty" xml:"priority,omitempty"`
	Sequence    uint     `json:"sequence,omitempty" yaml:"sequence,omitempty" xml:"sequence,omitempty"`
	GUID        string   `json:"guid" yaml:"guid" xml:"guid"`
}

Script represents a database migration or initialization script. Scripts can have dependencies and rollback capabilities.

func InitScript

func InitScript(name string) *Script

InitScript initializes a new Script with empty slices

func (*Script) SQLName

func (d *Script) SQLName() string

SQLName returns the script name in lowercase for SQL compatibility.

type Sequence

type Sequence struct {
	Name          string  `json:"name" yaml:"name" xml:"name"`
	Description   string  `json:"description,omitempty" yaml:"description,omitempty" xml:"description,omitempty"`
	Schema        string  `json:"schema" yaml:"schema" xml:"schema"`
	StartValue    int64   `json:"start_value" yaml:"start_value" xml:"start_value"`
	MinValue      int64   `json:"min_value,omitempty" yaml:"min_value,omitempty" xml:"min_value,omitempty"`
	MaxValue      int64   `json:"max_value,omitempty" yaml:"max_value,omitempty" xml:"max_value,omitempty"`
	IncrementBy   int64   `json:"increment_by" yaml:"increment_by" xml:"increment_by"`
	CacheSize     int64   `json:"cache_size,omitempty" yaml:"cache_size,omitempty" xml:"cache_size,omitempty"`
	Cycle         bool    `json:"cycle" yaml:"cycle" xml:"cycle"`
	OwnedByTable  string  `json:"owned_by_table,omitempty" yaml:"owned_by_table,omitempty" xml:"owned_by_table,omitempty"`
	OwnedByColumn string  `json:"owned_by_column,omitempty" yaml:"owned_by_column,omitempty" xml:"owned_by_column,omitempty"`
	Comment       string  `json:"comment,omitempty" yaml:"comment,omitempty" xml:"comment,omitempty"`
	Sequence      uint    `json:"sequence,omitempty" yaml:"sequence,omitempty" xml:"sequence,omitempty"`
	RefSchema     *Schema `json:"-" yaml:"-" xml:"-"` // Excluded to prevent circular references
	GUID          string  `json:"guid" yaml:"guid" xml:"guid"`
}

Sequence represents a database sequence (auto-increment generator)

func InitSequence

func InitSequence(name, schema string) *Sequence

InitSequence initializes a new Sequence with default values

func (*Sequence) SQLName

func (d *Sequence) SQLName() string

SQLName returns the sequence name in lowercase for SQL compatibility.

type SortOrder added in v1.0.4

type SortOrder bool

SortOrder represents the sort direction

const (
	// Ascending sort order
	Ascending SortOrder = false
	// Descending sort order
	Descending SortOrder = true
)

type Table

type Table struct {
	Name          string                   `json:"name" yaml:"name" xml:"name"`
	Description   string                   `json:"description,omitempty" yaml:"description,omitempty" xml:"description,omitempty"`
	Schema        string                   `json:"schema" yaml:"schema" xml:"schema"`
	Columns       map[string]*Column       `json:"columns" yaml:"columns" xml:"-"`
	Constraints   map[string]*Constraint   `json:"constraints" yaml:"constraints" xml:"-"`
	Indexes       map[string]*Index        `json:"indexes,omitempty" yaml:"indexes,omitempty" xml:"-"`
	Relationships map[string]*Relationship `json:"relationships,omitempty" yaml:"relationships,omitempty" xml:"-"`
	Comment       string                   `json:"comment,omitempty" yaml:"comment,omitempty" xml:"comment,omitempty"`
	Tablespace    string                   `json:"tablespace,omitempty" yaml:"tablespace,omitempty" xml:"tablespace,omitempty"`
	Metadata      map[string]any           `json:"metadata,omitempty" yaml:"metadata,omitempty" xml:"-"`
	Sequence      uint                     `json:"sequence,omitempty" yaml:"sequence,omitempty" xml:"sequence,omitempty"`
	RefSchema     *Schema                  `json:"-" yaml:"-" xml:"-"` // Excluded to prevent circular references
	UpdatedAt     string                   `json:"updatedat,omitempty" yaml:"updatedat,omitempty" xml:"updatedat,omitempty"`
	GUID          string                   `json:"guid" yaml:"guid" xml:"guid"`
}

Table represents a database table with its columns, constraints, indexes, and relationships. Tables are the primary data storage structures in a database.

func InitTable

func InitTable(name, schema string) *Table

InitTable initializes a new Table with empty maps

func (Table) GetForeignKeys

func (m Table) GetForeignKeys() []*Constraint

GetForeignKeys returns all foreign key constraints for the table.

func (Table) GetPrimaryKey

func (m Table) GetPrimaryKey() *Column

GetPrimaryKey returns the primary key column for the table, or nil if none exists.

func (*Table) SQLName

func (d *Table) SQLName() string

SQLName returns the table name in lowercase for SQL compatibility.

func (*Table) ToSummary

func (t *Table) ToSummary() *TableSummary

ToSummary converts a Table to a TableSummary with calculated counts.

func (*Table) UpdateDate added in v1.0.5

func (d *Table) UpdateDate()

UpdateDate sets the UpdatedAt field to the current time in RFC3339 format.

type TableSummary

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"`
}

TableSummary provides a compact overview of a table with aggregated statistics.

type View

type View struct {
	Name        string             `json:"name" yaml:"name" xml:"name"`
	Description string             `json:"description,omitempty" yaml:"description,omitempty" xml:"description,omitempty"`
	Schema      string             `json:"schema" yaml:"schema" xml:"schema"`
	Definition  string             `json:"definition" yaml:"definition" xml:"definition"` // SQL definition
	Columns     map[string]*Column `json:"columns" yaml:"columns" xml:"-"`
	Comment     string             `json:"comment,omitempty" yaml:"comment,omitempty" xml:"comment,omitempty"`
	Metadata    map[string]any     `json:"metadata,omitempty" yaml:"metadata,omitempty" xml:"-"`
	Sequence    uint               `json:"sequence,omitempty" yaml:"sequence,omitempty" xml:"sequence,omitempty"`
	RefSchema   *Schema            `json:"-" yaml:"-" xml:"-"` // Excluded to prevent circular references
	GUID        string             `json:"guid" yaml:"guid" xml:"guid"`
}

View represents a database view (read-only)

func InitView

func InitView(name, schema string) *View

InitView initializes a new View with empty maps

func (*View) SQLName

func (d *View) SQLName() string

SQLName returns the view name in lowercase for SQL compatibility.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL