Passed
Push — master ( 4bcd21...b44f1e )
by RN
07:16
created

WhereQueryBuilder::buildAllWhereQuery()   B

Complexity

Conditions 7
Paths 64

Size

Total Lines 34
Code Lines 20

Duplication

Lines 0
Ratio 0 %

Importance

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