我的任务是编写一个函数,该函数将作为字符串传递一个 sql 查询,它应该将这个查询解析为它找到的任何表前面的“tablename”。我已经得到了一些代码作为示例(它只是寻找“来自”和“哪里/内部/外部/左/右”等的实例......并假设中间的单词是一个表格......这是在 C# 中完成
我真正寻找的是最好的方法的指针......(虽然简短的是我将传递一个字符串,我将返回一个字符串,所以如果你有任何想法,请记住这一点!)
我曾尝试对以下示例查询使用 Sql 解析器,但它对我没有帮助
//"SELECT e.last_name, e.department_id, d.department_name FROM employees e LEFT OUTER JOIN department d ON ( e.department_id = d.department_id ); "
//"SELECT a.Id, a.Name AS Surname, b.preName, c.Busrel_Id FROM Customer a LEFT JOIN CustomerDetail b ON a.Id = b.fId LEFT JOIN Businessrel c ON b.cId = c.Bid JOIN Contract d ON c.Id = d.fId AND a.DATE < '20071204:13.23.14.000' AND a.cst_Class_cd IN ('01','02') AND b.Name LIKE 'W%' AND (SELECT MAX(e.TIMESTAMP) FROM dDate e WHERE e.colLid = a.Id) ORDER BY a.Name, b.preName";
//SELECT Quantity,b.Time,c.Description FROM (SELECT ID,Time FROM bTab) b INNER JOIN aTab a on a.ID=b.ID INNER JOIN cTab c on a.ID=c.ID
我使用的代码是
using gudusoft.gsqlparser;
using gudusoft.gsqlparser.Units;
TSelectSqlStatement stmt = (TSelectSqlStatement)sqlparser.SqlStatements[0];
foreach (TLzField lcfield in stmt.Fields)
{
lctablename = "NULL";
lcfieldalias = "NULL";
if (lcfield.FieldPrefix.Length > 0)
{
lctablename = findtablename(stmt.Tables, lcfield.FieldPrefix);
}
lcfieldname = lcfield.FieldName;
if (lcfield.FieldAlias.Length > 0)
{
lcfieldalias = lcfield.FieldAlias;
}
Console.WriteLine("{0},{1},{2}", lctablename, lcfieldname, lcfieldalias);
}
public static string findtablename(TLzTableList tbl, string tname)
{
foreach (TLzTable tb in tbl)
{
if (tb.TableName.CompareTo(tname) == 0)
{
return tb.TableName;
}
else if (tb.TableAlias.CompareTo(tname) == 0)
{
return tb.TableName;
}
}
return tname;
}
列出的查询是什么,例如
1)嵌套查询 2)子查询 3)联合查询
我无法检索该查询中指定的表名
等待您的回复和评论