Completed
Push — master ( efa6a6...b0b60e )
by Beniamin
03:03
created

QueryClausesParser::parseGroupByClause()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 8
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 4
CRAP Score 2

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 8
ccs 4
cts 4
cp 1
rs 9.4285
cc 2
eloc 4
nc 2
nop 0
crap 2
1
<?php
2
3
/**
4
 * This file is part of Phuria SQL Builder package.
5
 *
6
 * Copyright (c) 2016 Beniamin Jonatan Šimko
7
 *
8
 * For the full copyright and license information, please view the LICENSE
9
 * file that was distributed with this source code.
10
 */
11
12
namespace Phuria\QueryBuilder\Parser;
13
14
use Phuria\QueryBuilder\QueryBuilder;
15
use Phuria\QueryBuilder\QueryClauses;
16
use Phuria\QueryBuilder\Table\AbstractTable;
17
18
/**
19
 * @author Beniamin Jonatan Šimko <[email protected]>
20
 */
21
class QueryClausesParser
22
{
23
    /**
24
     * @var QueryClauses
25
     */
26
    private $queryClauses;
27
28
    /**
29
     * @var QueryBuilder $qb
30
     */
31
    private $qb;
32
33
    /**
34
     * @param QueryBuilder $qb
35
     */
36 20
    public function __construct(QueryBuilder $qb)
37
    {
38 20
        $this->queryClauses = $qb->getQueryClauses();
39 20
        $this->qb = $qb;
40 20
    }
41
42
    /**
43
     * @return string
44
     */
45 19
    public function parseSelectClause()
46
    {
47 19
        if ($clauses = $this->queryClauses->getSelectClauses()) {
48 19
            return 'SELECT ' . implode(', ', $clauses);
49
        }
50
51
        return '';
52
    }
53
54
    /**
55
     * @return string
56
     */
57 1
    public function parseUpdateClause()
58
    {
59 1
        $rootTables = $this->qb->getRootTables();
60
61 1
        if (0 === count($rootTables)) {
62
            return '';
63
        }
64
65
        return 'UPDATE ' . implode(', ', array_map(function (AbstractTable $table) {
66 1
            return $this->parseTableDeclaration($table);
67 1
        }, $rootTables));
68
    }
69
70
    /**
71
     * @return string
72
     */
73 19
    public function parseFromClause()
74
    {
75 19
        $rootTables = $this->qb->getRootTables();
76
77 19
        if (0 === count($rootTables)) {
78 1
            return '';
79
        }
80
81 18
        return 'FROM ' . implode(', ', array_map(function (AbstractTable $table) {
82 18
            return $this->parseTableDeclaration($table);
83 18
        }, $rootTables));
84
    }
85
86
    /**
87
     * @return string
88
     */
89 19
    public function parseJoinClause()
90
    {
91 19
        $joinTables = $this->qb->getJoinTables();
92
93 19
        if (0 === count($joinTables)) {
94 16
            return '';
95
        }
96
97 3
        $joins = [];
98
99 3
        foreach ($joinTables as $table) {
100 3
            $joins[] = $this->parseTableDeclaration($table);
101 3
        }
102
103 3
        return implode(' ', $joins);
104
    }
105
106
    /**
107
     * @return string
108
     */
109 19
    public function parseWhereClause()
110
    {
111 19
        if ($clauses = $this->queryClauses->getWhereClauses()) {
112 3
            return 'WHERE ' . implode(' AND ', $clauses);
113
        }
114
115 16
        return '';
116
    }
117
118
    /**
119
     * @return string
120
     */
121 19
    public function parseOrderByClause()
122
    {
123 19
        if ($clauses = $this->queryClauses->getOrderByClauses()) {
124 1
            return 'ORDER BY ' . implode(', ', $clauses);
125
        }
126
127 18
        return '';
128
    }
129
130
    /**
131
     * @return string
132
     */
133 1
    public function parseSetClause()
134
    {
135 1
        if ($clauses = $this->queryClauses->getSetClauses()) {
136 1
            return 'SET ' . implode(', ', $clauses);
137
        }
138
139
        return '';
140
    }
141
142
    /**
143
     * @return string
144
     */
145 19
    public function parseGroupByClause()
146
    {
147 19
        if ($clauses = $this->queryClauses->getGroupByClauses()) {
148 2
            return 'GROUP BY ' . implode(', ', $clauses);
149
        }
150
151 17
        return '';
152
    }
153
154
    /**
155
     * @return string
156
     */
157 19
    public function parseHavingClause()
158
    {
159 19
        if ($clauses = $this->queryClauses->getHavingClauses()) {
160 1
            return 'HAVING ' . implode(' AND ', $clauses);
161
        }
162
163 18
        return '';
164
    }
165
166
    /**
167
     * @return string
168
     */
169 19
    public function parseLimitClause()
170
    {
171 19
        if ($clause = $this->queryClauses->getLimitClause()) {
172 1
            return 'LIMIT ' . $clause;
173
        }
174
175 18
        return '';
176
    }
177
178
    /**
179
     * @param AbstractTable $table
180
     *
181
     * @return string
182
     */
183 19
    private function parseTableDeclaration(AbstractTable $table)
184
    {
185 19
        $declaration = '';
186
187 19
        if ($table->isJoin()) {
188 3
            $declaration .= $table->getJoinType() . ' ';
189 3
        }
190
191 19
        $declaration .= $table->getTableName();
192
193 19
        if ($alias = $table->getAlias()) {
194 8
            $declaration .= ' AS ' . $alias;
195 8
        }
196
197 19
        if ($joinOn = $table->getJoinOn()) {
198 2
            $declaration .= ' ON ' . $joinOn;
199 2
        }
200
201 19
        return $declaration;
202
    }
203
}