Passed
Pull Request — master (#172)
by David
06:43
created

StaticPartialQuery::getQueryFrom()   A

Complexity

Conditions 4
Paths 3

Size

Total Lines 21
Code Lines 14

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 14
c 1
b 0
f 0
dl 0
loc 21
rs 9.7998
cc 4
nc 3
nop 0
1
<?php
2
3
4
namespace TheCodingMachine\TDBM\QueryFactory\SmartEagerLoad\Query;
5
6
7
use Doctrine\DBAL\Connection;
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
            $sql = $this->magicQuery->build($sql, $this->parameters);
72
            $fromIndex = strpos($sql, 'FROM');
73
            if ($fromIndex === false) {
74
                throw new TDBMException('Expected smart eager loader query to contain a "FROM"');
75
            }
76
            $this->magicFrom = substr($sql, $fromIndex);
77
        }
78
        return $this->magicFrom;
79
    }
80
81
    /**
82
     * Returns a key representing the "path" to this query. This is meant to be used as a cache key.
83
     */
84
    public function getKey(): string
85
    {
86
        return '';
87
    }
88
89
    /**
90
     * Registers a dataloader for this query, if needed.
91
     */
92
    public function registerDataLoader(Connection $connection): void
93
    {
94
        throw new TDBMException('Cannot register a dataloader for root query');
95
    }
96
97
    /**
98
     * Returns the object in charge of storing the dataloader associated to this query.
99
     */
100
    public function getStorageNode(): StorageNode
101
    {
102
        return $this->storageNode;
103
    }
104
105
    /**
106
     * @return array<string, mixed>
107
     */
108
    public function getParameters(): array
109
    {
110
        return $this->parameters;
111
    }
112
}
113