Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: 支持使用create or replace创建函数、存储过程、视图以及包等语句 #2066

Merged
merged 9 commits into from
Mar 11, 2023
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