<?php
/* Example tables to diff


#
# Table structure for table `table1`
#

CREATE TABLE table1 (
  tbl_id int(10) unsigned NOT NULL auto_increment,
  str varchar(100) NOT NULL default '',
  params enum('y','n','k') NOT NULL default 'n',
  newfield varchar(10) NOT NULL default '',
  PRIMARY KEY  (tbl_id)
) TYPE=MyISAM;
# --------------------------------------------------------

#
# Table structure for table `table2`
#

CREATE TABLE table2 (
  tbl_id int(10) unsigned NOT NULL auto_increment,
  str varchar(10) NOT NULL default '',
  params enum('y','n') NOT NULL default 'n',
  PRIMARY KEY  (tbl_id)
) TYPE=MyISAM;



*/







/**
 * Gets some core libraries
 */
require('./libraries/grab_globals.lib.php');
$sub_part  = '_export';
require('./db_details_common.php');
$url_query .= '&amp;goto=db_details_export.php';
require('./db_details_db_info.php');

$js_to_run = 'functions.js';
require('./libraries/sqlparser.lib.php');
require('./libraries/build_dump.lib.php');

?>
<h1>Diff tables</h1><br>


<FORM METHOD=POST ACTION="">
<pre>
<?
#echo "<pre>";
#var_dump($GLOBALS);
#echo "</pre>";


function PMA_table_struct($db, $table){

	$struct = array();


	$res = PMA_mysql_query('DESCRIBE '.$db.'.'.$table);
	while( $row = PMA_mysql_fetch_array($res) ){

		$struct[$row['Field']] = array(
								'type'	=> $row['Type'],
								'null'	=> $row['Null'],
								'key'	=> $row['Key'],
								'default'=> $row['Default'],
								'extra'	=> $row['Extra']
								);



	}

	return $struct;

}

function PMA_diff_length($type){

	/* check length */
	if( eregi('\(([0-9]*)\)', $type, $regs) )
		$length = $regs[1];
	else
		$length = 0;

	return $length;
}

function PMA_diff_type($type){
	return $type;

	/* check type */
	if( ereg('^([^(]*)', $type,  $regs) )
		$type = $regs[1];
	else
		$type = '';

	$type = trim($type);

	return $type;


}

function PMA_alter_string($field_name, $struct, $alter_type, $first_row=false){
#	echo $field_name."\n";
#	var_dump($struct);

	$alter = '';
	if( !$first_row )
		$alter.=','.chr(10);
	$alter.=chr(9).$alter_type.' ';
	$alter.=$field_name.' ';

	if( $alter_type!='DROP' ){
		if( $alter_type=='CHANGE' )
			$alter.=' '.$field_name.' ';

		$alter.=$struct['type'];
		$alter.=' '.$struct['null'];
		if( $struct['default']!="" )
			$alter.=' DEFAULT "'.$struct['default'].'" ';
	}



	return $alter;
}


function PMA_diff_table_structs($original_table_struct, $target_table_struct, $target_table){

	if( is_array($original_table_struct) )
		reset($original_table_struct);
	if( is_array($target_table_struct) )
		reset($target_table_struct);


	$removed_fields = array();
	$added_fields = array();
	$modified_fields = array();

	while( list($field, $field_def)=each($original_table_struct) ){
#		echo $field_def['type']." - type:".PMA_diff_type($field_def['type'])."\n";
#		echo "length:".PMA_diff_length($field_def['type'])."\n";

		if( isset($target_table_struct[$field]) ){
			$mod = array();			
			$target_field_def = $target_table_struct[$field];
			$type = PMA_diff_type($field_def['type']);
			$length = PMA_diff_length($field_def['type']);
			$target_type = PMA_diff_type($target_field_def['type']);
			$target_length = PMA_diff_length($target_field_def['type']);

			if( $type!=$target_type )
				$mod[] = 'type';
			if( $length!=$target_length )
				$mod[] = 'length - '.$length.' - '.$target_length;
			if( $field_def['null']!=$target_field_def['null'] )
				$mod[] = 'null';
			if( $field_def['key']!=$target_field_def['key'] )
				$mod[] = 'key';
			if( $field_def['default']!=$target_field_def['default'] )
				$mod[] = 'default';
			if( $field_def['extra']!=$target_field_def['extra'] )
				$mod[] = 'extra';
				
			if( sizeof($mod)>0 )
				$modified_fields[$field] = $mod;
			

		}else{
			$added_fields[] = $field;
		}

	}

	while( list($field, $field_def)=each($target_table_struct) ){
		if( !isset($original_table_struct[$field]) )
			$removed_fields[] = $field;
	}

#	echo "\nAdded fields:\n";
#	var_dump($added_fields);
#	echo "\nRemoved fields:\n";
#	var_dump($removed_fields);
#	echo "\nModified fields:\n";
#	var_dump($modified_fields);

	if( sizeof($added_fields)>0 || sizeof($removed_fields)>0 || sizeof($modified_fields)>0 ){
		$alter_query = 'ALTER TABLE '.$target_table.' '.chr(10);

		$first_row = true;
		while( list(, $name)=each($added_fields) ){
			if( isset($original_table_struct[$name]) ){
				$alter_query.=PMA_alter_string($name, $original_table_struct[$name], 'ADD', $first_row);
				$last_row_count++;
			}
			$first_row = false;
		}

		while( list($name, $mod)=each($modified_fields) ){
			if( isset($original_table_struct[$name]) ){
				$alter_query.=PMA_alter_string($name, $original_table_struct[$name], 'CHANGE', $first_row);
				$last_row_count++;
			}
			$first_row = false;
		}

		while( list(, $name)=each($removed_fields) ){
			$alter_query.=PMA_alter_string($name, $original_table_struct[$name], 'DROP', $first_row);
			$last_row_count++;
			$first_row = false;
		}

		$alter_query.=';'.chr(10);
	}else{
		$alter_query = 'No diff';
	}

	return $alter_query;
}




