Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
| Total | |
0.00% |
0 / 27 |
|
0.00% |
0 / 21 |
CRAP | |
0.00% |
0 / 1 |
| Query | |
0.00% |
0 / 27 |
|
0.00% |
0 / 21 |
552 | |
0.00% |
0 / 1 |
| __construct | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
| from | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
| addFields | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
| setFields | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
| fields | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
6 | |||
| addVariables | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
| variables | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
| addCondition | |
0.00% |
0 / 3 |
|
0.00% |
0 / 1 |
2 | |||
| conditions | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
| addInnerJoin | |
0.00% |
0 / 2 |
|
0.00% |
0 / 1 |
2 | |||
| addJoin | |
0.00% |
0 / 3 |
|
0.00% |
0 / 1 |
2 | |||
| joins | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
| addGroupBy | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
| groupBy | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
| addOrderBy | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
| orderBy | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
| setLimit | |
0.00% |
0 / 2 |
|
0.00% |
0 / 1 |
2 | |||
| offset | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
| max | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
| shouldSelectAllFields | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
6 | |||
| allFields | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
| 1 | <?php |
| 2 | |
| 3 | namespace Dynart\Micro\Entities; |
| 4 | |
| 5 | use Dynart\Micro\Micro; |
| 6 | |
| 7 | class Query { |
| 8 | |
| 9 | const INNER_JOIN = 'inner'; |
| 10 | const LEFT_JOIN = 'left'; |
| 11 | const RIGHT_JOIN = 'right'; |
| 12 | const OUTER_JOIN = 'full outer'; |
| 13 | |
| 14 | protected string|Query $from; |
| 15 | protected array $variables = []; |
| 16 | protected array $fields = []; |
| 17 | protected array $joins = []; |
| 18 | protected array $conditions = []; |
| 19 | protected array $groups = []; |
| 20 | protected array $orders = []; |
| 21 | protected int $offset = -1; |
| 22 | protected int $max = -1; |
| 23 | |
| 24 | public function __construct(string|Query $from) { |
| 25 | $this->from = $from; |
| 26 | } |
| 27 | |
| 28 | public function from(): string|Query { |
| 29 | return $this->from; |
| 30 | } |
| 31 | |
| 32 | public function addFields(array $fields): void { |
| 33 | $this->fields = array_merge($this->fields, $fields); |
| 34 | } |
| 35 | |
| 36 | public function setFields(array $fields): void { |
| 37 | $this->fields = $fields; |
| 38 | } |
| 39 | |
| 40 | public function fields(): array { |
| 41 | return $this->shouldSelectAllFields() ? $this->allFields() : $this->fields; |
| 42 | } |
| 43 | |
| 44 | public function addVariables(array $variables): void { |
| 45 | $this->variables = array_merge($this->variables, $variables); |
| 46 | } |
| 47 | |
| 48 | public function variables(): array { |
| 49 | return $this->variables; |
| 50 | } |
| 51 | |
| 52 | public function addCondition(string $condition, array $variables = []): void { |
| 53 | $this->conditions[] = $condition; |
| 54 | $this->addVariables($variables); |
| 55 | } |
| 56 | |
| 57 | public function conditions(): array { |
| 58 | return $this->conditions; |
| 59 | } |
| 60 | |
| 61 | public function addInnerJoin(string|array $from, string $condition, array $variables = []): void { |
| 62 | $this->addJoin(self::INNER_JOIN, $from, $condition, $variables); |
| 63 | } |
| 64 | |
| 65 | public function addJoin(string $type, string|array $from, string $condition, array $variables = []): void { |
| 66 | $this->joins[] = [$type, $from, $condition]; |
| 67 | $this->addVariables($variables); |
| 68 | } |
| 69 | |
| 70 | public function joins(): array { |
| 71 | return $this->joins; |
| 72 | } |
| 73 | |
| 74 | public function addGroupBy(string $name): void { |
| 75 | $this->groups[] = $name; |
| 76 | } |
| 77 | |
| 78 | public function groupBy(): array { |
| 79 | return $this->groups; |
| 80 | } |
| 81 | |
| 82 | public function addOrderBy(string $name, string $dir = 'asc'): void { |
| 83 | $this->orders[] = [$name, $dir]; |
| 84 | } |
| 85 | |
| 86 | public function orderBy(): array { |
| 87 | return $this->orders; |
| 88 | } |
| 89 | |
| 90 | public function setLimit(int $offset, int $max): void { |
| 91 | $this->offset = $offset; |
| 92 | $this->max = $max; |
| 93 | } |
| 94 | |
| 95 | public function offset(): int { |
| 96 | return $this->offset; |
| 97 | } |
| 98 | |
| 99 | public function max(): int { |
| 100 | return $this->max; |
| 101 | } |
| 102 | |
| 103 | private function shouldSelectAllFields(): bool { |
| 104 | return empty($this->fields) && is_string($this->from); |
| 105 | } |
| 106 | |
| 107 | private function allFields(): array { |
| 108 | return array_keys(Micro::get(EntityManager::class)->tableColumns($this->from)); |
| 109 | } |
| 110 | } |