db

package
v1.0.6 Latest Latest
Warning

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

Go to latest
Published: Mar 20, 2026 License: MIT Imports: 7 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

View Source
var ErrUnsupported = errors.New("operation not supported by this provider")

ErrUnsupported is returned when a provider doesn't support an operation.

View Source
var ValidCanonicalTypes = []string{
	"string", "text", "integer", "bigint", "float", "decimal",
	"boolean", "timestamp", "date", "json", "blob", "uuid", "serial",
	"vector", "tsvector",
}

ValidCanonicalTypes lists all accepted canonical types.

Functions

func IsValidCanonicalType added in v1.0.5

func IsValidCanonicalType(t string) bool

IsValidCanonicalType checks if a type string is a known canonical type.

func MapCanonicalType added in v1.0.5

func MapCanonicalType(canonical string, provider ProviderKind) string

MapCanonicalType converts a canonical column type to the native type for the given provider.

func RegisterNonSQLProviderFactory added in v1.0.5

func RegisterNonSQLProviderFactory(driver string, factory NonSQLProviderFactory)

RegisterNonSQLProviderFactory registers a factory for a non-SQL driver.

func RegisterProviderFactory added in v1.0.5

func RegisterProviderFactory(driver string, factory ProviderFactory)

RegisterProviderFactory registers a factory for the given driver name. This is called from provider init() functions to avoid import cycles.

Types

type AppliedMigration added in v1.0.5

type AppliedMigration struct {
	Version   string `json:"version"`
	Name      string `json:"name"`
	AppliedAt string `json:"applied_at"`
}

AppliedMigration records a migration that has been applied.

type ColumnDef added in v1.0.5

type ColumnDef struct {
	Name          string `json:"name"`
	Type          string `json:"type"` // Canonical: string, text, integer, bigint, float, decimal, boolean, timestamp, date, json, blob, uuid, serial
	Nullable      bool   `json:"nullable"`
	Default       string `json:"default,omitempty"`
	PrimaryKey    bool   `json:"primary_key,omitempty"`
	AutoIncrement bool   `json:"auto_increment,omitempty"`
	Unique        bool   `json:"unique,omitempty"`
	References    string `json:"references,omitempty"` // "table(column)" for FK
}

ColumnDef describes a column/field for create/alter table operations.

type ColumnInfo added in v1.0.5

type ColumnInfo struct {
	Name       string `json:"name"`
	Type       string `json:"type"`
	Nullable   string `json:"nullable"`
	Default    string `json:"default"`
	PrimaryKey bool   `json:"primary_key"`
	Extra      string `json:"extra,omitempty"`
}

ColumnInfo describes an existing column/field.

type Connection

type Connection struct {
	ID       string
	Driver   string
	DSN      string
	DB       *sql.DB
	Provider Provider
}

Connection represents an active database connection.

type ConnectionInfo

type ConnectionInfo struct {
	ID     string `json:"id"`
	Driver string `json:"driver"`
	DSN    string `json:"dsn"`
}

ConnectionInfo is a read-only summary of a connection (no *sql.DB exposed).

type ConstraintInfo added in v1.0.5

type ConstraintInfo struct {
	Name       string   `json:"name"`
	Type       string   `json:"type"` // "primary_key", "foreign_key", "unique", "check"
	Columns    []string `json:"columns"`
	RefTable   string   `json:"ref_table,omitempty"`
	RefColumns []string `json:"ref_columns,omitempty"`
	Definition string   `json:"definition,omitempty"`
}

ConstraintInfo describes a table constraint.

type DbStats added in v1.0.5

type DbStats struct {
	SizeBytes  int64          `json:"size_bytes"`
	TableCount int            `json:"table_count"`
	IndexCount int            `json:"index_count"`
	Provider   string         `json:"provider"`
	Version    string         `json:"version,omitempty"`
	Extra      map[string]any `json:"extra,omitempty"`
}

DbStats holds database-level statistics.

type IndexDef added in v1.0.5

type IndexDef struct {
	Name    string   `json:"name"`
	Columns []string `json:"columns"`
	Unique  bool     `json:"unique"`
}

IndexDef describes an index to create.

type IndexInfo added in v1.0.5

type IndexInfo struct {
	Name    string   `json:"name"`
	Columns []string `json:"columns"`
	Unique  bool     `json:"unique"`
}

IndexInfo describes an existing index.

type Manager

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

Manager manages multiple database connections.

func NewManager

func NewManager() *Manager

NewManager creates a new Manager with an empty connection map.

func (*Manager) Connect

func (m *Manager) Connect(driver, dsn string) (string, error)

Connect opens a new database connection with the given driver and DSN. Supported drivers: "postgres", "sqlite3", "sqlite", "mysql", "redis". Returns the connection ID on success.

