Documentation ¶
Overview ¶
Package handler provides a input process handler implementation for usql.
Index ¶
- func WrapErr(buf string, err error) error
- type Error
- type Handler
- func (h *Handler) Begin(txOpts *sql.TxOptions) error
- func (h *Handler) BeginTx(ctx context.Context, txOpts *sql.TxOptions) error
- func (h *Handler) Buf() *stmt.Stmt
- func (h *Handler) ChangePassword(user string) (string, error)
- func (h *Handler) Close() error
- func (h *Handler) Commit() error
- func (h *Handler) DB() drivers.DB
- func (h *Handler) Execute(ctx context.Context, w io.Writer, opt metacmd.Option, prefix, sqlstr string, ...) error
- func (h *Handler) GetOutput() io.Writer
- func (h *Handler) GetTiming() bool
- func (h *Handler) Highlight(w io.Writer, buf string) error
- func (h *Handler) IO() rline.IO
- func (h *Handler) Include(path string, relative bool) error
- func (h *Handler) Last() string
- func (h *Handler) LastRaw() string
- func (h *Handler) MetadataWriter(ctx context.Context) (metadata.Writer, error)
- func (h *Handler) Open(ctx context.Context, params ...string) error
- func (h *Handler) Password(dsn string) (string, error)
- func (h *Handler) Print(format string, a ...interface{})
- func (h *Handler) Prompt(prompt string) string
- func (h *Handler) ReadVar(typ, prompt string) (string, error)
- func (h *Handler) Reset(r []rune)
- func (h *Handler) Rollback() error
- func (h *Handler) Run() error
- func (h *Handler) SetOutput(o io.WriteCloser)
- func (h *Handler) SetSingleLineMode(singleLineMode bool)
- func (h *Handler) SetTiming(timing bool)
- func (h *Handler) URL() *dburl.URL
- func (h *Handler) User() *user.User
- func (h *Handler) Version(ctx context.Context) error
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
Types ¶
type Error ¶
Error wraps handler errors
type Handler ¶
type Handler struct {
// contains filtered or unexported fields
}
Handler is a input process handler.
Glues together usql's components to provide a "read-eval-print loop" (REPL) for usql's interactive command-line and manages most of the core/high-level logic.
Manages the active statement buffer, application IO, executing/querying SQL statements, and handles backslash (\) commands encountered in the input stream.
func (*Handler) ChangePassword ¶ added in v0.5.0
ChangePassword changes a password for the user.
func (*Handler) Execute ¶
func (h *Handler) Execute(ctx context.Context, w io.Writer, opt metacmd.Option, prefix, sqlstr string, forceTrans bool) error
Execute executes a query against the connected database.
func (*Handler) Highlight ¶ added in v0.6.0
Highlight highlights using the current environment settings.
func (*Handler) LastRaw ¶ added in v0.7.0
LastRaw returns the last raw (non-interpolated) executed statement.
func (*Handler) MetadataWriter ¶ added in v0.9.0
MetadataWriter loads the metadata writer for the
func (*Handler) Open ¶
Open handles opening a specified database URL, passing either a single string in the form of a URL, or more than one string, in which case the first string is treated as a driver name, and the remaining strings are joined (with a space) and passed as a DSN to sql.Open.
If there is only one parameter, and it is not a well formatted URL, but appears to be a file on disk, then an attempt will be made to open it with an appropriate driver (mysql, postgres, sqlite3) depending on the type (unix domain socket, directory, or regular file, respectively).
func (*Handler) Password ¶ added in v0.4.0
Password collects a password from input, and returns a modified DSN including the collected password.
func (*Handler) Print ¶ added in v0.9.0
Print formats according to a format specifier and writes to handler's standard output.
func (*Handler) Prompt ¶ added in v0.4.0
Prompt parses a prompt.
NOTE: the documentation below is INCORRECT, as it is just copied from https://www.postgresql.org/docs/current/app-psql.html#APP-PSQL-PROMPTING
TODO/FIXME: complete this functionality (from psql documentation):
%M - The full host name (with domain name) of the database server, or [local] if the connection is over a Unix domain socket, or [local:/dir/name], if the Unix domain socket is not at the compiled in default location. %m - The host name of the database server, truncated at the first dot, or [local] if the connection is over a Unix domain socket. %> - The port number at which the database server is listening. %n - The database session user name. (The expansion of this value might change during a database session as the result of the command SET SESSION AUTHORIZATION.) %/ - The name of the current database. %~ - Like %/, but the output is ~ (tilde) if the database is your default database. %# - If the session user is a database superuser, then a #, otherwise a >. (The expansion of this value might change during a database session as the result of the command SET SESSION AUTHORIZATION.) %p - The process ID of the backend currently connected to. %R - In prompt 1 normally =, but @ if the session is in an inactive branch of a conditional block, or ^ if in single-line mode, or ! if the session is disconnected from the database (which can happen if \connect fails). In prompt 2 %R is replaced by a character that depends on why psql expects more input: - if the command simply wasn't terminated yet, but * if there is an unfinished /* ... */ comment, a single quote if there is an unfinished quoted string, a double quote if there is an unfinished quoted identifier, a dollar sign if there is an unfinished dollar-quoted string, or ( if there is an unmatched left parenthesis. In prompt 3 %R doesn't produce anything. %x - Transaction status: an empty string when not in a transaction block, or * when in a transaction block, or ! when in a failed transaction block, or ? when the transaction state is indeterminate (for example, because there is no connection). %l - The line number inside the current statement, starting from 1. %digits - The character with the indicated octal code is substituted. %:name: - The value of the psql variable name. See Variables, above, for details. %`command` - The output of command, similar to ordinary “back-tick” substitution. %[ ... %] - Prompts can contain terminal control characters which, for example, change the color, background, or style of the prompt text, or change the title of the terminal window. In order for the line editing features of Readline to work properly, these non-printing control characters must be designated as invisible by surrounding them with %[ and %]. Multiple pairs of these can occur within the prompt. For example: testdb=> \set PROMPT1 '%[%033[1;33;40m%]%n@%/%R%[%033[0m%]%# ' results in a boldfaced (1;) yellow-on-black (33;40) prompt on VT100-compatible, color-capable terminals. %w - Whitespace of the same width as the most recent output of PROMPT1. This can be used as a PROMPT2 setting, so that multi-line statements are aligned with the first line, but there is no visible secondary prompt.
To insert a percent sign into your prompt, write %%. The default prompts are '%/%R%x%# ' for prompts 1 and 2, and '>> ' for prompt 3.
func (*Handler) ReadVar ¶ added in v0.5.0
ReadVar reads a variable from the interactive prompt, saving it to environment variables.
func (*Handler) SetOutput ¶ added in v0.9.0
func (h *Handler) SetOutput(o io.WriteCloser)
SetOutput sets the output writer.
func (*Handler) SetSingleLineMode ¶ added in v0.7.1
SetSingleLineMode sets the single line mode toggle.