2007-07-11 16:02:57 +00:00
< ? php
2009-08-29 13:57:50 +00:00
$TABLE = $_GET [ " foreign " ];
2013-05-08 15:43:15 +00:00
$name = $_GET [ " name " ];
$row = $_POST ;
2007-07-11 22:37:43 +00:00
if ( $_POST && ! $error && ! $_POST [ " add " ] && ! $_POST [ " change " ] && ! $_POST [ " change-js " ]) {
2013-08-09 22:16:15 +00:00
$message = ( $_POST [ " drop " ] ? lang ( 'Foreign key has been dropped.' ) : ( $name != " " ? lang ( 'Foreign key has been altered.' ) : lang ( 'Foreign key has been created.' )));
$location = ME . " table= " . urlencode ( $TABLE );
2017-02-20 15:59:13 +00:00
if ( ! $_POST [ " drop " ]) {
$row [ " source " ] = array_filter ( $row [ " source " ], 'strlen' );
ksort ( $row [ " source " ]); // enforce input order
$target = array ();
foreach ( $row [ " source " ] as $key => $val ) {
$target [ $key ] = $row [ " target " ][ $key ];
}
$row [ " target " ] = $target ;
2013-08-09 22:16:15 +00:00
}
if ( $jush == " sqlite " ) {
queries_redirect ( $location , $message , recreate_table ( $TABLE , $TABLE , array (), array (), array ( " $name " => ( $_POST [ " drop " ] ? " " : " " . format_foreign_key ( $row )))));
2007-08-04 20:26:58 +00:00
} else {
2013-08-09 22:16:15 +00:00
$alter = " ALTER TABLE " . table ( $TABLE );
$drop = " \n DROP " . ( $jush == " sql " ? " FOREIGN KEY " : " CONSTRAINT " ) . idf_escape ( $name );
if ( $_POST [ " drop " ]) {
query_redirect ( $alter . $drop , $location , $message );
} else {
query_redirect ( $alter . ( $name != " " ? " $drop , " : " " ) . " \n ADD " . format_foreign_key ( $row ), $location , $message );
$error = lang ( 'Source and target columns must have the same data type, there must be an index on the target columns and referenced data must exist.' ) . " <br> $error " ; //! no partitioning
2007-07-16 11:53:55 +00:00
}
2007-07-11 22:37:43 +00:00
}
}
2009-07-20 12:12:55 +00:00
2013-07-05 08:31:53 +00:00
page_header ( lang ( 'Foreign key' ), $error , array ( " table " => $TABLE ), h ( $TABLE ));
2007-07-11 16:02:57 +00:00
2007-07-11 22:37:43 +00:00
if ( $_POST ) {
ksort ( $row [ " source " ]);
if ( $_POST [ " add " ]) {
$row [ " source " ][] = " " ;
} elseif ( $_POST [ " change " ] || $_POST [ " change-js " ]) {
$row [ " target " ] = array ();
}
2013-05-08 15:43:15 +00:00
} elseif ( $name != " " ) {
2009-08-29 13:57:50 +00:00
$foreign_keys = foreign_keys ( $TABLE );
2013-05-08 15:43:15 +00:00
$row = $foreign_keys [ $name ];
2007-08-04 20:26:58 +00:00
$row [ " source " ][] = " " ;
2013-05-08 15:43:15 +00:00
} else {
$row [ " table " ] = $TABLE ;
$row [ " source " ] = array ( " " );
2007-07-11 22:37:43 +00:00
}
?>
2007-08-04 20:26:58 +00:00
2007-07-11 22:37:43 +00:00
< form action = " " method = " post " >
2019-08-21 12:19:48 +00:00
< ? php
2019-08-21 13:00:11 +00:00
$source = array_keys ( fields ( $TABLE )); //! no text and blob
if ( $row [ " db " ] != " " ) {
$connection -> select_db ( $row [ " db " ]);
}
if ( $row [ " ns " ] != " " ) {
set_schema ( $row [ " ns " ]);
}
$referencable = array_keys ( array_filter ( table_status ( '' , true ), 'fk_support' ));
2019-08-21 13:09:47 +00:00
$target = ( $TABLE === $row [ " table " ] ? $source : array_keys ( fields ( in_array ( $row [ " table " ], $referencable ) ? $row [ " table " ] : reset ( $referencable ))));
2019-08-21 13:00:11 +00:00
$onchange = " this.form['change-js'].value = '1'; this.form.submit(); " ;
2019-08-21 13:05:02 +00:00
echo " <p> " . lang ( 'Target table' ) . " : " . html_select ( " table " , $referencable , $row [ " table " ], $onchange ) . " \n " ;
2019-08-21 13:00:11 +00:00
if ( $jush == " pgsql " ) {
2019-08-21 13:09:47 +00:00
echo lang ( 'Schema' ) . " : " . html_select ( " ns " , $adminer -> schemas (), $row [ " ns " ] != " " ? $row [ " ns " ] : $_GET [ " ns " ], $onchange );
2019-08-21 13:00:11 +00:00
} elseif ( $jush != " sqlite " ) {
$dbs = array ();
foreach ( $adminer -> databases () as $db ) {
if ( ! information_schema ( $db )) {
$dbs [] = $db ;
}
}
2019-08-21 13:09:47 +00:00
echo lang ( 'DB' ) . " : " . html_select ( " db " , $dbs , $row [ " db " ] != " " ? $row [ " db " ] : $_GET [ " db " ], $onchange );
2019-08-21 13:00:11 +00:00
}
?>
2009-07-11 20:30:40 +00:00
< input type = " hidden " name = " change-js " value = " " >
< noscript >< p >< input type = " submit " name = " change " value = " <?php echo lang('Change'); ?> " ></ noscript >
2009-05-01 10:41:33 +00:00
< table cellspacing = " 0 " >
2017-01-23 23:12:42 +00:00
< thead >< tr >< th id = " label-source " >< ? php echo lang ( 'Source' ); ?> <th id="label-target"><?php echo lang('Target'); ?></thead>
2007-07-11 22:37:43 +00:00
< ? php
2007-08-04 20:26:58 +00:00
$j = 0 ;
2007-07-11 22:37:43 +00:00
foreach ( $row [ " source " ] as $key => $val ) {
echo " <tr> " ;
2018-01-11 16:00:39 +00:00
echo " <td> " . html_select ( " source[ " . ( + $key ) . " ] " , array ( - 1 => " " ) + $source , $val , ( $j == count ( $row [ " source " ]) - 1 ? " foreignAddRow.call(this); " : 1 ), " label-source " );
2017-01-23 23:12:42 +00:00
echo " <td> " . html_select ( " target[ " . ( + $key ) . " ] " , $target , $row [ " target " ][ $key ], 1 , " label-target " );
2007-08-04 20:26:58 +00:00
$j ++ ;
2007-07-11 16:02:57 +00:00
}
2007-07-11 22:37:43 +00:00
?>
</ table >
< p >
2011-08-02 15:46:13 +00:00
< ? php echo lang ( 'ON DELETE' ); ?> : <?php echo html_select("on_delete", array(-1 => "") + explode("|", $on_actions), $row["on_delete"]); ?>
< ? php echo lang ( 'ON UPDATE' ); ?> : <?php echo html_select("on_update", array(-1 => "") + explode("|", $on_actions), $row["on_update"]); ?>
2013-08-06 01:55:27 +00:00
< ? php echo doc_link ( array (
'sql' => " innodb-foreign-key-constraints.html " ,
2018-02-08 12:56:22 +00:00
'mariadb' => " foreign-keys/ " ,
2013-08-06 01:55:27 +00:00
'pgsql' => " sql-createtable.html#SQL-CREATETABLE-REFERENCES " ,
'mssql' => " ms174979.aspx " ,
2019-08-27 14:33:36 +00:00
'oracle' => " https://docs.oracle.com/cd/B19306_01/server.102/b14200/clauses002.htm#sthref2903 " ,
2013-08-06 01:55:27 +00:00
)); ?>
2007-07-25 17:12:43 +00:00
< p >
2009-07-11 20:30:40 +00:00
< input type = " submit " value = " <?php echo lang('Save'); ?> " >
< noscript >< p >< input type = " submit " name = " add " value = " <?php echo lang('Add column'); ?> " ></ noscript >
2018-01-24 17:36:19 +00:00
< ? php if ( $name != " " ) { ?> <input type="submit" name="drop" value="<?php echo lang('Drop'); ?>"><?php echo confirm(lang('Drop %s?', $name)); ?><?php } ?>
2009-11-19 20:04:55 +00:00
< input type = " hidden " name = " token " value = " <?php echo $token ; ?> " >
2007-07-11 22:37:43 +00:00
</ form >