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