stmt

package
v0.6.0 Latest Latest
Warning

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

Go to latest
Published: Jan 14, 2018 License: MIT Imports: 4 Imported by: 0

Documentation

Index

Constants

View Source
const (
	// MinCapIncrease is the minimum amount by which to grow a Stmt.Buf.
	MinCapIncrease = 512
)

Variables

This section is empty.

Functions

func AllowCComments added in v0.6.0

func AllowCComments(enable bool) func(*Stmt)

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

func AllowDollar

func AllowDollar(enable bool) func(*Stmt)

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

func AllowHashComments added in v0.6.0

func AllowHashComments(enable bool) func(*Stmt)

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

func AllowMultilineComments

func AllowMultilineComments(enable bool) func(*Stmt)

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

func FindPrefix added in v0.5.0

func FindPrefix(s string, n int) string

FindPrefix finds the prefix in s up to n words.

func StartsWith

func StartsWith(r []rune, i, end int, s string) bool

StartsWith determines if r starts with s, ignoring case, and skipping initial whitespace and returning -1 if r does not start with s.

Note: assumes s contains at least one non space.

Types

type Option

type Option func(*Stmt)

Option is a statement buffer option.

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.

Append 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() (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()
    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) 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 added in v0.5.0

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

	// Q is the quote character used if the variable was quoted, 0 otherwise.
	Q rune

	// N is the actual variable name excluding ':' and any enclosing quote
	// characters.
	N string

	// Len is the length of the replaced variable.
	Len int
}

Var holds information about a variable.

Jump to

Keyboard shortcuts

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