[英]Stored Procedure File search between 2 keywords to parse all SQL
I want to search between 2 keywords (SELECT/INSERT/UPDATE/DELETE and semicolon ;) in my file of stored procedure code to filter all SQL queries in the file using python.我想在我的存储过程代码文件中搜索 2 个关键字(SELECT/INSERT/UPDATE/DELETE 和分号;),以使用 python 过滤文件中的所有 SQL 查询。 1 select(or even insert) can have nested select so if there is any select keyword it should continue till it find semicolon ignoring any other select/insert/delete/update keyword in between. 1 选择(甚至插入)可以有嵌套选择,因此如果有任何选择关键字,它应该继续直到找到分号,而忽略其间的任何其他选择/插入/删除/更新关键字。
The SQL query can be start of line or may not be & it will be multiline. SQL 查询可以是行首,也可以不是,它将是多行的。
select, insert, delete, update keyword can be in upper or lowercase. select、insert、delete、update 关键字可以是大写或小写。
Can anyone help with that?任何人都可以帮忙吗?
I tried split function but didn't work.我尝试了拆分功能,但没有奏效。
Sample Text input file:示例文本输入文件:
create procedure get_user ( in p_user_id int unsigned)
begin
declare v_id int;
select ADDR_ID into v_id from address where ADDR_ID = v_user_id;
new_v_id=v_id+1
delete from address_2 where ADDR_ID = new_v_id;
select * from address_2
where ADDR_ID in
(Select ADDR_ID from address
where ADDR_ID = v_user_id);
end
Required Output:所需输出:
select ADDR_ID into v_id from address where ADDR_ID = v_user_id;
delete from address_2 where ADDR_ID = new_v_id;
select * from address_2 where ADDR_ID in (Select ADDR_ID from address where ADDR_ID = v_user_id);
Referred the link & solved this problem.参考链接并解决了这个问题。
How to extract text between two substrings from a Python file 如何从 Python 文件中提取两个子字符串之间的文本
import re
start = ['SELECT','select','DELETE','delete','INSERT','insert','UPDATE','update']
end = ';'
for i in start:
rx = r'{}.*?{}'.format(re.escape(i), re.escape(end)) # Escape special chars, build pattern dynamically
with open('sp.txt') as myfile:
copy = False
contents = myfile.read() # Read file into a variable
for match in re.findall(rx, contents, re.S): # Note re.S will make . match line breaks, too
# Process each match individually
print (match)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.