1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
|
4
|
|
|
namespace TheCodingMachine\TDBM\QueryFactory\SmartEagerLoad\Query; |
5
|
|
|
|
6
|
|
|
use Doctrine\DBAL\Connection; |
7
|
|
|
use Doctrine\DBAL\Platforms\MySqlPlatform; |
8
|
|
|
use Mouf\Database\MagicQuery; |
9
|
|
|
use TheCodingMachine\TDBM\QueryFactory\SmartEagerLoad\ManyToOneDataLoader; |
10
|
|
|
use TheCodingMachine\TDBM\QueryFactory\SmartEagerLoad\StorageNode; |
11
|
|
|
|
12
|
|
|
class ManyToOnePartialQuery implements PartialQuery |
13
|
|
|
{ |
14
|
|
|
/** |
15
|
|
|
* @var string |
16
|
|
|
*/ |
17
|
|
|
private $mainTable; |
18
|
|
|
/** |
19
|
|
|
* @var string |
20
|
|
|
*/ |
21
|
|
|
private $key; |
22
|
|
|
/** |
23
|
|
|
* @var string |
24
|
|
|
*/ |
25
|
|
|
private $pk; |
26
|
|
|
/** |
27
|
|
|
* @var PartialQuery |
28
|
|
|
*/ |
29
|
|
|
private $partialQuery; |
30
|
|
|
/** |
31
|
|
|
* @var string |
32
|
|
|
*/ |
33
|
|
|
private $originTableName; |
34
|
|
|
/** |
35
|
|
|
* @var string |
36
|
|
|
*/ |
37
|
|
|
private $columnName; |
38
|
|
|
|
39
|
|
|
public function __construct(PartialQuery $partialQuery, string $originTableName, string $tableName, string $pk, string $columnName) |
40
|
|
|
{ |
41
|
|
|
$this->partialQuery = $partialQuery; |
42
|
|
|
$this->mainTable = $tableName; |
43
|
|
|
$this->key = $partialQuery->getKey().'__mto__'.$columnName; |
44
|
|
|
$this->pk = $pk; |
45
|
|
|
$this->originTableName = $originTableName; |
46
|
|
|
$this->columnName = $columnName; |
47
|
|
|
} |
48
|
|
|
|
49
|
|
|
/** |
50
|
|
|
* Returns the SQL of the query, starting at the FROM keyword. |
51
|
|
|
*/ |
52
|
|
|
public function getQueryFrom(): string |
53
|
|
|
{ |
54
|
|
|
$mysqlPlatform = new MySqlPlatform(); |
55
|
|
|
return 'FROM ' .$mysqlPlatform->quoteIdentifier($this->mainTable). |
56
|
|
|
' WHERE ' .$mysqlPlatform->quoteIdentifier($this->mainTable).'.'.$mysqlPlatform->quoteIdentifier($this->pk).' IN '. |
57
|
|
|
'(SELECT '.$mysqlPlatform->quoteIdentifier($this->originTableName).'.'.$mysqlPlatform->quoteIdentifier($this->columnName).' '.$this->partialQuery->getQueryFrom().')'; |
58
|
|
|
} |
59
|
|
|
|
60
|
|
|
/** |
61
|
|
|
* Returns the object in charge of storing the dataloader associated to this query. |
62
|
|
|
*/ |
63
|
|
|
public function getStorageNode(): StorageNode |
64
|
|
|
{ |
65
|
|
|
return $this->partialQuery->getStorageNode(); |
66
|
|
|
} |
67
|
|
|
|
68
|
|
|
/** |
69
|
|
|
* Returns a key representing the "path" to this query. This is meant to be used as a cache key. |
70
|
|
|
*/ |
71
|
|
|
public function getKey(): string |
72
|
|
|
{ |
73
|
|
|
return $this->key; |
74
|
|
|
} |
75
|
|
|
|
76
|
|
|
/** |
77
|
|
|
* Registers a dataloader for this query, if needed. |
78
|
|
|
*/ |
79
|
|
|
public function registerDataLoader(Connection $connection): void |
80
|
|
|
{ |
81
|
|
|
$storageNode = $this->getStorageNode(); |
82
|
|
|
if ($storageNode->hasManyToOneDataLoader($this->key)) { |
83
|
|
|
return; |
84
|
|
|
} |
85
|
|
|
|
86
|
|
|
$mysqlPlatform = new MySqlPlatform(); |
87
|
|
|
$sql = 'SELECT DISTINCT ' .$mysqlPlatform->quoteIdentifier($this->mainTable).'.* '.$this->getQueryFrom(); |
88
|
|
|
|
89
|
|
|
if (!$connection->getDatabasePlatform() instanceof MySqlPlatform) { |
90
|
|
|
// We need to convert the query from MySQL dialect to something else |
91
|
|
|
$sql = $this->getMagicQuery()->buildPreparedStatement($sql); |
92
|
|
|
} |
93
|
|
|
|
94
|
|
|
$storageNode->setManyToOneDataLoader($this->key, new ManyToOneDataLoader($connection, $sql, $this->pk)); |
95
|
|
|
} |
96
|
|
|
|
97
|
|
|
public function getMagicQuery(): MagicQuery |
98
|
|
|
{ |
99
|
|
|
return $this->partialQuery->getMagicQuery(); |
100
|
|
|
} |
101
|
|
|
} |
102
|
|
|
|