func (*Manager) Disconnect

func (m *Manager) Disconnect(id string) error

Disconnect closes and removes the connection with the given ID.

func (*Manager) Exec

func (m *Manager) Exec(id, query string, args ...any) (int64, error)

Exec executes a non-SELECT statement (INSERT, UPDATE, DELETE, etc.) on the given connection and returns the number of rows affected. Delegates to the connection's Provider.

func (*Manager) Get

func (m *Manager) Get(id string) (*Connection, error)

Get returns the connection with the given ID or an error if not found.

func (*Manager) GetProvider added in v1.0.5

func (m *Manager) GetProvider(id string) (Provider, error)

GetProvider returns the Provider for the connection with the given ID.

func (*Manager) List

func (m *Manager) List() []ConnectionInfo

List returns info about all active connections.

func (*Manager) Query

func (m *Manager) Query(id, query string, args ...any) ([]map[string]any, error)

Query executes a SELECT query on the given connection and returns rows as a slice of maps. Delegates to the connection's Provider.

type MigrationProvider added in v1.0.5

type MigrationProvider interface {
	EnsureMigrationTable(ctx context.Context) error
	AppliedMigrations(ctx context.Context) ([]AppliedMigration, error)
	RecordMigration(ctx context.Context, version, name string) error
	RemoveMigration(ctx context.Context, version string) error
}

MigrationProvider is optionally implemented by SQL providers for migration tracking.

type NonSQLProviderFactory added in v1.0.5

type NonSQLProviderFactory func(dsn string) (Provider, error)

NonSQLProviderFactory creates a Provider directly from a DSN string. Used for non-SQL databases (e.g. Redis) that don't use database/sql.

type Provider added in v1.0.5

type Provider interface {
	Kind() ProviderKind
	Close() error
	Ping(ctx context.Context) error

	// Query/Exec
	Query(ctx context.Context, query string, args ...any) ([]map[string]any, error)
	Exec(ctx context.Context, query string, args ...any) (int64, error)

	// Schema Inspection
	ListTables(ctx context.Context, schema string) ([]TableInfo, error)
	DescribeTable(ctx context.Context, table string) ([]ColumnInfo, error)
	ListIndexes(ctx context.Context, table string) ([]IndexInfo, error)
	ListConstraints(ctx context.Context, table string) ([]ConstraintInfo, error)
	ListViews(ctx context.Context, schema string) ([]ViewInfo, error)
	TableSize(ctx context.Context, table string) (*TableStats, error)
	DatabaseStats(ctx context.Context) (*DbStats, error)

	// DDL
	CreateTable(ctx context.Context, name string, columns []ColumnDef, ifNotExists bool) error
	AlterTableAdd(ctx context.Context, table string, column ColumnDef) error
	AlterTableDrop(ctx context.Context, table string, columnName string) error
	AlterTableRename(ctx context.Context, table, oldCol, newCol string) error
	DropTable(ctx context.Context, name string, ifExists bool) error
	CreateIndex(ctx context.Context, table string, index IndexDef) error
	DropIndex(ctx context.Context, table, indexName string) error
	CreateView(ctx context.Context, view ViewDef) error
	DropView(ctx context.Context, name string) error
}

Provider is the core abstraction for all database operations.

type ProviderFactory added in v1.0.5

type ProviderFactory func(sqlDB *sql.DB) Provider

ProviderFactory creates a Provider from a *sql.DB connection.

type ProviderKind added in v1.0.5

type ProviderKind string

ProviderKind identifies the database provider type.

const (
	ProviderSQLite    ProviderKind = "sqlite"
	ProviderPostgres  ProviderKind = "postgres"
	ProviderMySQL     ProviderKind = "mysql"
	ProviderMongoDB   ProviderKind = "mongodb"
	ProviderRedis     ProviderKind = "redis"
	ProviderFirestore ProviderKind = "firestore"
)

type TableInfo added in v1.0.5

type TableInfo struct {
	Name string `json:"name"`
}

TableInfo is a summary of a table/collection.

type TableStats added in v1.0.5

type TableStats struct {
	RowCount  int64 `json:"row_count"`
	SizeBytes int64 `json:"size_bytes"`
	IndexSize int64 `json:"index_size_bytes,omitempty"`
	TotalSize int64 `json:"total_size_bytes,omitempty"`
}

TableStats holds size/row statistics for a table.

type ViewDef added in v1.0.5

type ViewDef struct {
	Name       string `json:"name"`
	Definition string `json:"definition"`
}

ViewDef describes a view to create.

type ViewInfo added in v1.0.5

type ViewInfo struct {
	Name       string `json:"name"`
	Definition string `json:"definition"`
}

ViewInfo describes an existing view.

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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