stmt

package
v0.1.0 Latest Latest
Warning

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

Go to latest
Published: Mar 21, 2024 License: MIT Imports: 4 Imported by: 0

Documentation

Overview

Package stmt contains a statement buffer implementation.

Index

Constants

View Source
const MinCapIncrease = 512

MinCapIncrease is the minimum amount by which to grow a Stmt.Buf.

Variables

This section is empty.

Functions

func FindPrefix

func FindPrefix(s string, allowCComments, allowHashComments, allowMultilineComments bool) string

FindPrefix finds the first 6 prefix words in s.

func IsSpaceOrControl

func IsSpaceOrControl(r rune) bool

IsSpaceOrControl is a special test for either a space or a control (ie, \b) characters.

func RunesLastIndex

func RunesLastIndex(r []rune, needle rune) int

RunesLastIndex returns the last index in r of needle, or -1 if not found.

Types

type Option

type Option func(*Stmt)

Option is a statement buffer option.

func WithAllowCComments

func WithAllowCComments(enable bool) Option

WithAllowCComments is a statement buffer option to set allowing C-style comments (ie, // ...).

func WithAllowDollar

func WithAllowDollar(enable bool) Option

WithAllowDollar is a statement buffer option to set allowing dollar strings (ie, $$text$$ or $tag$text$tag$).

func WithAllowHashComments

func WithAllowHashComments(enable bool) Option

WithAllowHashComments is a statement buffer option to set allowing hash comments (ie, # ...).

func WithAllowMultilineComments

func WithAllowMultilineComments(enable bool) Option

WithAllowMultilineComments is a statement buffer option to set allowing multiline comments (ie, /* ... */).

type Params

type Params struct {
	R   []rune
	Len int
}

Params holds information about command parameters.

func DecodeParams

func DecodeParams(params string) *Params

DecodeParams decodes command parameters.

func (*Params) Get

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

Get reads the next command parameter using the provided substitution func. True indicates there are runes remaining in the command parameters to process.

func (*Params) GetAll

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

GetAll retrieves all remaining command parameters using the provided substitution func. Will return on the first encountered error.

func (*Params) GetRaw

func (p *Params) GetRaw() string

GetRaw reads all remaining runes. No substitution or whitespace removal is performed.

type Stmt

type Stmt struct {

	// Buf is the statement buffer
	Buf []rune
	// Len is the current len of any statement in Buf.
	Len int
	// Prefix is the detected prefix of the statement.
	Prefix string
	// Vars is the list of encountered variables.
	Vars []*Var
	// contains filtered or unexported fields
}

Stmt is a reusable statement buffer that handles reading and parsing SQL-like statements.

func New

func New(f func() ([]rune, error), opts ...Option) *Stmt

New creates a new Stmt using the supplied rune source f.

func (*Stmt) Append

func (b *Stmt) Append(r, sep []rune)

Append appends r to b.Buf separated by sep when b.Buf is not already empty.

Dynamically grows b.Buf as necessary to accommodate r and the separator. Specifically, when b.Buf is not empty, b.Buf will grow by increments of MinCapIncrease.

After a call to Append, b.Len will be len(b.Buf)+len(sep)+len(r). Call Reset to reset the Buf.

func (*Stmt) AppendString

func (b *Stmt) AppendString(s, sep string)

AppendString is a util func wrapping Append.

func (*Stmt) Next

func (b *Stmt) Next(unquote func(string, bool) (bool, string, error)) (string, string, error)

Next reads the next statement from the rune source, returning when either the statement has been terminated, or a meta command has been read from the rune source. After a call to Next, the collected statement is available in Stmt.Buf, or call Stmt.String() to convert it to a string.

After a call to Next, Reset should be called if the extracted statement was executed (ie, processed). Note that the rune source supplied to New will be called again only after any remaining collected runes have been processed.

Example:

buf := stmt.New(runeSrc)
for {
    cmd, params, err := buf.Next(unquoteFunc)
    if err { /* ... */ }

    execute, quit := buf.Ready() || cmd == "g", cmd == "q"

    // process command ...
    switch cmd {
        /* ... */
    }

    if quit {
        break
    }

    if execute {
       s := buf.String()
       res, err := db.Query(s)
       /* handle database ... */
       buf.Reset(nil)
    }
}

func (*Stmt) RawString

func (b *Stmt) RawString() string

RawString returns the non-interpolated version of the statement buffer.

func (*Stmt) Ready

func (b *Stmt) Ready() bool

Ready returns true when the statement buffer contains a non empty, balanced statement that has been properly terminated (ie, ended with a semicolon).

func (*Stmt) Reset

func (b *Stmt) Reset(r []rune)

Reset resets the statement buffer.

func (*Stmt) State

func (b *Stmt) State() string

State returns a string representing the state of statement parsing.

func (*Stmt) String

func (b *Stmt) String() string

String satisfies fmt.Stringer.

type Var

type Var struct {
	// I is where the variable starts (ie, ':') in Stmt.Buf.
	I int
	// End is where the variable ends in Stmt.Buf.
	End int
	// Quote is the quote character used if the variable was quoted, 0
	// otherwise.
	Quote rune
	// Name is the actual variable name excluding ':' and any enclosing quote
	// characters.
	Name string
	// Len is the length of the replaced variable.
	Len int
	// Defined indicates whether the variable has been defined.
	Defined bool
}

Var holds information about a variable.

func (*Var) String

func (v *Var) String() string

String satisfies the fmt.Stringer interface.

Jump to

Keyboard shortcuts

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