Passed
Push — master ( 4bcd21...b44f1e )
by RN
07:16
created

QueryBuilder   A

Complexity

Total Complexity 23

Size/Duplication

Total Lines 126
Duplicated Lines 0 %

Importance

Changes 2
Bugs 0 Features 0
Metric Value
eloc 67
dl 0
loc 126
rs 10
c 2
b 0
f 0
wmc 23

8 Methods

Rating   Name   Duplication   Size   Complexity  
A getPrefix() 0 3 1
A fetchType() 0 12 6
A queryPrefix() 0 3 1
A addAlias() 0 11 2
A quote() 0 7 2
A getQueryFields() 0 7 2
A enclose() 0 3 1
B buildQuery() 0 63 8
1
<?php
2
/**
3
 * The Query builder API.
4
 *
5
 * @author RN Kushwaha <[email protected]>
6
 *
7
 * @since v0.0.1 <Date: 16th April, 2019>
8
 */
9
10
namespace Dolphin\Builders;
11
12
use Dolphin\Connections\Connection;
13
14
/**
15
 * This class provides the mechanism to build the Queries.
16
 */
17
class QueryBuilder
18
{
19
    protected $whereAdded = false;
20
21
    public function queryPrefix($query)
22
    {
23
        return str_replace('#__', Connection::getPrefix(), $query);
24
    }
25
26
    public function getPrefix()
27
    {
28
        return Connection::getPrefix();
29
    }
30
31
    public function fetchType($fetchMode = 'FETCH_OBJ')
32
    {
33
        switch ($fetchMode) {
34
            case 'FETCH_ASSOC': $fetch = \PDO::FETCH_ASSOC; break;
35
            case 'FETCH_NUM': $fetch = \PDO::FETCH_NUM; break;
36
            case 'FETCH_BOTH': $fetch = \PDO::FETCH_BOTH; break;
37
            case 'FETCH_BOUND': $fetch = \PDO::FETCH_BOUND; break;
38
            case 'FETCH_CLASS': $fetch = \PDO::FETCH_CLASS; break;
39
            default: $fetch = \PDO::FETCH_OBJ;
40
        }
41
42
        return $fetch;
43
    }
44
45
    public function addAlias($tableName)
46
    {
47
        $tableAlias = '';
48
49
        if (stripos($tableName, ' as ') > 0) {
50
            $tblName = explode(' as ', $tableName);
51
            $tableAlias = ' AS '.self::quote($tblName[1]);
0 ignored issues
show
Bug Best Practice introduced by
The method Dolphin\Builders\QueryBuilder::quote() is not static, but was called statically. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

51
            $tableAlias = ' AS '.self::/** @scrutinizer ignore-call */ quote($tblName[1]);
Loading history...
52
            $tableName = $tblName[0];
53
        }
54
55
        return [$tableName, $tableAlias];
56
    }
57
58
    public function quote($field)
59
    {
60
        if (strpos($field, '.') !== false) {
61
            $field = str_replace('.', '`.`', $field);
62
        }
63
64
        return '`'.$field.'`';
65
    }
66
    public function enclose($field)
67
    {
68
        return "'".$field."'";
69
    }
70
71
    private function getQueryFields($fields, $tbl){
72
        $startQuery = join(', ', $fields);
73
        if (empty($fields)) {
74
            $startQuery = $this->quote($tbl).'.*';
75
        }
76
77
        return $startQuery;
78
    }
79
80
    public function buildQuery(array $params)
81
    {
82
        $jqb    = new JoinQueryBuilder();
83
        $wqb    = new WhereQueryBuilder();
84
85
        $prefix = $this->getPrefix();
86
        $tblWithPrefix = $params['table'];
87
        $tbl    = str_replace($prefix, '', $tblWithPrefix);
88
        $query  = [];
89
90
        $query[] = 'SELECT';
91
        $query[] = $this->getQueryFields($params['fields'], $tbl);
92
        $query[] = 'FROM';
93
        $query[] = $this->quote($tblWithPrefix).' AS '.$this->quote($tbl);
94
95
        $allJoinQuery = $jqb->buildAllJoinQuery(
96
                                $params['join'],
97
                                $params['leftJoin'],
98
                                $params['rightJoin'],
99
                                $params['crossJoin']
100
                            );
101
        if (count($allJoinQuery)) {
102
            $query = array_merge($query, $allJoinQuery);
103
        }
104
105
        $allWhereQuery = $wqb->buildAllWhereQuery(
106
                                    $params['where'],
107
                                    $params['whereRaw'],
108
                                    $params['whereIn'],
109
                                    $params['whereNotIn'],
110
                                    $params['whereNull'],
111
                                    $params['whereNotNull']
112
                                );
113
        if (count($allWhereQuery)) {
114
            $query = array_merge($query, $allWhereQuery);
115
        }
116
117
        if (!empty($params['groupBy'])) {
118
            $query[] = 'GROUP BY';
119
            $query[] = $params['groupBy'];
120
        }
121
122
        if (!empty($params['having'])) {
123
            $query[] = 'HAVING';
124
            $query[] = $params['having'];
125
        }
126
127
        if (!empty($params['orderBy'])) {
128
            $query[] = 'ORDER BY';
129
            $query[] = $params['orderBy'];
130
        }
131
132
        if (!empty($params['limit'])) {
133
            $query[] = 'LIMIT';
134
135
            if (!empty($params['offset'])) {
136
                $query[] = $params['offset'].',';
137
            }
138
139
            $query[] = $params['limit'];
140
        }
141
142
        return $query;
143
    }
144
}
145