-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathon_clause.class.php
More file actions
155 lines (122 loc) · 5.49 KB
/
on_clause.class.php
File metadata and controls
155 lines (122 loc) · 5.49 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
<?php
namespace PluSQL;
use Exception,mysqli;
class OnClause
{
private $left;
private $right;
private $link;
private $join_type;
public function __construct($link,$left,$right,$join_type = Table::INNER_JOIN)
{
$this->left = TableInspector::forTable($left,$link);
$this->right = TableInspector::forTable($right,$link);
$this->link = $link;
$this->join_type = $join_type;
}
public function toString()
{
$left_fields = $this->left->allFields();
$right_fields = $this->right->allFields();
$this->mapFields($left_map,$left_names,$left_fields);
$this->mapFields($right_map,$right_names,$right_fields);
$intersection = array_intersect($left_names,$right_names);
//NOW TEST IF THE COMPLETE KEY OF EITHER EXISTS IN THE INTERSECTION OF FIELDS
//IF THE COMPLETE KEY OF EITHER IS PRESENT IN IT'S ENTIRETY IN THE
//INTERSECTION, THEN WE USE THAT TO JOIN
//IF THE PRIMARY KEY OF ONE IS LONGER THAN THE OTHER, TEST THAT FIRST
$left_keys = $this->left->primaryKeys();
$right_keys = $this->right->primaryKeys();
if(count($left_keys) > count($right_keys))
{
$first = $left_keys;
$second = $right_keys;
}
else
{
$second = $left_keys;
$first = $right_keys;
}
$remainder = array_diff($first,$intersection);
$use = NULL;
if(count($remainder))
{
$remainder = array_diff($second,$intersection);
if(!count($remainder))
$use = $second;
}
else
$use = $first;
if(!$use)
{
$tables = array();
//OKAY THESE SUCKERS AREN'T RELATED - LET'S FIND OUT IF THERE'S A TABLE ANYWHERE CAPABLE OF JOINING THEM
if($this->link instanceof mysqli)
{
$res = $this->link->query('SHOW TABLES');
while($row = $res->fetch_assoc())
$tables[] = TableInspector::forTable(current($row),$this->link);
}
else
{
$res = mysql_query('SHOW TABLES',$this->link);
while($row = mysql_fetch_assoc($res))
$tables[] = TableInspector::forTable(current($row),$this->link);
}
$left_right = implode(',',$left_keys).','.implode(',',$right_keys);
$right_left = implode(',',$right_keys).','.implode(',',$left_keys);
$relationships = array();
foreach($tables as $t)
{
$test = implode(',',$t->primaryKeys());
if($test == $left_right)
$relationships[] = $t;
else if($test == $right_left)
$relationships[] = $t;
}
//IF WE CAN'T FIND ONE, THROW AN EXCEPTION
if(!count($relationships))
throw new UnableToDetermineOnClauseException('I could not automatically determine which fields to use when joining '.$this->left->name().' and '.$this->right->name().', and I could not find any suitable joining tables');
//IF WE FIND MORE THAN ONE, THROW AN EXCEPTION
if(count($relationships) > 1)
throw new UnableToDetermineOnClauseException('I could not automatically determine which fields to use when joining '.$this->left->name().' and '.$this->right->name().', and then I found more than one suitable joining table');
throw new ManyToManyJoinException(current($relationships));
}
$clauses = array();
foreach($use as $field)
$clauses[] = $this->left->name().'.'.$field.' = '.$this->right->name().'.'.$field;
$ret = implode(' AND ',$clauses);
if($this->join_type == Table::LEFT_JOIN)
{
$nulls = array();
foreach($use as $field)
$nulls[] = $this->right->name().'.'.$field.' IS NULL';
$ret = '('.$ret.' OR ('.implode(' AND ',$nulls).'))';
}
return $ret;
}
private function mapFields(&$map,&$names,$fields)
{
$map = array();
$names = array();
foreach($fields as $f)
{
$map[$f['Field']] = $f['Type'];
$names[] = $f['Field'];
}
}
}
class UnableToDetermineOnClauseException extends Exception{}
class ManyToManyJoinException extends Exception
{
private $joining_table;
public function __construct(TableInspectorWorker $joining_table)
{
parent::__construct('We have a many to many join');
$this->joining_table = $joining_table;
}
public function joiningTable()
{
return $this->joining_table;
}
}