Passed
Push — master ( 56b0ed...dd8496 )
by RN
01:44
created

WhereQueryBuilder::buildWhereNullQuery()   A

Complexity

Conditions 5
Paths 12

Size

Total Lines 25
Code Lines 16

Duplication

Lines 0
Ratio 0 %

Importance

Changes 3
Bugs 2 Features 0
Metric Value
cc 5
eloc 16
c 3
b 2
f 0
nc 12
nop 2
dl 0
loc 25
rs 9.4222
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 emptyConditionsResponse($conditions = array()){
25
        if (!count($conditions)) {
26
            return array();
27
        }
28
    }
29
30
    private function buildWhereInClauseQuery($terms = array())
31
    {
32
        if (is_int($terms[0])) {
33
            $dataStr = join(',', $terms);
34
        } elseif (is_string($terms[0])) {
35
            $dataStr = join("', '", $terms);
36
            $dataStr = "'".$dataStr."'";
37
        } else {
38
            return null;
39
        }
40
41
        return $dataStr;
42
    }
43
44
    public function buildWhereQuery($conditions = array(), $query = array())
45
    {
46
        $whereQuery = array();
47
        $this->emptyConditionsResponse($conditions);
48
49
        $firstTime = true;
50
        $whereQuery[] = 'WHERE';
51
        $this->whereAdded = true;
52
53
        foreach ($conditions as $where) {
54
            $sign = '=';
55
            if(count($where)==3) {
56
                $sign = $where[1];
57
            }
58
            if ($firstTime) {
59
                $whereQuery[] = $this->qb->quote(trim($where[0])).' '.$sign.' '.$this->qb->enclose(end($where));
60
                $firstTime = false;
61
            } else {
62
                $whereQuery[] = 'AND '.$this->qb->quote(trim($where[0])).' '.$sign.' '.$this->qb->enclose(end($where));
63
            }
64
        }
65
66
        if (count($whereQuery)) {
67
            $query = array_merge($query, $whereQuery);
68
        }
69
        return $query;
70
    }
71
72
    public function buildWhereRawQuery($conditions = array(), $query = array())
73
    {
74
        $whereRawQuery = array();
75
76
        $this->emptyConditionsResponse($conditions);
77
78
        $firstTime = false;
79
        if ($this->whereAdded === false) {
80
            $whereRawQuery[] = 'WHERE';
81
            $firstTime = true;
82
            $this->whereAdded = true;
83
        }
84
85
        foreach ($conditions as $whereRaw) {
86
            if ($firstTime) {
87
                $whereRawQuery[] = $whereRaw;
88
                $firstTime = false;
89
            } else {
90
                $whereRawQuery[] = 'AND '.$whereRaw;
91
            }
92
        }
93
94
        if (count($whereRawQuery)) {
95
            $query = array_merge($query, $whereRawQuery);
96
        }
97
        return $query;
98
    }
99
100
    public function buildWhereInQuery($conditions = array())
101
    {
102
        $whereInQuery = array();
103
104
        $this->emptyConditionsResponse($conditions);
105
106
        $firstTime = false;
107
        if ($this->whereAdded === false) {
108
            $whereInQuery[] = 'WHERE';
109
            $firstTime = true;
110
            $this->whereAdded = true;
111
        }
112
113
        foreach ($conditions as $whereIn) {
114
            $dataStr = $this->buildWhereInClauseQuery($whereIn[1]);
115
            if ($dataStr === null) {
116
                continue;
117
            }
118
119
            if ($firstTime) {
120
                $whereInQuery[] = trim($whereIn[0]).' IN ('.$dataStr.')';
121
                $firstTime = false;
122
            } else {
123
                $whereInQuery[] = 'AND '.trim($whereIn[0]).' IN ('.$dataStr.')';
124
            }
125
        }
126
127
        return $whereInQuery;
128
    }
129
130
    public function buildWhereNotInQuery($conditions = array())
