WhereQueryBuilder::buildWhereInQuery()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 6
Bugs 4 Features 0
Metric Value
cc 1
eloc 1
c 6
b 4
f 0
nc 1
nop 1
dl 0
loc 3
rs 10
1
<?php
2
/**
3
 * The Query builder API.
4
 *
5
 * @author RN Kushwaha <[email protected]>
6
 * @since v0.0.1 <Date: 16th April, 2019>
7
 */
8
9
namespace Dolphin\Builders;
10
11
use Dolphin\Parsers\WhereQueryParser;
12
13
/**
14
 * This class provides the mechanism to build the Where Queries.
15
 */
16
class WhereQueryBuilder extends QueryBuilder
17
{
18
    private $qb;
19
20
    public function __construct(){
21
        $this->qb = new QueryBuilder();
22
    }
23
24
    private function buildWhereInClauseQuery($terms = [])
25
    {
26
        if (is_int($terms[0])) {
27
            $dataStr = join(',', $terms);
28
            return $dataStr;
29
        } elseif (is_string($terms[0])) {
30
            $dataStr = join("', '", $terms);
31
            $dataStr = "'".$dataStr."'";
32
            return $dataStr;
33
        }
34
35
        return null;
36
    }
37
38
    private function whereAddedCondition($conditions = []){
39
      return $this->whereAdded === false && count($conditions);
40
    }
41
42
    public function buildWhereQuery($conditions = [])
43
    {
44
        $whereQuery = [];
45
        if (count($conditions)<=0) {
46
            return [];
47
        }
48
49
        $firstTime = true;
50
        if ($this->whereAddedCondition($conditions)) {
51
          $whereQuery[] = 'WHERE';
52
          $this->whereAdded = true;
53
        }
54
55
        foreach ($conditions as $where) {
56
            $sign = '=';
57
            $whereQueryPart = 'AND ';
58
            if(count($where)==3) {
59
                $sign = $where[1];
60
            }
61
62
            if ($firstTime) {
63
                $whereQueryPart = '';
64
                $firstTime = false;
65
            }
66
            $whereQuery[] = $whereQueryPart.$this->qb->quote(trim($where[0])).' '.$sign.' '.$this->qb->enclose(end($where));
67
        }
68
69
        return $whereQuery;
70
    }
71
72
    public function buildWhereRawQuery($conditions = [], $query = [])
73
    {
74
        $whereRawQuery = [];
75
76
        if (count($conditions)<=0) {
77
            return $query;
78
        }
79
80
        $firstTime = true;
81
        if ($this->whereAddedCondition($conditions)) {
82
            $whereRawQuery[] = 'WHERE';
83
            $this->whereAdded = true;
84
        }
85
86
        foreach ($conditions as $whereRaw) {
87
            $whereRawQueryPart = 'AND ';
88
            if ($firstTime === true) {
89
                $whereRawQueryPart = '';
90
                $firstTime = false;
91
            }
92
            $whereRawQuery[] = $whereRawQueryPart.$whereRaw;
93
        }
94
95
        if (count($whereRawQuery)) {
96
            $query = array_merge($query, $whereRawQuery);
97
        }
98
        return $query;
99
    }
100
101
    private function buildWhereInNotInQuery($conditions = [], $queryParam='IN')
102
    {
103
        $query = [];
104
105
        if (count($conditions)<=0) {
106
            return $query;
107
        }
108
109
        $firstTime = false;
110
        if ($this->whereAdded === false) {
111
            $query[] = 'WHERE';
112
            $firstTime = true;
113
            $this->whereAdded = true;
114
        }
115
116
        foreach ($conditions as $whereIn) {
117
            $dataStr = $this->buildWhereInClauseQuery($whereIn[1]);
118
            $queryPart = 'AND ';
119
            if ($dataStr === null) {
120
                continue;
121
            }
122
123
            if ($firstTime) {
124
                $queryPart = '';
125
                $firstTime = false;
126
            }
127
128
            $query[] = $queryPart.trim($whereIn[0]).' '.$queryParam.' ('.$dataStr.')';
129
        }
130
131
        return $query;
132
    }
133
134
    public function buildWhereInQuery($conditions = [])
135
    {
136
        return $this->buildWhereInNotInQuery($conditions, 'IN');
137
    }
138
139
    public function buildWhereNotInQuery($conditions = [])
140
    {
141
        return $this->buildWhereInNotInQuery($conditions, 'NOT IN');
142
    }
143
144
    private function buildWhereNullNotNullQuery($conditions = [], $query = [], $queryParam = 'IS NULL')
145
    {
146
        $whereQuery = [];
147
148
        if (count($conditions)<=0) {
149
            return $query;
150
        }
151
152
        $firstTime = false;
153
        if ($this->whereAdded === false) {
154
            $whereQuery[] = 'WHERE';
155
            $firstTime = true;
156
            $this->whereAdded = true;
157
        }
158
159
        foreach ($conditions as $whereNull) {
160
            $whereQueryPart = 'AND ';
161
            if ($firstTime) {
162
                $whereQueryPart = '';
163
                $firstTime = false;
164
            }
165
166
            $whereQuery[] = $whereQueryPart.trim($whereNull).' '.$queryParam;
167
        }
168
        if (count($whereQuery)) {
169
            $query = array_merge($query, $whereQuery);
170
        }
171
        return $query;
172
    }
173
174
    public function buildWhereNullQuery($conditions = [], $query = [])
175
    {
176
        return $this->buildWhereNullNotNullQuery($conditions, $query, 'IS NULL');
177
    }
178
179
    public function buildWhereNotNullQuery($conditions = [], $query = [])
180
    {
181
        return $this->buildWhereNullNotNullQuery($conditions, $query, 'IS NOT NULL');
182
    }
183
184
    public function buildAllWhereQuery($where, $whereRaw, $whereIn, $whereNotIn, $whereNull, $whereNotNull, $mainQuery = [])
185
    {
186
        $query = $this->buildWhereQuery($where);
187
        $query = $this->buildWhereRawQuery($whereRaw, $query);
188
        $whereInQuery = $this->buildWhereInQuery($whereIn);
189
        if (count($whereInQuery)) {
190
            $query = array_merge($query, $whereInQuery);
191
        }
192
        $whereNotInQuery = $this->buildWhereNotInQuery($whereNotIn);
193
        if (count($whereNotInQuery)) {
194
            $query = array_merge($query, $whereNotInQuery);
195
        }
196
        $query = $this->buildWhereNullQuery($whereNull, $query);
197
        $query = $this->buildWhereNotNullQuery($whereNotNull, $query);
198
199
        if (count($query)) {
200
            $mainQuery = array_merge($mainQuery, $query);
201
        }
202
        return $mainQuery;
203
    }
204
}
205