开发者

How to differentiate SQL statements that modify data from those that are read only?

开发者 https://www.devze.com 2023-01-16 15:36 出处:网络
I need to write a jdbc compl开发者_运维知识库iant driver wrapper whose purpose is to log all SQL statements that modify data. What is the easiest way to differentiate those statements that modify data

I need to write a jdbc compl开发者_运维知识库iant driver wrapper whose purpose is to log all SQL statements that modify data. What is the easiest way to differentiate those statements that modify data from those that only read data? I guess that only way is to parse SQL code on the fly, any libraries that can do that?


You could probably find some full blown sql parsers such as this one, but if your wrapper will intercept only single statements then you might (not enough detail) consider SELECT statements as read only and everything else as statements that modify data.


I think a pretty good start is looking for queries starting with INSERT, UPDATE or DELETE. Make sure to trim leading whitespace before testing the strings.

If you want to include schema altering statements, include commands such as CREATE, ALTER, DROP, and TRUNCATE.

The specifics of the above approach will depend on the database you are using. E.g., there may be batch commands in one string, separated by a semicolon. You will need to parse the string to look for instances such as this.


If this is not a general purpose wrapper I would just log every call to the executeXXX() methods, except calls to executeQuery(), and then just call the appropiate method in client code.

If you want it to be general purpose and would like to avoid parsing SQL, you can investigate the getUpdateCount()method, and the return values of the executeXXX() methods. That would imply logging after the statement was executed, and I do not think it would be 100% correct.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号