Intro note: I'm hoping a library or routine exists to do this, but I haven't been able to find anything like this. I'm really looking for direction and advice on where to start...
Here is the situation: I have a block of SQL commands coming as plain text. It might be one SQL commands or several. I need a way to split multiple SQL commands so I can run them one at a time. Microsoft SQL Management Studio does has this behavior out of box.
I'm trying to add this functionality to a PHP5/MySQL5 application running on Apache (Debian).
Some important points:
- I really do need to run them one at a time. Seriously.
- I don't want to require the user to enter a semi-colon after each SQL statement.
- SQL statements can be on one or multiple lines, so I can't wrap on LBs/CRs
- It needs to support SELECT, UPDATE, INSERT, DELETE at least.
- It needs to support queries that are sub-selects
- Neatly tabbed SQL needs to work
- (In the interest of usable software) I do not want to force the user to enter in any kind of delimiter.
Here is an example block of SQL I need to split into two statements:
select sMessage,
(
SELECT COUNT(sTag) FROM Tags WHERE ixTicket = note.ixTicket
) FROM note
select * from ticket
WHERE (SELECT MAX(nCount) FROM Counter WHERE ixTicket = ticket.ixTicket) > 5
I tried some RegEx attempts, but that doesn't seem to be powerful enough.
Any recommendation on an approach to tackle this problem?