You cannot select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
38 lines
785 B
Go
38 lines
785 B
Go
package command
|
|
|
|
import (
|
|
"strings"
|
|
|
|
"github.com/rqlite/sql"
|
|
)
|
|
|
|
// Rewrite rewrites the statements such that RANDOM is rewritten,
|
|
// if r is true.
|
|
func Rewrite(stmts []*Statement, r bool) error {
|
|
if !r {
|
|
return nil
|
|
}
|
|
|
|
rw := &sql.Rewriter{
|
|
RewriteRand: r,
|
|
}
|
|
|
|
for i := range stmts {
|
|
// Only replace the incoming statement with a rewritten version if
|
|
// there was no error, or if the rewriter did anything. If the statement
|
|
// is bad SQLite syntax, let SQLite deal with it -- and let its error
|
|
// be returned. Those errors will probably be clearer.
|
|
s, err := sql.NewParser(strings.NewReader(stmts[i].Sql)).ParseStatement()
|
|
if err != nil {
|
|
continue
|
|
}
|
|
s, f, err := rw.Do(s)
|
|
if err != nil || !f {
|
|
continue
|
|
}
|
|
|
|
stmts[i].Sql = s.String()
|
|
}
|
|
return nil
|
|
}
|