Documentation ¶
Index ¶
Constants ¶
const (
// MinCapIncrease is the minimum amount by which to grow a Stmt.Buf.
MinCapIncrease = 512
)
Variables ¶
This section is empty.
Functions ¶
func AllowDollar ¶
AllowDollar is a statement buffer option to set allowing dollar strings (ie, $$text$$ or $tag$text$tag$).
func AllowMultilineComments ¶
AllowMultilineComments is a statement buffer option to set allowing multiline comments (ie, /* ... */).
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 // 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).