Completed
Push — master ( fa4f9a...8a9929 )
by Beniamin
03:39
created

QueryClausesParser::parseQueryHints()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 11
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 7
CRAP Score 2

Importance

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