if( isset($HTTP_POST_VARS['original_table']) && isset($HTTP_POST_VARS['target_table']) ){
$crlf        = PMA_whichCrlf();

$original_table = $HTTP_POST_VARS['original_table'];
$target_table = $HTTP_POST_VARS['target_table'];
#echo "diffing ".$original_table." to ".$target_table."....<br>";
#echo "\n";
#echo "Struct for ".$original_table."\n\n";
	$original_table_struct = PMA_getTableDef($db, $original_table, "\n", $err_url);
#	echo $original_table_struct;
#echo "\n\nInfo\n";
$original_table_info = PMA_SQP_analyze(array($original_table_struct));
#var_dump($original_table_info);
#echo "\n\nBuilding struct...\n\n";
$original_table_arr_struct = PMA_table_struct($db, $original_table);
#var_dump($original_table_arr_struct);




#echo "\n\n\n";
#echo "Struct for ".$target_table."\n\n";
#echo PMA_getTableDef($db, $target_table, $crlf, $err_url);
#echo "\n\nBuilding struct...\n\n";
$target_table_arr_struct = PMA_table_struct($db, $target_table);
#var_dump($target_table_arr_struct);

#echo "\n\n\nDiff structs...\n\n\n";
?>
<TABLE cellpadding="0" cellspacing="0" border="0">
<TR>
	<TD valign="top">
	<?echo '<b>'.$original_table.'</b>';?>
<table border="<?php echo $cfg['Border']; ?>">
<tr>
    <td></td>
    <th>&nbsp;<?php echo ucfirst($strField); ?>&nbsp;</th>
    <th><?php echo ucfirst($strType); ?></th>
    <th><?php echo ucfirst($strAttr); ?></th>
    <th><?php echo ucfirst($strNull); ?></th>
    <th><?php echo ucfirst($strDefault); ?></th>
    <th><?php echo ucfirst($strExtra); ?></th>
</tr>
<?
$i=0;
while( list($field, $field_def)=each($original_table_arr_struct) ){
?>
<tr bgcolor="<? echo     (($i % 2) ? $cfg['BgcolorOne'] : $cfg['BgcolorTwo']);?>">
    <td></td>
    <td>&nbsp;<?php echo $field; ?>&nbsp;</td>
    <td><?php echo $field_def['type']; ?></td>
    <td>&nbsp;</td>
    <td><?php echo $field_def['null']; ?></td>
    <td><?php echo $field_def['default']; ?></td>
    <td><?php echo $field_def['extra']; ?></td>
</tr>

<?
$i++;
}
?>
</table>
	



</TD>
<TD align="right" valign="top">
	<?echo '<b>'.$target_table.'</b>';?>
<table border="<?php echo $cfg['Border']; ?>">
<tr>
    <td></td>
    <th>&nbsp;<?php echo ucfirst($strField); ?>&nbsp;</th>
    <th><?php echo ucfirst($strType); ?></th>
    <th><?php echo ucfirst($strAttr); ?></th>
    <th><?php echo ucfirst($strNull); ?></th>
    <th><?php echo ucfirst($strDefault); ?></th>
    <th><?php echo ucfirst($strExtra); ?></th>
</tr>
<? 
$i=0;
while( list($field, $field_def)=each($target_table_arr_struct) ){
?>
<tr bgcolor="<? echo     (($i % 2) ? $cfg['BgcolorOne'] : $cfg['BgcolorTwo']);?>">
    <td></td>
    <td>&nbsp;<?php echo $field; ?>&nbsp;</td>
    <td><?php echo $field_def['type']; ?></td>
    <td>&nbsp;</td>
    <td><?php echo $field_def['null']; ?></td>
    <td><?php echo $field_def['default']; ?></td>
    <td><?php echo $field_def['extra']; ?></td>
</tr>
<?
$i++;
}
?>
</table>

</TD>
</TR>
<tr>
<td bgcolor="<?echo $cfg['BgcolorOne'];?>" colspan="2">
<b>Alter query:</b><br>
<pre>
<?
$alter_query = PMA_diff_table_structs($original_table_arr_struct, $target_table_arr_struct, $target_table);

echo $alter_query;

?>
</pre>
<br>
<br>
</td>
</tr>
</TABLE>



<?



}


?>
</pre>
<br><br><br><br><br><br><br><br>
<TABLE>
<TR>
	<TD>
original table<br>
<SELECT NAME="original_table">
<?
$i=0;
while( $i<$num_tables ){
	echo '<option value="'.$tables[$i]['Name'].'">'.$tables[$i]['Name'].'</option>'.chr(10);
	$i++;
}

?>
</SELECT>
	
	
	</TD>
	<TD>
target table<br>
<SELECT NAME="target_table">
<?
$i=0;
while( $i<$num_tables ){
	echo '<option value="'.$tables[$i]['Name'].'">'.$tables[$i]['Name'].'</option>'.chr(10);
	$i++;
}

?>
</SELECT>
	
	
	
	
	</TD>
</TR>
</TABLE>
<br>
<br>
<INPUT TYPE="submit" value="Diff">


</FORM>




<?




require('./footer.inc.php');

?>