Skip to content

Commit ec214bc

Browse files
authored
Merge pull request #180 from dpDesignz/table-joins
fixBug: Conditional Stacked Functions in selecting
2 parents bcd5b92 + 6ece68e commit ec214bc

File tree

11 files changed

+233
-60
lines changed

11 files changed

+233
-60
lines changed

README.md

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -88,16 +88,16 @@ create('profile',
8888
---
8989

9090
innerJoin(string $leftTable = null, string $rightTable = null,
91-
string $leftColumn = null, string $rightColumn = null, $condition = EQ);
91+
string $leftColumn = null, string $rightColumn = null, string $tableAs = null, $condition = EQ);
9292

9393
leftJoin(string $leftTable = null, string $rightTable = null,
94-
string $leftColumn = null, string $rightColumn = null, $condition = EQ);
94+
string $leftColumn = null, string $rightColumn = null, string $tableAs = null, $condition = EQ);
9595

9696
rightJoin(string $leftTable = null, string $rightTable = null,
97-
string $leftColumn = null, string $rightColumn = null, $condition = EQ);
97+
string $leftColumn = null, string $rightColumn = null, string $tableAs = null, $condition = EQ);
9898

9999
fullJoin(string $leftTable = null, string $rightTable = null,
100-
string $leftColumn = null, string $rightColumn = null, $condition = EQ);
100+
string $leftColumn = null, string $rightColumn = null, string $tableAs = null, $condition = EQ);
101101
---
102102

