1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* PotatoORM manages the persistence of database CRUD operations. |
4
|
|
|
* |
5
|
|
|
* @package Ibonly\PotatoORM\Model |
6
|
|
|
* @author Ibraheem ADENIYI <[email protected]> |
7
|
|
|
* @license MIT <https://opensource.org/licenses/MIT> |
8
|
|
|
*/ |
9
|
|
|
|
10
|
|
|
namespace Ibonly\PotatoORM; |
11
|
|
|
|
12
|
|
|
use Ibonly\PotatoORM\DatabaseQuery; |
13
|
|
|
use Ibonly\PotatoORM\RelationshipsInterface; |
14
|
|
|
|
15
|
|
|
class Relationships extends DatabaseQuery implements RelationshipsInterface |
16
|
|
|
{ |
17
|
|
|
public function joinClause($con = null) |
18
|
|
|
{ |
19
|
|
|
$connection = self::checkConnection($con); |
20
|
|
|
|
21
|
|
|
$data = self::query('SELECT COLUMN_NAME, REFERENCED_TABLE_NAME, REFERENCED_COLUMN_NAME FROM INFORMATION_SCHEMA.KEY_COLUMN_USAGE WHERE TABLE_NAME="'.self::getTableName($connection).'"')->all(); |
22
|
|
|
|
23
|
|
|
array_shift($data); |
24
|
|
|
$output = ""; |
25
|
|
|
$i = 0; |
26
|
|
|
|
27
|
|
|
if ($data[0]->REFERENCED_TABLE_NAME !== null) { |
28
|
|
|
$arraySize = count($data); |
|
|
|
|
29
|
|
|
|
30
|
|
|
foreach($data as $key => $value) { |
31
|
|
|
$output .= ' JOIN '.$value->REFERENCED_TABLE_NAME; |
32
|
|
|
} |
33
|
|
|
foreach($data as $key => $value) { |
34
|
|
|
$i++; |
35
|
|
|
$whereAnd = $i > 1 ? 'AND' : 'WHERE'; |
36
|
|
|
$output .= ' '.$whereAnd.' '.self::getTableName($connection).'.'.$value->COLUMN_NAME.'='.$value->REFERENCED_TABLE_NAME.'.'.$value->REFERENCED_COLUMN_NAME.' '; |
37
|
|
|
} |
38
|
|
|
} else { |
39
|
|
|
$output = false; |
40
|
|
|
} |
41
|
|
|
return $output; |
42
|
|
|
} |
43
|
|
|
|
44
|
|
|
public function whereClause($data = null, $condition = null, $con = null) |
45
|
|
|
{ |
46
|
|
|
$joinClause = self::joinClause(); |
47
|
|
|
$connection = self::checkConnection($con); |
48
|
|
|
$tableName = self::getTableName($connection); |
49
|
|
|
$columnName = self::whereAndClause($tableName, $data, $condition); |
50
|
|
|
|
51
|
|
|
$query = 'SELECT * FROM '.$tableName; |
52
|
|
|
|
53
|
|
|
if (! $joinClause) { |
|
|
|
|
54
|
|
|
$query .= ' WHERE '.$columnName; |
55
|
|
|
} else { |
56
|
|
|
$query .= ($data === null) ? $joinClause : $joinClause .' AND '.$columnName; |
57
|
|
|
} |
58
|
|
|
return $query; |
59
|
|
|
} |
60
|
|
|
} |
This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.
Both the
$myVar
assignment in line 1 and the$higher
assignment in line 2 are dead. The first because$myVar
is never used and the second because$higher
is always overwritten for every possible time line.