Skip to content

Commit d8b675d

Browse files
committed
Fixing output dialect
1 parent 9baee5a commit d8b675d

File tree

3 files changed

+41
-24
lines changed

3 files changed

+41
-24
lines changed

src/QueryFactory/SmartEagerLoad/Query/ManyToOnePartialQuery.php

Lines changed: 33 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55

66
use Doctrine\DBAL\Connection;
77
use Doctrine\DBAL\Platforms\MySqlPlatform;
8+
use Mouf\Database\MagicQuery;
89
use TheCodingMachine\TDBM\QueryFactory\SmartEagerLoad\ManyToOneDataLoader;
910
use TheCodingMachine\TDBM\QueryFactory\SmartEagerLoad\StorageNode;
1011

@@ -13,59 +14,56 @@ class ManyToOnePartialQuery implements PartialQuery
1314
/**
1415
* @var string
1516
*/
16-
private $queryFrom;
17+
private $mainTable;
1718
/**
1819
* @var string
1920
*/
20-
private $mainTable;
21+
private $key;
2122
/**
22-
* @var StorageNode
23+
* @var string
2324
*/
24-
private $storageNode;
25+
private $pk;
26+
/**
27+
* @var PartialQuery
28+
*/
29+
private $partialQuery;
2530
/**
2631
* @var string
2732
*/
28-
private $key;
33+
private $originTableName;
2934
/**
3035
* @var string
3136
*/
32-
private $pk;
37+
private $columnName;
3338

3439
public function __construct(PartialQuery $partialQuery, string $originTableName, string $tableName, string $pk, string $columnName)
3540
{
3641
// TODO: move this in a separate function. The constructor is called for every bean.
37-
$mysqlPlatform = new MySqlPlatform();
38-
$this->queryFrom = 'FROM ' .$mysqlPlatform->quoteIdentifier($tableName).
39-
' WHERE ' .$mysqlPlatform->quoteIdentifier($tableName).'.'.$mysqlPlatform->quoteIdentifier($pk).' IN '.
40-
'(SELECT '.$mysqlPlatform->quoteIdentifier($originTableName).'.'.$mysqlPlatform->quoteIdentifier($columnName).' '.$partialQuery->getQueryFrom().')';
42+
$this->partialQuery = $partialQuery;
4143
$this->mainTable = $tableName;
42-
$this->storageNode = $partialQuery->getStorageNode();
4344
$this->key = $partialQuery->getKey().'__'.$columnName;
4445
$this->pk = $pk;
46+
$this->originTableName = $originTableName;
47+
$this->columnName = $columnName;
4548
}
4649

4750
/**
4851
* Returns the SQL of the query, starting at the FROM keyword.
4952
*/
5053
public function getQueryFrom(): string
5154
{
52-
return $this->queryFrom;
53-
}
54-
55-
/**
56-
* Returns the name of the main table (main objects returned by this query)
57-
*/
58-
public function getMainTable(): string
59-
{
60-
return $this->mainTable;
55+
$mysqlPlatform = new MySqlPlatform();
56+
return 'FROM ' .$mysqlPlatform->quoteIdentifier($this->mainTable).
57+
' WHERE ' .$mysqlPlatform->quoteIdentifier($this->mainTable).'.'.$mysqlPlatform->quoteIdentifier($this->pk).' IN '.
58+
'(SELECT '.$mysqlPlatform->quoteIdentifier($this->originTableName).'.'.$mysqlPlatform->quoteIdentifier($this->columnName).' '.$this->partialQuery->getQueryFrom().')';
6159
}
6260

6361
/**
6462
* Returns the object in charge of storing the dataloader associated to this query.
6563
*/
6664
public function getStorageNode(): StorageNode
6765
{
68-
return $this->storageNode;
66+
return $this->partialQuery->getStorageNode();
6967
}
7068

7169
/**
@@ -81,13 +79,24 @@ public function getKey(): string
8179
*/
8280
public function registerDataLoader(Connection $connection): void
8381
{
84-
if ($this->storageNode->hasManyToOneDataLoader($this->key)) {
82+
$storageNode = $this->getStorageNode();
83+
if ($storageNode->hasManyToOneDataLoader($this->key)) {
8584
return;
8685
}
8786

8887
$mysqlPlatform = new MySqlPlatform();
89-
$sql = 'SELECT DISTINCT ' .$mysqlPlatform->quoteIdentifier($this->mainTable).'.* '.$this->queryFrom;
88+
$sql = 'SELECT DISTINCT ' .$mysqlPlatform->quoteIdentifier($this->mainTable).'.* '.$this->getQueryFrom();
9089

91-
$this->storageNode->setManyToOneDataLoader($this->key, new ManyToOneDataLoader($connection, $sql, $this->pk));
90+
if (!$connection->getDatabasePlatform() instanceof MySqlPlatform) {
91+
// We need to convert the query from MySQL dialect to something else
92+
$sql = $this->getMagicQuery()->buildPreparedStatement($sql);
93+
}
94+
95+
$storageNode->setManyToOneDataLoader($this->key, new ManyToOneDataLoader($connection, $sql, $this->pk));
96+
}
97+
98+
public function getMagicQuery(): MagicQuery
99+
{
100+
return $this->partialQuery->getMagicQuery();
92101
}
93102
}

src/QueryFactory/SmartEagerLoad/Query/PartialQuery.php

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
namespace TheCodingMachine\TDBM\QueryFactory\SmartEagerLoad\Query;
55

66
use Doctrine\DBAL\Connection;
7+
use Mouf\Database\MagicQuery;
78
use TheCodingMachine\TDBM\QueryFactory\SmartEagerLoad\StorageNode;
89

910
/**
@@ -31,4 +32,6 @@ public function registerDataLoader(Connection $connection): void;
3132
* Returns the object in charge of storing the dataloader associated to this query.
3233
*/
3334
public function getStorageNode(): StorageNode;
35+
36+
public function getMagicQuery(): MagicQuery;
3437
}

src/QueryFactory/SmartEagerLoad/Query/StaticPartialQuery.php

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -111,4 +111,9 @@ public function getParameters(): array
111111
{
112112
return $this->parameters;
113113
}
114+
115+
public function getMagicQuery(): MagicQuery
116+
{
117+
return $this->magicQuery;
118+
}
114119
}

0 commit comments

Comments
 (0)