Passed
Push — master ( ded3fd...8acf3f )
by Aleksandar
03:45
created

SelectBuilder   A

Complexity

Total Complexity 12

Size/Duplication

Total Lines 99
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 33
c 1
b 0
f 0
dl 0
loc 99
ccs 32
cts 32
cp 1
rs 10
wmc 12

6 Methods

Rating   Name   Duplication   Size   Complexity  
A buildGroupBy() 0 12 3
A buildHaving() 0 3 1
A getInitialParts() 0 3 1
A buildOrderBy() 0 16 5
A getRequiredParts() 0 3 1
A getPartBuilders() 0 12 1
1
<?php
2
/**
3
 * Copyright 2021 Aleksandar Panic
4
 *
5
 * Licensed under the Apache License, Version 2.0 (the "License");
6
 * you may not use this file except in compliance with the License.
7
 * You may obtain a copy of the License at
8
 *
9
 *   http://www.apache.org/licenses/LICENSE-2.0
10
 *
11
 * Unless required by applicable law or agreed to in writing, software
12
 * distributed under the License is distributed on an "AS IS" BASIS,
13
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14
 * See the License for the specific language governing permissions and
15
 * limitations under the License.
16
 */
17
18
namespace ArekX\PQL\Drivers\MySql\Builder\Builders;
19
20
use ArekX\PQL\Contracts\StructuredQuery;
21
use ArekX\PQL\Drivers\MySql\Builder\Builders\Traits\AliasTrait;
22
use ArekX\PQL\Drivers\MySql\Builder\Builders\Traits\FromPartTrait;
23
use ArekX\PQL\Drivers\MySql\Builder\Builders\Traits\JoinTrait;
24
use ArekX\PQL\Drivers\MySql\Builder\Builders\Traits\NumberPartTrait;
25
use ArekX\PQL\Drivers\MySql\Builder\Builders\Traits\WhereTrait;
26
use ArekX\PQL\Drivers\MySql\Builder\MySqlQueryBuilderState;
27
28
/**
29
 * Represents a query builder for building an INSERT query.
30
 */
31
class SelectBuilder extends QueryPartBuilder
32
{
33
    use AliasTrait;
34
    use FromPartTrait;
35
    use WhereTrait;
36
    use NumberPartTrait;
37
    use JoinTrait;
38
39
    /**
40
     * @inheritDoc
41
     */
42 10
    protected function getInitialParts(): array
43
    {
44 10
        return ['SELECT'];
45
    }
46
47
    /**
48
     * @inheritDoc
49
     */
50 10
    protected function getRequiredParts(): array
51
    {
52 10
        return [];
53
    }
54
55
    /**
56
     * @inheritDoc
57
     */
58 10
    protected function getPartBuilders(): array
59
    {
60
        return [
61 10
            'columns' => fn($part, $state) => $this->buildAliasedNames($part, $state),
62 10
            'from' => fn($part, $state) => $this->buildFrom($part, $state),
63 10
            'join' => fn($part, $state) => $this->buildJoin($part, $state),
64 10
            'where' => fn($part, $state) => $this->buildWhere($part, $state),
65 10
            'groupBy' => fn($part, $state) => $this->buildGroupBy($part, $state),
66 10
            'having' => fn($part, $state) => $this->buildHaving($part, $state),
67 10
            'orderBy' => fn($part, $state) => $this->buildOrderBy($part, $state),
68 10
            'limit' => fn($part) => $this->buildLimit($part),
69 10
            'offset' => fn($part) => $this->buildOffset($part),
70
        ];
71
    }
72
73
    /**
74
     * Build GROUP BY part
75
     *
76
     * @param array|StructuredQuery $groupBy Part to be build
77
     * @param MySqlQueryBuilderState $state Query builder state
78
     * @return string
79
     */
80 1
    protected function buildGroupBy($groupBy, MySqlQueryBuilderState $state)
81
    {
82 1
        if ($groupBy instanceof StructuredQuery) {
83 1
            return 'GROUP BY ' . $this->buildQuery($groupBy, $state);
84
        }
85
86 1
        $result = [];
87 1
        foreach ($groupBy as $name) {
88 1
            $result[] = $this->quoteName($name);
89
        }
90
91 1
        return 'GROUP BY ' . implode(', ', $result);
92
    }
93
94
    /**
95
     * Build HAVING part
96
     *
97
     * @param array|StructuredQuery $condition Condition to be built.
98
     * @param MySqlQueryBuilderState $state Query builder state
99
     * @return string
100
     * @throws \Exception
101
     */
102 1
    protected function buildHaving($condition, MySqlQueryBuilderState $state)
103
    {
104 1
        return 'HAVING ' . $this->buildCondition($condition, $state);
105
    }
106
107
    /**
108
     * Build ORDER BY part
109
     *
110
     * @param array|StructuredQuery $orders Part to be built
111
     * @param MySqlQueryBuilderState $state Query builder state
112
     * @return string
113
     */
114 1
    protected function buildOrderBy($orders, $state)
115
    {
116 1
        if ($orders instanceof StructuredQuery) {
117 1
            return 'ORDER BY '. $this->buildQuery($orders, $state);
118
        }
119
120 1
        $result = [];
121 1
        foreach ($orders as $by => $order) {
122 1
            if (!is_string($order)) {
123 1
                $order = $order === SORT_ASC ? 'asc' : 'desc';
124
            }
125
126 1
            $result[] = $this->quoteName($by) . ' ' . strtoupper($order);
127
        }
128
129 1
        return 'ORDER BY ' . implode(', ', $result);
130
    }
131
}