Completed
Push — master ( b4b898...f81b53 )
by Woody
11s
created

SelectQuery::columns()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 1

Importance

Changes 0
Metric Value
cc 1
eloc 3
nc 1
nop 1
dl 0
loc 5
ccs 3
cts 3
cp 1
crap 1
rs 9.4285
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 12
    public function columns(...$columns): self
46
    {
47 12
        $this->columns = listing(identifyAll($columns));
48 12
        return $this;
49
    }
50
51 6
    public function join($table, CriteriaInterface $criteria, string $type = ''): self
52
    {
53 6
        $join = trim(sprintf('%s JOIN', strtoupper($type)));
54 6
        $this->joins[] = express("$join %s ON %s", identify($table), $criteria);
55 6
        return $this;
56
    }
57
58 1
    public function innerJoin($table, CriteriaInterface $criteria): self
59
    {
60 1
        return $this->join($table, $criteria, 'INNER');
61
    }
62
63 1
    public function leftJoin($table, CriteriaInterface $criteria): self
64
    {
65 1
        return $this->join($table, $criteria, 'LEFT');
66
    }
67
68 1
    public function rightJoin($table, CriteriaInterface $criteria): self
69
    {
70 1
        return $this->join($table, $criteria, 'RIGHT');
71
    }
72
73 1
    public function fullJoin($table, CriteriaInterface $criteria): self
74
    {
75 1
        return $this->join($table, $criteria, 'FULL');
76
    }
77
78 3
    public function groupBy(...$columns): self
79
    {
80 3
        $this->groupBy = identifyAll($columns);
81 3
        return $this;
82
    }
83
84 1
    public function having(CriteriaInterface $criteria): self
85
    {
86 1
        $this->having = $criteria;
87 1
        return $this;
88
    }
89
90 22
    public function asExpression(): ExpressionInterface
91
    {
92 22
        $query = $this->startExpression();
93 22
        $query = $this->applyDistinct($query);
94 22
        $query = $this->applyColumns($query);
95 22
        $query = $this->applyFrom($query);
96 22
        $query = $this->applyJoins($query);
97 22
        $query = $this->applyWhere($query);
98 22
        $query = $this->applyGroupBy($query);
99 22
        $query = $this->applyHaving($query);
100 22
        $query = $this->applyOrderBy($query);
101 22
        $query = $this->applyLimit($query);
102 22
        $query = $this->applyOffset($query);
103
104 22
        return $query;
105
    }
106
107 22
    protected function startExpression(): ExpressionInterface
108
    {
109 22
        return express('SELECT');
110
    }
111
112 22
    protected function applyDistinct(ExpressionInterface $query): ExpressionInterface
113
    {
114 22
        return $this->distinct ? $query->append('DISTINCT') : $query;
115
    }
116
117 22
    protected function applyColumns(ExpressionInterface $query): ExpressionInterface
118
    {
119 22
        return $this->columns ? $query->append('%s', $this->columns) : $query->append('*');
120
    }
121
122 22
    protected function applyJoins(ExpressionInterface $query): ExpressionInterface
123
    {
124 22
        return $this->joins ? $query->append('%s', listing($this->joins, ' ')) : $query;
125
    }
126
127 22
    protected function applyGroupBy(ExpressionInterface $query): ExpressionInterface
128
    {
129 22
        return $this->groupBy ? $query->append('GROUP BY %s', listing($this->groupBy)) : $query;
130
    }
131
132 22
    protected function applyHaving(ExpressionInterface $query): ExpressionInterface
133
    {
134 22
        return $this->having ? $query->append('HAVING %s', $this->having) : $query;
135
    }
136
}
137