正则替换sql为动态sql

版本一:
  1. #coding:utf-8
  2. import re
  3. #把文档中的单引号替换为2个单引号
  4. def replacestr(matched):
  5. matchedstr = matched.group()
  6. x = matchedstr.replace("‘", "‘‘")
  7. return x
  8. def main():
  9. f_sql = open(‘fzt_dd.sql‘, ‘r‘)
  10. sql = f_sql.read()
  11. f_sql.close()
  12. for a, b, c in patterns:
  13. sql = re.sub(a, b, sql, flags=c) #flags是匹配模式
  14. f_sql = open(‘2.sql‘,‘w‘)
  15. f_sql.write(sql)
  16. f_sql.close()
  17. patterns = \
  18. (
  19. (r"\bdelete\b", r"execute immediate ‘delete", re.I), #替换delete等
  20. (r"\bupdate\b", r"execute immediate ‘update", re.I),
  21. (r"\binsert\b", r"execute immediate ‘insert", re.I),
  22. (r"\balter\b", r"execute immediate ‘alter", re.I),
  23. (r"\bcreate\b", r"execute immediate ‘create", re.I),
  24. (r"\bdrop\s+TABLE\s+(\w+)\s*;", r"drop_table(‘\1‘);", re.I), #替换drop table为drop_table函数
  25. (r"execute immediate(.*?);", r"execute immediate\1‘;", re.S), #在execute imediate尾加单引号
  26. (r"(?<=execute immediate ‘).*?(?=‘;)", replacestr, re.S) #把内容中的单引号替换为2个单引号
  27. )
  28. if __name__ == ‘__main__‘:
  29. main()




版本二:
  1. #coding:utf-8
  2. import re
  3. f_sql = open(‘1.sql‘, ‘r‘)
  4. sql = f_sql.read()
  5. f_sql.close()
  6. drop_str = re.compile(r‘\bdrop\s+TABLE\s+(\w+)\s*;‘, re.I)
  7. start_str = re.compile(r‘\bcreate\b‘, re.I)
  8. end_str = re.compile(r‘execute immediate(.*?);‘, re.S)
  9. content_str = re.compile(r"(?<=execute immediate ‘).*?(?=‘;)", re.S)
  10. #替换以下开头的语句为execute immediate开始
  11. start_list = [‘delete‘, ‘update‘, ‘insert‘, ‘alter‘, ‘create‘]
  12. start_pattern = []
  13. for i in start_list:
  14. pa_str = r‘\b%s\b‘ % i
  15. result_str = r"execute immediate ‘%s " %i
  16. pa = re.compile(pa_str, re.I)
  17. sql = pa.sub(result_str, sql)
  18. #用drop_table函数替换drop语句
  19. sql = drop_str.sub(r"drop_table(‘\1‘);", sql)
  20. #在execute immediate语句结尾加单引号
  21. sql = end_str.sub(r"execute immediate\1‘;", sql)
  22. #把文档中的单引号替换为2个单引号
  23. def _replacestr(matched):
  24. matchedstr = matched.group()
  25. x = matchedstr.replace("‘", "‘‘")
  26. return x
  27. #用函数替换内容中的单引号为2个单引号
  28. sql = content_str.sub(_replacestr, sql)
  29. f_sql = open(‘2.sql‘,‘w‘)
  30. f_sql.write(sql)
  31. f_sql.close()

版本三:


  1. import re
  2. sql = ‘‘‘DROP TABLE FZT_OL_NBR;
  3. execute immediate ‘CREATE TABLE FZT_OL_NBR as
  4. SELECT OL_NBR FROM SP.CSIP_U2S_OL_RELA@OSS A WHERE A.CO_ID IN (SEL‘ECT B‘.CO_ID FROM FZT_CUST_ORDER B );
  5. DROP TABLE FZT_OL_NBR;
  6. execute immediate ‘CREATE TABLE FZT_OL_NBR as
  7. SELECT OL_NBR FROM SP.CSIP_U2S_OL_RELA@OSS A WHERE A.CO_ID IN (SEL‘ECT B‘.CO_ID FROM FZT_CUST_ORDER B );‘‘‘
  8. a = re.compile(r‘drop\s+TABLE\s+(\w+)\s*;‘, re.I)
  9. b = re.compile(r‘create\s+‘,re.I)
  10. b1 = re.compile(r‘execute immediate(.*?)(;)‘, re.S)
  11. p = re.compile(r"(?<=execute immediate ‘).*?(?=;)", re.S)
  12. def _replacestr(matched):
  13. matchedstr = matched.group()
  14. x = matchedstr.replace("‘", "‘‘")
  15. return x
  16. c = a.sub(r"drop_table(‘\1‘);", sql)
  17. d = b.sub(r"execute immediate ‘ create ", sql)
  18. e = b1.sub(r"execute immediate\1‘;", sql)
  19. f = p.sub(_replacestr, sql)
  20. print f




郑重声明:本站内容如果来自互联网及其他传播媒体,其版权均属原媒体及文章作者所有。转载目的在于传递更多信息及用于网络分享,并不代表本站赞同其观点和对其真实性负责,也不构成任何其他建议。