-
-
Notifications
You must be signed in to change notification settings - Fork 388
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Make it possible to addSql()
that is executed as a statement
#1326
base: 3.6.x
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -21,19 +21,22 @@ final class Query | |
/** @var mixed[] */ | ||
private array $types; | ||
|
||
private bool $executeAsStatement; | ||
|
||
/** | ||
* @param mixed[] $parameters | ||
* @param mixed[] $types | ||
*/ | ||
public function __construct(string $statement, array $parameters = [], array $types = []) | ||
public function __construct(string $statement, array $parameters = [], array $types = [], bool $executeAsStatement = false) | ||
{ | ||
if (count($types) > count($parameters)) { | ||
throw InvalidArguments::wrongTypesArgumentCount($statement, count($parameters), count($types)); | ||
} | ||
|
||
$this->statement = $statement; | ||
$this->parameters = $parameters; | ||
$this->types = $types; | ||
$this->statement = $statement; | ||
$this->parameters = $parameters; | ||
$this->types = $types; | ||
$this->executeAsStatement = $executeAsStatement; | ||
} | ||
|
||
public function __toString(): string | ||
|
@@ -57,4 +60,9 @@ public function getTypes(): array | |
{ | ||
return $this->types; | ||
} | ||
|
||
public function getExecuteAsStatement(): bool | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This implies that this is an SQL query, but for some reason we want to execute it as a statement. Apparently, it's the opposite: https://stackoverflow.com/questions/4735856/difference-between-a-statement-and-a-query-in-sql I would create a parent class called There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Well, the hard part being that even though queries are statements in the understanding of SQL in this SO answer, this is not entirely true for the DBAL API. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Also, DBAL (and also the ORM) seem to use Query as the more generic name (see There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Still, it feels weird to leak a DBAL implementation detail in the public API of this value object, or even for that value object to have knowledge about it. According to the SO answer, queries are statements, and not the other way around. According to DBAL, queries and statements are different things (so would be best modeled as 2 classes extending a common one I suppose?).
I don't think As for I suggest we model the SQL standard instead of mimicking the DBAL API, that way the Query class is not modeled after a lower-level API, but against a higher-level, common abstraction that the DBAL also knows about: SQL. |
||
{ | ||
return $this->executeAsStatement; | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Doctrine\Migrations\Tests\Version\Fixture; | ||
|
||
use Doctrine\DBAL\Schema\Schema; | ||
use Doctrine\Migrations\AbstractMigration; | ||
|
||
class TestMigrationWithStatement extends AbstractMigration | ||
{ | ||
public function up(Schema $schema): void | ||
{ | ||
$this->addSql('CREATE TRIGGER', executeAsStatement: true); | ||
} | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think boolean flags are a code smell. Let's add a new method called
addSqlStatement()
instead?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
You mean, like a static constructor method ob
Query
?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
No, I mean like a new method next to
addSql
, calledaddSqlStatement
.