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
|
3 |
|
public function __construct(array $fields = ['*']) |
28
|
|
|
{ |
29
|
3 |
|
array_map([$this, 'setField'], array_keys($fields), array_values($fields)); |
30
|
2 |
|
} |
31
|
|
|
|
32
|
3 |
|
private function setField($allias, $value): void |
33
|
|
|
{ |
34
|
3 |
|
preg_match("#(?<fieldName>\w{2,}|\*)(\|(?<function>\w{2,}))?#i", $value, $matches); |
35
|
3 |
|
$field = $matches['fieldName']; |
36
|
3 |
|
$object = $this->fields[$field] = new Fields($field); |
37
|
3 |
|
if (isset($matches['function'])) { |
38
|
1 |
|
$object->setFunction($matches['function']); |
39
|
|
|
} |
40
|
2 |
|
if (!\is_int($allias)) { |
41
|
1 |
|
$object->setAllias($allias); |
42
|
|
|
} |
43
|
2 |
|
} |
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
|
1 |
|
return $this->concat(array_map(function(Fields $field): string { |
63
|
1 |
|
return $field->result(); |
64
|
1 |
|
}, $this->fields)); |
65
|
|
|
} |
66
|
|
|
|
67
|
1 |
|
public function get(): string |
68
|
|
|
{ |
69
|
1 |
|
return 'SELECT ' . $this->getFields() . ' FROM ' |
70
|
1 |
|
. $this->table() |
71
|
1 |
|
. (null !== $this->join ? $this->join->result() : '') |
72
|
1 |
|
. $this->getParts(); |
73
|
|
|
} |
74
|
|
|
} |
75
|
|
|
|