1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/** |
4
|
|
|
* This file is part of Cycle ORM package. |
5
|
|
|
* |
6
|
|
|
* For the full copyright and license information, please view the LICENSE |
7
|
|
|
* file that was distributed with this source code. |
8
|
|
|
*/ |
9
|
|
|
|
10
|
|
|
declare(strict_types=1); |
11
|
|
|
|
12
|
|
|
namespace Cycle\Database\Injection; |
13
|
|
|
|
14
|
|
|
use Cycle\Database\Driver\CompilerInterface; |
15
|
|
|
use Cycle\Database\Query\Interpolator; |
16
|
|
|
use Cycle\Database\Query\QueryParameters; |
17
|
|
|
use Cycle\Database\Query\SelectQuery; |
18
|
|
|
|
19
|
|
|
class SubQuery implements FragmentInterface |
20
|
|
|
{ |
21
|
|
|
private SelectQuery $query; |
22
|
|
|
private string $alias; |
23
|
|
|
|
24
|
|
|
/** @var ParameterInterface[] */ |
25
|
|
|
private array $parameters; |
26
|
|
|
|
27
|
|
|
public function __construct(SelectQuery $query, string $alias) |
28
|
|
|
{ |
29
|
|
|
$this->query = $query; |
30
|
|
|
$this->alias = $alias; |
31
|
|
|
|
32
|
|
|
$parameters = new QueryParameters(); |
33
|
|
|
$this->query->sqlStatement($parameters); |
34
|
|
|
$this->parameters = $parameters->getParameters(); |
35
|
|
|
} |
36
|
|
|
|
37
|
|
|
public function getType(): int |
38
|
|
|
{ |
39
|
|
|
return CompilerInterface::SUBQUERY; |
40
|
|
|
} |
41
|
|
|
|
42
|
|
|
public function getTokens(): array |
43
|
|
|
{ |
44
|
|
|
return \array_merge( |
45
|
|
|
[ |
46
|
|
|
'alias' => $this->alias, |
47
|
|
|
'parameters' => $this->parameters, |
48
|
|
|
], |
49
|
|
|
$this->query->getTokens(), |
50
|
|
|
); |
51
|
|
|
} |
52
|
|
|
|
53
|
|
|
public function getQuery(): SelectQuery |
54
|
|
|
{ |
55
|
|
|
return $this->query; |
56
|
|
|
} |
57
|
|
|
|
58
|
|
|
public function __toString(): string |
59
|
|
|
{ |
60
|
|
|
$parameters = new QueryParameters(); |
61
|
|
|
|
62
|
|
|
return Interpolator::interpolate( |
63
|
|
|
$this->query->sqlStatement($parameters), |
64
|
|
|
$parameters->getParameters(), |
65
|
|
|
); |
66
|
|
|
} |
67
|
|
|
} |
68
|
|
|
|