2007-07-17 07:23:17 +00:00
< ? php
2013-07-05 08:31:53 +00:00
page_header ( lang ( 'Database schema' ), " " , array (), h ( DB . ( $_GET [ " ns " ] ? " . $_GET[ns] " : " " )));
2007-07-17 07:23:17 +00:00
2007-07-26 14:52:02 +00:00
$table_pos = array ();
$table_pos_js = array ();
2014-04-21 16:59:52 +00:00
$SCHEMA = ( $_GET [ " schema " ] ? $_GET [ " schema " ] : $_COOKIE [ " adminer_schema- " . str_replace ( " . " , " _ " , DB )]); // $_COOKIE["adminer_schema"] was used before 3.2.0 //! ':' in table name
2011-03-26 01:26:17 +00:00
preg_match_all ( '~([^:]+):([-0-9.]+)x([-0-9.]+)(_|$)~' , $SCHEMA , $matches , PREG_SET_ORDER );
2007-07-26 14:52:02 +00:00
foreach ( $matches as $i => $match ) {
2007-07-27 06:24:59 +00:00
$table_pos [ $match [ 1 ]] = array ( $match [ 2 ], $match [ 3 ]);
2010-10-22 21:07:30 +00:00
$table_pos_js [] = " \n \t ' " . js_escape ( $match [ 1 ]) . " ': [ $match[2] , $match[3] ] " ;
2007-07-26 14:52:02 +00:00
}
$top = 0 ;
2007-07-28 22:36:07 +00:00
$base_left = - 1 ;
2009-06-21 23:20:32 +00:00
$schema = array (); // table => array("fields" => array(name => field), "pos" => array(top, left), "references" => array(table => array(left => array(source, target))))
$referenced = array (); // target_table => array(table => array(left => target_column))
$lefts = array (); // float => bool
2013-04-27 03:04:57 +00:00
foreach ( table_status ( '' , true ) as $table => $table_status ) {
2013-04-27 02:11:42 +00:00
if ( is_view ( $table_status )) {
2007-07-17 07:23:17 +00:00
continue ;
}
2007-07-26 14:52:02 +00:00
$pos = 0 ;
2013-04-27 01:54:19 +00:00
$schema [ $table ][ " fields " ] = array ();
foreach ( fields ( $table ) as $name => $field ) {
2007-07-26 14:52:02 +00:00
$pos += 1.25 ;
$field [ " pos " ] = $pos ;
2013-04-27 01:54:19 +00:00
$schema [ $table ][ " fields " ][ $name ] = $field ;
2007-07-26 14:52:02 +00:00
}
2013-04-27 01:54:19 +00:00
$schema [ $table ][ " pos " ] = ( $table_pos [ $table ] ? $table_pos [ $table ] : array ( $top , 0 ));
foreach ( $adminer -> foreignKeys ( $table ) as $val ) {
2010-10-29 11:58:08 +00:00
if ( ! $val [ " db " ]) {
$left = $base_left ;
2013-04-27 01:54:19 +00:00
if ( $table_pos [ $table ][ 1 ] || $table_pos [ $val [ " table " ]][ 1 ]) {
$left = min ( floatval ( $table_pos [ $table ][ 1 ]), floatval ( $table_pos [ $val [ " table " ]][ 1 ])) - 1 ;
2010-10-29 11:58:08 +00:00
} else {
$base_left -= . 1 ;
2007-07-17 07:23:17 +00:00
}
2010-10-29 11:58:08 +00:00
while ( $lefts [( string ) $left ]) {
// find free $left
$left -= . 0001 ;
}
2013-04-27 01:54:19 +00:00
$schema [ $table ][ " references " ][ $val [ " table " ]][( string ) $left ] = array ( $val [ " source " ], $val [ " target " ]);
$referenced [ $val [ " table " ]][ $table ][( string ) $left ] = $val [ " target " ];
2010-10-29 11:58:08 +00:00
$lefts [( string ) $left ] = true ;
2007-07-17 07:23:17 +00:00
}
}
2013-04-27 01:54:19 +00:00
$top = max ( $top , $schema [ $table ][ " pos " ][ 0 ] + 2.5 + $pos );
2007-07-17 07:23:17 +00:00
}
2007-07-26 14:52:02 +00:00
2007-07-17 07:23:17 +00:00
?>
2018-01-12 15:51:45 +00:00
< div id = " schema " style = " height: <?php echo $top ; ?>em; " >
2018-01-13 21:17:00 +00:00
< script < ? php echo nonce (); ?> >
2018-01-12 15:51:45 +00:00
qs ( '#schema' ) . onselectstart = function () { return false ; };
2012-03-06 08:47:33 +00:00
var tablePos = { < ? php echo implode ( " , " , $table_pos_js ) . " \n " ; ?> };
2018-01-11 10:55:17 +00:00
var em = qs ( '#schema' ) . offsetHeight / < ? php echo $top ; ?> ;
2009-12-17 13:22:28 +00:00
document . onmousemove = schemaMousemove ;
2018-01-12 15:29:18 +00:00
document . onmouseup = partialArg ( schemaMouseup , '<?php echo js_escape(DB); ?>' );
2007-07-26 14:52:02 +00:00
</ script >
2007-07-17 07:23:17 +00:00
< ? php
2007-07-17 10:47:58 +00:00
foreach ( $schema as $name => $table ) {
2018-01-12 15:29:18 +00:00
echo " <div class='table' style='top: " . $table [ " pos " ][ 0 ] . " em; left: " . $table [ " pos " ][ 1 ] . " em;'> " ;
2012-03-06 08:46:54 +00:00
echo '<a href="' . h ( ME ) . 'table=' . urlencode ( $name ) . '"><b>' . h ( $name ) . " </b></a> " ;
2018-01-12 15:29:18 +00:00
echo script ( " qsl('div').onmousedown = schemaMousedown; " );
2013-05-02 01:28:04 +00:00
2007-07-26 14:52:02 +00:00
foreach ( $table [ " fields " ] as $field ) {
2009-08-25 15:42:43 +00:00
$val = '<span' . type_class ( $field [ " type " ]) . ' title="' . h ( $field [ " full_type " ] . ( $field [ " null " ] ? " NULL " : '' )) . '">' . h ( $field [ " field " ]) . '</span>' ;
2012-03-06 08:46:54 +00:00
echo " <br> " . ( $field [ " primary " ] ? " <i> $val </i> " : $val );
2007-07-26 14:52:02 +00:00
}
2013-05-02 01:28:04 +00:00
2007-07-26 14:52:02 +00:00
foreach (( array ) $table [ " references " ] as $target_name => $refs ) {
2008-11-01 20:38:06 +00:00
foreach ( $refs as $left => $ref ) {
2007-08-04 19:40:58 +00:00
$left1 = $left - $table_pos [ $name ][ 1 ];
2007-07-29 00:02:01 +00:00
$i = 0 ;
2008-11-01 20:38:06 +00:00
foreach ( $ref [ 0 ] as $source ) {
2012-03-06 08:46:54 +00:00
echo " \n <div class='references' title=' " . h ( $target_name ) . " ' id='refs $left - " . ( $i ++ ) . " ' style='left: $left1 " . " em; top: " . $table [ " fields " ][ $source ][ " pos " ] . " em; padding-top: .5em;'><div style='border-top: 1px solid Gray; width: " . ( - $left1 ) . " em;'></div></div> " ;
2007-07-26 14:52:02 +00:00
}
}
}
2013-05-02 01:28:04 +00:00
2007-07-28 22:36:07 +00:00
foreach (( array ) $referenced [ $name ] as $target_name => $refs ) {
foreach ( $refs as $left => $columns ) {
2007-08-04 19:40:58 +00:00
$left1 = $left - $table_pos [ $name ][ 1 ];
2007-07-29 00:02:01 +00:00
$i = 0 ;
2007-07-28 22:36:07 +00:00
foreach ( $columns as $target ) {
2012-03-06 08:46:54 +00:00
echo " \n <div class='references' title=' " . h ( $target_name ) . " ' id='refd $left - " . ( $i ++ ) . " ' style='left: $left1 " . " em; top: " . $table [ " fields " ][ $target ][ " pos " ] . " em; height: 1.25em; background: url(../adminer/static/arrow.gif) no-repeat right center;'><div style='height: .5em; border-bottom: 1px solid Gray; width: " . ( - $left1 ) . " em;'></div></div> " ;
2007-07-28 22:36:07 +00:00
}
2007-07-26 14:52:02 +00:00
}
2007-07-17 07:23:17 +00:00
}
2013-05-02 01:28:04 +00:00
2012-03-06 08:46:54 +00:00
echo " \n </div> \n " ;
2007-07-17 07:23:17 +00:00
}
2013-05-02 01:28:04 +00:00
2007-07-17 07:23:17 +00:00
foreach ( $schema as $name => $table ) {
2007-07-17 12:26:57 +00:00
foreach (( array ) $table [ " references " ] as $target_name => $refs ) {
2007-07-26 14:52:02 +00:00
foreach ( $refs as $left => $ref ) {
2007-07-17 10:47:58 +00:00
$min_pos = $top ;
2007-07-26 14:52:02 +00:00
$max_pos = - 10 ;
2008-11-01 20:38:06 +00:00
foreach ( $ref [ 0 ] as $key => $source ) {
2007-07-27 06:24:59 +00:00
$pos1 = $table [ " pos " ][ 0 ] + $table [ " fields " ][ $source ][ " pos " ];
2008-11-01 20:38:06 +00:00
$pos2 = $schema [ $target_name ][ " pos " ][ 0 ] + $schema [ $target_name ][ " fields " ][ $ref [ 1 ][ $key ]][ " pos " ];
2007-07-17 10:47:58 +00:00
$min_pos = min ( $min_pos , $pos1 , $pos2 );
$max_pos = max ( $max_pos , $pos1 , $pos2 );
}
2009-07-11 19:57:56 +00:00
echo " <div class='references' id='refl $left ' style='left: $left " . " em; top: $min_pos " . " em; padding: .5em 0;'><div style='border-right: 1px solid Gray; margin-top: 1px; height: " . ( $max_pos - $min_pos ) . " em;'></div></div> \n " ;
2007-07-17 10:47:58 +00:00
}
}
2007-07-17 07:23:17 +00:00
}
?>
</ div >
2013-07-09 23:44:00 +00:00
< p class = " links " >< a href = " <?php echo h(ME . " schema = " . urlencode( $SCHEMA )); ?> " id = " schema-link " >< ? php echo lang ( 'Permanent link' ); ?> </a>