Skip to content

Commit 60e9263

Browse files
committed
Adding a setOutputDialect method
This method allows to set the platform used to quote identifiers and strings (a different one from the one used in the connection)
1 parent af88971 commit 60e9263

File tree

2 files changed

+30
-2
lines changed

2 files changed

+30
-2
lines changed

src/Mouf/Database/MagicQuery.php

Lines changed: 20 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
namespace Mouf\Database;
44

55
use Doctrine\DBAL\Connection;
6+
use Doctrine\DBAL\Platforms\AbstractPlatform;
67
use Doctrine\DBAL\Platforms\MySqlPlatform;
78
use function array_filter;
89
use function array_keys;
@@ -34,6 +35,10 @@ class MagicQuery
3435
private $connection;
3536
private $cache;
3637
private $schemaAnalyzer;
38+
/**
39+
* @var AbstractPlatform
40+
*/
41+
private $platform;
3742
/**
3843
* @var \Twig_Environment
3944
*/
@@ -48,6 +53,7 @@ class MagicQuery
4853
public function __construct($connection = null, $cache = null, SchemaAnalyzer $schemaAnalyzer = null)
4954
{
5055
$this->connection = $connection;
56+
$this->platform = $connection ? $connection->getDatabasePlatform() : new MySqlPlatform();
5157
if ($cache) {
5258
$this->cache = $cache;
5359
} else {
@@ -73,6 +79,19 @@ public function setEnableTwig($enableTwig = true)
7379
return $this;
7480
}
7581

82+
/**
83+
* Overrides the output dialect used to generate SQL. By default, the dialect of the connection is used.
84+
* If no connection is used, MySQL platform is used by default.
85+
*
86+
* @param AbstractPlatform $platform
87+
*/
88+
public function setOutputDialect(AbstractPlatform $platform): self
89+
{
90+
$this->platform = $platform;
91+
92+
return $this;
93+
}
94+
7695
/**
7796
* Returns merged SQL from $sql and $parameters. Any parameters not available will be striped down
7897
* from the SQL.
@@ -175,8 +194,7 @@ public function parse($sql)
175194
*/
176195
public function toSql(NodeInterface $sqlNode, array $parameters = array(), bool $extrapolateParameters = true)
177196
{
178-
$platform = $this->connection ? $this->connection->getDatabasePlatform() : new MySqlPlatform();
179-
return (string) $sqlNode->toSql($parameters, $platform, 0, SqlRenderInterface::CONDITION_GUESS, $extrapolateParameters);
197+
return (string) $sqlNode->toSql($parameters, $this->platform, 0, SqlRenderInterface::CONDITION_GUESS, $extrapolateParameters);
180198
}
181199

182200
/**

tests/Mouf/Database/MagicQueryTest.php

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
namespace Mouf\Database;
44

55
use Doctrine\Common\Cache\ArrayCache;
6+
use Doctrine\DBAL\Platforms\PostgreSqlPlatform;
67
use Doctrine\DBAL\Schema\Schema;
78
use Mouf\Database\SchemaAnalyzer\SchemaAnalyzer;
89
use PHPUnit\Framework\TestCase;
@@ -434,4 +435,13 @@ public function testBuildPreparedStatement()
434435
$sql = 'SELECT id FROM users WHERE status IN :status';
435436
$this->assertEquals("SELECT id FROM users WHERE status IN (:status)", self::simplifySql($magicQuery->buildPreparedStatement($sql, ['status' => [1,2]])));
436437
}
438+
439+
public function testSetOutputDialect()
440+
{
441+
$magicQuery = new MagicQuery(null, new ArrayCache());
442+
$magicQuery->setOutputDialect(new PostgreSqlPlatform());
443+
444+
$sql = 'SELECT id FROM users';
445+
$this->assertEquals('SELECT "id" FROM "users"', self::simplifySql($magicQuery->buildPreparedStatement($sql)));
446+
}
437447
}

0 commit comments

Comments
 (0)