db

package
v0.1.2 Latest Latest
Warning

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

Go to latest
Published: May 27, 2026 License: MIT Imports: 28 Imported by: 0

Documentation

Overview

Package db opens database connections and executes SQL against supported drivers.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func DSN

func DSN(conn Connection) (string, error)

DSN returns an explicit DSN or builds one from structured connection fields using driver-specific defaults.

func DeleteRows

func DeleteRows(ctx context.Context, cfg Config, tableName, whereClause string) (int, error)

DeleteRows deletes rows matching whereClause.

func DumpTable

func DumpTable(ctx context.Context, cfg Config, tableName string, opts ExecuteOptions, w io.Writer, format string) (output.Result, error)

DumpTable streams all rows from tableName to w using the requested output format.

func Execute

func Execute(ctx context.Context, cfg Config, sqlText string, opts ExecuteOptions) (output.Result, error)

Execute runs each parsed statement in sqlText and returns the result from the last statement. Row-returning statements are scanned into output.Result, while write statements report affected row count.

func ExecuteToWriter

func ExecuteToWriter(ctx context.Context, cfg Config, sqlText string, opts ExecuteOptions, w io.Writer, format string) (output.Result, error)

ExecuteToWriter runs SQL and streams the last row-returning statement directly to w. Earlier statements are executed for side effects only, matching Execute's "last statement is the result" behavior.

func InsertRow

func InsertRow(ctx context.Context, cfg Config, tableName string, values map[string]string) (int, error)

InsertRow inserts one row into tableName.

func Open

func Open(ctx context.Context, cfg Config) (*sql.DB, string, error)

Open validates cfg, opens the matching database driver, and verifies the connection with PingContext. The returned driver is the normalized driver name.

func Schemas

func Schemas(ctx context.Context, cfg Config) ([]string, error)

Schemas returns schema/database names when the driver exposes them.

func UpdateRows

func UpdateRows(ctx context.Context, cfg Config, tableName string, values map[string]string, whereClause string) (int, error)

UpdateRows updates rows matching whereClause.

Types

type ColumnInfo

type ColumnInfo struct {
	Name       string `json:"name"`
	Type       string `json:"type"`
	Nullable   bool   `json:"nullable"`
	Primary    bool   `json:"primary"`
	Unique     bool   `json:"unique,omitempty"`
	Default    string `json:"default,omitempty"`
	References string `json:"references,omitempty"`
}

ColumnInfo describes one column and common relational constraints.

type Config

type Config struct {
	Driver string
	DSN    string
	Schema string
}

Config contains the normalized driver name and DSN needed to open a database.

type Connection

type Connection struct {
	Driver   string
	Host     string
	Port     int
	Database string
	User     string
	Password string
	SSLMode  string
	DSN      string
}

Connection is the user-facing connection model used to construct DSNs.

type ExecuteOptions

type ExecuteOptions struct {
	MaxRows     int
	Explain     bool
	Analyze     bool
	Transaction bool
}

ExecuteOptions controls optional execution behavior for a SQL request.

type GrantInfo

type GrantInfo struct {
	Grantee   string `json:"grantee"`
	Object    string `json:"object"`
	Privilege string `json:"privilege"`
	Grantable bool   `json:"grantable"`
	Raw       string `json:"raw,omitempty"`
}

GrantInfo describes a database privilege grant.

func Grants

func Grants(ctx context.Context, cfg Config, role string) ([]GrantInfo, error)

Grants returns privilege grants. Empty role means current user where the driver supports that directly.

type ImportResult

type ImportResult struct {
	RowsAffected int `json:"rows_affected"`
}

ImportResult summarizes a table import operation.

func LoadCSV

func LoadCSV(ctx context.Context, cfg Config, tableName string, r io.Reader) (ImportResult, error)

LoadCSV inserts CSV rows into tableName. The first CSV record is treated as a header containing destination column names.