103103
```php
@@ -179,7 +179,7 @@ foreach ($result as $row) {
179179
$result = $db->selecting('profile', 'name, email',
180180
// Conditionals can also be called, stacked with other functions like:
181181
// innerJoin(), leftJoin(), rightJoin(), fullJoin()
182-
// as (leftTable, rightTable, leftColumn, rightColumn, equal condition),
182+
// as (leftTable, rightTable, leftColumn, rightColumn, tableAs, equal condition),
183183
// where( eq( columns, values, _AND ), like( columns, _d ) ),
184184
// groupBy( columns ),
185185
// having( between( columns, values1, values2 ) ),

lib/ezFunctions.php

Lines changed: 12 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -370,11 +370,12 @@ function innerJoin(
370370
$rightTable = '',
371371
$leftColumn = null,
372372
$rightColumn = null,
373+
$tableAs = null,
373374
$condition = \EQ
374375
) {
375376
$ezQuery = \getInstance();
376-
return ($ezQuery instanceof DatabaseInterface)
377-
? $ezQuery->innerJoin($leftTable, $rightTable, $leftColumn, $rightColumn, $condition)
377+
return ($ezQuery instanceOf DatabaseInterface)
378+
? $ezQuery->innerJoin($leftTable, $rightTable, $leftColumn, $rightColumn, $tableAs, $condition)
378379
: false;
379380
}
380381

@@ -383,11 +384,12 @@ function leftJoin(
383384
$rightTable = '',
384385
$leftColumn = null,
385386
$rightColumn = null,
387+
$tableAs = null,
386388
$condition = \EQ
387389
) {
388390
$ezQuery = \getInstance();
389-
return ($ezQuery instanceof DatabaseInterface)
390-
? $ezQuery->leftJoin($leftTable, $rightTable, $leftColumn, $rightColumn, $condition)
391+
return ($ezQuery instanceOf DatabaseInterface)
392+
? $ezQuery->leftJoin($leftTable, $rightTable, $leftColumn, $rightColumn, $tableAs, $condition)
391393
: false;
392394
}
393395

@@ -396,11 +398,12 @@ function rightJoin(
396398
$rightTable = '',
397399
$leftColumn = null,
398400
$rightColumn = null,
401+
$tableAs = null,
399402
$condition = \EQ
400403
) {
401404
$ezQuery = \getInstance();
402-
return ($ezQuery instanceof DatabaseInterface)
403-
? $ezQuery->rightJoin($leftTable, $rightTable, $leftColumn, $rightColumn, $condition)
405+
return ($ezQuery instanceOf DatabaseInterface)
406+
? $ezQuery->rightJoin($leftTable, $rightTable, $leftColumn, $rightColumn, $tableAs, $condition)
404407
: false;
405408
}
406409

@@ -409,11 +412,12 @@ function fullJoin(
409412
$rightTable = '',
410413
$leftColumn = null,
411414
$rightColumn = null,
415+
$tableAs = null,
412416
$condition = \EQ
413417
) {
414418
$ezQuery = \getInstance();
415-
return ($ezQuery instanceof DatabaseInterface)
416-
? $ezQuery->fullJoin($leftTable, $rightTable, $leftColumn, $rightColumn, $condition)
419+
return ($ezQuery instanceOf DatabaseInterface)
420+
? $ezQuery->fullJoin($leftTable, $rightTable, $leftColumn, $rightColumn, $tableAs, $condition)
417421
: false;
418422
}
419423

lib/ezQuery.php

Lines changed: 21 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -186,6 +186,7 @@ public function innerJoin(
186186
string $rightTable = null,
187187
string $leftColumn = null,
188188
string $rightColumn = null,
189+
string $tableAs = null,
189190
$condition = \EQ
190191
) {
191192
return $this->joining(
@@ -194,6 +195,7 @@ public function innerJoin(
194195
$rightTable,
195196
$leftColumn,
196197
$rightColumn,
198+
$tableAs,
197199
$condition
198200
);
199201
}
@@ -203,6 +205,7 @@ public function leftJoin(
203205
string $rightTable = null,
204206
string $leftColumn = null,
205207
string $rightColumn = null,
208+
string $tableAs = null,
206209
$condition = \EQ
207210
) {
208211
return $this->joining(
@@ -211,6 +214,7 @@ public function leftJoin(
211214
$rightTable,
212215
$leftColumn,
213216
$rightColumn,
217+
$tableAs,
214218
$condition
215219
);
216220
}
@@ -220,6 +224,7 @@ public function rightJoin(
220224
string $rightTable = null,
221225
string $leftColumn = null,
222226
string $rightColumn = null,
227+
string $tableAs = null,
223228
$condition = \EQ
224229
) {
225230
return $this->joining(
@@ -228,6 +233,7 @@ public function rightJoin(
228233
$rightTable,
229234
$leftColumn,
230235
$rightColumn,
236+
$tableAs,
231237
$condition
232238
);
233239
}
@@ -237,14 +243,16 @@ public function fullJoin(
237243
string $rightTable = null,
238244
string $leftColumn = null,
239245
string $rightColumn = null,
246+
string $tableAs = null,
240247
$condition = \EQ
241248
) {
242249
return $this->joining(
243250
'FULL',
244251
$leftTable,
245252
$rightTable,
246-
$$leftColumn,
253+
$leftColumn,
247254
$rightColumn,
255+
$tableAs,
248256
$condition
249257
);
250258
}
@@ -269,36 +277,42 @@ public function fullJoin(
269277
*
270278
* @param string $leftColumn -
271279
* @param string $rightColumn -
280+
* @param string $tableAs -
272281
*
273282
* @param string $condition -
274283
*
275284
* @return bool|string JOIN sql statement, false for error
276-
*/
285+
*/
277286
private function joining(
278287
String $type = \_INNER,
279288
string $leftTable = null,
280289
string $rightTable = null,
281290
string $leftColumn = null,
282291
string $rightColumn = null,
292+
string $tableAs = null,
283293
$condition = \EQ
284294
) {
285295
if (
286296
!\in_array($type, \_JOINERS)
287297
|| !\in_array($condition, \_BOOLEAN)
288298
|| empty($leftTable)
289-
|| empty($rightTable) || empty($columnFields) || empty($leftColumn)
299+
|| empty($rightTable)
300+
|| empty($leftColumn)
290301
) {
291302
return false;
292303
}
293304

305+
if (empty($tableAs))
306+
$tableAs = $rightTable;
307+
294308
if (\is_string($leftColumn) && empty($rightColumn))
295-
$onCondition = ' ON ' . $leftTable . $leftColumn . ' = ' . $rightTable . $leftColumn;
309+
$onCondition = ' ON ' . $leftTable . '.' . $leftColumn . ' = ' . $tableAs . '.' . $leftColumn;
296310
elseif ($condition !== \EQ)
297-
$onCondition = ' ON ' . $leftTable . $leftColumn . " $condition " . $rightTable . $rightColumn;
311+
$onCondition = ' ON ' . $leftTable . '.' . $leftColumn . ' ' . $condition . ' ' . $tableAs . '.' . $rightColumn;
298312
else
299-
$onCondition = ' ON ' . $leftTable . $leftColumn . ' = ' . $rightTable . $rightColumn;
313+
$onCondition = ' ON ' . $leftTable . '.' . $leftColumn . ' = ' . $tableAs . '.' . $rightColumn;
300314

301-
return ' ' . $type . ' JOIN ' . $rightTable . $onCondition;
315+
return ' ' . $type . ' JOIN ' . $rightTable . ' AS ' . $tableAs . ' ' . $onCondition;
302316
}
303317

304318
public function orderBy($orderBy, $order)

lib/ezQueryInterface.php

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -107,6 +107,7 @@ public function having(...$having);
107107
* @param string $rightTable -
108108
* @param string $leftColumn -
109109
* @param string $rightColumn -
110+
* @param string $tableAs -
110111
* @param string $condition -
111112
*
112113
* @return bool|string JOIN sql statement, false for error
@@ -116,6 +117,7 @@ public function innerJoin(
116117
string $rightTable = null,
117118
string $leftColumn = null,
118119
string $rightColumn = null,
120+
string $tableAs = null,
119121
$condition = \EQ
120122
);
121123

@@ -139,6 +141,7 @@ public function innerJoin(
139141
* @param string $rightTable -
140142
* @param string $leftColumn -
141143
* @param string $rightColumn -
144+
* @param string $tableAs -
142145
* @param string $condition -
143146
*
144147
* @return bool|string JOIN sql statement, false for error
@@ -148,6 +151,7 @@ public function leftJoin(
148151
string $rightTable = null,
149152
string $leftColumn = null,
150153
string $rightColumn = null,
154+
string $tableAs = null,
151155
$condition = \EQ
152156
);
153157

@@ -171,6 +175,7 @@ public function leftJoin(
171175
* @param string $rightTable -
172176
* @param string $leftColumn -
173177
* @param string $rightColumn -
178+
* @param string $tableAs -
174179
* @param string $condition -
175180
*
176181
* @return bool|string JOIN sql statement, false for error
@@ -180,6 +185,7 @@ public function rightJoin(
180185
string $rightTable = null,
181186
string $leftColumn = null,
182187
string $rightColumn = null,
188+
string $tableAs = null,
183189
$condition = \EQ
184190
);
185191

@@ -202,6 +208,7 @@ public function rightJoin(
202208
* @param string $rightTable -
203209
* @param string $leftColumn -
204210
* @param string $rightColumn -
211+
* @param string $tableAs -
205212
* @param string $condition -
206213
*
207214
* @return bool|string JOIN sql statement, false for error
@@ -211,6 +218,7 @@ public function fullJoin(
211218
string $rightTable = null,
212219
string $leftColumn = null,
213220
string $rightColumn = null,
221+
string $tableAs = null,
214222
$condition = \EQ
215223
);
216224

0 commit comments

Comments
 (0)