Skip to content

Commit a631bde

Browse files
committed
Allowing to set to null the SQL dialect
1 parent 0fbf498 commit a631bde

File tree

2 files changed

+14
-4
lines changed

2 files changed

+14
-4
lines changed

src/Mouf/Database/MagicQuery.php

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -80,14 +80,18 @@ public function setEnableTwig($enableTwig = true)
8080
}
8181

8282
/**
83-
* Overrides the output dialect used to generate SQL. By default, the dialect of the connection is used.
83+
* Overrides the output dialect used to generate SQL. By default (or is you set this explicitly to null), the dialect of the connection is used.
8484
* If no connection is used, MySQL platform is used by default.
8585
*
8686
* @param AbstractPlatform $platform
8787
*/
88-
public function setOutputDialect(AbstractPlatform $platform): self
88+
public function setOutputDialect(?AbstractPlatform $platform): self
8989
{
90-
$this->platform = $platform;
90+
if ($platform !== null) {
91+
$this->platform = $platform;
92+
} else {
93+
$this->platform = $this->connection ? $this->connection->getDatabasePlatform() : new MySqlPlatform();
94+
}
9195

9296
return $this;
9397
}
@@ -134,7 +138,7 @@ public function buildPreparedStatement(string $sql, array $parameters = []): str
134138

135139
$availableParameterKeys = array_keys(array_filter($parameters, static function($param) { return $param !== null;}));
136140
// We choose md4 because it is fast.
137-
$cacheKey = 'request_build_'.hash('md4', $sql.'__'.implode('_/_', $availableParameterKeys));
141+
$cacheKey = 'request_build_'.hash('md4', get_class($this->platform).'__'.$sql.'__'.implode('_/_', $availableParameterKeys));
138142
$newSql = $this->cache->fetch($cacheKey);
139143
if ($newSql === false) {
140144
$select = $this->parse($sql);

tests/Mouf/Database/MagicQueryTest.php

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -443,5 +443,11 @@ public function testSetOutputDialect()
443443

444444
$sql = 'SELECT id FROM users';
445445
$this->assertEquals('SELECT "id" FROM "users"', self::simplifySql($magicQuery->buildPreparedStatement($sql)));
446+
447+
$magicQuery->setOutputDialect(null);
448+
449+
$sql = 'SELECT id FROM users';
450+
$this->assertEquals('SELECT id FROM users', self::simplifySql($magicQuery->buildPreparedStatement($sql)));
451+
446452
}
447453
}

0 commit comments

Comments
 (0)