handler

package
v0.0.0-...-90e67ca Latest Latest
Warning

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

Go to latest
Published: Nov 27, 2026 License: GPL-3.0 Imports: 40 Imported by: ? modules, 0 packages

The first number is the count of modules that contain a package that imports this one. The module count is a better indicator of popularity than the package count. A module with 100 importing packages is counted only once instead of 100 times, reflecting the fact that the choice to use the package was likely made only once, not 100 times.
A '?' means that the value hasn't been computed yet.

Documentation

Overview

Package handler provides a input process handler implementation for usql.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func WrapErr

func WrapErr(buf string, err error) error

WrapErr wraps an [error] using the specified driver when err is not nil.

Types

type Error

type Error struct {
	Buf string
	Err error
}

Error wraps handler errors.

func (*Error) Error

func (e *Error) Error() string

Error satisfies the [error] interface, returning the original error message.

func (*Error) Unwrap

func (e *Error) Unwrap() error

Unwrap returns the original error.

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 New

func New(l rline.IO, user *user.User, wd string, nopw bool) *Handler

New creates a new input handler.

func (*Handler) Begin

func (h *Handler) Begin(txOpts *sql.TxOptions) error

Begin begins a transaction.

func (*Handler) BeginTx

func (h *Handler) BeginTx(ctx context.Context, txOpts *sql.TxOptions) error

Begin begins a transaction in a context.

func (*Handler) Bind

func (h *Handler) Bind(bind []interface{})

Bind sets the bind parameters for the next query execution.

func (*Handler) Buf

func (h *Handler) Buf() *stmt.Stmt

Buf returns the current query statement buffer.

func (*Handler) ChangePassword

func (h *Handler) ChangePassword(user string) (string, error)

ChangePassword changes a password for the user.

func (*Handler) Close

func (h *Handler) Close() error

Close closes the database connection if it is open.

func (*Handler) Commit

func (h *Handler) Commit() error

Commit commits a transaction.

func (*Handler) DB

func (h *Handler) DB() drivers.DB

DB returns the active transaction, dedicated session, or connection pool.

func (*Handler) DiagnosticError

func (h *Handler) DiagnosticError(err error) error

func (*Handler) Execute

func (h *Handler) Execute(ctx context.Context, w io.Writer, opt metacmd.Option, prefix, sqlstr string, forceTrans bool, bind ...interface{}) error

Execute executes a query against the connected database.

func (*Handler) GetOutput

func (h *Handler) GetOutput() io.Writer

GetOutput gets the output writer.

func (*Handler) GetTiming

func (h *Handler) GetTiming() bool

GetTiming gets the timing toggle.

func (*Handler) Highlight

func (h *Handler) Highlight(w io.Writer, buf string) error

Highlight highlights using the current environment settings.

func (*Handler) IO

func (h *Handler) IO() rline.IO

IO returns the io for the handler.

func (*Handler) Include

func (h *Handler) Include(path string, relative bool) error

Include includes the specified path.

func (*Handler) Last

func (h *Handler) Last() string

Last returns the last executed statement.

func (*Handler) LastRaw

func (h *Handler) LastRaw() string

LastRaw returns the last raw (non-interpolated) executed statement.

func (*Handler) MaskDiagnostics

func (h *Handler) MaskDiagnostics()

MaskDiagnostics applies to complete error writes inside usql, before the terminal combines them with command echo, prompts and query results.

func (*Handler) MetadataWriter

func (h *Handler) MetadataWriter(ctx context.Context) (metadata.Writer, error)

MetadataWriter loads the metadata writer for the

func (*Handler) Open

func (h *Handler) Open(ctx context.Context, params ...string) error

func (*Handler) Password

func (h *Handler) Password(dsn string) (string, error)

Password collects a password from input, and returns a modified DSN including the collected password.

func (*Handler) Print

func (h *Handler) Print(format string, a ...interface{})

Print formats according to a format specifier and writes to handler's standard output.

func (*Handler) Prompt

func (h *Handler) Prompt(prompt string) string

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

func (h *Handler) ReadVar(typ, prompt string) (string, error)

ReadVar reads a variable from the interactive prompt, saving it to environment variables.

func (*Handler) Reset

func (h *Handler) Reset(r []rune)

Reset resets the handler's query statement buffer.

func (*Handler) Rollback

func (h *Handler) Rollback() error

Rollback rollbacks a transaction.

func (*Handler) Run

func (h *Handler) Run() error

Run executes queries and commands.

func (*Handler) SetMaskPolicy

func (h *Handler) SetMaskPolicy(policy masking.Policy)

SetMaskPolicy binds a compiled policy to this handler, not to the process.

func (*Handler) SetOutput

func (h *Handler) SetOutput(o io.WriteCloser)

SetOutput sets the output writer.

func (*Handler) SetSingleLineMode

func (h *Handler) SetSingleLineMode(singleLineMode bool)

SetSingleLineMode sets the single line mode toggle.

func (*Handler) SetTiming

func (h *Handler) SetTiming(timing bool)

SetTiming sets the timing toggle.

func (*Handler) URL

func (h *Handler) URL() *dburl.URL

URL returns the URL for the handler.

func (*Handler) User

func (h *Handler) User() *user.User

User returns the user for the handler.

func (*Handler) Version

func (h *Handler) Version(ctx context.Context) error

Version prints the database version information after a successful connection.

type WarpRows

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

WarpRows 是对 sql.Rows 的包装,支持按列索引脱敏

func NewWarpRows

func NewWarpRows(rows *sql.Rows, maskIndexes []int) *WarpRows

NewWarpRows 构造函数

func (*WarpRows) Close

func (w *WarpRows) Close() error

Close 代理

func (*WarpRows) ColumnTypes

func (w *WarpRows) ColumnTypes() ([]*sql.ColumnType, error)

ColumnTypes 代理

func (*WarpRows) Columns

func (w *WarpRows) Columns() ([]string, error)

Columns 代理

func (*WarpRows) Err

func (w *WarpRows) Err() error

Err 代理

func (*WarpRows) Next

func (w *WarpRows) Next() bool

Next 代理

func (*WarpRows) NextResultSet

func (w *WarpRows) NextResultSet() bool

NextResultSet 代理

func (*WarpRows) Scan

func (w *WarpRows) Scan(dest ...interface{}) error

Scan 对行进行读取并按规则脱敏

Jump to

Keyboard shortcuts

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