Documentation ¶
Overview ¶
Package stmt contains a statement buffer implementation.
Index ¶
- Constants
- func FindPrefix(s string, allowCComments, allowHashComments, allowMultilineComments bool) string
- func IsSpaceOrControl(r rune) bool
- func RunesLastIndex(r []rune, needle rune) int
- type Option
- type Params
- type Stmt
- func (b *Stmt) Append(r, sep []rune)
- func (b *Stmt) AppendString(s, sep string)
- func (b *Stmt) Next(unquote func(string, bool) (bool, string, error)) (string, string, error)
- func (b *Stmt) RawString() string
- func (b *Stmt) Ready() bool
- func (b *Stmt) Reset(r []rune)
- func (b *Stmt) State() string
- func (b *Stmt) String() string
- type Var
Constants ¶
const MinCapIncrease = 512
MinCapIncrease is the minimum amount by which to grow a Stmt.Buf.
Variables ¶
This section is empty.
Functions ¶
func FindPrefix ¶ added in v0.5.0
FindPrefix finds the first 6 prefix words in s.
func IsSpaceOrControl ¶ added in v0.8.2
IsSpaceOrControl is a special test for either a space or a control (ie, \b) characters.
func RunesLastIndex ¶ added in v0.8.2
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 ¶ added in v0.8.2
WithAllowCComments is a statement buffer option to set allowing C-style comments (ie, // ...).
func WithAllowDollar ¶ added in v0.8.2
WithAllowDollar is a statement buffer option to set allowing dollar strings (ie, $$text$$ or $tag$text$tag$).
func WithAllowHashComments ¶ added in v0.8.2
WithAllowHashComments is a statement buffer option to set allowing hash comments (ie, # ...).
func WithAllowMultilineComments ¶ added in v0.8.2
WithAllowMultilineComments is a statement buffer option to set allowing multiline comments (ie, /* ... */).
type Params ¶ added in v0.8.2
Params holds information about command parameters.
func DecodeParams ¶ added in v0.8.2
DecodeParams decodes command parameters.
func (*Params) Get ¶ added in v0.8.2
Get reads the next command parameter using the provided substitution func. True indicates there are runes remaining in the command parameters to process.
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 (*Stmt) Append ¶
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 ¶
AppendString is a util func wrapping Append.
func (*Stmt) Next ¶
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 ¶ added in v0.7.0
RawString returns the non-interpolated version of the statement buffer.
func (*Stmt) Ready ¶
Ready returns true when the statement buffer contains a non empty, balanced statement that has been properly terminated (ie, ended with a semicolon).
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 // 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.