From d94962ed41827ad4c59c51f4389656355aa9592e Mon Sep 17 00:00:00 2001 From: fukaya Date: Tue, 31 May 2016 17:14:35 +0900 Subject: [PATCH 1/4] Add a test case for values --- query/query_test.go | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/query/query_test.go b/query/query_test.go index ad3d75c..c99d6ec 100644 --- a/query/query_test.go +++ b/query/query_test.go @@ -248,6 +248,13 @@ func (s *TestSuite) TestFingerprintBasic(t *C) { "select sql_small_result sql_cache distinct centro_atividade from est_dia where unidade_id=? and item_id=? and item_id_red=?", ) + q = "insert into foo (a) values(0)" + t.Check( + query.Fingerprint(q), + Equals, + "insert into foo (a) values(?+)", + ) + q = "INSERT INTO t (ts) VALUES (NOW())" t.Check( query.Fingerprint(q), From 8d211f4cc0288b396bbf0ef09f7faacc49b922cb Mon Sep 17 00:00:00 2001 From: fukaya Date: Tue, 31 May 2016 17:26:27 +0900 Subject: [PATCH 2/4] Allocate enough memory to replace (x) with (?+) --- query/query.go | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/query/query.go b/query/query.go index 5921131..6ec8087 100644 --- a/query/query.go +++ b/query/query.go @@ -134,7 +134,8 @@ var ReplaceNumbersInWords = false 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 From c4e6f1e763f9352f5095cd90e702a26cd8f6e17d Mon Sep 17 00:00:00 2001 From: xuewindy Date: Mon, 10 Apr 2017 16:08:26 +0800 Subject: [PATCH 3/4] add buf init size in case of slice out of range memory --- query/query.go | 2 +- query/query_test.go | 7 +++++++ 2 files changed, 8 insertions(+), 1 deletion(-) diff --git a/query/query.go b/query/query.go index 116994b..2eab29f 100644 --- a/query/query.go +++ b/query/query.go @@ -138,7 +138,7 @@ var ReplaceNumbersInWords = false func Fingerprint(q string) string { q += " " // need range to run off end of original query prevWord := "" - f := make([]byte, len(q)) + f := make([]byte, len(q)+8) fi := 0 pr := rune(0) // previous rune s := unknown // current state diff --git a/query/query_test.go b/query/query_test.go index 9cc6e51..7eb955b 100644 --- a/query/query_test.go +++ b/query/query_test.go @@ -239,6 +239,13 @@ func (s *TestSuite) TestFingerprintBasic(t *C) { Equals, "select * from prices.rt_5min where id=?", ) + // Fingerprint Insert into tables; + q = "insert into t3 values(2);" + t.Check( + query.Fingerprint(q), + Equals, + "insert into t3 values(?+);", + ) // 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" From d709876ec7b204fb84f4d838f046d3f84412669b Mon Sep 17 00:00:00 2001 From: Kamil Dziedzic Date: Thu, 15 Feb 2018 18:13:52 +0100 Subject: [PATCH 4/4] cleanups --- query/query.go | 6 +++--- query/query_test.go | 1 + 2 files changed, 4 insertions(+), 3 deletions(-) diff --git a/query/query.go b/query/query.go index 99a5cca..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,7 +131,7 @@ 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. diff --git a/query/query_test.go b/query/query_test.go index 61218cf..eb56020 100644 --- a/query/query_test.go +++ b/query/query_test.go @@ -228,6 +228,7 @@ func TestFingerprintBasic(t *testing.T) { "select * from prices.rt_5min where id=?", query.Fingerprint(q), ) + // Fingerprint Insert into tables; q = "insert into t3 values(2);" assert.Equal(