|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Compolomus\LSQLQueryBuilder\Parts; |
|
4
|
|
|
|
|
5
|
|
|
use Compolomus\LSQLQueryBuilder\BuilderException; |
|
6
|
|
|
use Compolomus\LSQLQueryBuilder\System\{ |
|
7
|
|
|
Traits\Helper, |
|
8
|
|
|
Traits\GetParts, |
|
9
|
|
|
Traits\Join as TJoin, |
|
10
|
|
|
Traits\Limit as TLimit, |
|
11
|
|
|
Traits\Where as TWhere, |
|
12
|
|
|
Traits\Order as TOrder, |
|
13
|
|
|
Traits\Group as TGroup, |
|
14
|
|
|
Traits\Caller, |
|
15
|
|
|
Fields |
|
16
|
|
|
}; |
|
17
|
|
|
|
|
18
|
|
|
/** |
|
19
|
|
|
* @method string table() |
|
20
|
|
|
*/ |
|
21
|
|
|
class Select |
|
22
|
|
|
{ |
|
23
|
|
|
use TJoin, TLimit, TWhere, TOrder, TGroup, Caller, GetParts, Helper; |
|
24
|
|
|
|
|
25
|
|
|
private $fields = []; |
|
26
|
|
|
|
|
27
|
5 |
|
public function __construct(array $fields = ['*']) |
|
28
|
|
|
{ |
|
29
|
5 |
|
array_map([$this, 'setField'], array_keys($fields), array_values($fields)); |
|
30
|
4 |
|
} |
|
31
|
|
|
|
|
32
|
5 |
|
private function setField($allias, $value): void |
|
33
|
|
|
{ |
|
34
|
5 |
|
preg_match("#(?<fieldName>\w{2,}|\*)(\|(?<function>\w{2,}))?#i", $value, $matches); |
|
35
|
5 |
|
$field = $matches['fieldName']; |
|
36
|
5 |
|
$object = $this->fields[$field] = new Fields($field); |
|
37
|
5 |
|
if (isset($matches['function'])) { |
|
38
|
1 |
|
$object->setFunction($matches['function']); |
|
39
|
|
|
} |
|
40
|
4 |
|
if (!\is_int($allias)) { |
|
41
|
1 |
|
$object->setAllias($allias); |
|
42
|
|
|
} |
|
43
|
4 |
|
} |
|
44
|
|
|
|
|
45
|
2 |
|
public function setFunction(string $fieldName, string $function): Select |
|
46
|
|
|
{ |
|
47
|
2 |
|
if (!array_key_exists($fieldName, $this->fields)) { |
|
48
|
1 |
|
throw new BuilderException('Не найдено поле ' . $fieldName . ' |SELECT setFunction|'); |
|
49
|
|
|
} |
|
50
|
1 |
|
$this->fields[$fieldName]->setFunction($function); |
|
51
|
1 |
|
return $this; |
|
52
|
|
|
} |
|
53
|
|
|
|
|
54
|
1 |
|
public function setAllias(string $fieldName, string $allias): Select |
|
55
|
|
|
{ |
|
56
|
1 |
|
$this->fields[$fieldName]->setAllias($allias); |
|
57
|
1 |
|
return $this; |
|
58
|
|
|
} |
|
59
|
|
|
|
|
60
|
|
|
public function getFields(): string |
|
61
|
|
|
{ |
|
62
|
3 |
|
return $this->concat(array_map(function(Fields $field): string { |
|
63
|
3 |
|
return $field->result(); |
|
64
|
3 |
|
}, $this->fields)); |
|
65
|
|
|
} |
|
66
|
|
|
|
|
67
|
3 |
|
public function get(): string |
|
68
|
|
|
{ |
|
69
|
3 |
|
return 'SELECT ' . $this->getFields() . ' FROM ' |
|
70
|
3 |
|
. $this->table() |
|
71
|
3 |
|
. (null !== $this->join ? $this->join->result() : '') |
|
72
|
3 |
|
. $this->getParts(); |
|
73
|
|
|
} |
|
74
|
|
|
} |
|
75
|
|
|
|