metacmd

package
v0.18.1 Latest Latest
Warning

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

Go to latest
Published: Mar 28, 2024 License: MIT Imports: 20 Imported by: 3

Documentation

Overview

Package metacmd contains meta information and implentation for usql's backslash (\) commands.

Index

Constants

This section is empty.

Variables

SectionOrder is the order of sections to display via Listing.

Functions

func Listing

func Listing(w io.Writer)

Listing writes the formatted command listing to w, separated into different sections for all known commands.

Types

type Cmd

type Cmd struct {
	Section Section
	Name    string
	Desc    Desc
	Aliases map[string]Desc
	Process func(*Params) error
}

Cmd is a command implementation.

type Desc added in v0.12.0

type Desc struct {
	Desc   string
	Params string
}

Desc holds information about a command or alias description.

type ExecType

type ExecType int

ExecType represents the type of execution requested.

const (
	// ExecNone indicates no execution.
	ExecNone ExecType = iota
	// ExecOnly indicates plain execution only (\g).
	ExecOnly
	// ExecPipe indicates execution and piping results (\g |file)
	ExecPipe
	// ExecSet indicates execution and setting the resulting columns as
	// variables (\gset).
	ExecSet
	// ExecExec indicates execution and executing the resulting rows (\gexec).
	ExecExec
	// ExecCrosstab indicates execution using crosstabview (\crosstabview).
	ExecCrosstab
	// ExecWatch indicates repeated execution with a fixed time interval.
	ExecWatch
)

type Handler

type Handler interface {
	// IO handles the handler's IO.
	IO() rline.IO
	// User returns the current user.
	User() *user.User
	// URL returns the current database URL.
	URL() *dburl.URL
	// DB returns the current database connection.
	DB() drivers.DB
	// Last returns the last executed query.
	Last() string
	// LastRaw returns the last raw (non-interpolated) query.
	LastRaw() string
	// Buf returns the current query buffer.
	Buf() *stmt.Stmt
	// Reset resets the last and current query buffer.
	Reset([]rune)
	// Bind binds query parameters.
	Bind([]interface{})
	// Open opens a database connection.
	Open(context.Context, ...string) error
	// Close closes the current database connection.
	Close() error
	// ChangePassword changes the password for a user.
	ChangePassword(string) (string, error)
	// ReadVar reads a variable of a specified type.
	ReadVar(string, string) (string, error)
	// Include includes a file.
	Include(string, bool) error
	// Begin begins a transaction.
	Begin(*sql.TxOptions) error
	// Commit commits the current transaction.
	Commit() error
	// Rollback aborts the current transaction.
	Rollback() error
	// Highlight highlights the statement.
	Highlight(io.Writer, string) error
	// GetTiming mode.
	GetTiming() bool
	// SetTiming mode.
	SetTiming(bool)
	// GetOutput writer.
	GetOutput() io.Writer
	// SetOutput writer.
	SetOutput(io.WriteCloser)
	// MetadataWriter retrieves the metadata writer for the handler.
	MetadataWriter(context.Context) (metadata.Writer, error)
	// Print formats according to a format specifier and writes to handler's standard output.
	Print(string, ...interface{})
}

Handler is the shared interface for a command handler.

type Metacmd

type Metacmd uint

Metacmd represents a command and associated meta information about it.

