andialbrecht / sqlparse

A non-validating SQL parser module for Python
BSD 3-Clause "New" or "Revised" License
3.73k stars 695 forks source link

handling .sql files with delimiter changes #139

Open laurentbristiel opened 10 years ago

laurentbristiel commented 10 years ago

I want to parse an SQL script that contains some "delimiter" command. My situation is exactly the one as described in this SO: http://stackoverflow.com/questions/4045332/how-do-you-run-a-complex-sql-script-within-a-python-program Would there be a way to use or modify sqlparse to handle such situation?

andialbrecht commented 10 years ago

Did you try sqlparse.split(sql_file_contents)? It already recognizes things like BEGIN/END blocks.

laurentbristiel commented 10 years ago

yes, this is what I used so far. But it does not recognise the "delimiter" function. So file like:

statement 1;
statement 2;
delimiter //
statement 3//
statement 4//
delimiter ;
statement 5;

will be split in:

so obviously it fails when executing third one.

andialbrecht commented 10 years ago

at first sight, I'd say that this is expected since the part from "delimiter //" up to "delimiter ;" forms a logical block.

There's currently no support for the DELIMITER keyword, but this could be added to the splitter function. However, I'd make it an optional feature.

laurentbristiel commented 10 years ago

ok, I don't feel skilled enough to fork/code/pull-request but if someone is trying to lead this enhancement, I am ready to help as much as I can (review, test etc.)

jkatzer commented 8 years ago

+1 on this issue

cmartinez85 commented 7 years ago

I had troubles with this but finally I solve my problem removing delimiters: http://stackoverflow.com/questions/745538/create-function-through-mysqldb

dgcgh commented 7 years ago

+1 this is particularly a problem with generated DDL and with certain schemas containing trigger functions, they often use delimiter changes in order to be compatible with multiple engines.

tsroten commented 7 years ago

Over at mycli, we'd also love the ability to specify a delimiter when calling the sqlparse.split function.

https://github.com/dbcli/mycli/issues/383

andialbrecht commented 7 years ago

We could add this as an experimental feature. Defining custom delimiters may have strange side-effects. However, if someone uses custom delimiters he should know what he's doing ;)

Thomas Roten notifications@github.com schrieb am Do., 11. Mai 2017, 03:56:

Over at mycli http://mycli.net, we'd alo love the ability to specify a delimiter when calling the sqlparse.split function.

dbcli/mycli#383 https://github.com/dbcli/mycli/issues/383

— You are receiving this because you commented. Reply to this email directly, view it on GitHub https://github.com/andialbrecht/sqlparse/issues/139#issuecomment-300660970, or mute the thread https://github.com/notifications/unsubscribe-auth/AABkIIjSaR3jAs6WJWMh_WHuRSi4ARhJks5r4mrhgaJpZM4B-RJu .

cvicentiu commented 6 years ago

This is something that I'd like to have as well. It would be useful to be able to support \G delimiter currently present in mysql client.

srittau commented 5 years ago

Maybe this helps someone else: We worked around this using some similar to this gist.