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
- func ExecuteStream(ctx context.Context, db Execer, r io.Reader, onStatement func(count int)) (int, error)
- func InteractiveShell(ctx context.Context, db Conn, format Format) error
- func Run(ctx context.Context, db Conn, input string, out io.Writer, format Format) error
- func RunStatement(ctx context.Context, db Conn, stmt string, out io.Writer, format Format) error
- func Shell(ctx context.Context, db Conn, in io.Reader, out, errOut io.Writer, ...) error
- func SplitStatements(input string) ([]string, string)
- func SplitStatementsWithDelimiter(input, delimiter string) ([]string, string, string)
- type Conn
- type Execer
- type Format
Constants ¶
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 ¶
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 RunStatement ¶
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 ¶
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 ¶
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 ¶
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.
func ParseFormat ¶
ParseFormat validates a format name given on the command line.