func LoadJSON

func LoadJSON(ctx context.Context, cfg Config, tableName string, r io.Reader) (ImportResult, error)

LoadJSON inserts rows from either output.Result JSON or an array of objects.

func LoadJSONL

func LoadJSONL(ctx context.Context, cfg Config, tableName string, r io.Reader) (ImportResult, error)

LoadJSONL inserts newline-delimited JSON objects.

func LoadYAML

func LoadYAML(ctx context.Context, cfg Config, tableName string, r io.Reader) (ImportResult, error)

LoadYAML inserts rows from either output.Result YAML or an array of objects.

type IndexInfo

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

IndexInfo describes one table index.

type Migration

type Migration struct {
	Version        string `json:"version"`
	Name           string `json:"name"`
	Path           string `json:"path"`
	DownPath       string `json:"down_path,omitempty"`
	Applied        bool   `json:"applied"`
	AppliedAt      string `json:"applied_at,omitempty"`
	Checksum       string `json:"checksum,omitempty"`
	StoredChecksum string `json:"stored_checksum,omitempty"`
	Dirty          bool   `json:"dirty,omitempty"`
}

Migration describes one SQL migration file.

func MigrationStatus

func MigrationStatus(ctx context.Context, cfg Config, dir string) ([]Migration, error)

MigrationStatus returns SQL files in dir and whether they are already recorded in the sqio_migrations table.

type MigrationPlan

type MigrationPlan struct {
	Pending  []Migration `json:"pending"`
	Applied  []Migration `json:"applied"`
	Rollback []Migration `json:"rollback"`
}

MigrationPlan describes pending apply and rollback candidates.

func PlanMigrations

func PlanMigrations(ctx context.Context, cfg Config, dir string, rollbackLimit int) (MigrationPlan, error)

PlanMigrations returns pending apply and rollback candidates without changing the database.

type MigrationResult

type MigrationResult struct {
	Applied []Migration `json:"applied"`
}

MigrationResult summarizes an apply run.

func ApplyMigrations

func ApplyMigrations(ctx context.Context, cfg Config, dir string, limit int) (MigrationResult, error)

ApplyMigrations applies pending SQL migration files in filename order. Each file is executed in its own transaction.

func BaselineMigrations

func BaselineMigrations(ctx context.Context, cfg Config, dir, version string) (MigrationResult, error)

BaselineMigrations records migrations up to version as applied without executing their SQL.

func RollbackMigrations

func RollbackMigrations(ctx context.Context, cfg Config, dir string, limit int) (MigrationResult, error)

RollbackMigrations rolls back the most recently applied migrations that have matching .down.sql files.

type RoleInfo

type RoleInfo struct {
	Name       string `json:"name"`
	Host       string `json:"host,omitempty"`
	Login      bool   `json:"login"`
	Superuser  bool   `json:"superuser,omitempty"`
	CreateRole bool   `json:"create_role,omitempty"`
	CreateDB   bool   `json:"create_db,omitempty"`
}

RoleInfo describes a database role or user.

func Roles

func Roles(ctx context.Context, cfg Config) ([]RoleInfo, error)

Roles returns role/user metadata for the configured database.

type SchemaInfo

type SchemaInfo struct {
	Tables []TableInfo `json:"tables"`
}

SchemaInfo is database metadata collected from a live connection.

func Metadata

func Metadata(ctx context.Context, cfg Config) (SchemaInfo, error)

Metadata opens cfg and dispatches to the dialect-specific metadata reader.

type TableInfo

type TableInfo struct {
	Schema  string       `json:"schema,omitempty"`
	Name    string       `json:"name"`
	Columns []ColumnInfo `json:"columns"`
	Indexes []IndexInfo  `json:"indexes,omitempty"`
	DDL     string       `json:"ddl"`
}

TableInfo describes one table or view and its reconstructed DDL when available.

Jump to

Keyboard shortcuts

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