forked from crunchyjohn/phppgadmin
-
Notifications
You must be signed in to change notification settings - Fork 1
/
constraints.php
640 lines (559 loc) · 22.9 KB
/
constraints.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
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
<?php
/**
* List constraints on a table
*
* $Id: constraints.php,v 1.56 2007/12/31 16:46:07 xzilla Exp $
*/
// Include application functions
include_once('./libraries/lib.inc.php');
include_once('./classes/class.select.php');
$action = (isset($_REQUEST['action'])) ? $_REQUEST['action'] : '';
/**
* Confirm and then actually add a FOREIGN KEY constraint
*/
function addForeignKey($stage, $msg = '') {
global $data, $misc;
global $lang;
if (!isset($_POST['name'])) $_POST['name'] = '';
if (!isset($_POST['target'])) $_POST['target'] = '';
switch ($stage) {
case 2:
// Check that they've given at least one source column
if (!isset($_REQUEST['SourceColumnList']) && (!isset($_POST['IndexColumnList']) || !is_array($_POST['IndexColumnList']) || sizeof($_POST['IndexColumnList']) == 0))
addForeignKey(1, $lang['strfkneedscols']);
else {
// Copy the IndexColumnList variable from stage 1
if (isset($_REQUEST['IndexColumnList']) && !isset($_REQUEST['SourceColumnList']))
$_REQUEST['SourceColumnList'] = serialize($_REQUEST['IndexColumnList']);
// Initialise variables
if (!isset($_POST['upd_action'])) $_POST['upd_action'] = null;
if (!isset($_POST['del_action'])) $_POST['del_action'] = null;
if (!isset($_POST['match'])) $_POST['match'] = null;
if (!isset($_POST['deferrable'])) $_POST['deferrable'] = null;
if (!isset($_POST['initially'])) $_POST['initially'] = null;
$_REQUEST['target'] = unserialize($_REQUEST['target']);
$misc->printTrail('table');
$misc->printTitle($lang['straddfk'],'pg.constraint.foreign_key');
$misc->printMsg($msg);
// Unserialize target and fetch appropriate table. This is a bit messy
// because the table could be in another schema.
$data->setSchema($_REQUEST['target']['schemaname']);
$attrs = $data->getTableAttributes($_REQUEST['target']['tablename']);
$data->setSchema($_REQUEST['schema']);
$selColumns = new XHTML_select('TableColumnList', true, 10);
$selColumns->set_style('width: 15em;');
if ($attrs->recordCount() > 0) {
while (!$attrs->EOF) {
$selColumns->add(new XHTML_Option($attrs->fields['attname']));
$attrs->moveNext();
}
}
$selIndex = new XHTML_select('IndexColumnList[]', true, 10);
$selIndex->set_style('width: 15em;');
$selIndex->set_attribute('id', 'IndexColumnList');
$buttonAdd = new XHTML_Button('add', '>>');
$buttonAdd->set_attribute('onclick', 'buttonPressed(this);');
$buttonAdd->set_attribute('type', 'button');
$buttonRemove = new XHTML_Button('remove', '<<');
$buttonRemove->set_attribute('onclick', 'buttonPressed(this);');
$buttonRemove->set_attribute('type', 'button');
echo "<form onsubmit=\"doSelectAll();\" name=\"formIndex\" action=\"constraints.php\" method=\"post\">\n";
echo "<table>\n";
echo "<tr><th class=\"data\" colspan=\"3\">{$lang['strfktarget']}</th></tr>";
echo "<tr><th class=\"data\">{$lang['strtablecolumnlist']}</th><th class=\"data\"> </th><th class=data>{$lang['strfkcolumnlist']}</th></tr>\n";
echo "<tr><td class=\"data1\">" . $selColumns->fetch() . "</td>\n";
echo "<td class=\"data1\" style=\"text-align: center\">" . $buttonRemove->fetch() . $buttonAdd->fetch() . "</td>";
echo "<td class=\"data1\">" . $selIndex->fetch() . "</td></tr>\n";
echo "<tr><th class=\"data\" colspan=\"3\">{$lang['stractions']}</th></tr>";
echo "<tr>";
echo "<td class=\"data1\" colspan=\"3\">\n";
// ON SELECT actions
echo "{$lang['stronupdate']} <select name=\"upd_action\">";
foreach ($data->fkactions as $v)
echo "<option value=\"{$v}\"", ($_POST['upd_action'] == $v) ? ' selected="selected"' : '', ">{$v}</option>\n";
echo "</select><br />\n";
// ON DELETE actions
echo "{$lang['strondelete']} <select name=\"del_action\">";
foreach ($data->fkactions as $v)
echo "<option value=\"{$v}\"", ($_POST['del_action'] == $v) ? ' selected="selected"' : '', ">{$v}</option>\n";
echo "</select><br />\n";
// MATCH options
echo "<select name=\"match\">";
foreach ($data->fkmatches as $v)
echo "<option value=\"{$v}\"", ($_POST['match'] == $v) ? ' selected="selected"' : '', ">{$v}</option>\n";
echo "</select><br />\n";
// DEFERRABLE options
echo "<select name=\"deferrable\">";
foreach ($data->fkdeferrable as $v)
echo "<option value=\"{$v}\"", ($_POST['deferrable'] == $v) ? ' selected="selected"' : '', ">{$v}</option>\n";
echo "</select><br />\n";
// INITIALLY options
echo "<select name=\"initially\">";
foreach ($data->fkinitial as $v)
echo "<option value=\"{$v}\"", ($_POST['initially'] == $v) ? ' selected="selected"' : '', ">{$v}</option>\n";
echo "</select>\n";
echo "</td></tr>\n";
echo "</table>\n";
echo "<p><input type=\"hidden\" name=\"action\" value=\"save_add_foreign_key\" />\n";
echo $misc->form;
echo "<input type=\"hidden\" name=\"table\" value=\"", htmlspecialchars($_REQUEST['table']), "\" />\n";
echo "<input type=\"hidden\" name=\"name\" value=\"", htmlspecialchars($_REQUEST['name']), "\" />\n";
echo "<input type=\"hidden\" name=\"target\" value=\"", htmlspecialchars(serialize($_REQUEST['target'])), "\" />\n";
echo "<input type=\"hidden\" name=\"SourceColumnList\" value=\"", htmlspecialchars($_REQUEST['SourceColumnList']), "\" />\n";
echo "<input type=\"hidden\" name=\"stage\" value=\"3\" />\n";
echo "<input type=\"submit\" value=\"{$lang['stradd']}\" />\n";
echo "<input type=\"submit\" name=\"cancel\" value=\"{$lang['strcancel']}\" /></p>\n";
echo "</form>\n";
}
break;
case 3:
// Unserialize target
$_POST['target'] = unserialize($_POST['target']);
// Check that they've given at least one column
if (isset($_POST['SourceColumnList'])) $temp = unserialize($_POST['SourceColumnList']);
if (!isset($_POST['IndexColumnList']) || !is_array($_POST['IndexColumnList'])
|| sizeof($_POST['IndexColumnList']) == 0 || !isset($temp)
|| !is_array($temp) || sizeof($temp) == 0) addForeignKey(2, $lang['strfkneedscols']);
else {
$status = $data->addForeignKey($_POST['table'], $_POST['target']['schemaname'], $_POST['target']['tablename'],
unserialize($_POST['SourceColumnList']), $_POST['IndexColumnList'], $_POST['upd_action'], $_POST['del_action'],
$_POST['match'], $_POST['deferrable'], $_POST['initially'], $_POST['name']);
if ($status == 0)
doDefault($lang['strfkadded']);
else
addForeignKey(2, $lang['strfkaddedbad']);
}
break;
default:
$misc->printTrail('table');
$misc->printTitle($lang['straddfk'],'pg.constraint.foreign_key');
$misc->printMsg($msg);
$attrs = $data->getTableAttributes($_REQUEST['table']);
$tables = $data->getTables(true);
$selColumns = new XHTML_select('TableColumnList', true, 10);
$selColumns->set_style('width: 15em;');
if ($attrs->recordCount() > 0) {
while (!$attrs->EOF) {
$selColumns->add(new XHTML_Option($attrs->fields['attname']));
$attrs->moveNext();
}
}
$selIndex = new XHTML_select('IndexColumnList[]', true, 10);
$selIndex->set_style('width: 15em;');
$selIndex->set_attribute('id', 'IndexColumnList');
$buttonAdd = new XHTML_Button('add', '>>');
$buttonAdd->set_attribute('onclick', 'buttonPressed(this);');
$buttonAdd->set_attribute('type', 'button');
$buttonRemove = new XHTML_Button('remove', '<<');
$buttonRemove->set_attribute('onclick', 'buttonPressed(this);');
$buttonRemove->set_attribute('type', 'button');
echo "<form onsubmit=\"doSelectAll();\" name=\"formIndex\" action=\"constraints.php\" method=\"post\">\n";
echo "<table>\n";
echo "<tr><th class=\"data\" colspan=\"3\">{$lang['strname']}</th></tr>\n";
echo "<tr><td class=\"data1\" colspan=\"3\"><input type=\"text\" name=\"name\" size=\"32\" maxlength=\"{$data->_maxNameLen}\" /></td></tr>\n";
echo "<tr><th class=\"data\">{$lang['strtablecolumnlist']}</th><th class=\"data\"> </th><th class=\"data required\">{$lang['strfkcolumnlist']}</th></tr>\n";
echo "<tr><td class=\"data1\">" . $selColumns->fetch() . "</td>\n";
echo "<td class=\"data1\" style=\"text-align: center\">" . $buttonRemove->fetch() . $buttonAdd->fetch() . "</td>\n";
echo "<td class=data1>" . $selIndex->fetch() . "</td></tr>\n";
echo "<tr><th class=\"data\" colspan=\"3\">{$lang['strfktarget']}</th></tr>";
echo "<tr>";
echo "<td class=\"data1\" colspan=\"3\"><select name=\"target\">";
while (!$tables->EOF) {
$key = array('schemaname' => $tables->fields['nspname'], 'tablename' => $tables->fields['relname']);
$key = serialize($key);
echo "<option value=\"", htmlspecialchars($key), "\">";
if ($tables->fields['nspname'] != $_REQUEST['schema']) {
echo htmlspecialchars($tables->fields['nspname']), '.';
}
echo htmlspecialchars($tables->fields['relname']), "</option>\n";
$tables->moveNext();
}
echo "</select>\n";
echo "</td></tr>";
echo "</table>\n";
echo "<p><input type=\"hidden\" name=\"action\" value=\"save_add_foreign_key\" />\n";
echo $misc->form;
echo "<input type=\"hidden\" name=\"table\" value=\"", htmlspecialchars($_REQUEST['table']), "\" />\n";
echo "<input type=\"hidden\" name=\"stage\" value=\"2\" />\n";
echo "<input type=\"submit\" value=\"{$lang['stradd']}\" />\n";
echo "<input type=\"submit\" name=\"cancel\" value=\"{$lang['strcancel']}\" /></p>\n";
echo "</form>\n";
break;
}
}
/**
* Confirm and then actually add a PRIMARY KEY or UNIQUE constraint
*/
function addPrimaryOrUniqueKey($type, $confirm, $msg = '') {
global $data, $misc;
global $lang;
if (!isset($_POST['name'])) $_POST['name'] = '';
if ($confirm) {
if (!isset($_POST['name'])) $_POST['name'] = '';
if (!isset($_POST['tablespace'])) $_POST['tablespace'] = '';
$misc->printTrail('table');
switch ($type) {
case 'primary':
$misc->printTitle($lang['straddpk'],'pg.constraint.primary_key');
break;
case 'unique':
$misc->printTitle($lang['stradduniq'],'pg.constraint.unique_key');
break;
default:
doDefault($lang['strinvalidparam']);
return;
}
$misc->printMsg($msg);
$attrs = $data->getTableAttributes($_REQUEST['table']);
// Fetch all tablespaces from the database
if ($data->hasTablespaces()) $tablespaces = $data->getTablespaces();
$selColumns = new XHTML_select('TableColumnList', true, 10);
$selColumns->set_style('width: 15em;');
if ($attrs->recordCount() > 0) {
while (!$attrs->EOF) {
$selColumns->add(new XHTML_Option($attrs->fields['attname']));
$attrs->moveNext();
}
}
$selIndex = new XHTML_select('IndexColumnList[]', true, 10);
$selIndex->set_style('width: 15em;');
$selIndex->set_attribute('id', 'IndexColumnList');
$buttonAdd = new XHTML_Button('add', '>>');
$buttonAdd->set_attribute('onclick', 'buttonPressed(this);');
$buttonAdd->set_attribute('type', 'button');
$buttonRemove = new XHTML_Button('remove', '<<');
$buttonRemove->set_attribute('onclick', 'buttonPressed(this);');
$buttonRemove->set_attribute('type', 'button');
echo "<form onsubmit=\"doSelectAll();\" name=\"formIndex\" action=\"constraints.php\" method=\"post\">\n";
echo "<table>\n";
echo "<tr><th class=\"data\" colspan=\"3\">{$lang['strname']}</th></tr>";
echo "<tr>";
echo "<td class=\"data1\" colspan=\"3\"><input type=\"text\" name=\"name\" value=\"", htmlspecialchars($_POST['name']),
"\" size=\"32\" maxlength=\"{$data->_maxNameLen}\" /></td></tr>";
echo "<tr><th class=\"data\">{$lang['strtablecolumnlist']}</th><th class=\"data\"> </th><th class=\"data required\">{$lang['strindexcolumnlist']}</th></tr>\n";
echo "<tr><td class=\"data1\">" . $selColumns->fetch() . "</td>\n";
echo "<td class=\"data1\" style=\"text-align: center\">" . $buttonRemove->fetch() . $buttonAdd->fetch() . "</td>";
echo "<td class=data1>" . $selIndex->fetch() . "</td></tr>\n";
// Tablespace (if there are any)
if ($data->hasTablespaces() && $tablespaces->recordCount() > 0) {
echo "<tr><th class=\"data\" colspan=\"3\">{$lang['strtablespace']}</th></tr>";
echo "<tr><td class=\"data1\" colspan=\"3\"><select name=\"tablespace\">\n";
// Always offer the default (empty) option
echo "\t\t\t\t<option value=\"\"",
($_POST['tablespace'] == '') ? ' selected="selected"' : '', "></option>\n";
// Display all other tablespaces
while (!$tablespaces->EOF) {
$spcname = htmlspecialchars($tablespaces->fields['spcname']);
echo "\t\t\t\t<option value=\"{$spcname}\"",
($spcname == $_POST['tablespace']) ? ' selected="selected"' : '', ">{$spcname}</option>\n";
$tablespaces->moveNext();
}
echo "</select></td></tr>\n";
}
echo "</table>\n";
echo "<p><input type=\"hidden\" name=\"action\" value=\"save_add_primary_key\" />\n";
echo $misc->form;
echo "<input type=\"hidden\" name=\"table\" value=\"", htmlspecialchars($_REQUEST['table']), "\" />\n";
echo "<input type=\"hidden\" name=\"type\" value=\"", htmlspecialchars($type), "\" />\n";
echo "<input type=\"submit\" value=\"{$lang['stradd']}\" />\n";
echo "<input type=\"submit\" name=\"cancel\" value=\"{$lang['strcancel']}\" /></p>\n";
echo "</form>\n";
}
else {
// Default tablespace to empty if it isn't set
if (!isset($_POST['tablespace'])) $_POST['tablespace'] = '';
if ($_POST['type'] == 'primary') {
// Check that they've given at least one column
if (!isset($_POST['IndexColumnList']) || !is_array($_POST['IndexColumnList'])
|| sizeof($_POST['IndexColumnList']) == 0) addPrimaryOrUniqueKey($_POST['type'], true, $lang['strpkneedscols']);
else {
$status = $data->addPrimaryKey($_POST['table'], $_POST['IndexColumnList'], $_POST['name'], $_POST['tablespace']);
if ($status == 0)
doDefault($lang['strpkadded']);
else
addPrimaryOrUniqueKey($_POST['type'], true, $lang['strpkaddedbad']);
}
}
elseif ($_POST['type'] == 'unique') {
// Check that they've given at least one column
if (!isset($_POST['IndexColumnList']) || !is_array($_POST['IndexColumnList'])
|| sizeof($_POST['IndexColumnList']) == 0) addPrimaryOrUniqueKey($_POST['type'], true, $lang['struniqneedscols']);
else {
$status = $data->addUniqueKey($_POST['table'], $_POST['IndexColumnList'], $_POST['name'], $_POST['tablespace']);
if ($status == 0)
doDefault($lang['struniqadded']);
else
addPrimaryOrUniqueKey($_POST['type'], true, $lang['struniqaddedbad']);
}
}
else doDefault($lang['strinvalidparam']);
}
}
/**
* Confirm and then actually add a CHECK constraint
*/
function addCheck($confirm, $msg = '') {
global $data, $misc;
global $lang;
if (!isset($_POST['name'])) $_POST['name'] = '';
if (!isset($_POST['definition'])) $_POST['definition'] = '';
if ($confirm) {
$misc->printTrail('table');
$misc->printTitle($lang['straddcheck'],'pg.constraint.check');
$misc->printMsg($msg);
echo "<form action=\"constraints.php\" method=\"post\">\n";
echo "<table>\n";
echo "<tr><th class=\"data\">{$lang['strname']}</th>\n";
echo "<th class=\"data required\">{$lang['strdefinition']}</th></tr>\n";
echo "<tr><td class=\"data1\"><input name=\"name\" size=\"16\" maxlength=\"{$data->_maxNameLen}\" value=\"",
htmlspecialchars($_POST['name']), "\" /></td>\n";
echo "<td class=\"data1\">(<input name=\"definition\" size=\"32\" value=\"",
htmlspecialchars($_POST['definition']), "\" />)</td></tr>\n";
echo "</table>\n";
echo "<input type=\"hidden\" name=\"action\" value=\"save_add_check\" />\n";
echo "<input type=\"hidden\" name=\"table\" value=\"", htmlspecialchars($_REQUEST['table']), "\" />\n";
echo $misc->form;
echo "<p><input type=\"submit\" name=\"ok\" value=\"{$lang['stradd']}\" />\n";
echo "<input type=\"submit\" name=\"cancel\" value=\"{$lang['strcancel']}\" /></p>\n";
echo "</form>\n";
}
else {
if (trim($_POST['definition']) == '')
addCheck(true, $lang['strcheckneedsdefinition']);
else {
$status = $data->addCheckConstraint($_POST['table'],
$_POST['definition'], $_POST['name']);
if ($status == 0)
doDefault($lang['strcheckadded']);
else
addCheck(true, $lang['strcheckaddedbad']);
}
}
}
/**
* Show confirmation of drop and perform actual drop
*/
function doDrop($confirm) {
global $data, $misc;
global $lang;
if ($confirm) {
$misc->printTrail('constraint');
$misc->printTitle($lang['strdrop'],'pg.constraint.drop');
echo "<p>", sprintf($lang['strconfdropconstraint'], $misc->printVal($_REQUEST['constraint']),
$misc->printVal($_REQUEST['table'])), "</p>\n";
echo "<form action=\"constraints.php\" method=\"post\">\n";
echo "<input type=\"hidden\" name=\"action\" value=\"drop\" />\n";
echo "<input type=\"hidden\" name=\"table\" value=\"", htmlspecialchars($_REQUEST['table']), "\" />\n";
echo "<input type=\"hidden\" name=\"constraint\" value=\"", htmlspecialchars($_REQUEST['constraint']), "\" />\n";
echo "<input type=\"hidden\" name=\"type\" value=\"", htmlspecialchars($_REQUEST['type']), "\" />\n";
echo $misc->form;
echo "<p><input type=\"checkbox\" id=\"cascade\" name=\"cascade\" /> <label for=\"cascade\">{$lang['strcascade']}</label></p>\n";
echo "<input type=\"submit\" name=\"drop\" value=\"{$lang['strdrop']}\" />\n";
echo "<input type=\"submit\" name=\"cancel\" value=\"{$lang['strcancel']}\" />\n";
echo "</form>\n";
}
else {
$status = $data->dropConstraint($_POST['constraint'], $_POST['table'], $_POST['type'], isset($_POST['cascade']));
if ($status == 0)
doDefault($lang['strconstraintdropped']);
else
doDefault($lang['strconstraintdroppedbad']);
}
}
/**
* List all the constraints on the table
*/
function doDefault($msg = '') {
global $data, $misc, $lang;
function cnPre(&$rowdata) {
global $data;
if (is_null($rowdata->fields['consrc'])) {
$atts = $data->getAttributeNames($_REQUEST['table'], explode(' ', $rowdata->fields['indkey']));
$rowdata->fields['+definition'] = ($rowdata->fields['contype'] == 'u' ? "UNIQUE (" : "PRIMARY KEY (") . join(',', $atts) . ')';
} else {
$rowdata->fields['+definition'] = $rowdata->fields['consrc'];
}
}
$misc->printTrail('table');
$misc->printTabs('table','constraints');
$misc->printMsg($msg);
$constraints = $data->getConstraints($_REQUEST['table']);
$columns = array(
'constraint' => array(
'title' => $lang['strname'],
'field' => field('conname'),
),
'definition' => array(
'title' => $lang['strdefinition'],
'field' => field('+definition'),
'type' => 'pre',
),
'actions' => array(
'title' => $lang['stractions'],
),
'comment' => array(
'title' => $lang['strcomment'],
'field' => field('constcomment'),
),
);
$actions = array(
'drop' => array(
'content' => $lang['strdrop'],
'attr'=> array (
'href' => array (
'url' => 'constraints.php',
'urlvars' => array (
'action' => 'confirm_drop',
'table' => $_REQUEST['table'],
'constraint' => field('conname'),
'type' => field('contype')
)
)
)
)
);
$misc->printTable($constraints, $columns, $actions, 'constraints-constraints', $lang['strnoconstraints'], 'cnPre');
$navlinks = array (
'addcheck' => array (
'attr'=> array (
'href' => array (
'url' => 'constraints.php',
'urlvars' => array (
'action' => 'add_check',
'server' => $_REQUEST['server'],
'database' => $_REQUEST['database'],
'schema' => $_REQUEST['schema'],
'table' => $_REQUEST['table']
)
)
),
'content' => $lang['straddcheck'],
),
'adduniq' => array (
'attr'=> array (
'href' => array (
'url' => 'constraints.php',
'urlvars' => array (
'action' => 'add_unique_key',
'server' => $_REQUEST['server'],
'database' => $_REQUEST['database'],
'schema' => $_REQUEST['schema'],
'table' => $_REQUEST['table']
)
)
),
'content' => $lang['stradduniq'],
),
'addpk' => array (
'attr'=> array (
'href' => array (
'url' => 'constraints.php',
'urlvars' => array (
'action' => 'add_primary_key',
'server' => $_REQUEST['server'],
'database' => $_REQUEST['database'],
'schema' => $_REQUEST['schema'],
'table' => $_REQUEST['table']
)
)
),
'content' => $lang['straddpk'],
),
'addfk' => array (
'attr'=> array (
'href' => array (
'url' => 'constraints.php',
'urlvars' => array (
'action' => 'add_foreign_key',
'server' => $_REQUEST['server'],
'database' => $_REQUEST['database'],
'schema' => $_REQUEST['schema'],
'table' => $_REQUEST['table']
)
)
),
'content' => $lang['straddfk']
)
);
$misc->printNavLinks($navlinks, 'constraints-constraints', get_defined_vars());
}
function doTree() {
global $misc, $data;
$constraints = $data->getConstraints($_REQUEST['table']);
$reqvars = $misc->getRequestVars('schema');
function getIcon($f) {
switch($f['contype']) {
case 'u':
return 'UniqueConstraint';
case 'c':
return 'CheckConstraint';
case 'f':
return 'ForeignKey';
case 'p':
return 'PrimaryKey';
}
}
$attrs = array(
'text' => field('conname'),
'icon' => callback('getIcon'),
);
$misc->printTree($constraints, $attrs, 'constraints');
exit;
}
if ($action == 'tree') doTree();
$misc->printHeader($lang['strtables'] . ' - ' . $_REQUEST['table'] . ' - ' . $lang['strconstraints'],
"<script src=\"indexes.js\" type=\"text/javascript\"></script>");
if ($action == 'add_unique_key' || $action == 'save_add_unique_key'
|| $action == 'add_primary_key' || $action == 'save_add_primary_key'
|| $action == 'add_foreign_key' || $action == 'save_add_foreign_key')
echo "<body onload=\"init();\">";
else
$misc->printBody();
switch ($action) {
case 'add_foreign_key':
addForeignKey(1);
break;
case 'save_add_foreign_key':
if (isset($_POST['cancel'])) doDefault();
else addForeignKey($_REQUEST['stage']);
break;
case 'add_unique_key':
addPrimaryOrUniqueKey('unique', true);
break;
case 'save_add_unique_key':
if (isset($_POST['cancel'])) doDefault();
else addPrimaryOrUniqueKey('unique', false);
break;
case 'add_primary_key':
addPrimaryOrUniqueKey('primary', true);
break;
case 'save_add_primary_key':
if (isset($_POST['cancel'])) doDefault();
else addPrimaryOrUniqueKey('primary', false);
break;
case 'add_check':
addCheck(true);
break;
case 'save_add_check':
if (isset($_POST['cancel'])) doDefault();
else addCheck(false);
break;
case 'save_create':
doSaveCreate();
break;
case 'create':
doCreate();
break;
case 'drop':
if (isset($_POST['drop'])) doDrop(false);
else doDefault();
break;
case 'confirm_drop':
doDrop(true);
break;
default:
doDefault();
break;
}
$misc->printFooter();
?>