Completed
Push — master ( 211d4d...735fa4 )
by Woody
10s
created

SelectQuery::addColumns()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 0
Metric Value
cc 1
eloc 2
nc 1
nop 1
dl 0
loc 4
ccs 2
cts 2
cp 1
crap 1
rs 10
c 0
b 0
f 0
1
<?php
2
declare(strict_types=1);
3
4
namespace Latitude\QueryBuilder\Query;
5
6
use Latitude\QueryBuilder\CriteriaInterface;
7
use Latitude\QueryBuilder\ExpressionInterface;
8
use Latitude\QueryBuilder\StatementInterface;
9
10
use function Latitude\QueryBuilder\express;
11
use function Latitude\QueryBuilder\identify;
12
use function Latitude\QueryBuilder\identifyAll;
13
use function Latitude\QueryBuilder\listing;
14
15
class SelectQuery extends AbstractQuery
16
{
17
    use Capability\CanUnion;
18
    use Capability\HasFrom;
19
    use Capability\HasLimit;
20
    use Capability\HasOffset;
21
    use Capability\HasOrderBy;
22
    use Capability\HasWhere;
23
24
    /** @var bool */
25
    protected $distinct = false;
26
27
    /** @var StatementInterface[] */
28
    protected $columns = [];
29
30
    /** @var StatementInterface[] */
31
    protected $joins = [];
32
33
    /** @var StatementInterface[] */
34
    protected $groupBy = [];
35
36
    /** @var CriteriaInterface */
37
    protected $having;
38
39 2
    public function distinct($state = true): self
40
    {
41 2
        $this->distinct = $state;
42 2
        return $this;
43
    }
44
45 14
    public function columns(...$columns): self
46
    {
47 14
        $this->columns = identifyAll($columns);
48 14
        return $this;
49
    }
50
51 1
    public function addColumns(...$columns): self
52
    {
53 1
        return $this->columns(...array_merge($this->columns, $columns));
54
    }
55
56 6
    public function join($table, CriteriaInterface $criteria, string $type = ''): self
57
    {
58 6
        $join = trim(sprintf('%s JOIN', strtoupper($type)));
59 6
        $this->joins[] = express("$join %s ON %s", identify($table), $criteria);
60 6
        return $this;
61
    }
62
63 1
    public function innerJoin($table, CriteriaInterface $criteria): self
64
    {
65 1
        return $this->join($table, $criteria, 'INNER');
66
    }
67
68 1
    public function leftJoin($table, CriteriaInterface $criteria): self
69
    {
70 1
        return $this->join($table, $criteria, 'LEFT');
71
    }
72
73 1
    public function rightJoin($table, CriteriaInterface $criteria): self
74
    {
75 1
        return $this->join($table, $criteria, 'RIGHT');
76
    }
77
78 1
    public function fullJoin($table, CriteriaInterface $criteria): self
79
    {
80 1
        return $this->join($table, $criteria, 'FULL');
81
    }
82
83 3
    public function groupBy(...$columns): self
84
    {
85 3
        $this->groupBy = identifyAll($columns);
86 3
        return $this;
87
    }
88
89 1
    public function having(CriteriaInterface $criteria): self
90
    {
91 1
        $this->having = $criteria;
92 1
        return $this;
93
    }
94
95 28
    public function asExpression(): ExpressionInterface
96
    {
97 28
        $query = $this->startExpression();
98 28
        $query = $this->applyDistinct($query);
99 28
        $query = $this->applyColumns($query);
100 28
        $query = $this->applyFrom($query);
101 28
        $query = $this->applyJoins($query);
102 28
        $query = $this->applyWhere($query);
103 28
        $query = $this->applyGroupBy($query);
104 28
        $query = $this->applyHaving($query);
105 28
        $query = $this->applyOrderBy($query);
106 28
        $query = $this->applyLimit($query);
107 28
        $query = $this->applyOffset($query);
108
109 28
        return $query;
110
    }
111
112 28
    protected function startExpression(): ExpressionInterface
113
    {
114 28
        return express('SELECT');
115
    }
116
117 28
    protected function applyDistinct(ExpressionInterface $query): ExpressionInterface
118
    {
119 28
        return $this->distinct ? $query->append('DISTINCT') : $query;
120
    }
121
122 28
    protected function applyColumns(ExpressionInterface $query): ExpressionInterface
123
    {
124 28
        return $this->columns ? $query->append('%s', listing($this->columns)) : $query->append('*');
125
    }
126
127 28
    protected function applyJoins(ExpressionInterface $query): ExpressionInterface
128
    {
129 28
        return $this->joins ? $query->append('%s', listing($this->joins, ' ')) : $query;
130
    }
131
132 28
    protected function applyGroupBy(ExpressionInterface $query): ExpressionInterface
133
    {
134 28
        return $this->groupBy ? $query->append('GROUP BY %s', listing($this->groupBy)) : $query;
135
    }
136
137 28
    protected function applyHaving(ExpressionInterface $query): ExpressionInterface
138
    {
139 28
        return $this->having ? $query->append('HAVING %s', $this->having) : $query;
140
    }
141
}
142