Documentation ¶
Index ¶
- Constants
- func AllowCComments(enable bool) func(*Stmt)
- func AllowDollar(enable bool) func(*Stmt)
- func AllowHashComments(enable bool) func(*Stmt)
- func AllowMultilineComments(enable bool) func(*Stmt)
- func FindPrefix(s string, n int) string
- func StartsWith(r []rune, i, end int, s string) bool
- type Option
- type Stmt
- type Var
Constants ¶
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
AllowCComments is a statement buffer option to set allowing C-style comments (ie, // ...).
func AllowDollar ¶
AllowDollar is a statement buffer option to set allowing dollar strings (ie, $$text$$ or $tag$text$tag$).
func AllowHashComments ¶ added in v0.6.0
AllowHashComments is a statement buffer option to set allowing hash comments (ie, # ...).
func AllowMultilineComments ¶
AllowMultilineComments is a statement buffer option to set allowing multiline comments (ie, /* ... */).
func FindPrefix ¶ added in v0.5.0
FindPrefix finds the prefix in s up to n words.
Types ¶
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.
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 ¶
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() 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 ¶
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 // 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.