Passed
Push — master ( b44f1e...f5d8d4 )
by RN
01:44
created

WhereQueryBuilder::buildWhereNotNullQuery()   A

Complexity

Conditions 5
Paths 7

Size

Total Lines 25
Code Lines 15

Duplication

Lines 0
Ratio 0 %

Importance

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