[英]How to extract column names along with conditions from a SQL query using python
How to extract column names along with their conditions applied from a SQL query string using Python, so that I can map the result to other table and get the required data?如何使用 Python 从 SQL 查询字符串中提取列名及其条件,以便我可以将结果映射到其他表并获取所需的数据?
query = "SELECT COUNT(*) as cnt FROM dbo.main_table WHERE (VKRKA10001920015 = 1 AND age IN (15, 16, 17, 18, 19) AND income IN (101, 102, 301, 302, 305))"
Required_Output = {
'VKRKA10001920015': 1,
'age' : (15, 16, 17, 18, 19),
'income' : (101, 102, 301, 302, 305),
}
As of now I am able to extract only column names using sql-metadata python package.到目前为止,我只能使用 sql-metadata python 包提取列名。 But I need the conditions as well to perform other operations
但我也需要条件来执行其他操作
from sql_metadata.compat import get_query_columns
print(get_query_columns(query))
['VKRKA10001920015', 'age', 'householdincome']
To identify the tokens ("IN", "=", "LIKE") I have used the get_tokens() which recursively get the tokens added to the extracted_key() dictionary.为了识别标记(“IN”、“=”、“LIKE”),我使用了 get_tokens(),它递归地将标记添加到 extract_key() 字典中。 Here in the extracted_keys dict the key address stored instead of the actual key Since the functions is going recursively.
由于函数是递归的,因此在提取的密钥字典中存储的密钥地址而不是实际密钥。 So I have used create_dict() another function to create a proper dictionary out of extracted_keys dictionary.
所以我使用 create_dict() 另一个函数从提取的键字典中创建一个正确的字典。 For now we are able to extract columns along with their conditions from the SQL WHERE conditon for following operators ie, "IN" operator, "=" operator, "Like" operator.
现在,我们能够从 SQL WHERE 条件中提取列及其条件以用于以下运算符,即“IN”运算符、“=”运算符、“Like”运算符。
To install sqlparse module安装 sqlparse 模块
pip install sqlparse
import sqlparse
def get_tokens(where):
identifier = None
extracted_keys = {}
for i in where.tokens:
try:
name = i.get_real_name()
if name and isinstance(i, sqlparse.sql.Identifier):
name = i.get_real_name()
identifier = i
extracted_keys[identifier] = i.value
elif identifier and isinstance(i, sqlparse.sql.Parenthesis):
extracted_keys[identifier] = i.value
elif i and "in" not in i.value.lower() and "or" not in i.value.lower() and isinstance(i, sqlparse.sql.Comparison):
if "=" in i.value:
equal_index = i.value.index("=")
if i.value[equal_index -1] not in ["!", ">", "<"]:
key,value = i.value.split("=")[0], i.value.split("=")[1]
extracted_keys[key] = value
if "!=" in i.value:
key,value = i.value.split("!=")[0], i.value.split("!=")[1]
extracted_keys[key] = value
elif "like" in i.value.lower():
key,value = i.value.lower().split("like")[0].upper(), i.value.lower().split("like")[1]
extracted_keys[key] = value
else:
extracted_keys.update(get_tokens(i))
except Exception as error:
pass
return extracted_keys
def create_dict(extracted_keys):
cols = {}
for key, value in extracted_keys.items():
try:
if key.get_real_name():
cols[key.get_real_name()] = value
except Exception as e:
cols[key] = value
return cols
For example:例如:
query = "SELECT COUNT(*) as cnt FROM dbo.main_table WHERE (VKRKA10001920015 = 1 AND age IN (15, 16, 17, 18, 19) AND income IN (101, 102, 301, 302, 305))"
parsed = sqlparse.parse(query)
where = parsed[0][-1]
extracted_keys = get_tokens(where)
extracted_keys = create_dict(extracted_keys)
Output输出
{
'VKRKA10001920015': 1,
'age' : (15, 16, 17, 18, 19),
'income' : (101, 102, 301, 302, 305),
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.