Parse SQL and replace column name with another name
-
Suppose the sql is :
select empno, empname, deptname,sal from emp, dept where emp.did= dept.did
I need the following: If the column name is empno replace it by eno, If the table name is emp then replace it by employee, If the table is dept then replace it by department, if the column name is did replace it by deptid. That means after operation my the sql should be as follows:
select eno, empname, deptname, sal from employee,department where employee.deptid=department.deptid
I will be greatfull if any body help me to complete this. I got a parser but it only can parse the column name in select statement and table names. But I am not getting the column names in the join condition. After getting the column name or table name how can I replce those? Suppose I got emp as table but if I replace all the emp by employee then empname is also converting to employeename which is becoming to wrong column name
-
Suppose the sql is :
select empno, empname, deptname,sal from emp, dept where emp.did= dept.did
I need the following: If the column name is empno replace it by eno, If the table name is emp then replace it by employee, If the table is dept then replace it by department, if the column name is did replace it by deptid. That means after operation my the sql should be as follows:
select eno, empname, deptname, sal from employee,department where employee.deptid=department.deptid
I will be greatfull if any body help me to complete this. I got a parser but it only can parse the column name in select statement and table names. But I am not getting the column names in the join condition. After getting the column name or table name how can I replce those? Suppose I got emp as table but if I replace all the emp by employee then empname is also converting to employeename which is becoming to wrong column name
Use a
Dictionary<string,string>
? Populate it with the old and new values. Split the existing statement and iterate the parts. If a part exists in the dictionary retrieve its replacement. Build a new statement. -
Use a
Dictionary<string,string>
? Populate it with the old and new values. Split the existing statement and iterate the parts. If a part exists in the dictionary retrieve its replacement. Build a new statement.I got some idea. But do you know any god parsers name where i can easily find out the column names, table names, join tables names columns in join condition, column in where clause, column in group by clause and having clause.