Passed
Push — master ( 557494...54d8cd )
by RN
01:38
created

WhereQueryBuilder::parseWhereQuery()   A

Complexity

Conditions 6
Paths 6

Size

Total Lines 18
Code Lines 10

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 6
eloc 10
nc 6
nop 1
dl 0
loc 18
rs 9.2222
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($where[0]).' '.$sign.' '.end($where);
43
                $firstTime = false;
44
            } else {
45
                $query[] = 'AND '.$this->qb->quote($where[0]).' '.$sign.' '.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 buildWhereInQuery($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 $whereIn) {
82
            $dataStr = $this->buildWhereInClauseQuery($whereIn[1]);
83
            if ($dataStr === null) {
84
                continue;
85
            }
86
87
            if ($firstTime) {
88
                $query[] = $whereIn[0].' IN ('.$dataStr.')';
89
                $firstTime = false;
90
            } else {
91
                $query[] = 'AND '.$whereIn[0].' IN ('.$dataStr.')';
92
            }
93
        }
94
95
        return $query;
96
    }
97
98
    public function buildWhereNotInQuery($conditions = array())
99
    {
100
        $query = array();
101
102
        if (!count($conditions)) {
103
            return $query;
104
        }
105
            
106
        $firstTime = false;
107
        if ($this->whereAdded === false) {
108
            $query[] = 'WHERE';
109
            $firstTime = true;
110
            $this->whereAdded = true;
111
        }
112
113
        foreach ($conditions as $whereNotIn) {
114
            $dataStr = $this->buildWhereInClauseQuery($whereNotIn[1]);
115
            if ($dataStr === null) {
116
                continue;
117
            }
118
119
            if ($firstTime) {
120
                $query[] = $whereNotIn[0].' NOT IN ('.$dataStr.')';
121
                $firstTime = false;
122
            } else {
123
                $query[] = 'AND '.$whereNotIn[0].' NOT IN ('.$dataStr.')';
124
            }
125
        }
126
127
        return $query;
128
    }
129
130
    public function buildWhereNullQuery($conditions = array())
131
    {
132
        $query = array();
133
134
        if (!count($conditions)) {
135
            return $query;
136
        }
137
        
138
        $firstTime = false;
139
        if ($this->whereAdded === false) {
140
            $query[] = 'WHERE';
141
            $firstTime = true;
142
            $this->whereAdded = true;
143
        }
144
145
        foreach ($conditions as $whereNull) {
146
            if ($firstTime) {
147
                $query[] = $whereNull.' IS NULL';
148
                $firstTime = false;
149
            } else {
150
                $query[] = 'AND '.$whereNull.' IS NULL';
151
            }
152
        }
153
154
        return $query;
155
    }
156
157
    public function buildWhereNotNullQuery($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 $whereNotNull) {
173
            if ($firstTime) {
174
                $query[] = $whereNotNull.' IS NOT NULL';
175
                $firstTime = false;
176
            } else {
177
                $query[] = 'AND '.$whereNotNull.' IS NOT NULL';
178
            }
179
        }
180
181
        return $query;
182
    }
183
184
    public function buildAllWhereQuery($where, $whereIn, $whereNotIn, $whereNull, $whereNotNull)
185
    {
186
        $query = array();
187
        $whereQuery = $this->buildWhereQuery($where);
188
        if (count($whereQuery)) {
189
            $query = array_merge($query, $whereQuery);
190
        }
191
192
        $whereInQuery = $this->buildWhereInQuery($whereIn);
193
        if (count($whereInQuery)) {
194
            $query = array_merge($query, $whereInQuery);
195
        }
196
197
        $whereNotInQuery = $this->buildWhereNotInQuery($whereNotIn);
198
        if (count($whereNotInQuery)) {
199
            $query = array_merge($query, $whereNotInQuery);
200
        }
201
202
        $whereNullQuery = $this->buildWhereNullQuery($whereNull);
203
        if (count($whereNullQuery)) {
204
            $query = array_merge($query, $whereNullQuery);
205
        }
206
207
        $whereNotNullQuery = $this->buildWhereNotNullQuery($whereNotNull);
208
        if (count($whereNotNullQuery)) {
209
            $query = array_merge($query, $whereNotNullQuery);
210
        }
211
212
        return $query;
213
    }
214
}
215