Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add pattern replace #47

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 15 additions & 4 deletions query/query.go
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,7 @@ import (
"crypto/md5"
"fmt"
"io"
"regexp"
"strings"
)

Expand Down Expand Up @@ -126,6 +127,12 @@ var Debug bool = false
// look at test query_test.go/TestFingerprintWithNumberInDbName.
var ReplaceNumbersInWords = false

// spaceRe add a space character back and forth "/*%+-"
var spaceRe = regexp.MustCompile("([0-9]+)([/*%+-])([0-9]+)")

// questionMarksRe replace from several question mark to single one (e.i ??? -> ?)
var questionMarksRe = regexp.MustCompile("\\?+")

// Fingerprint returns the canonical form of q. The primary transformations are:
// - Replace values with ?
// - Collapse whitespace
Expand All @@ -136,7 +143,8 @@ var ReplaceNumbersInWords = false
// example, "ORDER BY col ASC" is the same as "ORDER BY col", so "ASC" in the
// fingerprint is removed.
func Fingerprint(q string) string {
q += " " // need range to run off end of original query
q = spaceRe.ReplaceAllString(q, "$1 $2 $3") // spaceRe add a space character back and forth "/*%+-"
q += " " // need range to run off end of original query
prevWord := ""
f := make([]byte, len(q))
fi := 0
Expand Down Expand Up @@ -406,7 +414,7 @@ func Fingerprint(q string) string {
if Debug {
fmt.Println("Multi-line comment")
}
pr = r
pr = r
s = inMLC
continue
} else {
Expand Down Expand Up @@ -777,8 +785,11 @@ func Fingerprint(q string) string {
fi--
}

// Clean up control characters, and return the fingerprint
return strings.Replace(string(f[0:fi]), "\x00", "", -1)
// Clean up control characters
f2 := strings.Replace(string(f[0:fi]), "\x00", "", -1)

// Replace from several question mark to single one, and return the fingerprint
return questionMarksRe.ReplaceAllString(f2, "?")
}

func isSpace(r rune) bool {
Expand Down