From 8bb48a2931266669097b480f381fad27fbdc08e3 Mon Sep 17 00:00:00 2001 From: Leon Zhang Date: Wed, 21 Nov 2018 15:23:52 +0800 Subject: [PATCH] fix #112 multi-line comment will cause line counter error, when -report-type=lint --- ast/token.go | 6 ++++-- cmd/soar/soar.go | 6 +++--- database/mysql.go | 2 +- 3 files changed, 8 insertions(+), 6 deletions(-) diff --git a/ast/token.go b/ast/token.go index c48ede94..8502b634 100644 --- a/ast/token.go +++ b/ast/token.go @@ -894,7 +894,8 @@ func Compress(sql string) string { } // SplitStatement SQL切分 -func SplitStatement(buf []byte, delimiter []byte) (string, []byte) { +// return original sql, remove comment sql, left over buf +func SplitStatement(buf []byte, delimiter []byte) (string, string, []byte) { var singleLineComment bool var multiLineComment bool var quoted bool @@ -988,8 +989,9 @@ func SplitStatement(buf []byte, delimiter []byte) (string, []byte) { sql = string(buf) } } + orgSQL := string(buf[:len(sql)]) buf = buf[len(sql):] - return strings.TrimSuffix(sql, string(delimiter)), buf + return orgSQL, strings.TrimSuffix(sql, string(delimiter)), buf } // LeftNewLines cal left new lines in space diff --git a/cmd/soar/soar.go b/cmd/soar/soar.go index 15c36785..82951aac 100644 --- a/cmd/soar/soar.go +++ b/cmd/soar/soar.go @@ -111,11 +111,11 @@ func main() { break } // 查询请求切分 - sql, bufBytes := ast.SplitStatement([]byte(buf), []byte(common.Config.Delimiter)) + orgSQL, sql, bufBytes := ast.SplitStatement([]byte(buf), []byte(common.Config.Delimiter)) // lineCounter - lc := ast.NewLines([]byte(sql)) + lc := ast.NewLines([]byte(orgSQL)) // leftLineCounter - llc := ast.LeftNewLines([]byte(sql)) + llc := ast.LeftNewLines([]byte(orgSQL)) lineCounter += llc buf = string(bufBytes) diff --git a/database/mysql.go b/database/mysql.go index 14783af0..6f1ce452 100644 --- a/database/mysql.go +++ b/database/mysql.go @@ -170,7 +170,7 @@ func (db *Connector) Source(file string) ([]*QueryResult, error) { } // 查询请求切分 - sql, bufBytes := ast.SplitStatement([]byte(buf), []byte(common.Config.Delimiter)) + _, sql, bufBytes := ast.SplitStatement([]byte(buf), []byte(common.Config.Delimiter)) buf = string(bufBytes) sql = strings.TrimSpace(sql) common.Log.Debug("Source Query SQL: %s", sql)