Help us shape the future of Go! Take the 2026 Go Developer Survey

sdb

package
v0.8.1 Latest Latest
Warning

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

Go to latest
Published: Oct 5, 2026 License: MIT Imports: 10 Imported by: 0

Documentation

Overview

Package sdb is the API surface handed to Go scripts. A script is a plain Go file (package main) defining:

func Run(s *sdb.S) error

It can query any configured connection, loop with parameters, push results into the results view, and export in any supported format.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func IsCanceled

func IsCanceled(err error) bool

IsCanceled reports whether an error from Query or Exec is the user stopping the run, as opposed to a genuine failure — so a script can unwind quietly:

if err != nil {
	if sdb.IsCanceled(err) {
		return nil
	}
	return err
}

Types

type Plan

type Plan = explain.Plan

Plan is an explained statement: its steps (Root, Nodes), what it cost or took, and dbc's findings about it (Insights). See package explain.

type PlanText

type PlanText = explain.TextOptions

PlanText shapes Plan.Text: width, the metric bars are sized by, color, and whether the findings are appended.

type Result

type Result = model.Result

Result is the tabular result type scripts receive from Query.

type S

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

S is the session handle passed to a script's Run function.

func New

func New(mgr *db.Manager, show func(*model.Result), print func(string)) *S

New builds a script session. show receives results pushed via Show; print receives Print output (results table / log pane in the TUI, stdout in headless mode).

func (*S) Canceled

func (s *S) Canceled() bool

Canceled reports whether the user has stopped this run.

func (*S) Conns

func (s *S) Conns() []string

Conns returns the configured connection names.

func (*S) Ctx

func (s *S) Ctx() context.Context

Ctx returns the session's context. Long-running scripts can select on s.Ctx().Done() (or check s.Canceled()) to bail out early.

func (*S) DB

func (s *S) DB(conn string) (*sql.DB, error)

DB exposes the raw *database/sql.DB for a connection — the escape hatch for transactions, prepared statements, or anything else database/sql can do.

func (*S) Exec

func (s *S) Exec(conn, stmt string, args ...any) (int64, error)

Exec runs a non-query statement and returns the rows affected.

func (*S) Explain

func (s *S) Explain(conn, stmt string, analyze bool) (*Plan, error)

Explain describes how conn's database runs stmt, as the TUI's Ctrl+X and `dbc explain` do. analyze runs the statement to measure it: on Postgres a write is analyzed inside a transaction that is rolled back; on the other engines a write is explained but not run. It runs on its own session, not one the script shares with Query.

p, err := s.Explain("pg", "SELECT * FROM orders WHERE user_id = 42", true)
s.Print("%s", p.Text(sdb.PlanText{Insights: true}))
for _, in := range p.Insights { if in.Severity != "info" { … } }

func (*S) Export

func (s *S) Export(r *Result, format, path string) error

Export renders a result in the named format (csv|tsv|markdown|html|json|text) and writes it to path — or to the system clipboard when path is empty.

func (*S) Print

func (s *S) Print(format string, args ...any)

Print writes a Printf-style message to the script output.

func (*S) Query

func (s *S) Query(conn, query string, args ...any) (*Result, error)

Query runs a statement with parameters on the named connection and returns the result set. Use the placeholder style of the target driver ($1 for postgres/bytdb, ? for mysql/sqlite).

func (*S) Show

func (s *S) Show(r *Result)

Show pushes a result to the results view (TUI) or prints it (headless).

func (*S) WithContext

func (s *S) WithContext(ctx context.Context) *S

WithContext attaches a cancellation context to the session and returns it. The host — not the script — sets this: when the user stops a run, the query in flight is aborted and every later Query or Exec fails immediately, so a looping script unwinds through its own error handling.

Jump to

Keyboard shortcuts

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