diff --git a/query/query.go b/query/query.go index 4f8bfcd..b02c62a 100644 --- a/query/query.go +++ b/query/query.go @@ -95,7 +95,7 @@ const ( inMySQLCode // /*! MySQL-specific code */ ) -var stateName map[byte]string = map[byte]string{ +var stateName = map[byte]string{ 0: "unknown", 1: "inWord", 2: "inNumber", @@ -119,7 +119,7 @@ var stateName map[byte]string = map[byte]string{ } // Debug prints very verbose tracing information to STDOUT. -var Debug bool = false +var Debug = true // ReplaceNumbersInWords enables replacing numbers in words. For example: // `SELECT c FROM org235.t` -> `SELECT c FROM org?.t`. For more examples @@ -131,14 +131,15 @@ var ReplaceNumbersInWords = false // - Collapse whitespace // - Remove comments // - Lowercase everything -// Additional trasnformations are performed which change the syntax of the +// Additional transformations are performed which change the syntax of the // original query without affecting its performance characteristics. For // 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 prevWord := "" - f := make([]byte, len(q)) + // allocate enough memory to replace (x) with (?+). + f := make([]byte, len(q)+len(q)/3) fi := 0 pr := rune(0) // previous rune s := unknown // current state diff --git a/query/query_test.go b/query/query_test.go index 1daf5fd..eb56020 100644 --- a/query/query_test.go +++ b/query/query_test.go @@ -229,6 +229,14 @@ func TestFingerprintBasic(t *testing.T) { query.Fingerprint(q), ) + // Fingerprint Insert into tables; + q = "insert into t3 values(2);" + assert.Equal( + t, + "insert into t3 values(?+);", + query.Fingerprint(q), + ) + // Fingerprint /* -- comment */ SELECT (bug 1174956) q = "/* -- S++ SU ABORTABLE -- spd_user: rspadim */SELECT SQL_SMALL_RESULT SQL_CACHE DISTINCT centro_atividade FROM est_dia WHERE unidade_id=1001 AND item_id=67 AND item_id_red=573" assert.Equal( @@ -237,6 +245,13 @@ func TestFingerprintBasic(t *testing.T) { query.Fingerprint(q), ) + q = "insert into foo (a) values(0)" + assert.Equal( + t, + "insert into foo (a) values(?+)", + query.Fingerprint(q), + ) + q = "INSERT INTO t (ts) VALUES (NOW())" assert.Equal( t,