131
    {
132
        $whereNotInQuery = array();
133
134
        $this->emptyConditionsResponse($conditions);
135
136
        $firstTime = false;
137
        if ($this->whereAdded === false) {
138
            $whereNotInQuery[] = 'WHERE';
139
            $firstTime = true;
140
            $this->whereAdded = true;
141
        }
142
143
        foreach ($conditions as $whereNotIn) {
144
            $dataStr = $this->buildWhereInClauseQuery($whereNotIn[1]);
145
            if ($dataStr === null) {
146
                continue;
147
            }
148
149
            if ($firstTime) {
150
                $whereNotInQuery[] = trim($whereNotIn[0]).' NOT IN ('.$dataStr.')';
151
                $firstTime = false;
152
            } else {
153
                $whereNotInQuery[] = 'AND '.trim($whereNotIn[0]).' NOT IN ('.$dataStr.')';
154
            }
155
        }
156
157
        return $whereNotInQuery;
158
    }
159
160
    public function buildWhereNullQuery($conditions = array(), $query = array())
161
    {
162
        $whereNullQuery = array();
163
164
        $this->emptyConditionsResponse($conditions);
165
166
        $firstTime = false;
167
        if ($this->whereAdded === false) {
168
            $whereNullQuery[] = 'WHERE';
169
            $firstTime = true;
170
            $this->whereAdded = true;
171
        }
172
173
        foreach ($conditions as $whereNull) {
174
            if ($firstTime) {
175
                $whereNullQuery[] = trim($whereNull).' IS NULL';
176
                $firstTime = false;
177
            } else {
178
                $whereNullQuery[] = 'AND '.trim($whereNull).' IS NULL';
179
            }
180
        }
181
        if (count($whereNullQuery)) {
182
            $query = array_merge($query, $whereNullQuery);
183
        }
184
        return $query;
185
    }
186
187
    public function buildWhereNotNullQuery($conditions = array(), $query = array())
188
    {
189
        $whereNotNullQuery = array();
190
191
        $this->emptyConditionsResponse($conditions);
192
193
        $firstTime = false;
194
        if ($this->whereAdded === false) {
195
            $whereNotNullQuery[] = 'WHERE';
196
            $firstTime = true;
197
            $this->whereAdded = true;
198
        }
199
200
        foreach ($conditions as $whereNotNull) {
201
            if ($firstTime) {
202
                $whereNotNullQuery[] = trim($whereNotNull).' IS NOT NULL';
203
                $firstTime = false;
204
            } else {
205
                $whereNotNullQuery[] = 'AND '.trim($whereNotNull).' IS NOT NULL';
206
            }
207
        }
208
        if (count($whereNotNullQuery)) {
209
            $query = array_merge($query, $whereNotNullQuery);
210
        }
211
        return $query;
212
    }
213
214
    public function buildAllWhereQuery($where, $whereRaw, $whereIn, $whereNotIn, $whereNull, $whereNotNull, $mainQuery = array())
215
    {
216
        $query = array();
217
        $query = $this->buildWhereQuery($where, $query);
218
        $query = $this->buildWhereRawQuery($whereRaw, $query);
219
        $whereInQuery = $this->buildWhereInQuery($whereIn);
220
        if (count($whereInQuery)) {
221
            $query = array_merge($query, $whereInQuery);
222
        }
223
        $whereNotInQuery = $this->buildWhereNotInQuery($whereNotIn);
224
        if (count($whereNotInQuery)) {
225
            $query = array_merge($query, $whereNotInQuery);
226
        }
227
        $query = $this->buildWhereNullQuery($whereNull, $query);
228
        $query = $this->buildWhereNotNullQuery($whereNotNull, $query);
229
230
        if (count($query)) {
231
            $mainQuery = array_merge($mainQuery, $query);
232
        }
233
        return $mainQuery;
234
    }
235
}
236