1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Bdf\Prime\Query\Extension; |
4
|
|
|
|
5
|
|
|
use Bdf\Prime\Exception\PrimeException; |
6
|
|
|
use Bdf\Prime\Query\Compiler\CompilerState; |
7
|
|
|
use Bdf\Prime\Query\Compiler\DeleteCompilerInterface; |
8
|
|
|
use Bdf\Prime\Query\Compiler\InsertCompilerInterface; |
9
|
|
|
use Bdf\Prime\Query\Compiler\SelectCompilerInterface; |
10
|
|
|
use Bdf\Prime\Query\Compiler\UpdateCompilerInterface; |
11
|
|
|
use Bdf\Prime\Query\Contract\Compilable; |
12
|
|
|
|
13
|
|
|
/** |
14
|
|
|
* Simple implementation for @see Compilable |
15
|
|
|
* |
16
|
|
|
* @property CompilerState $compilerState |
17
|
|
|
* @property object $compiler |
18
|
|
|
* |
19
|
|
|
* @psalm-require-implements Compilable |
20
|
|
|
*/ |
21
|
|
|
trait CompilableTrait |
22
|
|
|
{ |
23
|
|
|
/** |
24
|
|
|
* @var Compilable::TYPE_* |
25
|
|
|
*/ |
26
|
|
|
protected $type = Compilable::TYPE_SELECT; |
27
|
|
|
|
28
|
|
|
/** |
29
|
|
|
* {@inheritdoc} |
30
|
|
|
*/ |
31
|
954 |
|
public function compile(bool $forceRecompile = false) |
32
|
|
|
{ |
33
|
954 |
|
if ($forceRecompile) { |
34
|
4 |
|
$this->compilerState->invalidate('prepared'); |
35
|
954 |
|
} elseif ($this->compilerState->compiled) { |
36
|
173 |
|
return $this->compilerState->compiled; |
37
|
|
|
} |
38
|
|
|
|
39
|
947 |
|
return $this->compilerState->compiled = $this->doCompilation($this->type, $this->compiler); |
|
|
|
|
40
|
|
|
} |
41
|
|
|
|
42
|
|
|
/** |
43
|
|
|
* {@inheritdoc} |
44
|
|
|
*/ |
45
|
|
|
public function getBindings(): array |
46
|
|
|
{ |
47
|
|
|
return $this->compiler->getBindings($this); |
48
|
|
|
} |
49
|
|
|
|
50
|
|
|
/** |
51
|
|
|
* {@inheritdoc} |
52
|
|
|
* |
53
|
|
|
* @return Compilable::TYPE_* |
54
|
|
|
*/ |
55
|
852 |
|
public function type(): string |
56
|
|
|
{ |
57
|
852 |
|
return $this->type; |
|
|
|
|
58
|
|
|
} |
59
|
|
|
|
60
|
|
|
/** |
61
|
|
|
* Change the query type |
62
|
|
|
* This action will invalidate the current query |
63
|
|
|
* |
64
|
|
|
* @param Compilable::TYPE_* $type One of the Compilable::TYPE_* constant |
65
|
|
|
* |
66
|
|
|
* @return void |
67
|
|
|
*/ |
68
|
842 |
|
protected function setType(string $type): void |
69
|
|
|
{ |
70
|
842 |
|
if ($this->type !== $type) { |
|
|
|
|
71
|
709 |
|
$this->compilerState->invalidate(); |
72
|
709 |
|
$this->type = $type; |
|
|
|
|
73
|
|
|
} |
74
|
|
|
} |
75
|
|
|
|
76
|
|
|
/** |
77
|
|
|
* Perform the query compilation |
78
|
|
|
* Can be overridden for perform custom compilation process |
79
|
|
|
* |
80
|
|
|
* @param Compilable::TYPE_* $type The query type |
81
|
|
|
* @param object $compiler The related compiler |
82
|
|
|
* |
83
|
|
|
* @return mixed The compiled query |
84
|
|
|
* |
85
|
|
|
* @throws PrimeException When the compilation fail |
86
|
|
|
* @throws \LogicException If type is not supported by the query or the compiler |
87
|
|
|
*/ |
88
|
947 |
|
protected function doCompilation(string $type, object $compiler) |
89
|
|
|
{ |
90
|
|
|
switch (true) { |
91
|
947 |
|
case $type === Compilable::TYPE_SELECT && $compiler instanceof SelectCompilerInterface: |
92
|
901 |
|
return $compiler->compileSelect($this); |
|
|
|
|
93
|
|
|
|
94
|
713 |
|
case $type === Compilable::TYPE_UPDATE && $compiler instanceof UpdateCompilerInterface: |
95
|
91 |
|
return $compiler->compileUpdate($this); |
|
|
|
|
96
|
|
|
|
97
|
680 |
|
case $type === Compilable::TYPE_INSERT && $compiler instanceof InsertCompilerInterface: |
98
|
662 |
|
return $compiler->compileInsert($this); |
|
|
|
|
99
|
|
|
|
100
|
81 |
|
case $type === Compilable::TYPE_DELETE && $compiler instanceof DeleteCompilerInterface: |
101
|
79 |
|
return $compiler->compileDelete($this); |
|
|
|
|
102
|
|
|
|
103
|
|
|
default: |
104
|
2 |
|
throw new \LogicException('The query ' . static::class . ' do not supports type ' . $type); |
105
|
|
|
} |
106
|
|
|
} |
107
|
|
|
} |
108
|
|
|
|