sqlshell

package
v0.0.0-...-2028bbf Latest Latest
Warning

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

Go to latest
Published: Sep 7, 2026 License: MIT Imports: 14 Imported by: 0

Documentation

Overview

Package sqlshell implements a small SQL shell used by `project sql` and `project restore`: splitting scripts into statements, executing them and rendering results.

Index

Constants

View Source
const DefaultDelimiter = ";"

DefaultDelimiter is the statement terminator used unless a DELIMITER directive changes it.

Variables

This section is empty.

Functions

func ExecuteStream

func ExecuteStream(ctx context.Context, db Execer, r io.Reader, onStatement func(count int)) (int, error)

ExecuteStream reads SQL statements from r and executes them one by one, without rendering results. It is meant for restoring dumps, where the input can be far larger than memory. onStatement, when non-nil, is called after every executed statement with the running count. The number of executed statements is returned, also when an error aborts the run.

func InteractiveShell

func InteractiveShell(ctx context.Context, db Conn, format Format) error

InteractiveShell runs the read-eval-print loop as an inline bubbletea program, providing real line editing (word deletion via alt+backspace, ctrl+w and alt+d, cursor movement) and session history on the arrow keys. Results are printed into the normal terminal scrollback.

func Run

func Run(ctx context.Context, db Conn, input string, out io.Writer, format Format) error

Run executes all statements in input against db and renders the results to out.

func RunStatement

func RunStatement(ctx context.Context, db Conn, stmt string, out io.Writer, format Format) error

RunStatement executes a single statement and renders its result to out. Comment-only statements and stray DELIMITER directives (client commands, not SQL) are skipped.

func Shell

func Shell(ctx context.Context, db Conn, in io.Reader, out, errOut io.Writer, format Format) error

Shell runs an interactive read-eval-print loop until EOF or an exit command (exit, quit, \q). Statement errors are printed and do not end the session.

func SplitStatements

func SplitStatements(input string) ([]string, string)

SplitStatements splits input into complete SQL statements and returns the trailing incomplete remainder. It starts with the default ";" delimiter; use SplitStatementsWithDelimiter to carry delimiter state across calls.

func SplitStatementsWithDelimiter

func SplitStatementsWithDelimiter(input, delimiter string) ([]string, string, string)

SplitStatementsWithDelimiter splits input into complete SQL statements, terminated by delimiter, and returns the trailing incomplete remainder plus the delimiter active after the input. Terminators inside single-quoted, double-quoted or backtick-quoted sections, line comments (-- , #) and block comments (/* */) do not end a statement.

DELIMITER directives (a client feature used by dumps around triggers and routines) are consumed, not returned as statements: they change the active delimiter from their line onwards.

Only leading whitespace is trimmed from the remainder: its trailing whitespace is significant when more input is appended later (streaming).

Types

type Conn

type Conn interface {
	Execer
	QueryContext(ctx context.Context, query string, args ...any) (*sql.Rows, error)
}

Conn is the database handle the shell operates on. It is satisfied by *sql.Conn (preferred, since session state like SET or USE sticks to a single connection) and *sql.DB.

type Execer

type Execer interface {
	ExecContext(ctx context.Context, query string, args ...any) (sql.Result, error)
}

Execer executes statements that do not return rows.

type Format

type Format string

Format controls how result sets are rendered.

const (
	// FormatTable renders mysql-client style ASCII tables.
	FormatTable Format = "table"
	// FormatTSV renders tab-separated values with a header line.
	FormatTSV Format = "tsv"
	// FormatJSON renders each result set as a JSON array of row objects.
	FormatJSON Format = "json"
)

func ParseFormat

func ParseFormat(name string) (Format, error)

ParseFormat validates a format name given on the command line.

Jump to

Keyboard shortcuts

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