mssqlcatalog

package
v0.1.60 Latest Latest
Warning

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

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

Documentation

Overview

Package mssqlcatalog reads SQL Server catalog views into plain row structs.

It is the single owner of the catalog SQL shared by the schema inspector and the Atlas SQL Server driver. Every query is schema-scoped, runs in one round trip, pushes name filters into SQL as ordinary binds, and returns rows in a deterministic owner-then-natural order.

Queries must run on databases at compatibility level 100 (OIPA): no STRING_AGG, no OPENJSON. Ordered string aggregation uses FOR XML PATH, and name filters are plain `IN (@p1, @p2, ...)` lists. Each query text starts with its outer SELECT so a caller may prefix it with a TOP bound.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Schemas

func Schemas(ctx context.Context, q Queryer, names []string) ([]string, error)

Schemas lists non-system schemas, optionally restricted to names.

Types

type CheckConstraint

type CheckConstraint struct {
	Table      string
	Name       string
	Definition string
	Column     *string
	Disabled   bool
}

CheckConstraint is one CHECK constraint. Column is set only for a column-level constraint.

func CheckConstraints

func CheckConstraints(ctx context.Context, q Queryer, scope Scope) ([]CheckConstraint, error)

CheckConstraints lists every check constraint in the scope ordered by table then constraint name.

type Column

type Column struct {
	Table             string
	Name              string
	DataType          string
	Nullable          bool
	Default           *string
	DefaultConstraint *string
	Position          int
	CharMaxLength     *int
	NumericPrecision  *int
	NumericScale      *int
	MaxLength         *int
	Precision         *int
	Scale             *int
	PrimaryKey        bool
	Unique            bool
	Identity          bool
	IdentitySeed      *int64
	IdentityIncrement *int64
	Computed          bool
	UserDefinedType   bool
}

Column is one table or view column.

CharMaxLength, NumericPrecision and NumericScale come from INFORMATION_SCHEMA (character widths, -1 for max; nil for types without one). MaxLength, Precision and Scale are the raw sys.columns values (bytes, so nvarchar is doubled; -1 for max) that DDL generation needs; they are nil only when the sys.columns join misses. Default is the default expression and DefaultConstraint the name of the constraint holding it. IdentitySeed and IdentityIncrement are set for identity columns. Computed and UserDefinedType flag columns DDL generation cannot reproduce.

func Columns

func Columns(ctx context.Context, q Queryer, scope Scope) ([]Column, error)

Columns lists every column of every table and view in the scope, ordered by table then ordinal position. sys.columns is joined only by name for identity, raw widths and the default constraint, which avoids a per-row COLUMNPROPERTY(OBJECT_ID(...)) call at schema scope.

type ForeignKey

type ForeignKey struct {
	Table     string
	Name      string
	Column    string
	RefSchema string
	RefTable  string
	RefColumn string
	OnDelete  string
	OnUpdate  string
}

ForeignKey is one participating column of a foreign key. OnDelete and OnUpdate are the referential action descriptions (NO_ACTION, CASCADE, ...).

func ForeignKeys

func ForeignKeys(ctx context.Context, q Queryer, scope Scope) ([]ForeignKey, error)

ForeignKeys lists every foreign key column in the scope. Rows are ordered by table, constraint, then constraint_column_id; that last key is load-bearing, because callers pair local and referenced columns positionally.

type Index

type Index struct {
	Table            string
	Name             string
	Unique           bool
	Primary          bool
	UniqueConstraint bool
	KeyColumns       []string
	TypeDesc         string
	Filter           string
}

Index is one named index on a table. KeyColumns is the ordered key (included columns excluded); IndexColumns returns the full per-column detail. TypeDesc is sys.indexes.type_desc (CLUSTERED, NONCLUSTERED, ...); Filter is the filtered-index predicate, empty when unfiltered.

func Indexes

func Indexes(ctx context.Context, q Queryer, scope Scope) ([]Index, error)

Indexes lists every named index on every table in the scope, ordered by table then index name. Views are absent: the join is to sys.tables.

