Skip to content

Commit e64a2d1

Browse files
authored
Merge pull request #56 from moufmouf/platform_option
Adding an option to choose the SQL dialect generated
2 parents bc9b2f5 + 0fbf498 commit e64a2d1

30 files changed

+285
-248
lines changed

README.md

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -154,6 +154,15 @@ $conn = \Doctrine\DBAL\DriverManager::getConnection($connectionParams, $config);
154154
$magicQuery = new \Mouf\Database\MagicQuery($conn);
155155
```
156156

157+
Also, if you have no connection to your database configured but you want to generate SQL in some specific dialect, you can
158+
instead set the DBAL database platform used:
159+
160+
```php
161+
$magicQuery->setOutputDialect(new \Doctrine\DBAL\Platforms\PostgreSqlPlatform());
162+
$magicQuery = new \Mouf\Database\MagicQuery();
163+
```
164+
165+
157166
What about performances?
158167
------------------------
159168

phpstan.neon

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
parameters:
2-
level: 2
2+
level: 7
3+
inferPrivatePropertyTypeFromConstructor: true
34
ignoreErrors:
45
- "#Mouf\\\\MoufManager#"
5-
- "#Mouf\\\\MoufInstanceDescriptor#"
6+
- "#Mouf\\\\MoufInstanceDescriptor#"
7+
- '#Method Mouf\\Database\\QueryWriter\\Utils\\FindParametersService::findParameters\(\) should return array<string> but returns array<int, int\|string>#'

src/Mouf/Database/MagicQuery.php

Lines changed: 25 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,9 @@
22

33
namespace Mouf\Database;
44

5+
use Doctrine\DBAL\Connection;
6+
use Doctrine\DBAL\Platforms\AbstractPlatform;
7+
use Doctrine\DBAL\Platforms\MySqlPlatform;
58
use function array_filter;
69
use function array_keys;
710
use Doctrine\Common\Cache\VoidCache;
@@ -32,6 +35,10 @@ class MagicQuery
3235
private $connection;
3336
private $cache;
3437
private $schemaAnalyzer;
38+
/**
39+
* @var AbstractPlatform
40+
*/
41+
private $platform;
3542
/**
3643
* @var \Twig_Environment
3744
*/
@@ -46,6 +53,7 @@ class MagicQuery
4653
public function __construct($connection = null, $cache = null, SchemaAnalyzer $schemaAnalyzer = null)
4754
{
4855
$this->connection = $connection;
56+
$this->platform = $connection ? $connection->getDatabasePlatform() : new MySqlPlatform();
4957
if ($cache) {
5058
$this->cache = $cache;
5159
} else {
@@ -71,6 +79,19 @@ public function setEnableTwig($enableTwig = true)
7179
return $this;
7280
}
7381

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+
7495
/**
7596
* Returns merged SQL from $sql and $parameters. Any parameters not available will be striped down
7697
* from the SQL.
@@ -173,7 +194,7 @@ public function parse($sql)
173194
*/
174195
public function toSql(NodeInterface $sqlNode, array $parameters = array(), bool $extrapolateParameters = true)
175196
{
176-
return $sqlNode->toSql($parameters, $this->connection, 0, SqlRenderInterface::CONDITION_GUESS, $extrapolateParameters);
197+
return (string) $sqlNode->toSql($parameters, $this->platform, 0, SqlRenderInterface::CONDITION_GUESS, $extrapolateParameters);
177198
}
178199

179200
/**
@@ -284,15 +305,15 @@ private function getSchemaAnalyzer()
284305
throw new MagicQueryMissingConnectionException('In order to use MagicJoin, you need to configure a DBAL connection.');
285306
}
286307

287-
$this->schemaAnalyzer = new SchemaAnalyzer($this->connection->getSchemaManager(), $this->cache, $this->getConnectionUniqueId());
308+
$this->schemaAnalyzer = new SchemaAnalyzer($this->connection->getSchemaManager(), $this->cache, $this->getConnectionUniqueId($this->connection));
288309
}
289310

290311
return $this->schemaAnalyzer;
291312
}
292313

293-
private function getConnectionUniqueId()
314+
private function getConnectionUniqueId(Connection $connection)
294315
{
295-
return hash('md4', $this->connection->getHost().'-'.$this->connection->getPort().'-'.$this->connection->getDatabase().'-'.$this->connection->getDriver()->getName());
316+
return hash('md4', $connection->getHost().'-'.$connection->getPort().'-'.$connection->getDatabase().'-'.$connection->getDriver()->getName());
296317
}
297318

298319
/**

src/Mouf/Database/QueryWriter/QueryResult.php

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -75,21 +75,21 @@ public function setParameters($parameters)
7575
public function val()
7676
{
7777
$parameters = ValueUtils::val($this->parameters);
78-
$pdoStatement = $this->connection->query($this->select->toSql($parameters, $this->connection).DbHelper::getFromLimitString($this->offset, $this->limit));
78+
$pdoStatement = $this->connection->query($this->select->toSql($parameters, $this->connection->getDatabasePlatform()).DbHelper::getFromLimitString($this->offset, $this->limit));
7979

8080
return new ResultSet($pdoStatement);
8181
}
8282

8383
/**
8484
* Returns the SQL for this query-result (without pagination, but with parameters accounted for).
8585
*
86-
* @return string
86+
* @return string|null
8787
*/
8888
public function toSql()
8989
{
9090
$parameters = ValueUtils::val($this->parameters);
9191

92-
return $this->select->toSql($parameters, $this->connection);
92+
return $this->select->toSql($parameters, $this->connection->getDatabasePlatform());
9393
}
9494

9595
/**
@@ -133,9 +133,9 @@ public function sort($key, $direction = SortableInterface::ASC)
133133
*
134134
* @param string $key
135135
*
136-
* @return NodeInterface
136+
* @return NodeInterface|null
137137
*/
138-
private function findColumnByKey($key)
138+
private function findColumnByKey($key): ?NodeInterface
139139
{
140140
$columns = $this->select->getColumns();
141141
foreach ($columns as $column) {
@@ -152,6 +152,6 @@ private function findColumnByKey($key)
152152
}
153153
}
154154

155-
return;
155+
return null;
156156
}
157157
}

src/Mouf/Database/QueryWriter/ResultSet.php

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22

33
namespace Mouf\Database\QueryWriter;
44

5+
use Doctrine\DBAL\Statement;
6+
57
/**
68
* Wraps the results of a PDOStatement.
79
*
@@ -10,7 +12,7 @@
1012
class ResultSet implements \Iterator
1113
{
1214
/**
13-
* @var \PDOStatement
15+
* @var \PDOStatement|Statement
1416
*/
1517
private $statement;
1618
private $castToClass;
@@ -19,7 +21,7 @@ class ResultSet implements \Iterator
1921
private $fetched = false;
2022
private $rewindCalls = 0;
2123

22-
public function __construct(\PDOStatement $statement, $castToClass = '')
24+
public function __construct($statement, $castToClass = '')
2325
{
2426
$this->statement = $statement;
2527
$this->castToClass = $castToClass;

src/SQLParser/Node/AbstractManyInstancesOperator.php

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
namespace SQLParser\Node;
44

5-
use Doctrine\DBAL\Connection;
5+
use Doctrine\DBAL\Platforms\AbstractPlatform;
66
use Mouf\MoufManager;
77
use Mouf\MoufInstanceDescriptor;
88
use SQLParser\Node\Traverser\NodeTraverser;
@@ -56,18 +56,19 @@ public function toInstanceDescriptor(MoufManager $moufManager)
5656
/**
5757
* Renders the object as a SQL string.
5858
*
59-
* @param Connection $dbConnection
60-
* @param array $parameters
61-
* @param number $indent
62-
* @param int $conditionsMode
59+
* @param array $parameters
60+
* @param AbstractPlatform $platform
61+
* @param int $indent
62+
* @param int $conditionsMode
6363
*
64+
* @param bool $extrapolateParameters
6465
* @return string
6566
*/
66-
public function toSql(array $parameters = array(), Connection $dbConnection = null, $indent = 0, $conditionsMode = self::CONDITION_APPLY, bool $extrapolateParameters = true)
67+
public function toSql(array $parameters, AbstractPlatform $platform, int $indent = 0, $conditionsMode = self::CONDITION_APPLY, bool $extrapolateParameters = true): ?string
6768
{
6869
$sqlOperands = array();
6970
foreach ($this->operands as $operand) {
70-
$sql = NodeFactory::toSql($operand, $dbConnection, $parameters, ' ', true, $indent, $conditionsMode, $extrapolateParameters);
71+
$sql = NodeFactory::toSql($operand, $platform, $parameters, ' ', true, $indent, $conditionsMode, $extrapolateParameters);
7172
if ($sql != null) {
7273
$sqlOperands[] = $sql;
7374
}

src/SQLParser/Node/AbstractTwoOperandsOperator.php

Lines changed: 12 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,8 @@
22

33
namespace SQLParser\Node;
44

5+
use Doctrine\DBAL\Platforms\AbstractPlatform;
56
use Mouf\Utils\Common\ConditionInterface\ConditionTrait;
6-
use Doctrine\DBAL\Connection;
77
use Mouf\MoufManager;
88
use Mouf\MoufInstanceDescriptor;
99
use SQLParser\Node\Traverser\NodeTraverser;
@@ -89,14 +89,15 @@ public function toInstanceDescriptor(MoufManager $moufManager)
8989
/**
9090
* Renders the object as a SQL string.
9191
*
92-
* @param Connection $dbConnection
93-
* @param array $parameters
94-
* @param number $indent
95-
* @param int $conditionsMode
92+
* @param array $parameters
93+
* @param AbstractPlatform $platform
94+
* @param int $indent
95+
* @param int $conditionsMode
9696
*
97+
* @param bool $extrapolateParameters
9798
* @return string
9899
*/
99-
public function toSql(array $parameters = array(), Connection $dbConnection = null, $indent = 0, $conditionsMode = self::CONDITION_APPLY, bool $extrapolateParameters = true)
100+
public function toSql(array $parameters, AbstractPlatform $platform, int $indent = 0, $conditionsMode = self::CONDITION_APPLY, bool $extrapolateParameters = true): ?string
100101
{
101102
if ($conditionsMode == self::CONDITION_GUESS) {
102103
$bypass = false;
@@ -107,25 +108,25 @@ public function toSql(array $parameters = array(), Connection $dbConnection = nu
107108
$bypass = true;
108109
}
109110
if ($bypass === true) {
110-
return;
111+
return null;
111112
} else {
112113
$conditionsMode = self::CONDITION_IGNORE;
113114
}
114115
}
115116
if ($conditionsMode == self::CONDITION_IGNORE || !$this->condition || $this->condition->isOk($parameters)) {
116-
$sql = $this->getSql($parameters, $dbConnection, $indent, $conditionsMode, $extrapolateParameters);
117+
$sql = $this->getSql($parameters, $platform, $indent, $conditionsMode, $extrapolateParameters);
117118
} else {
118119
$sql = null;
119120
}
120121

121122
return $sql;
122123
}
123124

124-
protected function getSql(array $parameters = array(), Connection $dbConnection = null, $indent = 0, $conditionsMode = self::CONDITION_APPLY, bool $extrapolateParameters = true)
125+
protected function getSql(array $parameters, AbstractPlatform $platform, $indent = 0, $conditionsMode = self::CONDITION_APPLY, bool $extrapolateParameters = true)
125126
{
126-
$sql = NodeFactory::toSql($this->leftOperand, $dbConnection, $parameters, ' ', false, $indent, $conditionsMode, $extrapolateParameters);
127+
$sql = NodeFactory::toSql($this->leftOperand, $platform, $parameters, ' ', false, $indent, $conditionsMode, $extrapolateParameters);
127128
$sql .= ' '.$this->getOperatorSymbol().' ';
128-
$sql .= NodeFactory::toSql($this->rightOperand, $dbConnection, $parameters, ' ', false, $indent, $conditionsMode, $extrapolateParameters);
129+
$sql .= NodeFactory::toSql($this->rightOperand, $platform, $parameters, ' ', false, $indent, $conditionsMode, $extrapolateParameters);
129130

130131
return $sql;
131132
}

src/SQLParser/Node/AggregateFunction.php

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@
3232
*/
3333
namespace SQLParser\Node;
3434

35-
use Doctrine\DBAL\Connection;
35+
use Doctrine\DBAL\Platforms\AbstractPlatform;
3636
use Mouf\MoufManager;
3737
use Mouf\MoufInstanceDescriptor;
3838
use SQLParser\Node\Traverser\NodeTraverser;
@@ -144,16 +144,17 @@ public function toInstanceDescriptor(MoufManager $moufManager)
144144
/**
145145
* Renders the object as a SQL string.
146146
*
147-
* @param Connection $dbConnection
148-
* @param array $parameters
149-
* @param number $indent
150-
* @param int $conditionsMode
147+
* @param array $parameters
148+
* @param AbstractPlatform $platform
149+
* @param int $indent
150+
* @param int $conditionsMode
151151
*
152+
* @param bool $extrapolateParameters
152153
* @return string
153154
*/
154-
public function toSql(array $parameters = array(), Connection $dbConnection = null, $indent = 0, $conditionsMode = self::CONDITION_APPLY, bool $extrapolateParameters = true)
155+
public function toSql(array $parameters, AbstractPlatform $platform, int $indent = 0, $conditionsMode = self::CONDITION_APPLY, bool $extrapolateParameters = true): ?string
155156
{
156-
$subTreeSql = NodeFactory::toSql($this->subTree, $dbConnection, $parameters, ', ', false, $indent, $conditionsMode, $extrapolateParameters);
157+
$subTreeSql = NodeFactory::toSql($this->subTree, $platform, $parameters, ', ', false, $indent, $conditionsMode, $extrapolateParameters);
157158
if ($subTreeSql !== null) {
158159
$sql = $this->functionName.'(';
159160
$sql .= $subTreeSql;

0 commit comments

Comments
 (0)