inspector

package
v0.1.36 Latest Latest
Warning

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

Go to latest
Published: Sep 11, 2026 License: Apache-2.0 Imports: 5 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Bounds

type Bounds struct {
	Rows            int
	DefinitionBytes int
	Truncated       bool
}

Bounds is owned by one inspection. Rows caps each query; DefinitionBytes is consumed across queries and schemas. Oversized definitions are omitted rather than exposing incomplete SQL as a usable object definition.

type Column

type Column struct {
	TableName        string
	ColumnName       string
	DataType         string
	IsNullable       bool
	DefaultValue     *string
	Position         int
	IsAutoIncrement  bool
	IsPrimaryKey     bool
	IsUnique         bool
	MaxLength        *int // For varchar, char
	NumericScale     *int // For decimal, numeric
	NumericPrecision *int // For decimal, numeric
	Comment          *string
	EnumValues       []string // For enum types
}

Column represents a table column

type ForeignKey

type ForeignKey struct {
	TableName      string
	ColumnName     string
	RefSchemaName  string
	RefTableName   string
	RefColumnName  string
	ConstraintName string
}

ForeignKey represents a foreign key constraint

type Index

type Index struct {
	TableName string
	IndexName string
	IsUnique  bool
	IsPrimary bool
	Columns   []string // Ordered list of column names
	Type      string   // btree, hash, gin, gist, etc.
	Condition string   // WHERE clause for partial indexes
}

Index represents a table index

type Inspector

type Inspector interface {
	// Schema operations
	GetSchemas(ctx context.Context) ([]string, error)
	GetDefaultSchema(ctx context.Context) (string, error)
	GetDatabaseName(ctx context.Context) (string, error)

	// Table operations
	GetTables(ctx context.Context, schema string, tableType string) ([]*Table, error)
	GetColumns(ctx context.Context, schema string) ([]*Column, error)
	GetIndexes(ctx context.Context, schema string) ([]*Index, error)
	GetForeignKeys(ctx context.Context, schema string) ([]*ForeignKey, error)

	// Stored procedure operations
	GetStoredProcs(ctx context.Context, schema string) ([]*StoredProc, error)
	GetProcParams(ctx context.Context, schema string) ([]*ProcParam, error)

	// Trigger operations. Dialects without trigger support return (nil, nil);
	// only absence-of-feature is signalled by the nil slice, errors are real
	// introspection failures.
	GetTriggers(ctx context.Context, schema string) ([]*Trigger, error)
}

Inspector provides database introspection capabilities.

Every catalogue method is schema-scoped: it returns a bounded set of rows in one round trip, and each row carries the name of the object that owns it (Column.TableName, Index.TableName, ForeignKey.TableName, ProcParam.ProcName). Callers group; dialects do not. Asking per object turned a schema dump into thousands of round trips for no extra information.

Ordering is part of the contract, not an accident: every method returns rows ordered by owner and then by the object's own natural order (ordinal position, index name, constraint column position, parameter id). Consumers cache and compare catalogs, so row ordering must be deterministic.

func NewInspector

func NewInspector(db *sql.DB, driver string, bounds *Bounds) (Inspector, error)

NewInspector creates an inspector for the given database driver

type PostgresInspector

type PostgresInspector struct {
	// contains filtered or unexported fields
}

PostgresInspector provides PostgreSQL database introspection

func (*PostgresInspector) GetColumns

func (i *PostgresInspector) GetColumns(ctx context.Context, schema string) ([]*Column, error)

GetColumns returns every column of every table and view in the schema

func (*PostgresInspector) GetDatabaseName

func (i *PostgresInspector) GetDatabaseName(ctx context.Context) (string, error)

GetDatabaseName returns the current database name

func (*PostgresInspector) GetDefaultSchema

func (i *PostgresInspector) GetDefaultSchema(ctx context.Context) (string, error)

GetDefaultSchema returns the default schema for the current user

func (*PostgresInspector) GetForeignKeys

func (i *PostgresInspector) GetForeignKeys(ctx context.Context, schema string) ([]*ForeignKey, error)

GetForeignKeys returns every foreign key column in the schema.

func (*PostgresInspector) GetIndexes

func (i *PostgresInspector) GetIndexes(ctx context.Context, schema string) ([]*Index, error)

GetIndexes returns every index on every relation in the schema.

func (*PostgresInspector) GetProcParams

func (i *PostgresInspector) GetProcParams(ctx context.Context, schema string) ([]*ProcParam, error)

GetProcParams returns the parameters of every routine in the schema.

func (*PostgresInspector) GetSchemas

func (i *PostgresInspector) GetSchemas(ctx context.Context) ([]string, error)

GetSchemas returns all non-system schemas in the database

func (*PostgresInspector) GetStoredProcs

func (i *PostgresInspector) GetStoredProcs(ctx context.Context, schema string) ([]*StoredProc, error)

GetStoredProcs returns all stored procedures and functions in the specified schema

func (*PostgresInspector) GetTables

func (i *PostgresInspector) GetTables(ctx context.Context, schema string, tableType string) ([]*Table, error)

GetTables returns all tables or views in the specified schema

func (*PostgresInspector) GetTriggers

func (i *PostgresInspector) GetTriggers(ctx context.Context, schema string) ([]*Trigger, error)

