sqlinspect

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: 7 Imported by: 0

Documentation

Overview

Package sqlinspect provides reusable, read-only SQL catalog inspection.

Index

Constants

View Source
const (
	DefaultMaxRelations       = 5000
	DefaultMaxColumns         = 50000
	DefaultMaxRoutines        = 2000
	DefaultMaxDefinitionBytes = 4 * 1024 * 1024
)

Variables

This section is empty.

Functions

func ListDatabases

func ListDatabases(ctx context.Context, db *sql.DB, driver string) ([]string, error)

ListDatabases returns databases accessible to the current connection user.

func NewMemoryCache added in v0.1.35

func NewMemoryCache() *inspection.Memo[Catalog]

NewMemoryCache creates an isolated cache with the SQL freshness and capacity policy. Reuse it across inspections; nil Options.Cache uses the shared default. Like all Memos, it registers with inspection.Stats and inspection.Flush.

Types

type Cache added in v0.1.35

type Cache interface {
	Get(context.Context, inspection.GetOptions[Catalog]) (inspection.Result[Catalog], error)
}

Cache owns catalogue storage and fill coordination. Implementations must honor GetOptions.Refresh and inspection.WithRefresh, coalesce concurrent fills, and return cache metadata, including stale values alongside refresh errors. Load runs locally; a remote implementation stores the Catalog, not the callback. *inspection.Memo[Catalog] implements this interface.

type Catalog

type Catalog struct {
	Driver         string                    `json:"driver"`
	Database       string                    `json:"database,omitempty"`
	Databases      []string                  `json:"databases,omitempty"`
	DefaultSchema  string                    `json:"defaultSchema,omitempty"`
	Schemas        []Schema                  `json:"schemas"`
	Truncated      bool                      `json:"truncated,omitempty"`
	TruncateReason string                    `json:"truncateReason,omitempty"`
	Cache          *inspection.CacheMetadata `json:"cache,omitempty"`
}

func Inspect

func Inspect(ctx context.Context, db *sql.DB, driver string, limits Limits, options Options) (Catalog, error)

Inspect returns a cached catalogue of the current database, with freshness metadata. Pass either a caller-owned db or Options.Open and identify its connection, authorization context, revision, and selected database in CacheKey. Keep a caller-owned db open while background refreshes may use it. Returned slices and nested pointers are shared cache values and must be treated as read-only. A failed explicit refresh returns the last known catalogue alongside its error.

type Column

type Column struct {
	Name             string   `json:"name"`
	DataType         string   `json:"dataType,omitempty"`
	Ordinal          int      `json:"ordinal,omitempty"`
	Nullable         *bool    `json:"nullable,omitempty"`
	Default          *string  `json:"default,omitempty"`
	Identity         bool     `json:"identity,omitempty"`
	PrimaryKey       bool     `json:"primaryKey,omitempty"`
	Unique           bool     `json:"unique,omitempty"`
	MaxLength        *int     `json:"maxLength,omitempty"`
	NumericPrecision *int     `json:"numericPrecision,omitempty"`
	NumericScale     *int     `json:"numericScale,omitempty"`
	Comment          *string  `json:"comment,omitempty"`
	EnumValues       []string `json:"enumValues,omitempty"`
}

type ForeignKey added in v0.1.32

type ForeignKey struct {
	Name              string   `json:"name"`
	Columns           []string `json:"columns"`
	ReferencedSchema  string   `json:"referencedSchema,omitempty"`
	ReferencedTable   string   `json:"referencedTable"`
	ReferencedColumns []string `json:"referencedColumns"`
}

type Index added in v0.1.32

type Index struct {
	Name    string   `json:"name"`
	Unique  bool     `json:"unique,omitempty"`
	Primary bool     `json:"primary,omitempty"`
	Columns []string `json:"columns"`
	Type    string   `json:"type,omitempty"`
	Filter  string   `json:"filter,omitempty"`
}

type Limits

type Limits struct {
	MaxRelations       int
	MaxColumns         int
	MaxRoutines        int
	MaxDefinitionBytes int
}

type Options added in v0.1.35

type Options struct {
	// CacheKey must isolate the connection, authorization/context scope, selected
	// database, and connection revision. Change it when credentials or connection
	// settings change. Use an opaque identity, never credentials or a raw DSN.
	// Driver and effective limits are appended by Inspect.
	CacheKey string
	// Cache replaces the default process-lifetime memory cache; it is not layered
	// with it. A shared backend needs CacheKey values stable across processes.
	Cache   Cache
	Refresh bool
	// Open is used instead of db for short-lived connections. It is called only
	// on a fill, with the fill context, and Inspect closes the returned pool.
	Open func(context.Context) (*sql.DB, error)
}

Options identifies the source independently of transient SQL pool handles.

type Relation

type Relation struct {
	Name        string       `json:"name"`
	Type        string       `json:"type"`
	ViewDef     string       `json:"viewDefinition,omitempty"`
	Columns     []Column     `json:"columns"`
	Indexes     []Index      `json:"indexes,omitempty"`
	ForeignKeys []ForeignKey `json:"foreignKeys,omitempty"`
	Triggers    []Trigger    `json:"triggers,omitempty"`
}

type Routine added in v0.1.32

type Routine struct {
	ID         string             `json:"id"`
	Name       string             `json:"name"`
	Type       string             `json:"type"`
	SQL        string             `json:"sql,omitempty"`
	ReturnType string             `json:"returnType,omitempty"`
	Parameters []RoutineParameter `json:"parameters,omitempty"`
}

type RoutineParameter added in v0.1.32

type RoutineParameter struct {
	Name     string `json:"name"`
	DataType string `json:"dataType,omitempty"`
	Ordinal  int    `json:"ordinal,omitempty"`
}

type Schema

type Schema struct {
	Name      string     `json:"name"`
	Relations []Relation `json:"relations"`
	Routines  []Routine  `json:"routines,omitempty"`
}

type Trigger added in v0.1.32

type Trigger struct {
	Name     string `json:"name"`
	Event    string `json:"event,omitempty"`
	Timing   string `json:"timing,omitempty"`
	Disabled bool   `json:"disabled,omitempty"`
	SQL      string `json:"sql,omitempty"`
}

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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