Passed
Push — master ( d6def4...043641 )
by RN
01:52
created

QueryBuilder::queryPrefix()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 1
nc 1
nop 1
dl 0
loc 3
rs 10
c 0
b 0
f 0
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
    private function buildLimitQuery($limit, $offset, $query = array()){
81
      $limitQuery = [];
82
      if (!empty($limit)) {
83
          $query[] = 'LIMIT';
84
85
          if (!empty($offset)) {
86
              $query[] = $offset.',';
87
          }
88
89
          $query[] = $limit;
90
      }
91
92
      if (count($limitQuery)) {
93
          $query = array_merge($query, $limitQuery);
94
      }
95
      return $query;
96
    }
97
98
    public function buildQuery(array $params)
99
    {
100
        $jqb    = new JoinQueryBuilder();
101
        $wqb    = new WhereQueryBuilder();
102
103
        $prefix = $this->getPrefix();
104
        $tblWithPrefix = $params['table'];
105
        $tbl    = str_replace($prefix, '', $tblWithPrefix);
106
        $query  = [];
107
108
        $query[] = 'SELECT';
109
        $query[] = $this->getQueryFields($params['fields'], $tbl);
110
        $query[] = 'FROM';
111
        $query[] = $this->quote($tblWithPrefix).' AS '.$this->quote($tbl);
112
113
        $query = $jqb->buildAllJoinQuery(
114
                                $params['join'],
115
                                $params['leftJoin'],
116
                                $params['rightJoin'],
117
                                $params['crossJoin'],
118
                                $query
119
                            );
120
121
        $query = $wqb->buildAllWhereQuery(
122
                                    $params['where'],
123
                                    $params['whereRaw'],
124
                                    $params['whereIn'],
125
                                    $params['whereNotIn'],
126
                                    $params['whereNull'],
127
                                    $params['whereNotNull'],
128
                                    $query
129
                                );
130
131
        if (!empty($params['groupBy'])) {
132
            $query[] = 'GROUP BY';
133
            $query[] = $params['groupBy'];
134
        }
135
136
        if (!empty($params['having'])) {
137
            $query[] = 'HAVING';
138
            $query[] = $params['having'];
139
        }
140
141
        if (!empty($params['orderBy'])) {
142
            $query[] = 'ORDER BY';
143
            $query[] = $params['orderBy'];
144
        }
145
146
        $query = $this->buildLimitQuery($params['limit'], $params['offset'], $query);
147
148
        return $query;
149
    }
150
}
151