Search code examples
sqlsyntaxdoctrine

What's the SQL syntax in Doctrine's addSql?


I know I can pass an array to addSql. But I can also add multiple SQL satements separated by semicolons as a single string. Is that syntax specified anywhere (in the SQL standard preferably, or elsewhere)? And if so, is it actually a sequence of statements separated by semicolons, or terminated by semicolons?


Solution

  • (I'm not sure this answer is correct, so I'm willing to accept another one, even if it just confirms. Or negates, but with details.)

    Although nowhere in the documentation of PHP or Doctrine have I found it even remotely treated, it seems the binding style used by PHP's PDO::query and Doctrine's addSql (and equivalents in many other languages and frameworks) is neither SQL/CLI, nor SQL/OLB, nor embedded SQL, but rather direct SQL. The relevant production from "Information technology — Database languages — SQL — Part 2: Foundation (SQL/Foundation)" is therefore:

    <direct SQL statement> ::=
        <directly executable statement> <semicolon>
    

    It makes the semicolon mandatory. It also disallows multiple statements. (Unless the implementation defines them to be included in <direct implementation-defined statement> which is a possibility within <directly executable statement>. It can be anything, including a sequence of <directly executable statement>s separated by semicolons. But the final semicolon is outside that, so still mandatory.) If multiple SQL statements are to be executed, they should be provided separately. May be batched by the implementation (that's a details the standard doesn't concern itself with). That's what Doctrine indeed does when addSql is called many times in the same migration. And it even wraps them in a transaction by default.