Key columns are aggregated with FOR XML PATH, not STRING_AGG ... WITHIN GROUP, which compatibility levels below 140 reject. The `, TYPE).value(...)` projection keeps XML metacharacters intact and yields nvarchar(max).

type IndexColumn

type IndexColumn struct {
	Table      string
	Index      string
	Column     string
	KeyOrdinal int
	Included   bool
	Descending bool
}

IndexColumn is one column of a named index. KeyOrdinal is 0 for included columns.

func IndexColumns

func IndexColumns(ctx context.Context, q Queryer, scope Scope) ([]IndexColumn, error)

IndexColumns lists the key and included columns of every named index in the scope, ordered by table, index, key columns by key ordinal, then included columns by index column id.

type Parameter

type Parameter struct {
	Routine   string
	Name      string
	DataType  string
	Position  int
	MaxLength int
	Precision int
	Scale     int
	Output    bool
}

Parameter is one declared routine parameter. A scalar function's return value (parameter_id 0) is excluded; Routine.ReturnType carries it.

func Parameters

func Parameters(ctx context.Context, q Queryer, scope Scope) ([]Parameter, error)

Parameters lists the parameters of every routine in the scope ordered by routine name then parameter id.

type Queryer

type Queryer interface {
	QueryContext(ctx context.Context, query string, args ...any) (Rows, error)
}

Queryer runs a catalog query. Callers that bound or instrument queries implement it; FromDB adapts a plain *sql.DB.

func FromDB

func FromDB(db *sql.DB) Queryer

FromDB adapts db to Queryer.

type Routine

type Routine struct {
	Schema     string
	Name       string
	Type       RoutineType
	Definition *string
	ReturnType *string
}

Routine is one stored procedure or T-SQL function. Definition is the sys.sql_modules text (nil when withheld or encrypted). ReturnType is the scalar return type for FN, "TABLE" for IF/TF, and nil for procedures.

func Routines

func Routines(ctx context.Context, q Queryer, scope Scope) ([]Routine, error)

Routines lists the procedures and T-SQL functions in the scope ordered by name.

type RoutineType

type RoutineType string

RoutineType is the sys.objects type code of a routine.

const (
	Procedure           RoutineType = "P"
	ScalarFunction      RoutineType = "FN"
	InlineTableFunction RoutineType = "IF"
	TableFunction       RoutineType = "TF"
)

func (RoutineType) IsFunction

func (t RoutineType) IsFunction() bool

IsFunction reports whether t is one of the function types.

type Rows

type Rows interface {
	Next() bool
	Scan(dest ...any) error
	Err() error
	Close() error
}

Rows is the subset of *sql.Rows the catalog reads.

type Scope

type Scope struct {
	// Schema is the owning schema; required.
	Schema string
	// Names restricts rows to these owning objects (tables for table-level
	// queries, routines for Routines and Parameters). Empty means all.
	Names []string
	// DefinitionLimit truncates object definitions server-side to this many
	// characters. Zero returns definitions in full.
	DefinitionLimit int
}

Scope selects the catalog rows a query returns.

type Table

type Table struct {
	Schema     string
	Name       string
	Type       string
	Definition *string
}

Table is one INFORMATION_SCHEMA.TABLES row. Type is the catalog's TABLE_TYPE ("BASE TABLE" or "VIEW"); Definition is the view body, empty for tables and nil when the catalog withholds it.

func Tables

func Tables(ctx context.Context, q Queryer, scope Scope, tableType TableType) ([]Table, error)

Tables lists the tables and/or views of a schema ordered by name.

type TableFeature

type TableFeature struct {
	Table   string
	Feature string
}

TableFeature is a table property DDL generation does not manage: Feature is "partitioned", or "trigger <name>" for each trigger on the table.

func UnmanagedFeatures

func UnmanagedFeatures(ctx context.Context, q Queryer, scope Scope) ([]TableFeature, error)

UnmanagedFeatures lists the partitioned tables and table triggers in the scope, ordered by table then feature.

type TableType

type TableType string

TableType selects base tables, views, or (for any other value) both, matching the inspector's "table" / "view" vocabulary.

const (
	BaseTables TableType = "table"
	Views      TableType = "view"
)

Jump to

Keyboard shortcuts

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