Best practice: Import mySQL file in PHP; split queries

后端 未结 13 646
Happy的楠姐
Happy的楠姐 2020-11-29 00:08

I have a situation where I have to update a web site on a shared hosting provider. The site has a CMS. Uploading the CMS\'s files is pretty straightforward using FTP.

<
13条回答
  •  囚心锁ツ
    2020-11-29 00:41

    Splitting a query cannot be reliably done without parsing. Here is valid SQL that would be impossible to split correctly with a regular expression.

    SELECT ";"; SELECT ";\"; a;";
    SELECT ";
        abc";
    

    I wrote a small SqlFormatter class in PHP that includes a query tokenizer. I added a splitQuery method to it that splits all queries (including the above example) reliably.

    https://github.com/jdorn/sql-formatter/blob/master/SqlFormatter.php

    You can remove the format and highlight methods if you don't need them.

    One downside is that it requires the whole sql string to be in memory, which could be a problem if you're working with huge sql files. I'm sure with a little bit of tinkering, you could make the getNextToken method work on a file pointer instead.

提交回复
热议问题