2

I am trying to use split method in sqlparse module to split the queries file into queries list. When I use, 'create procedure' query, it is splitting correctly. But when I use 'replace procedure', it is splitting a single query into two queries. It is splitting with semicolon and ignoring begin and end.

import sqlparse
from sqlparse.keywords import *


query = """REPLACE PROCEDURE u1.SP_Employee (
   IN EmpNo INTEGER, IN EmpName INTEGER
)
BEGIN
   INSERT INTO u1.employee (
      EmpNo,
      EmpName

   )
   VALUES (
      :EmpNo,
      :EmpName
   );
END;
"""

sql_formats = sqlparse.format(query,strip_comments=True, reindent=True)
result = sqlparse.parse(query)
for i in range(0,len(result)) :
    print("index is "+str(i))
    print(result[i])

print("Result printing is done for replace query")

query = """create PROCEDURE u1.SP_Employee (
   IN EmpNo INTEGER, IN EmpName INTEGER
)
BEGIN
   INSERT INTO u1.employee (
      EmpNo,
      EmpName

   )
   VALUES (
      :EmpNo,
      :EmpName
   );
END;
"""

sql_formats = sqlparse.format(query,strip_comments=True, reindent=True)
result = sqlparse.parse(query)
for i in range(0,len(result)) :
    print("index is `enter code here`"+str(i))
    print(result[i])

print("Result printing is done for create query")


**Output :-**
index is 0
REPLACE PROCEDURE u1.SP_Employee (
   IN EmpNo INTEGER, IN EmpName INTEGER
)
BEGIN
   INSERT INTO u1.employee (
      EmpNo,
      EmpName

   )
   VALUES (
      :EmpNo,
      :EmpName
   );
index is 1

END;
index is 2


Result printing is done for replace query
index is 0
create PROCEDURE u1.SP_Employee (
   IN EmpNo INTEGER, IN EmpName INTEGER
)
BEGIN
   INSERT INTO u1.employee (
      EmpNo,
      EmpName

   )
   VALUES (
      :EmpNo,
      :EmpName
   );
END;
index is 1


Result printing is done for create query
Nickolay
  • 31,095
  • 13
  • 107
  • 185

1 Answers1

0

sqlparse currently supports CREATE and CREATE OR REPLACE, you could edit https://github.com/andialbrecht/sqlparse/blob/3013ef4826ce8f7bf46c450debe816c9fcae2a05/sqlparse/keywords.py#L78 and/or submit a bug report indicating which DB you're using.

Nickolay
  • 31,095
  • 13
  • 107
  • 185