I'm trying to maintain someone else's code and would like to put them in order and fin out the relations between the files and the tables in the database. I have many ASP file, with most of them using VB script. I would like to extract the SQL queries from the files. My thought was to use regular expression, but I can't seem to find the correct pattern to use. (or maybe I'm just not searching with the correct key words?)
For example, I have the below code:
sql="select id from employee where no='"&no&"'"
rs.Open sql,smoothconn,adOpenStatic,adLockReadOnly
if not rs.EOF then
if sql1 <> "" then
sql1 = sql1 & " or "
end if
sql1 = sql1 & " (a.id='"&rs("id")&"')"
end if
rs.Close
chgdate = ""
sqlh = ""
sql="select a.chg, b.oldvalue, e.enter from employeechange a left join changeitem b on a.id=b.changeid " &_
"left join employee e on a.id=e.id " &_
"where (e.no like '"&no&"%' or e.name like '"&no&"%' or e.name1 like '"&no&"%') and b.field='id' " &_
"order by a.chg "
'Response.Write sql & "<br>"
I would like to get the SQL queries within this code. I would like to get at least the following:
select id from employee where no='"&no&"'
"select a.chg, b.oldvalue, e.enter from employeechange a left join changeitem b on a.id=b.changeid " &_
"left join employee e on a.id=e.id " &_
"where (e.no like '"&no&"%' or e.name like '"&no&"%' or e.name1 like '"&no&"%') and b.field='id' " &_
"order by a.chg "
Basically, I need to know the action (INSERT/UPDATE/DELETE) and the tables involved.
Here are some things I think is important when generating the pattern:
1. can be INSERT, UPDATE, or DELETE
2. sql in string concatanation, and can be single-line or multi-line
3. no upper limits of number of tables involved (joined)
4. select can be * or specific column(s)
Here are some of the patterns but none of them seem to work correctly.
(SELECT|UPDATE|INSERT)[\s\S]+?\;\s*?$
(INSERT INTO|UPDATE|SELECT|DELETE)(?:[^;]|(?:'.*?'))+;\\s*
(SELECT|UPDATE|INSERT)[\s\S]+?\;\s*?$
I really appreciate the help.