|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Ubiquity\orm\parser; |
|
4
|
|
|
|
|
5
|
|
|
use Ubiquity\orm\OrmUtils; |
|
6
|
|
|
|
|
7
|
|
|
/** |
|
8
|
|
|
* ManyToManyParser |
|
9
|
|
|
* |
|
10
|
|
|
* @author jc |
|
11
|
|
|
* @version 1.1.3 |
|
12
|
|
|
*/ |
|
13
|
|
|
class ManyToManyParser { |
|
14
|
|
|
private $table; |
|
15
|
|
|
private $member; |
|
16
|
|
|
private $joinTable; |
|
17
|
|
|
private $myFkField; |
|
18
|
|
|
private $fkField; |
|
19
|
|
|
private $targetEntity; |
|
20
|
|
|
private $targetEntityClass; |
|
21
|
|
|
private $targetEntityTable; |
|
22
|
|
|
private $myPk; |
|
23
|
|
|
private $inversedBy; |
|
24
|
|
|
private $pk; |
|
25
|
|
|
private $instance; |
|
26
|
|
|
private $whereValues; |
|
27
|
|
|
|
|
28
|
|
|
/** |
|
29
|
|
|
* |
|
30
|
|
|
* @var \Ubiquity\db\Database |
|
31
|
|
|
*/ |
|
32
|
|
|
private $db; |
|
33
|
|
|
|
|
34
|
26 |
|
public function __construct($db, $instance, $member = null) { |
|
35
|
26 |
|
$this->instance = $instance; |
|
36
|
26 |
|
$this->member = $member; |
|
37
|
26 |
|
$this->whereValues = [ ]; |
|
38
|
26 |
|
$this->db = $db; |
|
39
|
26 |
|
} |
|
40
|
|
|
|
|
41
|
26 |
|
public function init($annot = false) { |
|
42
|
26 |
|
$member = $this->member; |
|
43
|
26 |
|
$class = $this->instance; |
|
44
|
26 |
|
if (\is_object ( $class )) { |
|
45
|
23 |
|
$class = get_class ( $class ); |
|
46
|
|
|
} |
|
47
|
26 |
|
if ($annot === false) { |
|
48
|
4 |
|
$annot = OrmUtils::getAnnotationInfoMember ( $class, "#manyToMany", $member ); |
|
49
|
|
|
} |
|
50
|
26 |
|
if ($annot !== false) { |
|
51
|
26 |
|
$this->_init ( $class, $annot ); |
|
52
|
26 |
|
return true; |
|
53
|
|
|
} |
|
54
|
|
|
return false; |
|
55
|
|
|
} |
|
56
|
|
|
|
|
57
|
26 |
|
private function _init($class, $annot) { |
|
58
|
26 |
|
$this->table = OrmUtils::getTableName ( $class ); |
|
59
|
26 |
|
$this->targetEntity = $annot ["targetEntity"]; |
|
60
|
26 |
|
$this->inversedBy = lcfirst ( $this->targetEntity ) . "s"; |
|
61
|
26 |
|
if (! is_null ( $annot ["inversedBy"] )) |
|
62
|
26 |
|
$this->inversedBy = $annot ["inversedBy"]; |
|
63
|
26 |
|
$this->targetEntityClass = get_class ( new $this->targetEntity () ); |
|
64
|
|
|
|
|
65
|
26 |
|
$annotJoinTable = OrmUtils::getAnnotationInfoMember ( $class, "#joinTable", $this->member ); |
|
66
|
26 |
|
$this->joinTable = $annotJoinTable ["name"]; |
|
67
|
|
|
|
|
68
|
26 |
|
$this->myFkField = OrmUtils::getDefaultFk ( $class ); |
|
69
|
26 |
|
$this->myPk = OrmUtils::getFirstKey ( $class ); |
|
70
|
26 |
|
if (isset ( $annotJoinTable ["joinColumns"] )) { |
|
71
|
|
|
$joinColumnsAnnot = $annotJoinTable ["joinColumns"]; |
|
72
|
|
|
if (! is_null ( $joinColumnsAnnot )) { |
|
73
|
|
|
$this->myFkField = $joinColumnsAnnot ["name"]; |
|
74
|
|
|
$this->myPk = $joinColumnsAnnot ["referencedColumnName"]; |
|
75
|
|
|
} |
|
76
|
|
|
} |
|
77
|
26 |
|
$this->targetEntityTable = OrmUtils::getTableName ( $this->targetEntity ); |
|
78
|
26 |
|
$this->fkField = OrmUtils::getDefaultFk ( $this->targetEntityClass ); |
|
79
|
26 |
|
$this->pk = OrmUtils::getFirstKey ( $this->targetEntityClass ); |
|
80
|
26 |
|
if (isset ( $annotJoinTable ["inverseJoinColumns"] )) { |
|
81
|
|
|
$inverseJoinColumnsAnnot = $annotJoinTable ["inverseJoinColumns"]; |
|
82
|
|
|
if (! is_null ( $inverseJoinColumnsAnnot )) { |
|
83
|
|
|
$this->fkField = $inverseJoinColumnsAnnot ["name"]; |
|
84
|
|
|
$this->pk = $inverseJoinColumnsAnnot ["referencedColumnName"]; |
|
85
|
|
|
} |
|
86
|
|
|
} |
|
87
|
26 |
|
} |
|
88
|
|
|
|
|
89
|
|
|
public function getMember() { |
|
90
|
|
|
return $this->member; |
|
91
|
|
|
} |
|
92
|
|
|
|
|
93
|
|
|
public function setMember($member) { |
|
94
|
|
|
$this->member = $member; |
|
95
|
|
|
return $this; |
|
96
|
|
|
} |
|
97
|
|
|
|
|
98
|
4 |
|
public function getJoinTable() { |
|
99
|
4 |
|
return $this->joinTable; |
|
100
|
|
|
} |
|
101
|
|
|
|
|
102
|
|
|
public function setJoinTable($joinTable) { |
|
103
|
|
|
$this->joinTable = $joinTable; |
|
104
|
|
|
return $this; |
|
105
|
|
|
} |
|
106
|
|
|
|
|
107
|
4 |
|
public function getMyFkField() { |
|
108
|
4 |
|
return $this->myFkField; |
|
109
|
|
|
} |
|
110
|
|
|
|
|
111
|
|
|
public function setMyFkField($myFkField) { |
|
112
|
|
|
$this->myFkField = $myFkField; |
|
113
|
|
|
return $this; |
|
114
|
|
|
} |
|
115
|
|
|
|
|
116
|
4 |
|
public function getFkField() { |
|
117
|
4 |
|
return $this->fkField; |
|
118
|
|
|
} |
|
119
|
|
|
|
|
120
|
|
|
public function setFkField($fkField) { |
|
121
|
|
|
$this->fkField = $fkField; |
|
122
|
|
|
return $this; |
|
123
|
|
|
} |
|
124
|
|
|
|
|
125
|
|
|
public function getTargetEntity() { |
|
126
|
|
|
return $this->targetEntity; |
|
127
|
|
|
} |
|
128
|
|
|
|
|
129
|
|
|
public function setTargetEntity($targetEntity) { |
|
130
|
|
|
$this->targetEntity = $targetEntity; |
|
131
|
|
|
return $this; |
|
132
|
|
|
} |
|
133
|
|
|
|
|
134
|
26 |
|
public function getTargetEntityClass() { |
|
135
|
26 |
|
return $this->targetEntityClass; |
|
136
|
|
|
} |
|
137
|
|
|
|
|
138
|
|
|
public function setTargetEntityClass($targetEntityClass) { |
|
139
|
|
|
$this->targetEntityClass = $targetEntityClass; |
|
140
|
|
|
return $this; |
|
141
|
|
|
} |
|
142
|
|
|
|
|
143
|
26 |
|
public function getTargetEntityTable() { |
|
144
|
26 |
|
return $this->targetEntityTable; |
|
145
|
|
|
} |
|
146
|
|
|
|
|
147
|
|
|
public function setTargetEntityTable($targetEntityTable) { |
|
148
|
|
|
$this->targetEntityTable = $targetEntityTable; |
|
149
|
|
|
return $this; |
|
150
|
|
|
} |
|
151
|
|
|
|
|
152
|
23 |
|
public function getMyPk() { |
|
153
|
23 |
|
return $this->myPk; |
|
154
|
|
|
} |
|
155
|
|
|
|
|
156
|
|
|
public function setMyPk($myPk) { |
|
157
|
|
|
$this->myPk = $myPk; |
|
158
|
|
|
return $this; |
|
159
|
|
|
} |
|
160
|
|
|
|
|
161
|
26 |
|
public function getPk() { |
|
162
|
26 |
|
return $this->pk; |
|
163
|
|
|
} |
|
164
|
|
|
|
|
165
|
|
|
public function setPk($pk) { |
|
166
|
|
|
$this->pk = $pk; |
|
167
|
|
|
return $this; |
|
168
|
|
|
} |
|
169
|
|
|
|
|
170
|
|
|
public function getInversedBy() { |
|
171
|
|
|
return $this->inversedBy; |
|
172
|
|
|
} |
|
173
|
|
|
|
|
174
|
|
|
public function setInversedBy($inversedBy) { |
|
175
|
|
|
$this->inversedBy = $inversedBy; |
|
176
|
|
|
return $this; |
|
177
|
|
|
} |
|
178
|
|
|
|
|
179
|
|
|
public function getInstance() { |
|
180
|
|
|
return $this->instance; |
|
181
|
|
|
} |
|
182
|
|
|
|
|
183
|
|
|
public function setInstance($instance) { |
|
184
|
|
|
$this->instance = $instance; |
|
185
|
|
|
return $this; |
|
186
|
|
|
} |
|
187
|
|
|
|
|
188
|
3 |
|
public function getSQL($alias = "", $aliases = null) { |
|
189
|
3 |
|
$quote = $this->db->quote; |
|
190
|
3 |
|
if ($alias !== "") { |
|
191
|
3 |
|
$targetEntityTable = $alias; |
|
192
|
3 |
|
$alias = $quote . $alias . $quote; |
|
193
|
|
|
} else { |
|
194
|
|
|
$targetEntityTable = $this->targetEntityTable; |
|
195
|
|
|
} |
|
196
|
3 |
|
$jtAlias = uniqid ( $this->joinTable ); |
|
197
|
3 |
|
$table = $this->table; |
|
198
|
3 |
|
if (is_array ( $aliases )) { |
|
199
|
3 |
|
if (isset ( $aliases [$this->table] )) |
|
200
|
|
|
$table = $aliases [$this->table]; |
|
201
|
|
|
} |
|
202
|
3 |
|
return " INNER JOIN {$quote}" . $this->joinTable . "{$quote} {$quote}{$jtAlias}{$quote} on {$quote}" . $jtAlias . "{$quote}.{$quote}" . $this->myFkField . "{$quote}={$quote}" . $table . "{$quote}.{$quote}" . $this->myPk . $quote . " INNER JOIN {$quote}" . $this->targetEntityTable . "{$quote} {$alias} on {$quote}" . $jtAlias . "{$quote}.{$quote}" . $this->fkField . "{$quote}={$quote}" . $targetEntityTable . "{$quote}.{$quote}" . $this->pk . $quote; |
|
203
|
|
|
} |
|
204
|
|
|
|
|
205
|
23 |
|
public function getConcatSQL() { |
|
206
|
23 |
|
$quote = $this->db->quote; |
|
207
|
23 |
|
$concat = $this->db->getSpecificSQL ( 'groupconcat', [ $quote . $this->fkField . $quote,',' ] ); |
|
208
|
23 |
|
return "SELECT {$quote}" . $this->myFkField . "{$quote} as _field ,{$concat} as _concat FROM {$quote}" . $this->joinTable . "{$quote} {condition} GROUP BY 1"; |
|
209
|
|
|
} |
|
210
|
|
|
|
|
211
|
23 |
|
public function getParserWhereMask($mask = "'{value}'") { |
|
212
|
23 |
|
$quote = $this->db->quote; |
|
213
|
23 |
|
$toString = $this->db->getSpecificSQL ( 'tostring' ); |
|
214
|
23 |
|
return $quote . $this->getTargetEntityTable () . "{$quote}.{$quote}" . $this->getPk () . "{$quote}{$toString}=" . $mask; |
|
215
|
|
|
} |
|
216
|
|
|
|
|
217
|
13 |
|
private function getParserConcatWhereMask($mask = "'{value}'") { |
|
218
|
13 |
|
$quote = $this->db->quote; |
|
219
|
13 |
|
return $quote . $this->myFkField . "{$quote}=" . $mask; |
|
220
|
|
|
} |
|
221
|
|
|
|
|
222
|
13 |
|
private function getParserConcatWhereInMask($mask = "'{values}'") { |
|
223
|
13 |
|
$quote = $this->db->quote; |
|
224
|
13 |
|
return " INNER JOIN (" . $mask . ") as _tmp ON {$quote}" . $this->myFkField . "{$quote}=_tmp._id"; |
|
225
|
|
|
} |
|
226
|
|
|
|
|
227
|
23 |
|
public function generateConcatSQL() { |
|
228
|
23 |
|
$sql = $this->getConcatSQL (); |
|
229
|
23 |
|
$where = ""; |
|
230
|
23 |
|
if (($size = sizeof ( $this->whereValues )) > 0) { |
|
231
|
23 |
|
if ($size > 3) { |
|
232
|
13 |
|
$res = array_fill ( 0, $size, "?" ); |
|
233
|
13 |
|
$res [0] = "SELECT ? as _id"; |
|
234
|
13 |
|
$where = $this->getParserConcatWhereInMask ( implode ( " UNION ALL SELECT ", $res ) ); |
|
235
|
|
|
} else { |
|
236
|
13 |
|
$mask = $this->getParserConcatWhereMask ( " ?" ); |
|
237
|
13 |
|
$res = array_fill ( 0, $size, $mask ); |
|
238
|
13 |
|
$where = "WHERE " . implode ( " OR ", $res ); |
|
239
|
|
|
} |
|
240
|
|
|
} |
|
241
|
23 |
|
return str_replace ( "{condition}", $where, $sql ); |
|
242
|
|
|
} |
|
243
|
|
|
|
|
244
|
23 |
|
public function addValue($value) { |
|
245
|
23 |
|
$this->whereValues [$value] = true; |
|
246
|
23 |
|
} |
|
247
|
|
|
|
|
248
|
|
|
/** |
|
249
|
|
|
* |
|
250
|
|
|
* @return array |
|
251
|
|
|
*/ |
|
252
|
23 |
|
public function getWhereValues() { |
|
253
|
23 |
|
return array_keys ( $this->whereValues ); |
|
254
|
|
|
} |
|
255
|
|
|
} |
|
256
|
|
|
|