Skip to content

Commit dc5a2e1

Browse files
committed
Improve statemnt blocks recognition using regexp
1 parent a762ab4 commit dc5a2e1

1 file changed

Lines changed: 15 additions & 4 deletions

File tree

src/DatabaseLibrary/query.py

Lines changed: 15 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
# limitations under the License.
1414

1515
import inspect
16+
import re
1617
import sys
1718
from typing import List, Optional
1819

@@ -277,12 +278,17 @@ def execute_sql_script(self, sqlScriptFileName: str, sansTran: bool = False, ali
277278
logger.info(f"Executing : Execute SQL Script | {sqlScriptFileName}")
278279
current_statement = ""
279280
inside_statements_group = False
280-
281+
proc_start_pattern = re.compile("create( or replace)? (procedure|function){1}( )?")
282+
proc_end_pattern = re.compile("end(?!( if;| loop;| case;| while;| repeat;)).*;()?")
281283
for line in sql_file:
282284
line = line.strip()
283285
if line.startswith("#") or line.startswith("--") or line == "/":
284286
continue
285-
if line.lower().startswith("begin"):
287+
288+
# check if the line matches the creating procedure regexp pattern
289+
elif proc_start_pattern.match(line.lower()):
290+
inside_statements_group = True
291+
elif line.lower().startswith("begin"):
286292
inside_statements_group = True
287293

288294
# semicolons inside the line? use them to separate statements
@@ -297,12 +303,16 @@ def execute_sql_script(self, sqlScriptFileName: str, sansTran: bool = False, ali
297303
for sqlFragment in sqlFragments:
298304
if len(sqlFragment.strip()) == 0:
299305
continue
306+
300307
if inside_statements_group:
301308
# if statements inside a begin/end block have semicolns,
302309
# they must persist - even with oracle
303310
sqlFragment += "; "
304-
if sqlFragment.lower() == "end; ":
311+
312+
if proc_end_pattern.match(sqlFragment.lower()):
305313
inside_statements_group = False
314+
elif proc_start_pattern.match(sqlFragment.lower()):
315+
inside_statements_group = True
306316
elif sqlFragment.lower().startswith("begin"):
307317
inside_statements_group = True
308318

@@ -326,7 +336,8 @@ def execute_sql_script(self, sqlScriptFileName: str, sansTran: bool = False, ali
326336

327337
for statement in statements_to_execute:
328338
logger.info(f"Executing statement from script file: {statement}")
329-
omit_semicolon = not statement.lower().endswith("end;")
339+
line_ends_with_proc_end = re.compile(r"(\s|;)" + proc_end_pattern.pattern + "$")
340+
omit_semicolon = not line_ends_with_proc_end.search(statement.lower())
330341
self.__execute_sql(cur, statement, omit_semicolon)
331342
if not sansTran:
332343
db_connection.client.commit()

0 commit comments

Comments
 (0)