From 3dd33af2d66785075d1b2f00d9bc0306dceb9005 Mon Sep 17 00:00:00 2001 From: Lunny Xiao Date: Fri, 11 Apr 2014 23:32:10 +0800 Subject: [PATCH] add SeqFilter --- filter.go | 24 +++++++++++++++++++++++- 1 file changed, 23 insertions(+), 1 deletion(-) diff --git a/filter.go b/filter.go index 072bc589..60caaf29 100644 --- a/filter.go +++ b/filter.go @@ -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 +}