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

Extract SQL Fingerprint #4369

Open
emorydu opened this issue Dec 27, 2024 · 2 comments
Open

Extract SQL Fingerprint #4369

emorydu opened this issue Dec 27, 2024 · 2 comments

Comments

@emorydu
Copy link

emorydu commented Dec 27, 2024

Hats off:
If I want to use the g4 file provided with the library and follow the documentation generated for the Go language to complete the extraction of SQL fingerprints using go-antlr4 which of the many generated listener implementation functions should I start with.
Example:
SELECT * FROM helloworld WHERE username = '1_32-32' AND number >= -3+4-0 AND number2 <= -8 AND expression_3414123 LIKE '%_43421'

I expect to get
template := SELECT * FROM helloworld WHERE username = ? AND number >= ? AND number2 <= ? AND expression_3414123 LIKE ?
Parameters: ['1_32-32', -3+4-0, -8, '%_43421']

Includes other syntax

@kaby76
Copy link
Contributor

kaby76 commented Dec 27, 2024

The first question is which SQL? This can be parsed using at least four different SQL grammars: mysql/Oracle, mysql/Positive-Technologies, plsql, postgresql.

After choosing a grammar, parse the input, and print the parse tree. From that, you can get an idea of what you need to write in an Antlr Listener.

Also, I don't know about the Antlr Listener pattern in Go, but I raised the of Antlr Visitor patterns do not work for Go as one would expect coming from any of the other targets. antlr/antlr4#4398. The problem is that Go is not object-oriented, which means that you have to implement every visitor ParserRuleContext node "method". I do not remember if it works as expected for Listeners.

@emorydu
Copy link
Author

emorydu commented Dec 30, 2024

Thank you for your answer.
Prior to this I have completed the way you described above by analyzing it and also printing the entire syntax tree structure, and in the generated code there is only the default empty implementation of the listener.

I understand that it is necessary to go for the implementation of the adoptable listener approach, and by checking the relevant information, similar to tidb's sqlparser library, it seems that there is no visitor interface.

Going back to the original question, my goal is to implement most of the SQL under grammars-v4, not limited to mysql, plsql, postgresql.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants