Passed
Push — master ( f6b2ac...6bb1e5 )
by RN
01:58
created

QueryBuilder   A

Complexity

Total Complexity 23

Size/Duplication

Total Lines 128
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 69
c 1
b 0
f 0
dl 0
loc 128
rs 10
wmc 23

7 Methods

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