Completed
Push — master ( cc1f48...9b2df7 )
by Beniamin
04:10
created

SelectQueryCompiler::canHandleQuery()   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 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 4
ccs 2
cts 2
cp 1
rs 10
cc 1
eloc 2
nc 1
nop 1
crap 1
1
<?php
2
3
namespace Phuria\QueryBuilder\QueryCompiler;
4
5
use Phuria\QueryBuilder\Compiler\SeparatedListCompiler;
6
use Phuria\QueryBuilder\QueryBuilder;
7
8
/**
9
 * @author Beniamin Jonatan Šimko <[email protected]>
10
 */
11
class SelectQueryCompiler implements QueryCompilerInterface
12
{
13
    /**
14
     * @inheritdoc
15
     */
16 22
    public function canHandleQuery(QueryBuilder $qb)
17
    {
18 22
        return (bool) $qb->getSelectClauses();
19
    }
20
21
    /**
22
     * @inheritdoc
23
     */
24 21
    public function compile(QueryBuilder $qb)
25
    {
26 21
        $commaSeparated = new SeparatedListCompiler(', ');
27 21
        $andSeparated = new SeparatedListCompiler(' AND ');
28 21
        $spaceSeparated = new SeparatedListCompiler(' ');
29
30 21
        $select = $commaSeparated->compile($qb->getSelectClauses());
31 21
        $afterSelectHints = $spaceSeparated->compile($qb->getHints());
32 21
        $where = $andSeparated->compile($qb->getWhereClauses());
33 21
        $from = $commaSeparated->compile($qb->getRootTables());
34 21
        $join = $spaceSeparated->compile($qb->getJoinTables());
35 21
        $orderBy = $commaSeparated->compile($qb->getOrderByClauses());
36 21
        $groupBy = $commaSeparated->compile($qb->getGroupByClauses());
37 21
        $having = $andSeparated->compile($qb->getHavingClauses());
38
39 21
        $sql = "SELECT";
40
41 21
        if ($afterSelectHints) {
42 1
            $sql .= ' ' . $afterSelectHints;
43 1
        }
44
45 21
        if ($select) {
46 21
            $sql .= ' ' . $select;
47 21
        }
48
49 21
        if ($from) {
50 20
            $sql .= ' FROM ' . $from;
51 20
        }
52
53 21
        if ($join) {
54 3
            $sql .= ' ' . $join;
55 3
        }
56
57 21
        if ($where) {
58 3
            $sql .= ' WHERE ' . $where;
59 3
        }
60
61 21
        if ($groupBy) {
62 2
            $sql .= ' GROUP BY ' . $groupBy;
63 2
        }
64
65 21
        if ($having) {
66 1
            $sql .= ' HAVING ' . $having;
67 1
        }
68
69 21
        if ($orderBy) {
70 1
            $sql .= ' ORDER BY ' . $orderBy;
71 1
        }
72
73 21
        return $sql;
74
    }
75
}