code cleanup

This commit is contained in:
Marc Delisle
2009-12-14 18:01:16 +00:00
parent 35f9118a67
commit 8de4e7aec8

View File

@@ -490,13 +490,11 @@ function PMA_insertIntoTargetTable($matching_table, $src_db, $trg_db, $src_link,
{
if(isset($array_insert[$matching_table_index])) {
if (sizeof($array_insert[$matching_table_index])) {
for ($insert_row = 0; $insert_row< sizeof($array_insert[$matching_table_index]); $insert_row++)
{
for ($insert_row = 0; $insert_row< sizeof($array_insert[$matching_table_index]); $insert_row++) {
if (isset($array_insert[$matching_table_index][$insert_row][$matching_tables_keys[$matching_table_index][0]])) {
$select_query = "SELECT * FROM " . PMA_backquote($src_db) . "." . PMA_backquote($matching_table[$matching_table_index]) . " WHERE ";
for ($i=0; $i < sizeof($matching_tables_keys[$matching_table_index]); $i++)
{
for ($i = 0; $i < sizeof($matching_tables_keys[$matching_table_index]); $i++) {
$select_query .= $matching_tables_keys[$matching_table_index][$i] . "='";
$select_query .= $array_insert[$matching_table_index][$insert_row][$matching_tables_keys[$matching_table_index][$i]] . "'" ;
@@ -532,7 +530,6 @@ function PMA_insertIntoTargetTable($matching_table, $src_db, $trg_db, $src_link,
unset($uncommon_columns[$table_index[0]]);
}
if (isset($add_column_array[$table_index[0]])) {
PMA_findDeleteRowsFromTargetTables($delete_array, $matching_tables, $table_index[0], $target_tables_keys, $matching_tables_keys,
$trg_db, $trg_link, $src_db, $src_link);
@@ -547,7 +544,6 @@ function PMA_insertIntoTargetTable($matching_table, $src_db, $trg_db, $src_link,
}
if (isset($add_indexes_array[$table_index[0]]) || isset($remove_indexes_array[$table_index[0]])
|| isset($alter_indexes_array[$table_index[0]])) {
PMA_applyIndexesDiff ($trg_db, $trg_link, $matching_tables, $source_indexes, $target_indexes, $add_indexes_array, $alter_indexes_array,
$remove_indexes_array, $table_index[0], $display);
@@ -577,8 +573,7 @@ function PMA_insertIntoTargetTable($matching_table, $src_db, $trg_db, $src_link,
if (sizeof($table_fields[$matching_table_index]) == 1) {
$insert_query .= "'" . $result[0] . "'";
} else {
for($field_index=0; $field_index < sizeof($table_fields[$matching_table_index]); $field_index++)
{
for ($field_index = 0; $field_index < sizeof($table_fields[$matching_table_index]); $field_index++) {
if (isset($result[0][$table_fields[$matching_table_index][$field_index]])) {
$insert_query .= "'" . $result[0][$table_fields[$matching_table_index][$field_index]] . "'";
} else {
@@ -618,11 +613,9 @@ function PMA_insertIntoTargetTable($matching_table, $src_db, $trg_db, $src_link,
function PMA_createTargetTables($src_db, $trg_db, $src_link, $trg_link, &$uncommon_tables, $table_index, &$uncommon_tables_fields, $display)
{
if (isset($uncommon_tables[$table_index])) {
$fields_result = PMA_DBI_get_fields($src_db, $uncommon_tables[$table_index], $src_link);
$fields = array();
foreach ($fields_result as $each_field)
{
foreach ($fields_result as $each_field) {
$field_name = $each_field['Field'];
$fields[] = $field_name;
}
@@ -715,21 +708,15 @@ function PMA_populateTargetTables($src_db, $trg_db, $src_link, $trg_link, $uncom
*/
function PMA_deleteFromTargetTable($trg_db, $trg_link, $matching_tables, $table_index, $target_tables_keys, $delete_array, $display)
{
for($i=0; $i < sizeof($delete_array[$table_index]); $i++)
{
$delete_query;
for($i = 0; $i < sizeof($delete_array[$table_index]); $i++) {
if (isset($target_tables_keys[$table_index])) {
$delete_query = 'DELETE FROM ' . PMA_backquote($trg_db) . '.' .PMA_backquote($matching_tables[$table_index]) . ' WHERE ';
for($y=0; $y < sizeof($target_tables_keys[$table_index]) ;$y++)
{
for($y = 0; $y < sizeof($target_tables_keys[$table_index]); $y++) {
$delete_query .= $target_tables_keys[$table_index][$y] . " = '";
if (sizeof($target_tables_keys[$table_index]) == 1) {
$delete_query .= $delete_array[$table_index][$i] . "'";
} elseif (sizeof($target_tables_keys[$table_index]) > 1) {
$delete_query .= $delete_array[$table_index][$i][$target_tables_keys[$table_index][$y]] . "'";
}
if ($y < (sizeof($target_tables_keys[$table_index]) - 1)) {
@@ -743,10 +730,8 @@ function PMA_deleteFromTargetTable($trg_db, $trg_link, $matching_tables, $table_
$result_size = sizeof($pk_query_result);
if ($result_size > 0) {
for ($b=0; $b < $result_size; $b++)
{
$drop_pk_query = "DELETE FROM " . PMA_backquote($pk_query_result[$b]['TABLE_SCHEMA']) .".".PMA_backquote($pk_query_result[$b]['TABLE_NAME'])."
WHERE " . $pk_query_result[$b]['COLUMN_NAME']." = ".$target_tables_keys[$table_index][$y].";";
for ($b = 0; $b < $result_size; $b++) {
$drop_pk_query = "DELETE FROM " . PMA_backquote($pk_query_result[$b]['TABLE_SCHEMA']) . "." . PMA_backquote($pk_query_result[$b]['TABLE_NAME']) . " WHERE " . $pk_query_result[$b]['COLUMN_NAME'] . " = " . $target_tables_keys[$table_index][$y] . ";";
PMA_DBI_try_query($drop_pk_query, $trg_link, 0);
}
}
@@ -760,10 +745,10 @@ function PMA_deleteFromTargetTable($trg_db, $trg_link, $matching_tables, $table_
}
/**
* PMA_structureDiffInTables() Gets all the column information for source and target table.
* Compare columns on the basics of their names.
* If column exist in target then compare Type, Null, Collation, Key, Default and Comment for that column.
* Compare columns on their names.
* If column exists in target then compare Type, Null, Collation, Key, Default and Comment for that column.
* If column does not exist in target table then it is placed in $add_column_array.
* If column exist in target table but criteria is different then it is palced in $alter_str_array.
* If column exists in target table but criteria is different then it is palced in $alter_str_array.
* If column does not exist in source table but is present in target table then it is placed in $uncommon_columns.
* Keys for all the source tables that have a corresponding target table are placed in $matching_tables_keys.
* Keys for all the target tables that have a corresponding source table are placed in $target_tables_keys.
@@ -794,12 +779,10 @@ function PMA_structureDiffInTables($src_db, $trg_db, $src_link, $trg_link, $matc
//Gets column information for source and target table
$source_columns[$matching_table_index] = PMA_DBI_get_columns_full($src_db, $matching_tables[$matching_table_index], null, $src_link);
$target_columns[$matching_table_index] = PMA_DBI_get_columns_full($trg_db, $matching_tables[$matching_table_index], null, $trg_link);
foreach ($source_columns[$matching_table_index] as $column_name => $each_column)
{
foreach ($source_columns[$matching_table_index] as $column_name => $each_column) {
if (isset($target_columns[$matching_table_index][$column_name]['Field'])) {
//If column exist in target table then matches criterias like type, null, collation, key, default, comment of the column
for ($i=0; $i<sizeof($criteria); $i++)
{
//If column exists in target table then matches criterias like type, null, collation, key, default, comment of the column
for ($i = 0; $i < sizeof($criteria); $i++) {
if ($source_columns[$matching_table_index][$column_name][$criteria[$i]] != $target_columns[$matching_table_index][$column_name][$criteria[$i]]) {
if (($criteria[$i] == 'Default') && ($source_columns[$matching_table_index][$column_name][$criteria[$i]] == '' )) {
$alter_str_array[$matching_table_index][$column_name][$criteria[$i]] = 'None';
@@ -818,8 +801,7 @@ function PMA_structureDiffInTables($src_db, $trg_db, $src_link, $trg_link, $matc
}
}
//Finds column names that are present in target table but not in source table
foreach ($target_columns[$matching_table_index] as $fld_name => $each_column)
{
foreach ($target_columns[$matching_table_index] as $fld_name => $each_column) {
if (! (isset($source_columns[$matching_table_index][$fld_name]['Field']))) {
$fields_uncommon[] = $fld_name;
}
@@ -864,10 +846,8 @@ function PMA_structureDiffInTables($src_db, $trg_db, $src_link, $trg_link, $matc
function PMA_addColumnsInTargetTable($src_db, $trg_db, $src_link, $trg_link, $matching_tables, $source_columns, &$add_column_array, $matching_tables_fields,
$criteria, $matching_tables_keys, $target_tables_keys, $uncommon_tables, &$uncommon_tables_fields, $table_counter, $uncommon_cols, $display)
{
for ($i = 0; $i<sizeof($matching_tables_fields[$table_counter]); $i++)
{
for ($i = 0; $i < sizeof($matching_tables_fields[$table_counter]); $i++) {
if (isset($add_column_array[$table_counter][$matching_tables_fields[$table_counter][$i]])) {
$query = "ALTER TABLE " . PMA_backquote($trg_db) . '.' . PMA_backquote($matching_tables[$table_counter]). " ADD COLUMN " .
$add_column_array[$table_counter][$matching_tables_fields[$table_counter][$i]] . " " . $source_columns[$table_counter][$matching_tables_fields[$table_counter][$i]]['Type'];
@@ -886,14 +866,12 @@ function PMA_addColumnsInTargetTable($src_db, $trg_db, $src_link, $trg_link, $ma
$query .= " COMMENT " . $source_columns[$table_counter][$matching_tables_fields[$table_counter][$i]]['Comment'];
}
if ($source_columns[$table_counter][$matching_tables_fields[$table_counter][$i]]['Key'] == 'PRI' ) {
$trg_key_size = sizeof($target_tables_keys[$table_counter]);
if ($trg_key_size) {
$check = 1;
for ($a=0; ($a < $trg_key_size) && ($check); $a++ )
{
$check = true;
for ($a = 0; ($a < $trg_key_size) && ($check); $a++) {
if (! (in_array($target_tables_keys[$table_counter], $uncommon_cols))) {
$check = 0;
$check = false;
}
}
if (! $check) {
@@ -901,8 +879,7 @@ function PMA_addColumnsInTargetTable($src_db, $trg_db, $src_link, $trg_link, $ma
}
}
$query .= " , ADD PRIMARY KEY (";
for ($t = 0; $t< sizeof($matching_tables_keys[$table_counter]); $t++)
{
for ($t = 0; $t < sizeof($matching_tables_keys[$table_counter]); $t++) {
$query .= $matching_tables_keys[$table_counter][$t];
if ($t < (sizeof($matching_tables_keys[$table_counter]) - 1)) {
$query .= " , " ;
@@ -967,8 +944,7 @@ function PMA_checkForeignKeys($src_db, $src_link, $trg_db, $trg_link ,$reference
$is_fk_result = PMA_DBI_fetch_result($is_fk_query, null, null, $src_link);
if (sizeof($is_fk_result) > 0) {
for ($j = 0; $j < sizeof($is_fk_result); $j++)
{
for ($j = 0; $j < sizeof($is_fk_result); $j++) {
if (in_array($is_fk_result[$j]['REFERENCED_TABLE_NAME'], $uncommon_tables)) {
$table_index = array_keys($uncommon_tables, $is_fk_result[$j]['REFERENCED_TABLE_NAME']);
PMA_checkForeignKeys($src_db, $src_link, $trg_db, $trg_link, $is_fk_result[$j]['REFERENCED_TABLE_NAME'], $uncommon_tables,
@@ -1008,8 +984,7 @@ function PMA_alterTargetTableStructure($trg_db, $trg_link, $matching_tables, &$s
$found = false;
//Checks if the criteria to be altered is primary key
for($v=0; $v < sizeof($matching_tables_fields[$matching_table_index]); $v++)
{
for ($v = 0; $v < sizeof($matching_tables_fields[$matching_table_index]); $v++) {
if (isset($alter_str_array[$matching_table_index][$matching_tables_fields[$matching_table_index][$v]]['Key'])) {
if ($alter_str_array[$matching_table_index][$matching_tables_fields[$matching_table_index][$v]]['Key'] == 'PRI' ) {
$check = false;
@@ -1017,14 +992,13 @@ function PMA_alterTargetTableStructure($trg_db, $trg_link, $matching_tables, &$s
}
}
$pri_query;
if (!($check)) {
if (! $check) {
$pri_query = "ALTER TABLE " . PMA_backquote($trg_db) . '.' . PMA_backquote($matching_tables[$matching_table_index]);
if (sizeof($target_tables_keys[$matching_table_index]) > 0) {
$pri_query .= " DROP PRIMARY KEY ," ;
}
$pri_query .= " ADD PRIMARY KEY (";
for ($z = 0; $z< sizeof($matching_tables_keys[$matching_table_index]); $z++)
{
for ($z = 0; $z < sizeof($matching_tables_keys[$matching_table_index]); $z++) {
$pri_query .= $matching_tables_keys[$matching_table_index][$z];
if ($z < (sizeof($matching_tables_keys[$matching_table_index]) - 1)) {
$pri_query .= " , " ;
@@ -1039,8 +1013,7 @@ function PMA_alterTargetTableStructure($trg_db, $trg_link, $matching_tables, &$s
}
PMA_DBI_try_query($pri_query, $trg_link, 0);
}
for ($t = 0; $t < sizeof($matching_tables_fields[$matching_table_index]); $t++)
{
for ($t = 0; $t < sizeof($matching_tables_fields[$matching_table_index]); $t++) {
if ((isset($alter_str_array[$matching_table_index][$matching_tables_fields[$matching_table_index][$t]])) && (sizeof($alter_str_array[$matching_table_index][$matching_tables_fields[$matching_table_index][$t]]) > 0)) {
$sql_query = 'ALTER TABLE ' . PMA_backquote($trg_db) . '.' . PMA_backquote($matching_tables[$matching_table_index]) . ' MODIFY ' .
$matching_tables_fields[$matching_table_index][$t] . ' ' . $source_columns[$matching_table_index][$matching_tables_fields[$matching_table_index][$t]]['Type'];
@@ -1104,8 +1077,7 @@ function PMA_alterTargetTableStructure($trg_db, $trg_link, $matching_tables, &$s
}
$check = false;
$query = "ALTER TABLE " . PMA_backquote($trg_db) . '.' . PMA_backquote($matching_tables[$matching_table_index]);
for($p=0; $p< sizeof($matching_tables_keys[$matching_table_index]); $p++)
{
for($p = 0; $p < sizeof($matching_tables_keys[$matching_table_index]); $p++) {
if ((isset($alter_str_array[$matching_table_index][$matching_tables_keys[$matching_table_index][$p]]['Key']))) {
$check = true;
$query .= ' MODIFY ' . $matching_tables_keys[$matching_table_index][$p] . ' '
@@ -1140,8 +1112,7 @@ function PMA_removeColumnsFromTargetTable($trg_db, $trg_link, $matching_tables,
{
if (isset($uncommon_columns[$table_counter])) {
$drop_query = "ALTER TABLE " . PMA_backquote($trg_db) . "." . PMA_backquote($matching_tables[$table_counter]);
for ($a = 0; $a < sizeof($uncommon_columns[$table_counter]); $a++)
{
for ($a = 0; $a < sizeof($uncommon_columns[$table_counter]); $a++) {
//Checks if column to be removed is a foreign key in any table
$pk_query = "SELECT * FROM information_schema.KEY_COLUMN_USAGE WHERE REFERENCED_TABLE_SCHEMA = '" . $trg_db . "'
AND REFERENCED_TABLE_NAME = '" . $matching_tables[$table_counter]."' AND REFERENCED_COLUMN_NAME = '"
@@ -1151,8 +1122,7 @@ function PMA_removeColumnsFromTargetTable($trg_db, $trg_link, $matching_tables,
$result_size = sizeof($pk_query_result);
if ($result_size > 0) {
for ($b=0; $b< $result_size; $b++)
{
for ($b = 0; $b < $result_size; $b++) {
$drop_pk_query = "ALTER TABLE " . PMA_backquote($pk_query_result[$b]['TABLE_SCHEMA']) . "." . PMA_backquote($pk_query_result[$b]['TABLE_NAME']) . "
DROP FOREIGN KEY " . $pk_query_result[$b]['CONSTRAINT_NAME'] . ", DROP COLUMN " . $pk_query_result[$b]['COLUMN_NAME'] . ";";
PMA_DBI_try_query($drop_pk_query, $trg_link, 0);
@@ -1205,8 +1175,7 @@ function PMA_indexesDiffInTables($src_db, $trg_db, $src_link, $trg_link, $matchi
//Gets indexes information for source and target table
$source_indexes[$table_counter] = PMA_DBI_get_table_indexes($src_db, $matching_tables[$table_counter],$src_link);
$target_indexes[$table_counter] = PMA_DBI_get_table_indexes($trg_db, $matching_tables[$table_counter],$trg_link);
for ($a = 0; $a < sizeof($source_indexes[$table_counter]); $a++)
{
for ($a = 0; $a < sizeof($source_indexes[$table_counter]); $a++) {
$found = false;
$z = 0;
//Compares key name and non_unique characteristic of source indexes with target indexes
@@ -1233,8 +1202,7 @@ function PMA_indexesDiffInTables($src_db, $trg_db, $src_link, $trg_link, $matchi
}
//Finds indexes that exist on target table but not on source table
for ($b = 0; $b < sizeof($target_indexes[$table_counter]); $b++)
{
for ($b = 0; $b < sizeof($target_indexes[$table_counter]); $b++) {
$found = false;
$c = 0;
while (($c <= sizeof($source_indexes[$table_counter])) && ($found == false))
@@ -1272,11 +1240,9 @@ function PMA_applyIndexesDiff ($trg_db, $trg_link, $matching_tables, $source_ind
//Adds indexes on target table
if (isset($add_indexes_array[$table_counter])) {
$sql = "ALTER TABLE " . PMA_backquote($trg_db) . "." . PMA_backquote($matching_tables[$table_counter]) . " ADD" ;
for ($a = 0; $a < sizeof($source_indexes[$table_counter]); $a++)
{
for ($a = 0; $a < sizeof($source_indexes[$table_counter]); $a++) {
if (isset($add_indexes_array[$table_counter][$a])) {
for ($b = 0; $b < sizeof($source_indexes[$table_counter]); $b++)
{
for ($b = 0; $b < sizeof($source_indexes[$table_counter]); $b++) {
if ($source_indexes[$table_counter][$b]['Column_name'] == $add_indexes_array[$table_counter][$a]) {
if ($source_indexes[$table_counter][$b]['Non_unique'] == '0') {
$sql .= " UNIQUE ";
@@ -1324,8 +1290,7 @@ function PMA_applyIndexesDiff ($trg_db, $trg_link, $matching_tables, $source_ind
//Removes indexes from target table
if (isset($remove_indexes_array[$table_counter])) {
$drop_index_query = "ALTER TABLE " . PMA_backquote($trg_db) . "." . PMA_backquote($matching_tables[$table_counter]);
for ($a = 0; $a < sizeof($target_indexes[$table_counter]); $a++)
{
for ($a = 0; $a < sizeof($target_indexes[$table_counter]); $a++) {
if (isset($remove_indexes_array[$table_counter][$a])) {
$drop_index_query .= " DROP INDEX " . $remove_indexes_array[$table_counter][$a];
}