Skip to content

Commit 6f2f9f1

Browse files
committed
fix comment
1 parent e50400e commit 6f2f9f1

File tree

1 file changed

+40
-41
lines changed

1 file changed

+40
-41
lines changed

pum/utils/execute_sql.py

Lines changed: 40 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -28,51 +28,50 @@ def execute_sql(
2828
RuntimeError: If the SQL execution fails.
2929
"""
3030
cursor = conn.cursor()
31-
try:
32-
if isinstance(sql, Path):
33-
logger.debug(
34-
f"Executing SQL from file: {sql} with parameters: {parameters}",
35-
)
36-
with open(sql) as file:
37-
sql_content = file.read()
38-
# Remove SQL comments
39-
def remove_sql_comments(sql):
40-
# Remove multiline comments (/* ... */)
41-
sql = re.sub(r'/\*.*?\*/', '', sql, flags=re.DOTALL)
42-
# Remove single-line comments (-- ...)
43-
sql = re.sub(r'--.*', '', sql)
44-
return sql
45-
sql_content = remove_sql_comments(sql_content)
46-
if parameters:
47-
for key, value in parameters.items():
48-
sql_content = sql_content.replace(f"{{{{ {key} }}}}", str(value))
49-
sql_code = sql_content
50-
else:
51-
sql_code = sql_content
31+
if isinstance(sql, Path):
32+
logger.debug(
33+
f"Executing SQL from file: {sql} with parameters: {parameters}",
34+
)
35+
with open(sql) as file:
36+
sql_content = file.read()
37+
# Remove SQL comments
38+
def remove_sql_comments(sql):
39+
# Remove multiline comments (/* ... */)
40+
sql = re.sub(r'/\*.*?\*/', '', sql, flags=re.DOTALL)
41+
# Remove single-line comments (-- ...)
42+
sql = re.sub(r'--(?=(?:[^\'"]|\'[^\']*\'|"[^"]*")*$).*', '', sql)
43+
return sql
44+
sql_content = remove_sql_comments(sql_content)
45+
if parameters:
46+
for key, value in parameters.items():
47+
sql_content = sql_content.replace(f"{{{{ {key} }}}}", str(value))
48+
sql_code = sql_content
49+
else:
50+
sql_code = sql_content
5251

53-
def split_sql_statements(sql):
54-
pattern = r'(?:[^;\'"]|\'[^\']*\'|"[^"]*")*;'
55-
matches = re.finditer(pattern, sql, re.DOTALL)
56-
statements = []
57-
last_end = 0
58-
for match in matches:
59-
end = match.end()
60-
statements.append(sql[last_end : end - 1].strip())
61-
last_end = end
62-
if last_end < len(sql):
63-
statements.append(sql[last_end:].strip())
64-
return [stmt for stmt in statements if stmt]
52+
def split_sql_statements(sql):
53+
pattern = r'(?:[^;\'"]|\'[^\']*\'|"[^"]*")*;'
54+
matches = re.finditer(pattern, sql, re.DOTALL)
55+
statements = []
56+
last_end = 0
57+
for match in matches:
58+
end = match.end()
59+
statements.append(sql[last_end : end - 1].strip())
60+
last_end = end
61+
if last_end < len(sql):
62+
statements.append(sql[last_end:].strip())
63+
return [stmt for stmt in statements if stmt]
6564

66-
sql_code = split_sql_statements(sql_code)
67-
else:
68-
sql_code = [sql]
65+
sql_code = split_sql_statements(sql_code)
66+
else:
67+
sql_code = [sql]
6968

70-
for statement in sql_code:
69+
for statement in sql_code:
70+
try:
7171
cursor.execute(statement)
72-
73-
except SyntaxError as e:
74-
logger.debug(f"Error executing SQL: {sql_code}")
75-
raise PumSqlException(f"SQL execution failed for the following code: {sql} {e}") from e
72+
except SyntaxError as e:
73+
logger.debug(f"Error executing SQL: {statement}")
74+
raise PumSqlException(f"SQL execution failed for the following code: {sql} {e}") from e
7675
if commit:
7776
conn.commit()
7877

0 commit comments

Comments
 (0)