-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathCreate_Mptt_Sql.php
357 lines (308 loc) · 13.3 KB
/
Create_Mptt_Sql.php
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
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
<?php
/**
* Mptt
*
* @package
* @version 0.1
* @copyright
* @author Nobuhiko
* @license
*/
class Mptt {
public function __construct() {
$this->properties = array(
'table_name' => '',
'id' => '',
'name' => '',
'left' => '',
'right' => '',
'level' => '',
);
}
/**
* �롼�Ȥ�id���֤�
*
* @access public
* @return void
*/
public function getRootId() {
return "
SELECT
" . $this->properties['id'] . "
FROM
" . $this->properties['table_name'] . "
WHERE
" . $this->properties['left'] . " = ?
";
}
/**
* Tree��¤����������
*
* @param mixed $direct_children_only
* @access public
* @return string
*/
public function getTree($direct_children_only = false) {
$sql = "
SELECT
Child.*
FROM
" . $this->properties['table_name'] . " AS Parent
INNER JOIN
" . $this->properties['table_name'] . " AS Child
ON
Child." . $this->properties['left'] . " > Parent." . $this->properties['left'] . "
AND Child." . $this->properties['right'] . " < Parent." . $this->properties['right'] . "
";
// �Ҷ��Τ�������������
if ($direct_children_only === true) {
$sql .= "
AND Child." . $this->properties['level'] . " = Parent." . $this->properties['level'] . " + 1
";
}
$sql .= "
WHERE
Parent." . $this->properties['id'] . " = ?
ORDER BY Child." . $this->properties['left'] . "
";
return $sql;
}
/**
* �ѥ��κ����ʲ��������ɤ�������
*
* @param mixed $direct_parent_only ľ�ܤοƤΤ����������� true
* @param mixed $exclusion_id ��������id
* @access public
* @return string
*/
public function getPath($direct_parent_only = false, $exclusion_id = NULL) {
$sql = "
SELECT
Parent.*
FROM
" . $this->properties['table_name'] . " AS Child
INNER JOIN
" . $this->properties['table_name'] . " AS Parent
ON
Parent." . $this->properties['left'] . " < Child." . $this->properties['left'] . "
AND Parent." . $this->properties['right'] . " > Child." . $this->properties['right'] . "
";
// ��ʬ�οƤ�ɬ��1���ؾ�
if ($direct_parent_only === true) {
$sql .= "
AND Parent." . $this->properties['level'] . " = Child." . $this->properties['level'] . " - 1
";
}
$sql .= "
WHERE
Child." . $this->properties['id'] . " = ?";
if ($exclusion_id) {
$sql .= "
AND Parent." . $this->properties['id'] . " <> " . $exclusion_id;
}
$sql .= "
ORDER BY Parent." . $this->properties['left'] . "
";
return $sql;
}
/**
* selectbox���ǻȤ��䤹���褦�� id => name �Υꥹ�Ȥ���������
*
* @param string $separator level�β������ѥ졼��������������
* @param mixed $exclusion_id ��������id
* @access public
* @return string
*/
function getSelectedList($separator = ' �� ', $exclusion_id = NULL) {
$sql = "
SELECT
Child." . $this->properties['id'] . "
,CONCAT(REPEAT( '". $separator ."', Child." . $this->properties['level'] . " - 1), Child." . $this->properties['name'] . ") AS " . $this->properties['name'] . "
FROM
" . $this->properties['table_name'] . " AS Parent
INNER JOIN
" . $this->properties['table_name'] . " AS Child
ON
Child." . $this->properties['left'] . " >= Parent." . $this->properties['left'] . "
AND Child." . $this->properties['right'] . " <= Parent." . $this->properties['right'] . "
WHERE
Parent." . $this->properties['id'] . " = ?
";
if ($exclusion_id) {
$sql .= "
AND Child." . $this->properties['id'] . " <> " . $exclusion_id;
}
$sql .= "
ORDER BY Child." . $this->properties['left'];
return $sql;
}
/**
* �դΥǡ�������������
* (�Ƥ�id�����˼��ä��ۤ�����Ψ���ɤ�)
*
* @access public
* @return string
*/
public function getLeaf() {
$sql = "
SELECT
" . $this->properties['id'] . "
," . $this->properties['name'] . "
," . $this->properties['left'] . "
," . $this->properties['right'] . "
," . $this->properties['level'] . "
FROM
" . $this->properties['table_name'] . "
WHERE
" . $this->properties['table_name'] . "." . $this->properties['id'] . " = ?
";
return $sql;
}
/**
* �Ƥ����˻Ҥ��ɲä���(�٤˾�����������)
*
* @param mixed $rgt �Ƥ�right
* @access private
* @return string
*/
private function blankAsLastChildOf($rgt) {
// �Ƥ����ʤ�������SQL
return "
UPDATE " . $this->properties['table_name'] . "
SET " . $this->properties['left'] . " = CASE WHEN " . $this->properties['left'] . " > $rgt
THEN " . $this->properties['left'] . " + 2
ELSE " . $this->properties['left'] . " END
," . $this->properties['right'] . " = CASE WHEN " . $this->properties['right'] . " >= $rgt
THEN " . $this->properties['right'] . " + 2
ELSE " . $this->properties['right'] . " END
WHERE
" . $this->properties['right'] . " >= $rgt
";
}
/**
* ���ꤷ��id��������������(�٤˾�����������)
*
* @param mixed $left
* @param mixed $right
* @access private
* @return string
*/
private function blankAsPrevChildOf($left, $right) {
return "
UPDATE " . $this->properties['table_name'] . "
SET " . $this->properties['left'] . " = CASE WHEN " . $this->properties['left'] . " >= $left
THEN " . $this->properties['left'] . " + 2
ELSE " . $this->properties['left'] . " END
," . $this->properties['right'] . " = CASE WHEN " . $this->properties['right'] . " >= $left
THEN " . $this->properties['right'] . " + 2
ELSE " . $this->properties['right'] . " END
WHERE
" . $this->properties['right'] . " >= $right
";
}
/**
* ���ꤷ�����롼�פ������ؤ���
*
* @param mixed $left_1
* @param mixed $right_1
* @param mixed $left_2
* @param mixed $right_2
* @access private
* @return string
*/
private function replaceLeaf($left_1, $right_1, $left_2, $right_2) {
return "
UPDATE " . $this->properties['table_name'] . " SET
" . $this->properties['left'] . " = CASE WHEN " . $this->properties['left'] . " BETWEEN $left_1 AND $right_1 THEN $right_2 + " . $this->properties['left'] . " - $right_1 WHEN " . $this->properties['left'] . " BETWEEN $left_2 AND $right_2 THEN $left_1 + " . $this->properties['left'] . " - $left_2 WHEN " . $this->properties['left'] . " BETWEEN $right_1 + 1 AND $left_2 - 1 THEN $left_1 + $right_2 + " . $this->properties['left'] . " - $right_1 - $left_2 ELSE " . $this->properties['left'] . " END,
" . $this->properties['right'] . " = CASE WHEN " . $this->properties['right'] . " BETWEEN $left_1 AND $right_1 THEN $right_2 + " . $this->properties['right'] . " - $right_1 WHEN " . $this->properties['right'] . " BETWEEN $left_2 AND $right_2 THEN $left_1 + " . $this->properties['right'] . " - $left_2 WHEN " . $this->properties['right'] . " BETWEEN $right_1 + 1 AND $left_2 - 1 THEN $left_1 + $right_2 + " . $this->properties['right'] . " - $right_1 - $left_2 ELSE " . $this->properties['right'] . " END
WHERE
" . $this->properties['left'] . " BETWEEN $left_1 AND $right_2
AND $left_1 < $right_1
AND $right_1 < $left_2
AND $left_2 < $right_2
";
}
/**
* �Ƥ����ذ�ư����
*
* @param mixed $group_id �Ƥ�id
* @param mixed $level �Ƥ�level
* @access public
* @return string
*/
public function moveAsLastChildOf($group_id, $level) {
return "
UPDATE
" . $this->properties['table_name'] . " Object
INNER JOIN " . $this->properties['table_name'] . " Shift
ON Shift." . $this->properties['left'] . " >= Object." . $this->properties['left'] . "
AND Shift." . $this->properties['right'] . " <= Object." . $this->properties['right'] . "
INNER JOIN " . $this->properties['table_name'] . " Target
ON Target." . $this->properties['id'] . " = $group_id
SET
Shift." . $this->properties['left'] . " = Shift." . $this->properties['left'] . " + (Target." . $this->properties['right'] . " - 1 - Object." . $this->properties['right'] . ")
,Shift." . $this->properties['right'] . " = Shift." . $this->properties['right'] . " + (Target." . $this->properties['right'] . " - 1 - Object." . $this->properties['right'] . ")
,Shift." . $this->properties['level'] . " = Shift." . $this->properties['level'] . " + ($level + 1 - Object." . $this->properties['level'] . ")
WHERE
Object." . $this->properties['id'] . " = ?
";
}
/**
* ���ꤷ�����������˰�ư����
*
* @param mixed $group_id ������id
* @param mixed $level ������level
* @access public
* @return string
*/
public function moveAsPrevChildOf($group_id, $level) {
return "
UPDATE
" . $this->properties['table_name'] . " Object
INNER JOIN " . $this->properties['table_name'] . " Shift
ON Shift." . $this->properties['left'] . " >= Object." . $this->properties['left'] . "
AND Shift." . $this->properties['right'] . " <= Object." . $this->properties['right'] . "
INNER JOIN " . $this->properties['table_name'] . " Target
ON Target." . $this->properties['id'] . " = $group_id
SET
Shift." . $this->properties['left'] . " = Shift." . $this->properties['left'] . " + (Target." . $this->properties['left'] . " - 1 - Object." . $this->properties['right'] . ")
,Shift." . $this->properties['right'] . " = Shift." . $this->properties['right'] . " + (Target." . $this->properties['left'] . " - 1 - Object." . $this->properties['right'] . ")
,Shift." . $this->properties['level'] . " = Shift." . $this->properties['level'] . " + ($level + 1 - Object." . $this->properties['level'] . ")
WHERE
Object." . $this->properties['id'] . " = '" . $data['group_id'] . "'
";
}
/**
* �Ҷ����ޤ��ƺ�������
*
* @param mixed $left ���������оݤ�left
* @param mixed $right ���������оݤ�right
* @access public
* @return array
*/
public function delete($left, $right) {
$deletSql = "
DELETE FROM
" . $this->properties['table_name'] . "
WHERE
" . $this->properties['left'] . " >= " . $lft . " AND " . $this->properties['right'] . " <= " . $rgt
;
// ���֤餹��������
$target_rl_difference = "$right - $left + 1";
$leftSql = "
UPDATE
" . $this->properties['table_name'] . "
SET
" . $this->properties['left'] . " = " . $this->properties['left'] . " - " . $target_rl_difference . "
WHERE
" . $this->properties['left'] . " > " . $left;
$rightSql = "
UPDATE
" . $this->properties['table_name'] . "
SET
" . $this->properties['right'] . " = " . $this->properties['right'] . " - " . $target_rl_difference . "
WHERE
" . $this->properties['right'] . " > " . $left;
return array($deletSql, $leftSql, $rightSql);
}
}