add SeqFilter

This commit is contained in:
Lunny Xiao 2014-04-11 23:32:10 +08:00
parent dbcb6fe173
commit 3dd33af2d6
1 changed files with 23 additions and 1 deletions

View File

@ -1,6 +1,9 @@
package core
import "strings"
import (
"fmt"
"strings"
)
// Filter is an interface to filter SQL
type Filter interface {
@ -40,3 +43,22 @@ func (i *IdFilter) Do(sql string, dialect Dialect, table *Table) string {
}
return sql
}
// SeqFilter filter SQL replace ?, ? ... to $1, $2 ...
type SeqFilter struct {
Prefix string
Start int
}
func (s *SeqFilter) Do(sql string, dialect Dialect, table *Table) string {
segs := strings.Split(sql, "?")
size := len(segs)
res := ""
for i, c := range segs {
if i < size-1 {
res += c + fmt.Sprintf("%s%v", s.Prefix, i+s.Start)
}
}
res += segs[size-1]
return res
}