catalog

package
v0.6.3 Latest Latest
Warning

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

Go to latest
Published: Aug 5, 2026 License: MIT Imports: 9 Imported by: 0

Documentation

Overview

Package catalog reads schema metadata from the server.

Every query here runs on the connection's control connection, so browsing the schema stays responsive while a large result is still streaming.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func ColumnKey

func ColumnKey(schema, table string) string

ColumnKey builds the map key Snapshot.Columns uses.

func DefaultCachePath

func DefaultCachePath() (string, error)

DefaultCachePath returns the on-disk location, honouring XDG_STATE_HOME.

func QuoteIdentifier

func QuoteIdentifier(name string) string

QuoteIdentifier renders a name as a backtick-quoted MySQL identifier.

func Schemas

func Schemas(ctx context.Context, conn *db.Conn) ([]string, error)

Schemas lists the user-visible databases on the server.

func TableDDL

func TableDDL(ctx context.Context, conn *db.Conn, schema, table string, isView bool) (string, error)

TableDDL returns the CREATE statement for a table or view.

It runs on the control connection, alongside the other catalog reads, and deliberately bypasses guard: the statement is read-only and its text is built here rather than typed by the user.

Types

type Cache

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

Cache stores schema metadata locally.

Completion reads from here rather than from information_schema: on a server with tens of thousands of tables that query takes hundreds of milliseconds, which is far too slow to run on a keystroke.

func OpenCache

func OpenCache(path string) (*Cache, error)

OpenCache opens or creates the cache at path.

func (*Cache) Close

func (c *Cache) Close() error

Close releases the database handle.

func (*Cache) Columns

func (c *Cache) Columns(ctx context.Context, datasource, schema, table string) ([]Column, error)

Columns lists the cached columns of a table, in declaration order.

func (*Cache) Names

func (c *Cache) Names() *NameSource

Names returns a completion-friendly view of the cache.

func (*Cache) Save

func (c *Cache) Save(ctx context.Context, datasource string, snap Snapshot) error

Save replaces everything stored for a datasource.

Replacing rather than merging is deliberate: a table dropped on the server has to stop being offered, and a merge would keep suggesting it forever. The whole write is one transaction, so a failure part-way leaves the previous snapshot intact rather than a half-updated one.

func (*Cache) Schemas

func (c *Cache) Schemas(ctx context.Context, datasource string) ([]string, error)

Schemas lists the cached schema names for a datasource.

func (*Cache) SearchTables

func (c *Cache) SearchTables(ctx context.Context, datasource, needle string, limit int) ([]QualifiedTable, error)

SearchTables finds tables whose name contains the given text, across every schema of a datasource. It backs the go-to-table jump.

func (*Cache) Tables

func (c *Cache) Tables(ctx context.Context, datasource, schema string) ([]Table, error)

Tables lists the cached tables of a schema.

type Column

type Column struct {
	Name         string
	Type         string
	Nullable     bool
	IsPrimaryKey bool
	Default      string
	Comment      string
}

Column is one column of a table.

func Columns

func Columns(ctx context.Context, conn *db.Conn, schema, table string) ([]Column, error)

Columns lists a table's columns in declaration order, which is also the order "SELECT *" returns them in.

type NameSource

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

NameSource adapts the cache to the name-only lookup the completion engine needs.

The engine deals in identifiers, not in schema detail, so this is the whole surface it sees — which is what keeps the engine testable with a map.

func (*NameSource) Columns

func (n *NameSource) Columns(ctx context.Context, datasource, schema, table string) ([]string, error)

func (*NameSource) Schemas

func (n *NameSource) Schemas(ctx context.Context, datasource string) ([]string, error)

func (*NameSource) Tables

func (n *NameSource) Tables(ctx context.Context, datasource, schema string) ([]string, error)

type QualifiedTable

type QualifiedTable struct {
	Schema string
	Name   string
	IsView bool
}

QualifiedTable is a table together with the schema holding it.

type Snapshot

type Snapshot struct {
	Schemas []string
	Tables  map[string][]Table
	Columns map[string][]Column
}

Snapshot is a datasource's schema as read from the server.

Tables are keyed by schema name; columns by "schema.table". Maps keep Save a single call rather than an interface the caller has to drive.

func FetchSnapshot

func FetchSnapshot(ctx context.Context, conn *db.Conn) (Snapshot, error)

FetchSnapshot reads a datasource's whole schema from the server.

Columns are fetched one query per schema rather than one per table. A server with a few thousand tables would otherwise need a few thousand round trips — minutes through a bastion — and the point of the cache is that completion is ready shortly after connecting, not eventually.

type Table

type Table struct {
	Name   string
	IsView bool
	// Rows is the optimiser's estimate, not an exact count. Counting for
	// real would mean a full scan per table on every expand.
	Rows int64
}

Table is one table or view in a schema.

func Tables

func Tables(ctx context.Context, conn *db.Conn, schema string) ([]Table, error)

Tables lists the tables and views in a schema.

Jump to

Keyboard shortcuts

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