Parsing SQL string

前端 未结 5 1077
我寻月下人不归
我寻月下人不归 2021-01-21 08:44

What is a good method for parsing an sql string into it separate components. I\'v tried with a regex, however I can\'t get it to work just right.

Say for instance:

相关标签:
5条回答
  • 2021-01-21 09:09

    The example that you have shown,is a code that will read your SQL statement and tokenize with spaced in between and store each token in the array. If you are looking for the such pice of code, you can make that with split functions in php: http://ca.php.net/split

    Regards,
    Pooria.

    0 讨论(0)
  • 2021-01-21 09:23

    The full syntax of an SQL SELECT statement is very complex. I think you may want to limit yourself to a subset of it.

    0 讨论(0)
  • 2021-01-21 09:26

    A regular expression can parse only a regular language. That is, a language that can be expressed with a finite state machine.

    Most programming languages, including SQL, cannot be expressed as a regular language, and thus cannot be parsed with regular expressions. Even doing limited tasks like splitting SQL expressions is going to be maddeningly difficult with regular expressions.

    You'll need to use a more robust parser generator that can handle more complex grammars. You tag your question with PHP, so you might want to look into Lime.

    You'll also need to find a grammar file for PHP compatible with the parser generator you choose. I'm not sure where to get one for PHP and Lime, but there's a pure Perl project called DBIx::MyParsePP that is based on MySQL's grammar.

    0 讨论(0)
  • 2021-01-21 09:29

    Parsing SQL correctly and completely is hard. For free you could use a parser generator toolkit like ANTLR along with a SQL 'grammar'. For cost, I know that Visual Studio Team System 2008 Database Edition includes a SQL parser.

    0 讨论(0)
  • 2021-01-21 09:32

    I'd go for a sql parser that could be integrated to your language: don't reinvent the wheel.

    I haven't used one in particular to recommend, but I'm sure that you can find one that do what you need.

    See related: PHP MySQL SQL parser (INSERT and UPDATE)

    0 讨论(0)
提交回复
热议问题