Skip to content

Commit

Permalink
fix: 支持使用create or replace创建函数、存储过程、视图以及包等语句 (#2066)
Browse files Browse the repository at this point in the history
* fix: 支持使用create or replace创建函数、存储过程、视图以及包等语句

原来代码只支持使用create创建函数、存储过程、视图以及包,使用create or replace创建时获取不到对象名导致报错,实际运维中上线SQL大多使用create or replace方式,修改后同时支持使用create 和 create or replace两个方式。

* Update oracle.py

fix-支持Oracle创建存储过程、函数、包等,使用create or replace方式

* Update oracle.py

优化使用正则提取SQL语句中 object name 部分的代码逻辑

* Update oracle.py

优化获取object name部分代码

* Update oracle.py

优化代码

* Update oracle.py

修复lint报错(代码规范)

* Update oracle.py

处理lint报错(代码规范)

* Update oracle.py

* Update oracle.py

调整代码规范
  • Loading branch information
songtao12 committed Mar 11, 2023
1 parent b18de11 commit 96094b4
Showing 1 changed file with 52 additions and 44 deletions.
96 changes: 52 additions & 44 deletions sql/engines/oracle.py
Original file line number Diff line number Diff line change
Expand Up @@ -437,49 +437,38 @@ def object_name_check(self, db_name=None, object_name=""):
def get_sql_first_object_name(sql=""):
"""获取sql文本中的object_name"""
object_name = ""
if re.match(r"^create\s+table\s", sql, re.M | re.IGNORECASE):
object_name = re.match(
r"^create\s+table\s(.+?)(\s|\()", sql, re.M | re.IGNORECASE
).group(1)
elif re.match(r"^create\s+index\s", sql, re.M | re.IGNORECASE):
object_name = re.match(
r"^create\s+index\s(.+?)\s", sql, re.M | re.IGNORECASE
).group(1)
elif re.match(r"^create\s+unique\s+index\s", sql, re.M | re.IGNORECASE):
object_name = re.match(
r"^create\s+unique\s+index\s(.+?)\s", sql, re.M | re.IGNORECASE
).group(1)
elif re.match(r"^create\s+sequence\s", sql, re.M | re.IGNORECASE):
object_name = re.match(
r"^create\s+sequence\s(.+?)(\s|$)", sql, re.M | re.IGNORECASE
).group(1)
elif re.match(r"^alter\s+table\s", sql, re.M | re.IGNORECASE):
object_name = re.match(
r"^alter\s+table\s(.+?)\s", sql, re.M | re.IGNORECASE
).group(1)
elif re.match(r"^create\s+function\s", sql, re.M | re.IGNORECASE):
object_name = re.match(
r"^create\s+function\s(.+?)(\s|\()", sql, re.M | re.IGNORECASE
).group(1)
elif re.match(r"^create\s+view\s", sql, re.M | re.IGNORECASE):
object_name = re.match(
r"^create\s+view\s(.+?)\s", sql, re.M | re.IGNORECASE
).group(1)
elif re.match(r"^create\s+procedure\s", sql, re.M | re.IGNORECASE):
object_name = re.match(
r"^create\s+procedure\s(.+?)\s", sql, re.M | re.IGNORECASE
).group(1)
elif re.match(r"^create\s+package\s+body", sql, re.M | re.IGNORECASE):
object_name = re.match(
r"^create\s+package\s+body\s(.+?)\s", sql, re.M | re.IGNORECASE
).group(1)
elif re.match(r"^create\s+package\s", sql, re.M | re.IGNORECASE):
object_name = re.match(
r"^create\s+package\s(.+?)\s", sql, re.M | re.IGNORECASE
).group(1)
else:
return object_name.strip()
return object_name.strip()
# 匹配表、索引、序列
pattern = r"^(create|alter)\s+(table|index|unique\sindex|sequence)\s"
groups = re.match(pattern, sql, re.M | re.IGNORECASE)

if groups:
object_name = (
re.match(
r"^(create|alter)\s+(table|index|unique\sindex|sequence)\s+(.+?)(\s|\()",
sql,
re.M | re.IGNORECASE,
)
.group(3)
.strip()
)
return object_name

# 匹配创建或者替换SQL块
pattern = r"^create\s+(or\s+replace\s+)?(function|view|procedure|trigger|package\sbody|package|type\sbody|type)\s"
groups = re.match(pattern, sql, re.M | re.IGNORECASE)

if groups:
object_name = (
re.match(
r"^create\s+(or\s+replace\s+)?(function|view|procedure|trigger|package\sbody|package|type\sbody|type)\s+(.+?)(\s|\()",
sql,
re.M | re.IGNORECASE,
)
.group(3)
.strip()
)
return object_name
return object_name

@staticmethod
def check_create_index_table(sql="", object_name_list=None, db_name=""):
Expand Down Expand Up @@ -966,7 +955,26 @@ def execute_check(self, db_name=None, sql="", close_conn=True):
object_name = object_name.upper()

object_name = f"""{schema_name}.{object_name}"""
if (
if re.match(r"^create\sor\sreplace", sql_lower) and (
self.object_name_check(
db_name=db_name, object_name=object_name
)
or object_name in object_name_list
):
result = ReviewResult(
id=line,
errlevel=1,
stagestatus=f"""{object_name}对象已经存在,请确认是否替换!""",
errormessage=f"""{object_name}对象已经存在,请确认是否替换!""",
sql=sqlitem.statement,
stmt_type=sqlitem.stmt_type,
object_owner=sqlitem.object_owner,
object_type=sqlitem.object_type,
object_name=sqlitem.object_name,
affected_rows=0,
execute_time=0,
)
elif (
self.object_name_check(
db_name=db_name, object_name=object_name
)
Expand Down

0 comments on commit 96094b4

Please sign in to comment.