Skip to content

Commit 3edd444

Browse files
committed
Using internally AbstractPlatform instead of Connection.
1 parent bc9b2f5 commit 3edd444

27 files changed

+211
-216
lines changed

src/Mouf/Database/MagicQuery.php

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
namespace Mouf\Database;
44

5+
use Doctrine\DBAL\Platforms\MySqlPlatform;
56
use function array_filter;
67
use function array_keys;
78
use Doctrine\Common\Cache\VoidCache;
@@ -173,7 +174,8 @@ public function parse($sql)
173174
*/
174175
public function toSql(NodeInterface $sqlNode, array $parameters = array(), bool $extrapolateParameters = true)
175176
{
176-
return $sqlNode->toSql($parameters, $this->connection, 0, SqlRenderInterface::CONDITION_GUESS, $extrapolateParameters);
177+
$platform = $this->connection ? $this->connection->getDatabasePlatform() : new MySqlPlatform();
178+
return $sqlNode->toSql($parameters, $platform, 0, SqlRenderInterface::CONDITION_GUESS, $extrapolateParameters);
177179
}
178180

179181
/**

src/Mouf/Database/QueryWriter/QueryResult.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -75,7 +75,7 @@ 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
}
@@ -89,7 +89,7 @@ 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
/**

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, $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, $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 \SQLParser\Node\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, $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;

src/SQLParser/Node/Between.php

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

33
namespace SQLParser\Node;
44

5-
use Doctrine\DBAL\Connection;
5+
use Doctrine\DBAL\Platforms\AbstractPlatform;
6+
67
use Mouf\MoufManager;
78
use Mouf\MoufInstanceDescriptor;
89
use Mouf\Utils\Common\ConditionInterface\ConditionInterface;
@@ -145,14 +146,15 @@ public function toInstanceDescriptor(MoufManager $moufManager)
145146
/**
146147
* Renders the object as a SQL string.
147148
*
148-
* @param Connection $dbConnection
149-
* @param array $parameters
150-
* @param number $indent
151-
* @param int $conditionsMode
149+
* @param array $parameters
150+
* @param AbstractPlatform $platform
151+
* @param int $indent
152+
* @param int $conditionsMode
152153
*
154+
* @param bool $extrapolateParameters
153155
* @return string
154156
*/
155-
public function toSql(array $parameters = array(), Connection $dbConnection = null, $indent = 0, $conditionsMode = self::CONDITION_APPLY, bool $extrapolateParameters = true)
157+
public function toSql(array $parameters, AbstractPlatform $platform, $indent = 0, $conditionsMode = self::CONDITION_APPLY, bool $extrapolateParameters = true): ?string
156158
{
157159
$minBypass = false;
158160
$maxBypass = false;
@@ -182,19 +184,19 @@ public function toSql(array $parameters = array(), Connection $dbConnection = nu
182184
}
183185

184186
if (!$minBypass && !$maxBypass) {
185-
$sql = NodeFactory::toSql($this->leftOperand, $dbConnection, $parameters, ' ', false, $indent, $conditionsMode, $extrapolateParameters);
187+
$sql = NodeFactory::toSql($this->leftOperand, $platform, $parameters, ' ', false, $indent, $conditionsMode, $extrapolateParameters);
186188
$sql .= ' BETWEEN ';
187-
$sql .= NodeFactory::toSql($this->minValueOperand, $dbConnection, $parameters, ' ', false, $indent, $conditionsMode, $extrapolateParameters);
189+
$sql .= NodeFactory::toSql($this->minValueOperand, $platform, $parameters, ' ', false, $indent, $conditionsMode, $extrapolateParameters);
188190
$sql .= ' AND ';
189-
$sql .= NodeFactory::toSql($this->maxValueOperand, $dbConnection, $parameters, ' ', false, $indent, $conditionsMode, $extrapolateParameters);
191+
$sql .= NodeFactory::toSql($this->maxValueOperand, $platform, $parameters, ' ', false, $indent, $conditionsMode, $extrapolateParameters);
190192
} elseif (!$minBypass && $maxBypass) {
191-
$sql = NodeFactory::toSql($this->leftOperand, $dbConnection, $parameters, ' ', false, $indent, $conditionsMode, $extrapolateParameters);
193+
$sql = NodeFactory::toSql($this->leftOperand, $platform, $parameters, ' ', false, $indent, $conditionsMode, $extrapolateParameters);
192194
$sql .= ' >= ';
193-
$sql .= NodeFactory::toSql($this->minValueOperand, $dbConnection, $parameters, ' ', false, $indent, $conditionsMode, $extrapolateParameters);
195+
$sql .= NodeFactory::toSql($this->minValueOperand, $platform, $parameters, ' ', false, $indent, $conditionsMode, $extrapolateParameters);
194196
} elseif ($minBypass && !$maxBypass) {
195-
$sql = NodeFactory::toSql($this->leftOperand, $dbConnection, $parameters, ' ', false, $indent, $conditionsMode, $extrapolateParameters);
197+
$sql = NodeFactory::toSql($this->leftOperand, $platform, $parameters, ' ', false, $indent, $conditionsMode, $extrapolateParameters);
196198
$sql .= ' <= ';
197-
$sql .= NodeFactory::toSql($this->maxValueOperand, $dbConnection, $parameters, ' ', false, $indent, $conditionsMode, $extrapolateParameters);
199+
$sql .= NodeFactory::toSql($this->maxValueOperand, $platform, $parameters, ' ', false, $indent, $conditionsMode, $extrapolateParameters);
198200
} else {
199201
$sql = null;
200202
}

src/SQLParser/Node/CaseOperation.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;
@@ -52,16 +52,17 @@ public function toInstanceDescriptor(MoufManager $moufManager)
5252
/**
5353
* Renders the object as a SQL string.
5454
*
55-
* @param Connection $dbConnection
56-
* @param array $parameters
57-
* @param number $indent
58-
* @param int $conditionsMode
55+
* @param array $parameters
56+
* @param AbstractPlatform $platform
57+
* @param int $indent
58+
* @param int $conditionsMode
5959
*
60+
* @param bool $extrapolateParameters
6061
* @return string
6162
*/
62-
public function toSql(array $parameters = array(), Connection $dbConnection = null, $indent = 0, $conditionsMode = self::CONDITION_APPLY, bool $extrapolateParameters = true)
63+
public function toSql(array $parameters, AbstractPlatform $platform, $indent = 0, $conditionsMode = self::CONDITION_APPLY, bool $extrapolateParameters = true): ?string
6364
{
64-
$sql = 'CASE '.NodeFactory::toSql($this->operation, $dbConnection, $parameters, ' ', false, $indent, $conditionsMode).' END';
65+
$sql = 'CASE '.NodeFactory::toSql($this->operation, $platform, $parameters, ' ', false, $indent, $conditionsMode).' END';
6566

6667
return $sql;
6768
}

src/SQLParser/Node/ColRef.php

Lines changed: 10 additions & 9 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\VisitorInterface;
@@ -184,24 +184,25 @@ public function toInstanceDescriptor(MoufManager $moufManager)
184184
/**
185185
* Renders the object as a SQL string.
186186
*
187-
* @param Connection $dbConnection
188-
* @param array $parameters
189-
* @param number $indent
190-
* @param int $conditionsMode
187+
* @param array $parameters
188+
* @param AbstractPlatform $platform
189+
* @param int $indent
190+
* @param int $conditionsMode
191191
*
192+
* @param bool $extrapolateParameters
192193
* @return string
193194
*/
194-
public function toSql(array $parameters = array(), Connection $dbConnection = null, $indent = 0, $conditionsMode = self::CONDITION_APPLY, bool $extrapolateParameters = true)
195+
public function toSql(array $parameters, AbstractPlatform $platform, $indent = 0, $conditionsMode = self::CONDITION_APPLY, bool $extrapolateParameters = true): ?string
195196
{
196197
$sql = '';
197198
if ($this->database) {
198-
$sql .= NodeFactory::escapeDBItem($this->database, $dbConnection).'.';
199+
$sql .= $platform->quoteSingleIdentifier($this->database).'.';
199200
}
200201
if ($this->table) {
201-
$sql .= NodeFactory::escapeDBItem($this->table, $dbConnection).'.';
202+
$sql .= $platform->quoteSingleIdentifier($this->table).'.';
202203
}
203204
if ($this->column !== '*') {
204-
$sql .= NodeFactory::escapeDBItem($this->column, $dbConnection);
205+
$sql .= $platform->quoteSingleIdentifier($this->column);
205206
} else {
206207
$sql .= '*';
207208
}

src/SQLParser/Node/ConstNode.php

Lines changed: 8 additions & 9 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\VisitorInterface;
@@ -98,23 +98,22 @@ public function toInstanceDescriptor(MoufManager $moufManager)
9898
/**
9999
* Renders the object as a SQL string.
100100
*
101-
* @param Connection $dbConnection
102-
* @param array $parameters
103-
* @param number $indent
104-
* @param int $conditionsMode
101+
* @param array $parameters
102+
* @param AbstractPlatform $platform
103+
* @param int $indent
104+
* @param int $conditionsMode
105105
*
106+
* @param bool $extrapolateParameters
106107
* @return string
107108
*/
108-
public function toSql(array $parameters = array(), Connection $dbConnection = null, $indent = 0, $conditionsMode = self::CONDITION_APPLY, bool $extrapolateParameters = true)
109+
public function toSql(array $parameters, AbstractPlatform $platform, $indent = 0, $conditionsMode = self::CONDITION_APPLY, bool $extrapolateParameters = true): ?string
109110
{
110111
if ($this->value === null) {
111112
return 'NULL';
112113
} elseif (!$this->isString) {
113114
return $this->value;
114-
} elseif ($dbConnection != null) {
115-
return $dbConnection->quote($this->value);
116115
} else {
117-
return "'".addslashes($this->value)."'";
116+
return $platform->quoteStringLiteral($this->value);
118117
}
119118
}
120119

src/SQLParser/Node/Different.php

Lines changed: 4 additions & 4 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

77
/**
88
* This class represents a Is operation in an SQL expression.
@@ -21,7 +21,7 @@ protected function getOperatorSymbol()
2121
return '<>';
2222
}
2323

24-
protected function getSql(array $parameters = array(), Connection $dbConnection = null, $indent = 0, $conditionsMode = self::CONDITION_APPLY, bool $extrapolateParameters = true)
24+
protected function getSql(array $parameters, AbstractPlatform $platform, $indent = 0, $conditionsMode = self::CONDITION_APPLY, bool $extrapolateParameters = true)
2525
{
2626
$rightOperand = $this->getRightOperand();
2727
if ($rightOperand instanceof Parameter && !isset($parameters[$rightOperand->getName()])) {
@@ -30,12 +30,12 @@ protected function getSql(array $parameters = array(), Connection $dbConnection
3030
$isNull = false;
3131
}
3232

33-
$sql = NodeFactory::toSql($this->getLeftOperand(), $dbConnection, $parameters, ' ', false, $indent, $conditionsMode, $extrapolateParameters);
33+
$sql = NodeFactory::toSql($this->getLeftOperand(), $platform, $parameters, ' ', false, $indent, $conditionsMode, $extrapolateParameters);
3434
if ($isNull) {
3535
$sql .= ' IS NOT null';
3636
} else {
3737
$sql .= ' '.$this->getOperatorSymbol().' ';
38-
$sql .= NodeFactory::toSql($rightOperand, $dbConnection, $parameters, ' ', false, $indent, $conditionsMode, $extrapolateParameters);
38+
$sql .= NodeFactory::toSql($rightOperand, $platform, $parameters, ' ', false, $indent, $conditionsMode, $extrapolateParameters);
3939
}
4040

4141
return $sql;

0 commit comments

Comments
 (0)