2
/* $Id: tbl_relation.php 9809 2006-12-29 16:27:25Z lem9 $ */
3
// vim: expandtab sw=4 ts=4 sts=4:
6
* Gets some core libraries
8
require_once('./libraries/common.lib.php');
9
require_once('./libraries/tbl_common.php');
10
$url_query .= '&goto=tbl_sql.php';
14
* Gets tables informations
16
require_once('./libraries/tbl_info.inc.php');
18
// Note: in libraries/tbl_links.inc.php we get and display the table comment.
19
// For InnoDB, this comment contains the REFER information but any update
20
// has not been done yet (will be done in tbl_relation.php later).
21
$avoid_show_comment = TRUE;
24
* Displays top menu links
26
require_once('./libraries/tbl_links.inc.php');
28
require_once('./libraries/relation.lib.php');
30
$options_array = array('CASCADE' => 'CASCADE', 'SET_NULL' => 'SET NULL', 'NO_ACTION' => 'NO ACTION', 'RESTRICT' => 'RESTRICT');
33
* Generate dropdown choices
35
* @param string Message to display
36
* @param string Name of the <select> field
37
* @param array Choices for dropdown
38
* @return string The existing value (for selected)
42
function PMA_generate_dropdown($dropdown_question, $radio_name, $choices, $selected_value)
44
echo $dropdown_question . ' ';
46
echo '<select name="' . $radio_name . '">' . "\n";
47
echo '<option value="nix">--</option>' . "\n";
49
foreach ($choices AS $one_value => $one_label) {
50
echo '<option value="' . $one_value . '"';
51
if ($selected_value == $one_value) {
52
echo ' selected="selected" ';
54
echo '>' . $one_label . '</option>' . "\n";
56
echo '</select>' . "\n";
62
* Gets the relation settings
64
$cfgRelation = PMA_getRelationsParam();
71
// ensure we are positionned to our current db (since the previous reading
72
// of relations makes pmadb the current one, maybe depending on the MySQL version)
73
PMA_DBI_select_db($db);
75
if ($cfgRelation['relwork']) {
76
$existrel = PMA_getForeigners($db, $table, '', 'internal');
78
if ($tbl_type=='INNODB') {
79
$existrel_innodb = PMA_getForeigners($db, $table, '', 'innodb');
81
if ($cfgRelation['displaywork']) {
82
$disp = PMA_getDisplayField($db, $table);
85
// u p d a t e s f o r I n t e r n a l r e l a t i o n s
86
if (isset($destination) && $cfgRelation['relwork']) {
88
foreach ($destination AS $master_field => $foreign_string) {
89
if ($foreign_string != 'nix') {
90
list($foreign_db, $foreign_table, $foreign_field) = explode('.', $foreign_string);
91
if (!isset($existrel[$master_field])) {
92
$upd_query = 'INSERT INTO ' . PMA_backquote($GLOBALS['cfgRelation']['db']) . '.' . PMA_backquote($cfgRelation['relation'])
93
. '(master_db, master_table, master_field, foreign_db, foreign_table, foreign_field)'
95
. '\'' . PMA_sqlAddslashes($db) . '\', '
96
. '\'' . PMA_sqlAddslashes($table) . '\', '
97
. '\'' . PMA_sqlAddslashes($master_field) . '\', '
98
. '\'' . PMA_sqlAddslashes($foreign_db) . '\', '
99
. '\'' . PMA_sqlAddslashes($foreign_table) . '\','
100
. '\'' . PMA_sqlAddslashes($foreign_field) . '\')';
101
} elseif ($existrel[$master_field]['foreign_db'] . '.' .$existrel[$master_field]['foreign_table'] . '.' . $existrel[$master_field]['foreign_field'] != $foreign_string) {
102
$upd_query = 'UPDATE ' . PMA_backquote($GLOBALS['cfgRelation']['db']) . '.' . PMA_backquote($cfgRelation['relation']) . ' SET'
103
. ' foreign_db = \'' . PMA_sqlAddslashes($foreign_db) . '\', '
104
. ' foreign_table = \'' . PMA_sqlAddslashes($foreign_table) . '\', '
105
. ' foreign_field = \'' . PMA_sqlAddslashes($foreign_field) . '\' '
106
. ' WHERE master_db = \'' . PMA_sqlAddslashes($db) . '\''
107
. ' AND master_table = \'' . PMA_sqlAddslashes($table) . '\''
108
. ' AND master_field = \'' . PMA_sqlAddslashes($master_field) . '\'';
109
} // end if... else....
110
} elseif (isset($existrel[$master_field])) {
111
$upd_query = 'DELETE FROM ' . PMA_backquote($GLOBALS['cfgRelation']['db']) . '.' . PMA_backquote($cfgRelation['relation'])
112
. ' WHERE master_db = \'' . PMA_sqlAddslashes($db) . '\''
113
. ' AND master_table = \'' . PMA_sqlAddslashes($table) . '\''
114
. ' AND master_field = \'' . PMA_sqlAddslashes($master_field) . '\'';
115
} // end if... else....
116
if (isset($upd_query)) {
117
$upd_rs = PMA_query_as_cu($upd_query);
121
} // end if (updates for internal relations)
123
// u p d a t e s f o r I n n o D B
124
// ( for now, one index name only; we keep the definitions if the
125
// foreign db is not the same)
126
// I use $sql_query to be able to display directly the query via
129
if (isset($_REQUEST['destination_innodb'])) {
132
foreach ($_REQUEST['destination_innodb'] as $master_field => $foreign_string) {
133
if ($foreign_string != 'nix') {
134
list($foreign_db, $foreign_table, $foreign_field) = explode('.', $foreign_string);
135
if (!isset($existrel_innodb[$master_field])) {
136
// no key defined for this field
138
// The next few lines are repeated below, so they
139
// could be put in an include file
140
// Note: I tried to enclose the db and table name with
141
// backquotes but MySQL 4.0.16 did not like the syntax
142
// (for example: `base2`.`table1` )
144
$sql_query = 'ALTER TABLE ' . PMA_backquote($table)
145
. ' ADD FOREIGN KEY ('
146
. PMA_backquote($master_field) . ')'
148
. PMA_backquote($foreign_db) . '.'
149
. PMA_backquote($foreign_table) . '('
150
. PMA_backquote($foreign_field) . ')';
152
if ($_REQUEST['on_delete'][$master_field] != 'nix') {
153
$sql_query .= ' ON DELETE ' . $options_array[$_REQUEST['on_delete'][$master_field]];
155
if ($_REQUEST['on_update'][$master_field] != 'nix') {
156
$sql_query .= ' ON UPDATE ' . $options_array[$_REQUEST['on_update'][$master_field]];
159
$display_query .= $sql_query . "\n";
162
} elseif (($existrel_innodb[$master_field]['foreign_db'] . '.' .$existrel_innodb[$master_field]['foreign_table'] . '.' . $existrel_innodb[$master_field]['foreign_field'] != $foreign_string)
163
|| ( $_REQUEST['on_delete'][$master_field] != (!empty($existrel_innodb[$master_field]['on_delete']) ? $existrel_innodb[$master_field]['on_delete'] : ''))
164
|| ( $_REQUEST['on_update'][$master_field] != (!empty($existrel_innodb[$master_field]['on_update']) ? $existrel_innodb[$master_field]['on_update'] : ''))
166
// another foreign key is already defined for this field
168
// an option has been changed for ON DELETE or ON UPDATE
170
// remove existing key
171
if (PMA_MYSQL_INT_VERSION >= 40013) {
172
$sql_query = 'ALTER TABLE ' . PMA_backquote($table)
173
. ' DROP FOREIGN KEY '
174
. PMA_backquote($existrel_innodb[$master_field]['constraint']) . ';';
176
// I tried to send both in one query but it failed
177
$upd_rs = PMA_DBI_query($sql_query);
178
$display_query .= $sql_query . "\n";
182
$sql_query = 'ALTER TABLE ' . PMA_backquote($table)
183
. ' ADD FOREIGN KEY ('
184
. PMA_backquote($master_field) . ')'
186
. PMA_backquote($foreign_db) . '.'
187
. PMA_backquote($foreign_table) . '('
188
. PMA_backquote($foreign_field) . ')';
190
if ($_REQUEST['on_delete'][$master_field] != 'nix') {
191
$sql_query .= ' ON DELETE '
192
. $options_array[$_REQUEST['on_delete'][$master_field]];
194
if ($_REQUEST['on_update'][$master_field] != 'nix') {
195
$sql_query .= ' ON UPDATE '
196
. $options_array[$_REQUEST['on_update'][$master_field]];
199
$display_query .= $sql_query . "\n";
201
} // end if... else....
202
} elseif (isset($existrel_innodb[$master_field])) {
203
if (PMA_MYSQL_INT_VERSION >= 40013) {
204
$sql_query = 'ALTER TABLE ' . PMA_backquote($table)
205
. ' DROP FOREIGN KEY '
206
. PMA_backquote($existrel_innodb[$master_field]['constraint']);
208
$display_query .= $sql_query . "\n";
210
} // end if... else....
212
if (isset($sql_query)) {
213
$upd_rs = PMA_DBI_try_query($sql_query);
214
$tmp_error = PMA_DBI_getError();
215
if (! empty($tmp_error)) {
218
if (substr($tmp_error, 1, 4) == '1216'
219
|| substr($tmp_error, 1, 4) == '1452') {
220
PMA_mysqlDie($tmp_error, $sql_query, FALSE, '', FALSE);
221
echo PMA_showMySQLDocu('manual_Table_types', 'InnoDB_foreign_key_constraints') . "\n";
223
if (substr($tmp_error, 1, 4) == '1005') {
224
echo '<p class="warning">' . $strForeignKeyError . ' : ' . $master_field
225
.'</p>' . PMA_showMySQLDocu('manual_Table_types', 'InnoDB_foreign_key_constraints') . "\n";
227
unset($sql_query, $tmp_error);
230
if (!empty($display_query)) {
232
PMA_showMessage($strError);
234
PMA_showMessage($strSuccess);
237
} // end if isset($destination_innodb)
240
// U p d a t e s f o r d i s p l a y f i e l d
242
if ($cfgRelation['displaywork']
243
&& isset($display_field)) {
246
if ($display_field != '') {
247
$upd_query = 'UPDATE ' . PMA_backquote($GLOBALS['cfgRelation']['db']) . '.' . PMA_backquote($cfgRelation['table_info'])
248
. ' SET display_field = \'' . PMA_sqlAddslashes($display_field) . '\''
249
. ' WHERE db_name = \'' . PMA_sqlAddslashes($db) . '\''
250
. ' AND table_name = \'' . PMA_sqlAddslashes($table) . '\'';
252
$upd_query = 'DELETE FROM ' . PMA_backquote($GLOBALS['cfgRelation']['db']) . '.' . PMA_backquote($cfgRelation['table_info'])
253
. ' WHERE db_name = \'' . PMA_sqlAddslashes($db) . '\''
254
. ' AND table_name = \'' . PMA_sqlAddslashes($table) . '\'';
256
} elseif ($display_field != '') {
257
$upd_query = 'INSERT INTO ' . PMA_backquote($GLOBALS['cfgRelation']['db']) . '.' . PMA_backquote($cfgRelation['table_info'])
258
. '(db_name, table_name, display_field) '
260
. '\'' . PMA_sqlAddslashes($db) . '\','
261
. '\'' . PMA_sqlAddslashes($table) . '\','
262
. '\'' . PMA_sqlAddslashes($display_field) . '\')';
265
if (isset($upd_query)) {
266
$upd_rs = PMA_query_as_cu($upd_query);
270
// If we did an update, refresh our data
271
if (isset($destination) && $cfgRelation['relwork']) {
272
$existrel = PMA_getForeigners($db, $table, '', 'internal');
274
if (isset($destination_innodb) && $tbl_type=='INNODB') {
275
$existrel_innodb = PMA_getForeigners($db, $table, '', 'innodb');
278
if ($cfgRelation['displaywork']) {
279
$disp = PMA_getDisplayField($db, $table);
288
echo '<form method="post" action="tbl_relation.php">' . "\n";
289
echo PMA_generate_common_hidden_inputs($db, $table);
294
if ($cfgRelation['relwork'] || $tbl_type=='INNODB') {
295
// To choose relations we first need all tables names in current db
296
// and if PMA version permits and the main table is innodb,
297
// we use SHOW TABLE STATUS because we need to find other InnoDB tables
299
if ($tbl_type=='INNODB') {
300
$tab_query = 'SHOW TABLE STATUS FROM ' . PMA_backquote($db);
301
// [0] of the row is the name
304
$tab_query = 'SHOW TABLES FROM ' . PMA_backquote($db);
306
// [0] of the row is the name
308
$tab_rs = PMA_DBI_query($tab_query, null, PMA_DBI_QUERY_STORE);
309
$selectboxall['nix'] = '--';
310
$selectboxall_innodb['nix'] = '--';
312
while ($curr_table = @PMA_DBI_fetch_row($tab_rs)) {
313
if (($curr_table[0] != $table) && ($curr_table[0] != $cfg['Server']['relation'])) {
314
PMA_DBI_select_db($db);
316
// need to use PMA_DBI_QUERY_STORE with PMA_DBI_num_rows() in mysqli
317
$fi_rs = PMA_DBI_query('SHOW KEYS FROM ' . PMA_backquote($curr_table[0]) . ';', null, PMA_DBI_QUERY_STORE);
318
if ($fi_rs && PMA_DBI_num_rows($fi_rs) > 0) {
319
$seen_a_primary = FALSE;
320
while ($curr_field = PMA_DBI_fetch_assoc($fi_rs)) {
321
if (isset($curr_field['Key_name']) && $curr_field['Key_name'] == 'PRIMARY') {
322
$seen_a_primary = TRUE;
323
$field_full = $db . '.' .$curr_field['Table'] . '.' . $curr_field['Column_name'];
324
$field_v = $curr_field['Table'] . '->' . $curr_field['Column_name'];
325
$selectboxall[$field_full] = $field_v;
326
// there could be more than one segment of the primary
329
// Please watch here, tbl_type is INNODB but the
330
// resulting value of SHOW KEYS is InnoDB
332
if ($tbl_type=='INNODB' && isset($curr_table[1]) && $curr_table[1]=='InnoDB') {
333
$selectboxall_innodb[$field_full] = $field_v;
336
} elseif (isset($curr_field['Non_unique']) && $curr_field['Non_unique'] == 0 && $seen_a_primary==FALSE) {
337
// if we can't find a primary key we take any unique one
338
// (in fact, we show all segments of unique keys
339
// and all unique keys)
340
$field_full = $db . '.' . $curr_field['Table'] . '.' . $curr_field['Column_name'];
341
$field_v = $curr_field['Table'] . '->' . $curr_field['Column_name'];
342
$selectboxall[$field_full] = $field_v;
343
if ($tbl_type=='INNODB' && isset($curr_table[1]) && $curr_table[1]=='InnoDB') {
344
$selectboxall_innodb[$field_full] = $field_v;
347
// for InnoDB, any index is allowed
348
} elseif ($tbl_type=='INNODB' && isset($curr_table[1]) && $curr_table[1]=='InnoDB') {
349
$field_full = $db . '.' . $curr_field['Table'] . '.' . $curr_field['Column_name'];
350
$field_v = $curr_field['Table'] . '->' . $curr_field['Column_name'];
351
$selectboxall_innodb[$field_full] = $field_v;
354
} // end while over keys
355
} // end if (PMA_DBI_num_rows)
356
PMA_DBI_free_result($fi_rs);
358
// Mike Beck - 24.07.02: i've been asked to add all keys of the
359
// current table (see bug report #574851)
360
} elseif ($curr_table[0] == $table) {
361
PMA_DBI_select_db($db);
363
// need to use PMA_DBI_QUERY_STORE with PMA_DBI_num_rows() in mysqli
364
$fi_rs = PMA_DBI_query('SHOW KEYS FROM ' . PMA_backquote($curr_table[0]) . ';', null, PMA_DBI_QUERY_STORE);
365
if ($fi_rs && PMA_DBI_num_rows($fi_rs) > 0) {
366
while ($curr_field = PMA_DBI_fetch_assoc($fi_rs)) {
367
$field_full = $db . '.' . $curr_field['Table'] . '.' . $curr_field['Column_name'];
368
$field_v = $curr_field['Table'] . '->' . $curr_field['Column_name'];
369
$selectboxall[$field_full] = $field_v;
370
if ($tbl_type=='INNODB' && isset($curr_table[1]) && $curr_table[1]=='InnoDB') {
371
$selectboxall_innodb[$field_full] = $field_v;
374
} // end if (PMA_DBI_num_rows)
375
PMA_DBI_free_result($fi_rs);
378
} // end while over tables
383
// Now find out the columns of our $table
384
// need to use PMA_DBI_QUERY_STORE with PMA_DBI_num_rows() in mysqli
385
$col_rs = PMA_DBI_try_query('SHOW COLUMNS FROM ' . PMA_backquote($table) . ';', null, PMA_DBI_QUERY_STORE);
387
if ($col_rs && PMA_DBI_num_rows($col_rs) > 0) {
388
while ($row = PMA_DBI_fetch_assoc($col_rs)) {
391
$saved_row_cnt = count($save_row);
394
<legend><?php echo $strLinksTo; ?></legend>
399
if ( $cfgRelation['relwork'] ) {
400
echo '<th>' . $strInternalRelations;
401
if ($tbl_type=='INNODB') {
402
echo PMA_showHint( $strInternalNotNecessary );
406
if ( $tbl_type=='INNODB' ) {
407
echo '<th colspan="2">InnoDB';
408
if (PMA_MYSQL_INT_VERSION < 40013) {
417
for ($i = 0; $i < $saved_row_cnt; $i++) {
418
$myfield = $save_row[$i]['Field'];
420
<tr class="<?php echo $odd_row ? 'odd' : 'even'; $odd_row = ! $odd_row; ?>">
422
<b><?php echo $save_row[$i]['Field']; ?></b></td>
424
if ($cfgRelation['relwork']) {
426
<td><select name="destination[<?php echo htmlspecialchars($save_row[$i]['Field']); ?>]">
428
// PMA internal relations
429
if (isset($existrel[$myfield])) {
430
$foreign_field = $existrel[$myfield]['foreign_db'] . '.'
431
. $existrel[$myfield]['foreign_table'] . '.'
432
. $existrel[$myfield]['foreign_field'];
434
$foreign_field = FALSE;
437
foreach ($selectboxall AS $key => $value) {
439
. '<option value="' . htmlspecialchars($key) . '"';
440
if ($foreign_field && $key == $foreign_field) {
441
echo ' selected="selected"';
444
echo '>' . $value . '</option>'. "\n";
447
// if the link defined in relationtable points to a foreign field
448
// that is not a key in the foreign table, we show the link
449
// (will not be shown with an arrow)
450
if ($foreign_field && !$seen_key) {
452
.'<option value="' . htmlspecialchars($foreign_field) . '"'
453
.' selected="selected"'
454
.'>' . $foreign_field . '</option>'. "\n";
460
} // end if (internal relations)
462
if ($tbl_type=='INNODB') {
464
if (!empty($save_row[$i]['Key'])) {
466
<span class="formelement">
467
<select name="destination_innodb[<?php echo htmlspecialchars($save_row[$i]['Field']); ?>]">
469
if (isset($existrel_innodb[$myfield])) {
470
$foreign_field = $existrel_innodb[$myfield]['foreign_db'] . '.'
471
. $existrel_innodb[$myfield]['foreign_table'] . '.'
472
. $existrel_innodb[$myfield]['foreign_field'];
474
$foreign_field = FALSE;
477
$found_foreign_field = FALSE;
478
foreach ($selectboxall_innodb AS $key => $value) {
480
. '<option value="' . htmlspecialchars($key) . '"';
481
if ($foreign_field && $key == $foreign_field) {
482
echo ' selected="selected"';
483
$found_foreign_field = TRUE;
485
echo '>' . $value . '</option>'. "\n";
488
// we did not find the foreign field in the tables of current db,
489
// must be defined in another db so show it to avoid erasing it
490
if (!$found_foreign_field && $foreign_field) {
492
. '<option value="' . htmlspecialchars($foreign_field) . '"';
493
echo ' selected="selected"';
494
echo '>' . $foreign_field . '</option>' . "\n";
500
<span class="formelement">
502
PMA_generate_dropdown('ON DELETE',
503
'on_delete[' . htmlspecialchars($save_row[$i]['Field']) . ']',
505
isset($existrel_innodb[$myfield]['on_delete']) ? $existrel_innodb[$myfield]['on_delete']: '' );
507
echo '</span>' . "\n"
508
.'<span class="formelement">' . "\n";
510
PMA_generate_dropdown('ON UPDATE',
511
'on_update[' . htmlspecialchars($save_row[$i]['Field']) . ']',
513
isset($existrel_innodb[$myfield]['on_update']) ? $existrel_innodb[$myfield]['on_update']: '' );
514
echo '</span>' . "\n";
517
} // end if (a key exists)
525
echo ' </table>' . "\n";
526
echo '</fieldset>' . "\n";
528
if ($cfgRelation['displaywork']) {
529
// Get "display_field" infos
530
$disp = PMA_getDisplayField($db, $table);
533
<label><?php echo $strChangeDisplay . ': '; ?></label>
534
<select name="display_field" style="vertical-align: middle">
535
<option value="">---</option>
537
foreach ($save_row AS $row) {
538
echo ' <option value="' . htmlspecialchars($row['Field']) . '"';
539
if (isset($disp) && $row['Field'] == $disp) {
540
echo ' selected="selected"';
542
echo '>' . htmlspecialchars($row['Field']) . '</option>'. "\n";
548
} // end if (displayworks)
550
<fieldset class="tblFooters">
551
<input type="submit" value="<?php echo $strSave; ?>" />
555
} // end if (we have columns in this table)
557
if ( $tbl_type === 'INNODB' && PMA_MYSQL_INT_VERSION < 40013 ) {
558
echo '<div class="warning">'
559
.'** ' . sprintf($strUpgrade, 'MySQL', '4.0.13')
564
* Displays the footer
566
require_once('./libraries/footer.inc.php');