GetTriggers returns triggers in the schema. Not yet implemented for Postgres — returns nil, nil so the extractor can degrade gracefully. TODO: triggers — MSSQL-only for now.

type ProcParam

type ProcParam struct {
	ProcID    string
	ProcName  string
	ParamName string
	DataType  string
	Position  int
}

ProcParam represents a stored procedure parameter

type SQLServerInspector

type SQLServerInspector struct {
	// contains filtered or unexported fields
}

SQLServerInspector provides SQL Server database introspection

func (*SQLServerInspector) GetColumns

func (i *SQLServerInspector) GetColumns(ctx context.Context, schema string) ([]*Column, error)

GetColumns returns every column of every table and view in the schema.

Beyond name/type/nullability it reads the facts a consumer cannot reconstruct afterwards: the character width and numeric precision, whether the column is part of the primary key, and whether the database generates its value. Those come from the same catalogue in the same round trip, so collecting them costs nothing extra and not collecting them forced every caller to ask again.

The width/precision/scale columns deliberately come from INFORMATION_SCHEMA rather than sys.columns: sys.columns.max_length is in *bytes* (nvarchar is 2x), reports 0 instead of NULL for non-character types, and disagrees on text/ntext. The browser needs character widths rather than storage sizes.

sys.columns is joined only for is_identity, which replaces a per-row COLUMNPROPERTY(OBJECT_ID(...)) scalar call — harmless at table scope, tens of thousands of calls in a single statement at schema scope.

func (*SQLServerInspector) GetDatabaseName

func (i *SQLServerInspector) GetDatabaseName(ctx context.Context) (string, error)

GetDatabaseName returns the current database name

func (*SQLServerInspector) GetDefaultSchema

func (i *SQLServerInspector) GetDefaultSchema(ctx context.Context) (string, error)

GetDefaultSchema returns the default schema for the current user

func (*SQLServerInspector) GetForeignKeys

func (i *SQLServerInspector) GetForeignKeys(ctx context.Context, schema string) ([]*ForeignKey, error)

GetForeignKeys returns every foreign key column in the schema, one row per participating column.

The ordering by fkc.constraint_column_id is load-bearing, not tidiness: the caller pairs a constraint's local and referenced columns positionally, so rows arriving in an arbitrary order emit a composite key whose columns are matched to the wrong targets. constraint_column_id is that position.

func (*SQLServerInspector) GetIndexes

func (i *SQLServerInspector) GetIndexes(ctx context.Context, schema string) ([]*Index, error)

GetIndexes returns every named index on every table in the schema.

Included columns are excluded because Columns describes the ordered key. Views are absent by construction: the join is to sys.tables.

func (*SQLServerInspector) GetProcParams

func (i *SQLServerInspector) GetProcParams(ctx context.Context, schema string) ([]*ProcParam, error)

GetProcParams returns the parameters of every stored procedure and function in the schema.

parameter_id > 0 excludes a scalar function's return value, which sys.parameters reports as an unnamed parameter at id 0. Emitting it as Params[0] produced a parameter with an empty name that also duplicated what GetStoredProcs already reports as ReturnType.

The object-type filter mirrors GetStoredProcs so only objects that can be emitted are queried. It is not a narrowing: object names are unique per schema in SQL Server, so the old `o.name = @p2` predicate matched the same single row.

func (*SQLServerInspector) GetSchemas

func (i *SQLServerInspector) GetSchemas(ctx context.Context) ([]string, error)

GetSchemas returns all non-system schemas in the database

func (*SQLServerInspector) GetStoredProcs

func (i *SQLServerInspector) GetStoredProcs(ctx context.Context, schema string) ([]*StoredProc, error)

GetStoredProcs returns all stored procedures and functions in the specified schema

func (*SQLServerInspector) GetTables

func (i *SQLServerInspector) GetTables(ctx context.Context, schema string, tableType string) ([]*Table, error)

GetTables returns all tables or views in the specified schema

func (*SQLServerInspector) GetTriggers

func (i *SQLServerInspector) GetTriggers(ctx context.Context, schema string) ([]*Trigger, error)

GetTriggers returns all DML triggers in the given schema, grouped by (schema, name). A single trigger firing on multiple events (e.g. INSERT+UPDATE) is returned as one row with a comma-joined Event string.

type StoredProc

type StoredProc struct {
	ID         string
	Schema     string
	Name       string
	Type       string // "procedure" or "function"
	SQL        string // Complete SQL body/definition
	ReturnType string // For functions: the SQL return type (e.g. "int", "TABLE"); empty for procedures
}

StoredProc represents a stored procedure or function

type Table

type Table struct {
	Schema  string
	Name    string
	Type    string // "table" or "view"
	ViewDef string // SQL definition for views
}

Table represents a database table or view

type Trigger

type Trigger struct {
	Schema     string
	Name       string
	TableName  string
	Event      string
	Timing     string
	IsDisabled bool
	SQL        string // CREATE TRIGGER body from OBJECT_DEFINITION
}

Trigger represents a database trigger attached to a table.

Event is one or more trigger events, comma-joined when a single trigger fires on multiple DML actions (e.g. "INSERT,UPDATE"). Timing is "AFTER" or "INSTEAD OF" for SQL Server, which does not support BEFORE triggers.

Jump to

Keyboard shortcuts

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