简体   繁体   English

如何从DML语句获取column_name和table_name?

[英]How to get column_name and table_name from DML statement?

I am building an algorithm to mock DB operations. 我正在建立一种模拟数据库操作的算法。

I have one problem, how i can get Table name and column names from a DML statement? 我有一个问题,如何从DML语句获取表名和列名?

ie: 即:

 string = "SELECT id,name FROM USER_TBL"
 string TABLE_NAME = getTableName(string); //this will return "USER_TBL"
 array COLUMN_NAME = getColumnNames(string); //this will return ["id","name"]

If i consider these as string manipulation, how i can design algorithm to get table name & column names? 如果我将这些视为字符串操作,如何设计算法来获取表名和列名?

Currently i use following logic, 目前,我使用以下逻辑,

function getTableName(iString){
   //find string between "FROM " to next " "(space) and return it 
}

function getColumnNames(string){
   //get string between "SELECT " and " FROM" and split string based on "," (comma) and return it
}

I wanted to know is there any algorithm already available for this(for reference)? 我想知道是否有任何算法可用于此(供参考)? What and all the cases i need to handle other than these? 除这些以外,我还需要处理什么情况?

I don't know an algorithm, but a few things that come to my mind: Table name - Tablenames can have aliases, but with your logic that should be OK - Can be prefixed with the database name (dbname.tblname) -> so you might need to look for dots before returning the string - Tablename can be a derived table --> select statement in brackets. 我不知道算法,但是我想到了一些事情:表名-表名可以有别名,但是逻辑上应该可以-您可以在数据库名(dbname.tblname)之前加上前缀->这样在返回字符串之前,您可能需要查找点-Tablename可以是派生表->放在方括号中的select语句。 In that case there is not really a simple to tablename derive 在这种情况下,表名派生实际上并不简单

Column names - Column names can have aliases, not sure how you want to handle that. 列名-列名可以有别名,不确定要如何处理。 - What about calculations, conditions (Case when...) --> that is not just a column name but a combined or calculated value. -关于计算,条件(何时…)->不仅是列名,而且是组合值或计算值。 In that case no real column name to retrieve - You can have also select statements for single columns in the resultset 在这种情况下,没有要检索的实际列名-您还可以在结果集中为单个列选择语句

So overall, your approach works if there is no complexity in the sql queries, if yes, your logic might not return what you want to. 因此,总的来说,如果sql查询中没有复杂性,则您的方法有效,如果是,则您的逻辑可能不会返回您想要的结果。

What about other statements, like UPDATE and INSERT? 那么其他语句(例如UPDATE和INSERT)呢? Is that also something that you want to parse? 您还想解析吗?

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM