-
Notifications
You must be signed in to change notification settings - Fork 5.9k
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
*: Remove leading comment when check IsQuery #210
Changes from 1 commit
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -335,19 +335,21 @@ func (s *testParserSuite) TestParser0(c *C) { | |
|
||
// For select with where clause | ||
{"SELECT * FROM t WHERE 1 = 1", true}, | ||
|
||
// For comment in query | ||
{"/*comment*/ /*comment*/ select c /* this is a comment */ from t;", true}, | ||
} | ||
|
||
for _, t := range table { | ||
fmt.Printf("%s\n", t.src) | ||
l := NewLexer(t.src) | ||
ok := yyParse(l) == 0 | ||
c.Assert(ok, Equals, t.ok, Commentf("source %v", t.src)) | ||
|
||
switch ok { | ||
case true: | ||
c.Assert(len(l.errs), Equals, 0) | ||
c.Assert(len(l.errs), Equals, 0, Commentf("src: %s", t.src)) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Can use HasLen instead of old code. |
||
case false: | ||
c.Assert(len(l.errs), Not(Equals), 0) | ||
c.Assert(len(l.errs), Not(Equals), 0, Commentf("src: %s", t.src)) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Not(HasLen)? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. In the next pr. |
||
} | ||
} | ||
|
||
|
@@ -391,4 +393,12 @@ func (s *testParserSuite) TestParser0(c *C) { | |
cv, ok := ss.Fields[0].Expr.(*expressions.FunctionCast) | ||
c.Assert(ok, IsTrue) | ||
c.Assert(cv.FunctionType, Equals, expressions.ConvertFunction) | ||
|
||
// For query start with comment | ||
src = "/* some comments */ /*comment*/ SELECT /*comment*/ CONVERT('111', /*comment*/ SIGNED) /*comment*/;" | ||
l = NewLexer(src) | ||
c.Assert(yyParse(l), Equals, 0) | ||
st = l.Stmts()[0] | ||
ss, ok = st.(*stmts.SelectStmt) | ||
c.Assert(ok, IsTrue) | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -243,9 +243,24 @@ func NewStore(uri string) (kv.Storage, error) { | |
|
||
var queryStmtTable = []string{"explain", "select", "show", "execute", "describe", "desc"} | ||
|
||
func trimSQL(sql string) string { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. s/trim/stripComments ? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. And comments may start with -- There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. If comments start with -- or # the entire line is comment. Only /**/ can mix with sql statement. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. trim comment and space There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. We need to strip comments start with -- There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. what I mean is to check the trimming result, not only IsQuery true or false. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @ngaut We can handle the sql in #199 now. For sql text start with --, we just need to drop the entire sql text and do nothing else. ➜ interpreter git:(shenli/comment-in-query) ✗ cat xx +--------------------------+------------------------+ Bye There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Yes, it is my fault. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Looks like mp didn't handle comments start -- correctly There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I test tidb-server and it is ok now. Database changed mysql> /comment/ select 1; mysql> |
||
// Trim space. | ||
sql = strings.TrimSpace(sql) | ||
// Trim leading /*comment*/ | ||
// There may be multiple comments | ||
for strings.HasPrefix(sql, "/*") { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. How about strings.HasPrefix(sql, "/*") is false, then comment will not be trimed? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Have same doubt too. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This function is used to fix bug when sql text start with "/comment/" IsQuery always returns false. |
||
i := strings.Index(sql, "*/") | ||
if i != -1 && i < len(sql)+1 { | ||
sql = sql[i+2:] | ||
} | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. If SQL like There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. PTAL |
||
sql = strings.TrimSpace(sql) | ||
} | ||
return sql | ||
} | ||
|
||
// IsQuery checks if a sql statement is a query statement. | ||
func IsQuery(sql string) bool { | ||
sqlText := strings.ToLower(strings.TrimSpace(sql)) | ||
sqlText := strings.ToLower(trimSQL(sql)) | ||
for _, key := range queryStmtTable { | ||
if strings.HasPrefix(sqlText, key) { | ||
return true | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It is better to add different types of comments.
like
/* xxx */ select * from t
select * /* xxx */ from t;
select * from t /* xxx */