Skip to content

Commit 575a781

Browse files
committed
Added $tableAs option in joins
Added the ability to set a $tableAs for joins if the left table is the same as the right table. Will default to the right table name if not set.
1 parent 2b39304 commit 575a781

File tree

3 files changed

+244
-224
lines changed

3 files changed

+244
-224
lines changed

lib/ezFunctions.php

Lines changed: 94 additions & 90 deletions
Original file line numberDiff line numberDiff line change
@@ -94,25 +94,25 @@ function dropColumn(string $columnName, ...$data)
9494
}
9595

9696
function createCertificate(
97-
string $privatekeyFile = 'certificate.key',
98-
string $certificateFile = 'certificate.crt',
99-
string $signingFile = 'certificate.csr',
100-
// string $caCertificate = null,
101-
string $ssl_path = null,
97+
string $privatekeyFile = 'certificate.key',
98+
string $certificateFile = 'certificate.crt',
99+
string $signingFile = 'certificate.csr',
100+
// string $caCertificate = null,
101+
string $ssl_path = null,
102102
array $details = ["commonName" => "localhost"]
103-
)
103+
)
104104
{
105105
return ezQuery::createCertificate($privatekeyFile, $certificateFile, $signingFile, $ssl_path, $details);
106106
}
107107

108108
/**
109109
* Creates an array from expressions in the following format
110-
*
110+
*
111111
* @param strings $x, - The left expression.
112-
* @param strings $operator, - One of
113-
* '<', '>', '=', '!=', '>=', '<=', '<>', 'IN',, 'NOT IN', 'LIKE',
112+
* @param strings $operator, - One of
113+
* '<', '>', '=', '!=', '>=', '<=', '<>', 'IN',, 'NOT IN', 'LIKE',
114114
* 'NOT LIKE', 'BETWEEN', 'NOT BETWEEN', 'IS', 'IS NOT', or the constants above.
115-
*
115+
*
116116
* @param strings $y, - The right expression.
117117
* @param strings $and, - combine additional expressions with, 'AND','OR', 'NOT', 'AND NOT'.
118118
* @param strings $args - for any extras
@@ -121,10 +121,10 @@ function createCertificate(
121121
* {
122122
* return array($x, $operator, $y, $and, ...$args);
123123
* }
124-
*
124+
*
125125
* @return array
126126
*/
127-
127+
128128
/**
129129
* Creates an equality comparison expression with the given arguments.
130130
*/
@@ -154,7 +154,7 @@ function ne($x, $y, $and = null, ...$args)
154154
\array_push($expression, $x, \NE, $y, $and, ...$args);
155155
return $expression;
156156
}
157-
157+
158158
/**
159159
* Creates a lower-than comparison expression with the given arguments.
160160
*/
@@ -274,7 +274,7 @@ function notBetween($x, $y, $y2, ...$args)
274274
\array_push($expression, $x, \_notBETWEEN, $y, $y2, \_AND, ...$args);
275275
return $expression;
276276
}
277-
277+
278278
/**
279279
* Using global class instances, setup functions to call class methods directly.
280280
*
@@ -287,7 +287,7 @@ function setInstance($ezSQL = '') {
287287
if ($ezSQL instanceOf DatabaseInterface) {
288288
$ezInstance = $ezSQL;
289289
$status = true;
290-
}
290+
}
291291

292292
return $status;
293293
}
@@ -305,164 +305,168 @@ function clearInstance() {
305305

306306
function cleanInput($string) {
307307
return ezQuery::clean($string);
308-
}
308+
}
309309

310310
function select($table = '', $columns = '*', ...$args) {
311311
$ezQuery = \getInstance();
312-
return ($ezQuery instanceOf DatabaseInterface)
313-
? $ezQuery->selecting($table, $columns, ...$args)
312+
return ($ezQuery instanceOf DatabaseInterface)
313+
? $ezQuery->selecting($table, $columns, ...$args)
314314
: false;
315-
}
316-
315+
}
316+
317317
function select_into($table, $columns = '*', $old = null, ...$args) {
318318
$ezQuery = \getInstance();
319-
return ($ezQuery instanceOf DatabaseInterface)
320-
? $ezQuery->select_into($table, $columns, $old, ...$args)
319+
return ($ezQuery instanceOf DatabaseInterface)
320+
? $ezQuery->select_into($table, $columns, $old, ...$args)
321321
: false;
322-
}
323-
322+
}
323+
324324
function insert_select($totable = '', $columns = '*', $fromTable, $from = '*', ...$args) {
325325
$ezQuery = \getInstance();
326-
return ($ezQuery instanceOf DatabaseInterface)
327-
? $ezQuery->insert_select($totable, $columns, $fromTable, $from, ...$args)
326+
return ($ezQuery instanceOf DatabaseInterface)
327+
? $ezQuery->insert_select($totable, $columns, $fromTable, $from, ...$args)
328328
: false;
329-
}
330-
329+
}
330+
331331
function create_select($table, $from, $old = null, ...$args) {
332332
$ezQuery = \getInstance();
333-
return ($ezQuery instanceOf DatabaseInterface)
334-
? $ezQuery->create_select($table, $from, $old, ...$args)
333+
return ($ezQuery instanceOf DatabaseInterface)
334+
? $ezQuery->create_select($table, $from, $old, ...$args)
335335
: false;
336-
}
337-
336+
}
337+
338338
function where( ...$args) {
339339
$ezQuery = \getInstance();
340-
return ($ezQuery instanceOf DatabaseInterface)
341-
? $ezQuery->where( ...$args)
340+
return ($ezQuery instanceOf DatabaseInterface)
341+
? $ezQuery->where( ...$args)
342342
: false;
343-
}
344-
343+
}
344+
345345
function groupBy($groupBy) {
346346
$ezQuery = \getInstance();
347-
return ($ezQuery instanceOf DatabaseInterface)
348-
? $ezQuery->groupBy($groupBy)
347+
return ($ezQuery instanceOf DatabaseInterface)
348+
? $ezQuery->groupBy($groupBy)
349349
: false;
350-
}
351-
350+
}
351+
352352
function having( ...$args) {
353353
$ezQuery = \getInstance();
354-
return ($ezQuery instanceOf DatabaseInterface)
355-
? $ezQuery->having( ...$args)
354+
return ($ezQuery instanceOf DatabaseInterface)
355+
? $ezQuery->having( ...$args)
356356
: false;
357357
}
358358

359359
function innerJoin(
360360
$leftTable = '',
361-
$rightTable = '',
362-
$leftColumn = null,
363-
$rightColumn = null,
361+
$rightTable = '',
362+
$leftColumn = null,
363+
$rightColumn = null,
364+
$tableAs = null,
364365
$condition = \EQ
365366
) {
366367
$ezQuery = \getInstance();
367-
return ($ezQuery instanceOf DatabaseInterface)
368-
? $ezQuery->innerJoin($leftTable, $rightTable, $leftColumn, $rightColumn, $condition)
368+
return ($ezQuery instanceOf DatabaseInterface)
369+
? $ezQuery->innerJoin($leftTable, $rightTable, $leftColumn, $rightColumn, $tableAs, $condition)
369370
: false;
370371
}
371372

372373
function leftJoin(
373374
$leftTable = '',
374-
$rightTable = '',
375-
$leftColumn = null,
376-
$rightColumn = null,
375+
$rightTable = '',
376+
$leftColumn = null,
377+
$rightColumn = null,
378+
$tableAs = null,
377379
$condition = \EQ
378380
) {
379381
$ezQuery = \getInstance();
380-
return ($ezQuery instanceOf DatabaseInterface)
381-
? $ezQuery->leftJoin($leftTable, $rightTable, $leftColumn, $rightColumn, $condition)
382+
return ($ezQuery instanceOf DatabaseInterface)
383+
? $ezQuery->leftJoin($leftTable, $rightTable, $leftColumn, $rightColumn, $tableAs, $condition)
382384
: false;
383385
}
384386

385387
function rightJoin(
386388
$leftTable = '',
387-
$rightTable = '',
388-
$leftColumn = null,
389-
$rightColumn = null,
389+
$rightTable = '',
390+
$leftColumn = null,
391+
$rightColumn = null,
392+
$tableAs = null,
390393
$condition = \EQ
391394
) {
392395
$ezQuery = \getInstance();
393-
return ($ezQuery instanceOf DatabaseInterface)
394-
? $ezQuery->rightJoin($leftTable, $rightTable, $leftColumn, $rightColumn, $condition)
396+
return ($ezQuery instanceOf DatabaseInterface)
397+
? $ezQuery->rightJoin($leftTable, $rightTable, $leftColumn, $rightColumn, $tableAs, $condition)
395398
: false;
396399
}
397400

398401
function fullJoin(
399402
$leftTable = '',
400-
$rightTable = '',
401-
$leftColumn = null,
402-
$rightColumn = null,
403+
$rightTable = '',
404+
$leftColumn = null,
405+
$rightColumn = null,
406+
$tableAs = null,
403407
$condition = \EQ
404408
) {
405409
$ezQuery = \getInstance();
406-
return ($ezQuery instanceOf DatabaseInterface)
407-
? $ezQuery->fullJoin($leftTable, $rightTable, $leftColumn, $rightColumn, $condition)
410+
return ($ezQuery instanceOf DatabaseInterface)
411+
? $ezQuery->fullJoin($leftTable, $rightTable, $leftColumn, $rightColumn, $tableAs, $condition)
408412
: false;
409413
}
410414

411415
function union($table = '', $columnFields = '*', ...$conditions) {
412416
$ezQuery = \getInstance();
413-
return ($ezQuery instanceOf DatabaseInterface)
414-
? $ezQuery->union($table, $columnFields, ...$conditions)
417+
return ($ezQuery instanceOf DatabaseInterface)
418+
? $ezQuery->union($table, $columnFields, ...$conditions)
415419
: false;
416-
}
420+
}
417421

418422
function unionAll($table = '', $columnFields = '*', ...$conditions) {
419423
$ezQuery = \getInstance();
420-
return ($ezQuery instanceOf DatabaseInterface)
421-
? $ezQuery->unionAll($table, $columnFields, ...$conditions)
424+
return ($ezQuery instanceOf DatabaseInterface)
425+
? $ezQuery->unionAll($table, $columnFields, ...$conditions)
422426
: false;
423-
}
427+
}
424428

425429
function orderBy($orderBy, $order) {
426430
$ezQuery = \getInstance();
427-
return ($ezQuery instanceOf DatabaseInterface)
428-
? $ezQuery->orderBy($orderBy, $order)
431+
return ($ezQuery instanceOf DatabaseInterface)
432+
? $ezQuery->orderBy($orderBy, $order)
429433
: false;
430-
}
434+
}
431435

432436
function limit($numberOf, $offset = null) {
433437
$ezQuery = \getInstance();
434-
return ($ezQuery instanceOf DatabaseInterface)
435-
? $ezQuery->limit($numberOf, $offset)
438+
return ($ezQuery instanceOf DatabaseInterface)
439+
? $ezQuery->limit($numberOf, $offset)
436440
: false;
437-
}
438-
441+
}
442+
439443
function insert($table = '', $keyValue) {
440444
$ezQuery = \getInstance();
441-
return ($ezQuery instanceOf DatabaseInterface)
442-
? $ezQuery->insert($table, $keyValue)
445+
return ($ezQuery instanceOf DatabaseInterface)
446+
? $ezQuery->insert($table, $keyValue)
443447
: false;
444-
}
445-
448+
}
449+
446450
function update($table = '', $keyValue, ...$args) {
447451
$ezQuery = \getInstance();
448-
return ($ezQuery instanceOf DatabaseInterface)
449-
? $ezQuery->update($table, $keyValue, ...$args)
452+
return ($ezQuery instanceOf DatabaseInterface)
453+
? $ezQuery->update($table, $keyValue, ...$args)
450454
: false;
451-
}
452-
455+
}
456+
453457
function deleting($table = '', ...$args) {
454458
$ezQuery = \getInstance();
455-
return ($ezQuery instanceOf DatabaseInterface)
456-
? $ezQuery->delete($table, ...$args)
459+
return ($ezQuery instanceOf DatabaseInterface)
460+
? $ezQuery->delete($table, ...$args)
457461
: false;
458-
}
459-
462+
}
463+
460464
function replace($table = '', $keyValue) {
461465
$ezQuery = \getInstance();
462-
return ($ezQuery instanceOf DatabaseInterface)
463-
? $ezQuery->replace($table, $keyValue)
466+
return ($ezQuery instanceOf DatabaseInterface)
467+
? $ezQuery->replace($table, $keyValue)
464468
: false;
465-
}
469+
}
466470

467471
function ezFunctions() {
468472
return true;

lib/ezQuery.php

Lines changed: 18 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -186,37 +186,41 @@ public function having(...$having)
186186
public function innerJoin(
187187
string $leftTable = null,
188188
string $rightTable = null,
189-
string $leftColumn = null, string $rightColumn = null, $condition = \EQ)
189+
string $leftColumn = null, string $rightColumn = null,
190+
string $tableAs = null, $condition = \EQ)
190191
{
191192
return $this->joining(
192-
'INNER', $leftTable, $rightTable, $leftColumn, $rightColumn, $condition);
193+
'INNER', $leftTable, $rightTable, $leftColumn, $rightColumn, $tableAs, $condition);
193194
}
194195

195196
public function leftJoin(
196197
string $leftTable = null,
197198
string $rightTable = null,
198-
string $leftColumn = null, string $rightColumn = null, $condition = \EQ)
199+
string $leftColumn = null, string $rightColumn = null,
200+
string $tableAs = null, $condition = \EQ)
199201
{
200202
return $this->joining(
201-
'LEFT', $leftTable, $rightTable, $leftColumn, $rightColumn, $condition);
203+
'LEFT', $leftTable, $rightTable, $leftColumn, $rightColumn, $tableAs, $condition);
202204
}
203205

204206
public function rightJoin(
205207
string $leftTable = null,
206208
string $rightTable = null,
207-
string $leftColumn = null, string $rightColumn = null, $condition = \EQ)
209+
string $leftColumn = null, string $rightColumn = null,
210+
string $tableAs = null, $condition = \EQ)
208211
{
209212
return $this->joining(
210-
'RIGHT', $leftTable, $rightTable, $leftColumn, $rightColumn, $condition);
213+
'RIGHT', $leftTable, $rightTable, $leftColumn, $rightColumn, $tableAs, $condition);
211214
}
212215

213216
public function fullJoin(
214217
string $leftTable = null,
215218
string $rightTable = null,
216-
string $leftColumn = null, string $rightColumn = null, $condition = \EQ)
219+
string $leftColumn = null, string $rightColumn = null,
220+
string $tableAs = null, $condition = \EQ)
217221
{
218222
return $this->joining(
219-
'FULL', $leftTable, $rightTable, $$leftColumn, $rightColumn, $condition);
223+
'FULL', $leftTable, $rightTable, $$leftColumn, $rightColumn, $tableAs, $condition);
220224
}
221225

222226
/**
@@ -248,7 +252,8 @@ private function joining(
248252
String $type = \_INNER,
249253
string $leftTable = null,
250254
string $rightTable = null,
251-
string $leftColumn = null, string $rightColumn = null, $condition = \EQ)
255+
string $leftColumn = null, string $rightColumn = null,
256+
string $tableAs = null, $condition = \EQ)
252257
{
253258
if (!\in_array($type, \_JOINERS)
254259
|| !\in_array($condition, \_BOOLEAN)
@@ -258,14 +263,17 @@ private function joining(
258263
return false;
259264
}
260265

266+
if (empty($tableAs))
267+
$tableAs = $rightTable;
268+
261269
if (\is_string($leftColumn) && empty($rightColumn))
262270
$onCondition = ' ON '.$leftTable.'.'.$leftColumn.' = '.$rightTable.'.'.$leftColumn;
263271
elseif ($condition !== \EQ)
264272
$onCondition = ' ON '.$leftTable.'.'.$leftColumn." $condition ".$rightTable.'.'.$rightColumn;
265273
else
266274
$onCondition = ' ON '.$leftTable.'.'.$leftColumn.' = '.$rightTable.'.'.$rightColumn;
267275

268-
return ' '.$type.' JOIN '.$rightTable.$onCondition;
276+
return ' '.$type.' JOIN '.$rightTable.' AS '.$tableAs.' '.$onCondition;
269277
}
270278

271279
public function orderBy($orderBy, $order)

0 commit comments

Comments
 (0)