const (
	// None is an empty command.
	None Metacmd = iota
	// Question is question meta command (\?)
	Question
	// Quit is the quit meta command (\?).
	Quit
	// Copyright is the copyright meta command (\copyright).
	Copyright
	// Connect is the connect meta command (\c, \connect).
	Connect
	// Copy is the copy meta command (\copy).
	Copy
	// Disconnect is the disconnect meta command (\Z).
	Disconnect
	// Password is the change password meta command (\password).
	Password
	// ConnectionInfo is the connection info meta command (\conninfo).
	ConnectionInfo
	// Drivers is the driver info meta command (\drivers).
	Drivers
	// Describe is the describe meta command (\d and variants).
	Describe
	// Bind is the bind meta command (\bind).
	Bind
	// Exec is the execute meta command (\g and variants).
	Exec
	// Edit is the edit query buffer meta command (\e).
	Edit
	// Print is the print query buffer meta command (\p, \print, \raw).
	Print
	// Reset is the reset query buffer meta command (\r, \reset).
	Reset
	// Echo is the echo meta command (\echo, \warn, \qecho).
	Echo
	// Write is the write meta command (\w).
	Write
	// ChangeDir is the system change directory meta command (\cd).
	ChangeDir
	// SetEnv is the system set environment variable meta command (\setenv).
	SetEnv
	// GetEnv is the system get environment variable meta command (\getenv).
	GetEnv
	// Shell is the system shell exec meta command (\!).
	Shell
	// Out is the switch output meta command (\o).
	Out
	// Include is the system include file meta command (\i and variants).
	Include
	// Transact is the transaction meta command (\begin, \commit, \rollback).
	Transact
	// Prompt is the variable prompt meta command (\prompt).
	Prompt
	// SetVar is the set variable meta command (\set).
	SetVar
	// Unset is the variable unset meta command (\unset).
	Unset
	// SetFormatVar is the set format variable meta commands (\pset, \a, \C, \f, \H, \t, \T, \x).
	SetFormatVar
	// Timing is the timing meta command (\timing).
	Timing
	// Stats is the show stats meta command (\ss and variants).
	Stats
)

Command types.

type Option added in v0.9.0

type Option struct {
	// Quit instructs the handling code to quit.
	Quit bool
	// Exec informs the handling code of the type of execution.
	Exec ExecType
	// Params are accompanying string parameters for execution.
	Params map[string]string
	// Crosstab are the crosstab column parameters.
	Crosstab []string
	// Watch is the watch duration interval.
	Watch time.Duration
}

Option contains parsed result options of a metacmd.

func (*Option) ParseParams added in v0.9.0

func (opt *Option) ParseParams(params []string, defaultKey string) error

type Params added in v0.5.0

type Params struct {
	// Handler is the process handler.
	Handler Handler
	// Name is the name of the metacmd.
	Name string
	// Params are the actual statement parameters.
	Params *stmt.Params
	// Option contains resulting command execution options.
	Option Option
}

Params wraps metacmd parameters.

func (*Params) Get added in v0.7.1

func (p *Params) Get(exec bool) (string, error)

Get returns the next command parameter, using env.Unquote to decode quoted strings.

func (*Params) GetAll added in v0.7.1

func (p *Params) GetAll(exec bool) ([]string, error)

GetAll gets all remaining command parameters using env.Unquote to decode quoted strings.

func (*Params) GetOK added in v0.8.2

func (p *Params) GetOK(exec bool) (bool, string, error)

GetOK returns the next command parameter, using env.Unquote to decode quoted strings.

func (*Params) GetOptional added in v0.7.1

func (p *Params) GetOptional(exec bool) (bool, string, error)

GetOptional returns the next command parameter, using env.Unquote to decode quoted strings, returns true when the value is prefixed with a "-", along with the value sans the "-" prefix. Otherwise returns false and the value.

func (*Params) GetRaw added in v0.8.2

func (p *Params) GetRaw() string

GetRaw gets the remaining command parameters as a raw string.

Note: no other processing is done to interpolate variables or to decode string values.

type Runner

type Runner interface {
	Run(Handler) (Option, error)
}

Runner is a runner interface type.

func Decode

func Decode(name string, params *stmt.Params) (Runner, error)

Decode converts a command name (or alias) into a Runner.

type RunnerFunc

type RunnerFunc func(Handler) (Option, error)

RunnerFunc is a type wrapper for a single func satisfying Runner.Run.

func (RunnerFunc) Run

func (f RunnerFunc) Run(h Handler) (Option, error)

Run satisfies the Runner interface.

type Section

type Section string

Section is a meta command section.

const (
	SectionGeneral         Section = "General"
	SectionQueryExecute    Section = "Query Execute"
	SectionQueryBuffer     Section = "Query Buffer"
	SectionHelp            Section = "Help"
	SectionTransaction     Section = "Transaction"
	SectionInputOutput     Section = "Input/Output"
	SectionInformational   Section = "Informational"
	SectionFormatting      Section = "Formatting"
	SectionConnection      Section = "Connection"
	SectionOperatingSystem Section = "Operating System"
	SectionVariables       Section = "Variables"
)

Meta command section types.

func (Section) String

func (s Section) String() string

String satisfies stringer.

Jump to

Keyboard shortcuts

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