1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
namespace LaravelFreelancerNL\Aranguent\Query\Concerns; |
6
|
|
|
|
7
|
|
|
use Exception; |
8
|
|
|
use Illuminate\Support\Str; |
9
|
|
|
|
10
|
|
|
trait HasAliases |
11
|
|
|
{ |
12
|
|
|
protected $tableAliases = []; |
13
|
|
|
|
14
|
|
|
protected $columnAliases = []; |
15
|
|
|
|
16
|
|
|
/** |
17
|
|
|
* @param string $table |
18
|
|
|
* @param string|null $alias |
19
|
|
|
* @return string |
20
|
|
|
*/ |
21
|
162 |
|
public function registerTableAlias(string $table, string $alias = null): string |
22
|
|
|
{ |
23
|
162 |
|
if ($alias == null && stripos($table, ' as ') !== false) { |
|
|
|
|
24
|
|
|
[$table, $alias] = explode(' as ', $table); |
25
|
|
|
} |
26
|
162 |
|
if ($alias == null) { |
|
|
|
|
27
|
162 |
|
$alias = $this->generateTableAlias($table); |
28
|
|
|
} |
29
|
162 |
|
$this->tableAliases[$table] = $alias; |
30
|
|
|
|
31
|
162 |
|
return $alias; |
32
|
|
|
} |
33
|
|
|
|
34
|
117 |
|
protected function isTableAlias(string $alias) |
35
|
|
|
{ |
36
|
117 |
|
return in_array($alias, $this->tableAliases); |
37
|
|
|
} |
38
|
|
|
|
39
|
138 |
|
protected function getTableAlias(string $table): string|null |
40
|
|
|
{ |
41
|
138 |
|
if (isset($this->tableAliases[$table])) { |
42
|
118 |
|
return $this->tableAliases[$table]; |
43
|
|
|
} |
44
|
|
|
|
45
|
117 |
|
return null; |
46
|
|
|
} |
47
|
|
|
|
48
|
|
|
/** |
49
|
|
|
* Extract table and alias from sql alias notation (entity AS `alias`) |
50
|
|
|
* |
51
|
|
|
* @param string $entity |
52
|
|
|
* @param int|string|null $key |
53
|
|
|
* @return array<mixed> |
54
|
34 |
|
* @throws Exception |
55
|
|
|
*/ |
56
|
34 |
|
protected function extractAlias(string $entity, int|string $key = null): array |
57
|
34 |
|
{ |
58
|
14 |
|
$results = preg_split("/\sas\s/i", $entity); |
59
|
|
|
|
60
|
34 |
|
if ($results === false) { |
61
|
23 |
|
throw new Exception('Column splitting failed'); |
62
|
|
|
} |
63
|
|
|
|
64
|
34 |
|
if (isset($results[1])) { |
65
|
|
|
$results[1] = trim($results[1], '`'); |
66
|
|
|
} |
67
|
162 |
|
if (! isset($results[1]) && is_string($key)) { |
68
|
|
|
$results[1] = $key; |
69
|
162 |
|
} |
70
|
|
|
if (! isset($results[1])) { |
71
|
|
|
$results[1] = $results[0]; |
72
|
|
|
} |
73
|
|
|
|
74
|
|
|
return $results; |
75
|
|
|
} |
76
|
|
|
|
77
|
|
|
protected function generateTableAlias(string $table, string $postfix = 'Doc'): string |
78
|
|
|
{ |
79
|
|
|
return Str::camel(Str::singular($table)) . $postfix; |
80
|
|
|
} |
81
|
|
|
|
82
|
|
|
protected function replaceTableForAlias($reference): string |
83
|
|
|
{ |
84
|
|
|
$referenceParts = explode('.', $reference); |
85
|
|
|
$first = array_shift($referenceParts); |
86
|
|
|
$alias = $this->getTableAlias($first); |
87
|
|
|
if ($alias == null) { |
|
|
|
|
88
|
|
|
$alias = $first; |
89
|
|
|
} |
90
|
|
|
array_unshift($referenceParts, $alias); |
91
|
|
|
|
92
|
|
|
return implode('.', $referenceParts); |
93
|
|
|
} |
94
|
|
|
|
95
|
|
|
protected function prefixAlias(string $target, string $value): string |
96
|
|
|
{ |
97
|
|
|
$alias = $this->getTableAlias($target); |
98
|
|
|
|
99
|
|
|
if (Str::startsWith($value, $alias . '.')) { |
100
|
|
|
return $value; |
101
|
1 |
|
} |
102
|
|
|
|
103
|
1 |
|
return $alias . '.' . $value; |
104
|
|
|
} |
105
|
|
|
|
106
|
|
|
/** |
107
|
1 |
|
* @param string $column |
108
|
1 |
|
* @param string|null $alias |
109
|
1 |
|
* @return bool |
110
|
|
|
* @throws Exception |
111
|
|
|
*/ |
112
|
|
|
public function registerColumnAlias(string $column, string $alias = null): bool |
113
|
|
|
{ |
114
|
|
|
if (preg_match("/\sas\s/i", $column)) { |
115
|
|
|
[$column, $alias] = $this->extractAlias($column); |
116
|
|
|
} |
117
|
|
|
|
118
|
|
|
if (isset($alias)) { |
119
|
|
|
$this->columnAliases[$column] = $alias; |
120
|
|
|
return true; |
121
|
|
|
} |
122
|
|
|
|
123
|
|
|
return false; |
124
|
121 |
|
} |
125
|
|
|
|
126
|
121 |
|
protected function getColumnAlias(string $column): string|null |
127
|
3 |
|
{ |
128
|
|
|
if (isset($this->columnAliases[$column])) { |
129
|
|
|
return $this->columnAliases[$column]; |
130
|
120 |
|
} |
131
|
|
|
|
132
|
120 |
|
return null; |
133
|
1 |
|
} |
134
|
|
|
|
135
|
|
|
/** |
136
|
120 |
|
* @param array<mixed>|string $column |
137
|
3 |
|
* @return array<mixed>|string |
138
|
|
|
*/ |
139
|
|
|
protected function convertColumnId(array|string $column): array|string |
140
|
117 |
|
{ |
141
|
117 |
|
if (is_string($column) || is_array($column)) { |
|
|
|
|
142
|
|
|
$column = $this->convertIdToKey($column); |
|
|
|
|
143
|
|
|
} |
144
|
|
|
|
145
|
117 |
|
return $column; |
146
|
|
|
} |
147
|
|
|
} |
148
|
|
|
|