1 | <?php |
||||
2 | |||||
3 | namespace Jabe\Impl; |
||||
4 | |||||
5 | use Jabe\ProcessEngineException; |
||||
6 | use Jabe\Impl\Context\Context; |
||||
7 | use Jabe\Impl\Interceptor\{ |
||||
8 | CommandInterface, |
||||
9 | CommandContext, |
||||
10 | CommandExecutorInterface |
||||
11 | }; |
||||
12 | use Jabe\Query\NativeQueryInterface; |
||||
13 | |||||
14 | abstract class AbstractNativeQuery implements CommandInterface, NativeQueryInterface |
||||
15 | { |
||||
16 | private const RESULT_TYPES = [ |
||||
17 | 'LIST' => 'LIST', 'LIST_PAGE' => 'LIST_PAGE', 'SINGLE_RESULT' => 'SINGLE_RESULT', 'COUNT' => 'COUNT' |
||||
18 | ]; |
||||
19 | |||||
20 | protected $commandExecutor; |
||||
21 | protected $commandContext; |
||||
22 | |||||
23 | protected $maxResults = PHP_INT_MAX; |
||||
24 | protected $firstResult = 0; |
||||
25 | protected $resultType; |
||||
26 | |||||
27 | private $parameters = []; |
||||
28 | private $sqlStatement; |
||||
29 | |||||
30 | protected function __construct($command) |
||||
31 | { |
||||
32 | if ($command instanceof CommandExecutorInterface) { |
||||
33 | $this->commandExecutor = $command; |
||||
34 | } elseif ($command instanceof CommandContext) { |
||||
35 | $this->commandContext = $command; |
||||
36 | } |
||||
37 | } |
||||
38 | |||||
39 | public function setCommandExecutor(CommandExecutorInterface $commandExecutor): AbstractNativeQuery |
||||
40 | { |
||||
41 | $this->commandExecutor = $commandExecutor; |
||||
42 | return $this; |
||||
43 | } |
||||
44 | |||||
45 | public function sql(string $sqlStatement): NativeQueryInterface |
||||
46 | { |
||||
47 | $this->sqlStatement = $sqlStatement; |
||||
48 | return $this; |
||||
49 | } |
||||
50 | |||||
51 | public function parameter(string $name, $value): NativeQueryInterface |
||||
52 | { |
||||
53 | $this->parameters[$name] = $value; |
||||
54 | return $this; |
||||
55 | } |
||||
56 | |||||
57 | public function singleResult() |
||||
58 | { |
||||
59 | $this->resultType = self::RESULT_TYPES['SINGLE_RESULT']; |
||||
60 | if ($this->commandExecutor !== null) { |
||||
61 | return $this->commandExecutor->execute($this); |
||||
62 | } |
||||
63 | return $this->executeSingleResult(Context::getCommandContext()); |
||||
0 ignored issues
–
show
Bug
introduced
by
![]() |
|||||
64 | } |
||||
65 | |||||
66 | public function list(): array |
||||
67 | { |
||||
68 | $this->resultType = self::RESULT_TYPES['LIST']; |
||||
69 | if ($this->commandExecutor !== null) { |
||||
70 | return $this->commandExecutor->execute($this); |
||||
71 | } |
||||
72 | return $this->executeList(Context::getCommandContext(), $this->getParameterMap(), 0, PHP_INT_MAX); |
||||
0 ignored issues
–
show
It seems like
Jabe\Impl\Context\Context::getCommandContext() can also be of type null ; however, parameter $commandContext of Jabe\Impl\AbstractNativeQuery::executeList() does only seem to accept Jabe\Impl\Interceptor\CommandContext , maybe add an additional type check?
(
Ignorable by Annotation
)
If this is a false-positive, you can also ignore this issue in your code via the
![]() |
|||||
73 | } |
||||
74 | |||||
75 | public function listPage(int $firstResult, int $maxResults): array |
||||
76 | { |
||||
77 | $this->firstResult = $firstResult; |
||||
78 | $this->maxResults = $maxResults; |
||||
79 | $this->resultType = self::RESULT_TYPES['LIST_PAGE']; |
||||
80 | if ($this->commandExecutor !== null) { |
||||
81 | return $this->commandExecutor->execute($this); |
||||
82 | } |
||||
83 | return $this->executeList(Context::getCommandContext(), $this->getParameterMap(), $this->firstResult, $this->maxResults); |
||||
0 ignored issues
–
show
It seems like
Jabe\Impl\Context\Context::getCommandContext() can also be of type null ; however, parameter $commandContext of Jabe\Impl\AbstractNativeQuery::executeList() does only seem to accept Jabe\Impl\Interceptor\CommandContext , maybe add an additional type check?
(
Ignorable by Annotation
)
If this is a false-positive, you can also ignore this issue in your code via the
![]() |
|||||
84 | } |
||||
85 | |||||
86 | public function count(): int |
||||
87 | { |
||||
88 | $this->resultType = self::RESULT_TYPES['COUNT']; |
||||
89 | if ($this->commandExecutor !== null) { |
||||
90 | return $this->commandExecutor->execute($this); |
||||
91 | } |
||||
92 | return $this->executeCount(Context::getCommandContext(), $this->getParameterMap()); |
||||
0 ignored issues
–
show
It seems like
Jabe\Impl\Context\Context::getCommandContext() can also be of type null ; however, parameter $commandContext of Jabe\Impl\AbstractNativeQuery::executeCount() does only seem to accept Jabe\Impl\Interceptor\CommandContext , maybe add an additional type check?
(
Ignorable by Annotation
)
If this is a false-positive, you can also ignore this issue in your code via the
![]() |
|||||
93 | } |
||||
94 | |||||
95 | public function execute(CommandContext $commandContext) |
||||
96 | { |
||||
97 | if ($this->resultType == self::RESULT_TYPES['LIST']) { |
||||
98 | return $this->executeList($commandContext, $this->getParameterMap(), 0, PHP_INT_MAX); |
||||
99 | } elseif ($this->resultType == self::RESULT_TYPES['LIST_PAGE']) { |
||||
100 | $parameterMap = $this->getParameterMap(); |
||||
101 | $parameterMap["resultType"] = "LIST_PAGE"; |
||||
102 | $parameterMap["firstResult"] = $this->firstResult; |
||||
103 | $parameterMap["maxResults"] = $this->maxResults; |
||||
104 | $parameterMap["internalOrderBy"] = "RES.ID_ asc"; |
||||
105 | |||||
106 | $firstRow = $this->firstResult + 1; |
||||
107 | $parameterMap["firstRow"] = $firstRow; |
||||
108 | $lastRow = 0; |
||||
109 | if ($maxResults == PHP_INT_MAX) { |
||||
0 ignored issues
–
show
Comprehensibility
Best Practice
introduced
by
|
|||||
110 | $lastRow = $this->maxResults; |
||||
111 | } else { |
||||
112 | $lastRow = $this->firstResult + $this->maxResults + 1; |
||||
113 | } |
||||
114 | $parameterMap["lastRow"] = $lastRow; |
||||
115 | return $this->executeList($commandContext, $parameterMap, $this->firstResult, $this->maxResults); |
||||
116 | } elseif ($this->resultType == self::RESULT_TYPES['SINGLE_RESULT']) { |
||||
117 | return $this->executeSingleResult($commandContext); |
||||
118 | } else { |
||||
119 | return $this->executeCount($commandContext, $this->getParameterMap()); |
||||
120 | } |
||||
121 | } |
||||
122 | |||||
123 | abstract public function executeCount(CommandContext $commandContext, array $parameterMap): int; |
||||
124 | |||||
125 | /** |
||||
126 | * Executes the actual query to retrieve the list of results. |
||||
127 | * @param maxResults |
||||
128 | * @param firstResult |
||||
129 | * |
||||
130 | * @param page |
||||
131 | * used if the results must be paged. If null, no paging will be |
||||
132 | * applied. |
||||
133 | */ |
||||
134 | abstract public function executeList(CommandContext $commandContext, array $parameterMap, int $firstResult, int $maxResults): array; |
||||
135 | |||||
136 | public function executeSingleResult(CommandContext $commandContext) |
||||
137 | { |
||||
138 | $results = $this->executeList($commandContext, $this->getParameterMap(), 0, PHP_INT_MAX); |
||||
139 | if (count($results) == 1) { |
||||
140 | return $results[0]; |
||||
141 | } elseif (count($results) > 1) { |
||||
142 | throw new ProcessEngineException("Query return " . count($results) . " results instead of max 1"); |
||||
143 | } |
||||
144 | return null; |
||||
145 | } |
||||
146 | |||||
147 | private function getParameterMap(): array |
||||
148 | { |
||||
149 | $parameterMap = $this->parameters; |
||||
150 | $parameterMap["sql"] = $this->sqlStatement; |
||||
151 | return $parameterMap; |
||||
152 | } |
||||
153 | |||||
154 | public function getParameters(): array |
||||
155 | { |
||||
156 | return $this->parameters; |
||||
157 | } |
||||
158 | } |
||||
159 |