|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
declare(strict_types=1); |
|
4
|
|
|
|
|
5
|
|
|
namespace Cycle\ORM\Select\Traits; |
|
6
|
|
|
|
|
7
|
|
|
use Cycle\Database\Query\SelectQuery; |
|
8
|
|
|
use JetBrains\PhpStorm\Pure; |
|
9
|
|
|
|
|
10
|
|
|
/** |
|
11
|
|
|
* Provides ability to add aliased columns into SelectQuery. |
|
12
|
|
|
* |
|
13
|
|
|
* @internal |
|
14
|
|
|
*/ |
|
15
|
|
|
trait ColumnsTrait |
|
16
|
|
|
{ |
|
17
|
|
|
/** |
|
18
|
|
|
* List of columns associated with the loader. |
|
19
|
|
|
* |
|
20
|
|
|
* @var array<non-empty-string, non-empty-string> |
|
|
|
|
|
|
21
|
|
|
*/ |
|
22
|
|
|
protected array $columns = []; |
|
23
|
|
|
|
|
24
|
|
|
/** |
|
25
|
|
|
* Return column name associated with given field. |
|
26
|
5988 |
|
*/ |
|
27
|
|
|
public function fieldAlias(string $field): ?string |
|
28
|
5988 |
|
{ |
|
29
|
|
|
return $this->columns[$field] ?? null; |
|
30
|
|
|
} |
|
31
|
|
|
|
|
32
|
|
|
/** |
|
33
|
|
|
* Set columns into SelectQuery. |
|
34
|
|
|
* |
|
35
|
|
|
* @param bool $minify Minify column names (will work in case when query parsed in |
|
36
|
|
|
* FETCH_NUM mode). |
|
37
|
|
|
* @param string $prefix Prefix to be added for each column name. |
|
38
|
|
|
* @param bool $overwrite When set to true existed columns will be removed. |
|
39
|
6494 |
|
*/ |
|
40
|
|
|
protected function mountColumns( |
|
41
|
|
|
SelectQuery $query, |
|
42
|
|
|
bool $minify = false, |
|
43
|
|
|
string $prefix = '', |
|
44
|
|
|
bool $overwrite = false |
|
45
|
6494 |
|
): SelectQuery { |
|
46
|
6494 |
|
$alias = $this->getAlias(); |
|
47
|
|
|
$columns = $overwrite ? [] : $query->getColumns(); |
|
48
|
6494 |
|
|
|
49
|
6494 |
|
foreach ($this->columns as $internal => $external) { |
|
50
|
6494 |
|
$name = $internal; |
|
51
|
2302 |
|
if ($minify) { |
|
52
|
|
|
//Let's use column number instead of full name |
|
53
|
|
|
$name = 'c' . \count($columns); |
|
54
|
6494 |
|
} |
|
55
|
|
|
|
|
56
|
6494 |
|
$columns[] = "{$alias}.{$external} AS {$prefix}{$name}"; |
|
57
|
|
|
} |
|
58
|
6494 |
|
|
|
59
|
|
|
return $query->columns($columns); |
|
60
|
|
|
} |
|
61
|
6494 |
|
|
|
62
|
|
|
/** |
|
63
|
|
|
* Return original column names. |
|
64
|
6494 |
|
*/ |
|
65
|
|
|
protected function columnNames(): array |
|
66
|
|
|
{ |
|
67
|
|
|
return \array_keys($this->columns); |
|
68
|
|
|
} |
|
69
|
|
|
|
|
70
|
6382 |
|
/** |
|
71
|
|
|
* Table alias of the loader. |
|
72
|
6382 |
|
*/ |
|
73
|
6382 |
|
abstract protected function getAlias(): string; |
|
74
|
6382 |
|
|
|
75
|
2262 |
|
/** |
|
76
|
|
|
* @param non-empty-string[] $columns |
|
|
|
|
|
|
77
|
5264 |
|
* |
|
78
|
|
|
* @return array<non-empty-string, non-empty-string> |
|
|
|
|
|
|
79
|
|
|
* |
|
80
|
|
|
* @psalm-pure |
|
81
|
6382 |
|
*/ |
|
82
|
|
|
#[Pure] |
|
83
|
|
|
private function normalizeColumns(array $columns): array |
|
84
|
|
|
{ |
|
85
|
|
|
$result = []; |
|
86
|
|
|
foreach ($columns as $alias => $column) { |
|
87
|
|
|
$result[\is_int($alias) ? $column : $alias] = $column; |
|
88
|
|
|
} |
|
89
|
|
|
|
|
90
|
|
|
return $result; |
|
91
|
|
|
} |
|
92
|
|
|
} |
|
93
|
|
|
|