|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
|
|
4
|
|
|
namespace TheCodingMachine\TDBM\QueryFactory\SmartEagerLoad\Query; |
|
5
|
|
|
|
|
6
|
|
|
use Doctrine\DBAL\Connection; |
|
7
|
|
|
use Doctrine\DBAL\Platforms\AbstractPlatform; |
|
8
|
|
|
use Doctrine\DBAL\Platforms\MySqlPlatform; |
|
9
|
|
|
use Mouf\Database\MagicQuery; |
|
10
|
|
|
use TheCodingMachine\TDBM\QueryFactory\SmartEagerLoad\StorageNode; |
|
11
|
|
|
use TheCodingMachine\TDBM\TDBMException; |
|
12
|
|
|
use function implode; |
|
13
|
|
|
use function strpos; |
|
14
|
|
|
|
|
15
|
|
|
class StaticPartialQuery implements PartialQuery |
|
16
|
|
|
{ |
|
17
|
|
|
/** |
|
18
|
|
|
* @var string |
|
19
|
|
|
*/ |
|
20
|
|
|
private $queryFrom; |
|
21
|
|
|
/** |
|
22
|
|
|
* @var string[] |
|
23
|
|
|
*/ |
|
24
|
|
|
private $mainTables; |
|
25
|
|
|
/** |
|
26
|
|
|
* @var StorageNode |
|
27
|
|
|
*/ |
|
28
|
|
|
private $storageNode; |
|
29
|
|
|
/** |
|
30
|
|
|
* @var array<string, mixed> |
|
31
|
|
|
*/ |
|
32
|
|
|
private $parameters; |
|
33
|
|
|
/** |
|
34
|
|
|
* @var MagicQuery |
|
35
|
|
|
*/ |
|
36
|
|
|
private $magicQuery; |
|
37
|
|
|
/** |
|
38
|
|
|
* @var string |
|
39
|
|
|
*/ |
|
40
|
|
|
private $magicFrom; |
|
41
|
|
|
|
|
42
|
|
|
/** |
|
43
|
|
|
* @param array<string, mixed> $parameters |
|
44
|
|
|
* @param string[] $mainTables |
|
45
|
|
|
*/ |
|
46
|
|
|
public function __construct(string $queryFrom, array $parameters, array $mainTables, StorageNode $storageNode, MagicQuery $magicQuery) |
|
47
|
|
|
{ |
|
48
|
|
|
$this->queryFrom = $queryFrom; |
|
49
|
|
|
$this->mainTables = $mainTables; |
|
50
|
|
|
$this->storageNode = $storageNode; |
|
51
|
|
|
$this->parameters = $parameters; |
|
52
|
|
|
$this->magicQuery = $magicQuery; |
|
53
|
|
|
} |
|
54
|
|
|
|
|
55
|
|
|
/** |
|
56
|
|
|
* Returns the SQL of the query, starting at the FROM keyword. |
|
57
|
|
|
*/ |
|
58
|
|
|
public function getQueryFrom(): string |
|
59
|
|
|
{ |
|
60
|
|
|
if ($this->magicFrom === null) { |
|
61
|
|
|
// FIXME: we need to use buildPreparedStatement for better performances here. |
|
62
|
|
|
$sql = 'SELECT '; |
|
63
|
|
|
$mysqlPlatform = new MySqlPlatform(); |
|
64
|
|
|
$tables = []; |
|
65
|
|
|
foreach ($this->mainTables as $table) { |
|
66
|
|
|
$tables[] = $mysqlPlatform->quoteIdentifier($table).'.*'; |
|
67
|
|
|
} |
|
68
|
|
|
$sql .= implode(', ', $tables); |
|
69
|
|
|
$sql .= ' '.$this->queryFrom; |
|
70
|
|
|
|
|
71
|
|
|
$this->magicQuery->setOutputDialect($mysqlPlatform); |
|
72
|
|
|
$sql = $this->magicQuery->build($sql, $this->parameters); |
|
73
|
|
|
$this->magicQuery->setOutputDialect(null); |
|
74
|
|
|
$fromIndex = strpos($sql, 'FROM'); |
|
75
|
|
|
if ($fromIndex === false) { |
|
76
|
|
|
throw new TDBMException('Expected smart eager loader query to contain a "FROM"'); // @codeCoverageIgnore |
|
77
|
|
|
} |
|
78
|
|
|
$this->magicFrom = substr($sql, $fromIndex); |
|
79
|
|
|
} |
|
80
|
|
|
return $this->magicFrom; |
|
81
|
|
|
} |
|
82
|
|
|
|
|
83
|
|
|
/** |
|
84
|
|
|
* Returns a key representing the "path" to this query. This is meant to be used as a cache key. |
|
85
|
|
|
*/ |
|
86
|
|
|
public function getKey(): string |
|
87
|
|
|
{ |
|
88
|
|
|
return ''; |
|
89
|
|
|
} |
|
90
|
|
|
|
|
91
|
|
|
/** |
|
92
|
|
|
* Registers a dataloader for this query, if needed. |
|
93
|
|
|
*/ |
|
94
|
|
|
public function registerDataLoader(Connection $connection): void |
|
95
|
|
|
{ |
|
96
|
|
|
throw new TDBMException('Cannot register a dataloader for root query'); |
|
97
|
|
|
} |
|
98
|
|
|
|
|
99
|
|
|
/** |
|
100
|
|
|
* Returns the object in charge of storing the dataloader associated to this query. |
|
101
|
|
|
*/ |
|
102
|
|
|
public function getStorageNode(): StorageNode |
|
103
|
|
|
{ |
|
104
|
|
|
return $this->storageNode; |
|
105
|
|
|
} |
|
106
|
|
|
|
|
107
|
|
|
/** |
|
108
|
|
|
* @return array<string, mixed> |
|
109
|
|
|
*/ |
|
110
|
|
|
public function getParameters(): array |
|
111
|
|
|
{ |
|
112
|
|
|
return $this->parameters; |
|
113
|
|
|
} |
|
114
|
|
|
|
|
115
|
|
|
public function getMagicQuery(): MagicQuery |
|
116
|
|
|
{ |
|
117
|
|
|
return $this->magicQuery; |
|
118
|
|
|
} |
|
119
|
|
|
} |
|
120
|
|
|
|