Fix CodeGen export
[phpmyadmin.git] / libraries / server_synchronize.lib.php
blob54f15bdbf3fa8b6570bbea3331de0b42d602a76c
1 <?php
2 /* vim: set expandtab sw=4 ts=4 sts=4: */
4 /**
5 * PMA_getMatchingTables places matching tables in source
6 * and target databases in $matching_tables array whereas
7 * $uncommon_source_tables array gets the tables present in
8 * source database but are absent from target database.
9 * Criterion for matching tables is just comparing their names.
11 * @param $trg_tables array of target database table names,
12 * @param $src_tables array of source database table names,
14 * @param &$matching_tables empty array passed by reference to save names of matching tables,
15 * @param &$uncommon_source_tables empty array passed by reference to save names of tables present in
16 * source database but absent from target database
18 function PMA_getMatchingTables($trg_tables, $src_tables, &$matching_tables, &$uncommon_source_tables)
20 for($k=0; $k< sizeof($src_tables); $k++) {
21 $present_in_target = false;
22 for($l=0; $l < sizeof($trg_tables); $l++) {
23 if ($src_tables[$k] === $trg_tables[$l]) {
24 $present_in_target = true;
25 $matching_tables[] = $src_tables[$k];
28 if ($present_in_target === false) {
29 $uncommon_source_tables[] = $src_tables[$k];
34 /**
35 * PMA_getNonMatchingTargetTables() places tables present
36 * in target database but are absent from source database
38 * @param $trg_tables array of target database table names,
40 * @param $matching_tables $matching tables array containing names of matching tables,
41 * @param &$uncommon_target_tables empty array passed by reference to save names of tables presnet in
42 * target database but absent from source database
44 function PMA_getNonMatchingTargetTables($trg_tables, $matching_tables, &$uncommon_target_tables)
46 for($c=0; $c<sizeof($trg_tables) ;$c++) {
47 $match = false;
48 for($d=0; $d < sizeof($matching_tables); $d++)
50 if ($trg_tables[$c] === $matching_tables[$d]) {
51 $match=true;
54 if ($match === false) {
55 $uncommon_target_tables[] = $trg_tables[$c];
60 /**
61 * PMA_dataDiffInTables() finds the difference in source and target matching tables by
62 * first comparing source table's primary key entries with target table enteries.
63 * It gets the field names for the matching table also for comparisons.
64 * If the entry is found in target table also then it is checked for the remaining
65 * field values also, in order to check whether update is required or not.
66 * If update is required, it is placed in $update_array
67 * Otherwise that entry is placed in the $insert_array.
70 * @param $src_db name of source database
71 * @param $trg_db name of target database
72 * @param $src_link connection established with source server
73 * @param $trg_link connection established with target server
74 * @param $matching_table array containing matching table names
75 * @param $matching_tables_fields A two dimensional array passed by reference to contain names of fields for each matching table
76 * @param $update_array A three dimensional array passed by reference to
77 * contain updates required for each matching table
78 * @param $insert_array A three dimensional array passed by reference to
79 * contain inserts required for each matching table
80 * @param $delete_array Unused
81 * @param $fields_num A two dimensional array passed by reference to
82 * contain number of fields for each matching table
83 * @param $matching_table_index Index of a table from $matching_table array
84 * @param $matching_tables_keys A two dimensional array passed by reference to contain names of keys for each matching table
86 function PMA_dataDiffInTables($src_db, $trg_db, $src_link, $trg_link, &$matching_table, &$matching_tables_fields,
87 &$update_array, &$insert_array, &$delete_array, &$fields_num, $matching_table_index, &$matching_tables_keys)
89 if (isset($matching_table[$matching_table_index])) {
90 $fld = array();
91 $fld_results = PMA_DBI_get_columns($src_db, $matching_table[$matching_table_index], true, $src_link);
92 $is_key = array();
93 if (isset($fld_results)) {
94 foreach ($fld_results as $each_field) {
95 $field_name = $each_field['Field'];
96 if ($each_field['Key'] == 'PRI') {
97 $is_key[] = $field_name;
99 $fld[] = $field_name;
102 $matching_tables_fields[$matching_table_index] = $fld;
103 $fields_num[$matching_table_index] = sizeof($fld);
104 $matching_tables_keys[$matching_table_index] = $is_key;
106 $source_result_set = PMA_get_column_values($src_db, $matching_table[$matching_table_index], $is_key, $src_link);
107 $source_size = sizeof($source_result_set);
109 $trg_fld_results = PMA_DBI_get_columns($trg_db, $matching_table[$matching_table_index], true, $trg_link);
110 $all_keys_match = true;
111 $trg_keys = array();
113 if (isset($trg_fld_results)) {
114 foreach ($trg_fld_results as $each_field) {
115 if ($each_field['Key'] == 'PRI') {
116 $trg_keys[] = $each_field['Field'];
117 if (! (in_array($each_field['Field'], $is_key))) {
118 $all_keys_match = false;
123 $update_row = 0;
124 $insert_row = 0;
125 $update_field = 0;
126 $insert_field = 0;
127 $starting_index = 0;
129 for ($j = 0; $j < $source_size; $j++) {
130 $starting_index = 0;
131 $update_field = 0;
133 if (isset($source_result_set[$j]) && ($all_keys_match)) {
135 // Query the target server to see which rows already exist
136 $trg_select_query = "SELECT * FROM " . PMA_backquote($trg_db) . "."
137 . PMA_backquote($matching_table[$matching_table_index]) . " WHERE ";
139 if (sizeof($is_key) == 1) {
140 $trg_select_query .= $is_key[0]. "='" . $source_result_set[$j] . "'";
141 } elseif (sizeof($is_key) > 1){
142 for ($k=0; $k < sizeof($is_key); $k++) {
143 $trg_select_query .= $is_key[$k] . "='" . $source_result_set[$j][$is_key[$k]] . "'";
144 if ($k < (sizeof($is_key)-1)){
145 $trg_select_query .= " AND ";
150 $target_result_set = PMA_DBI_fetch_result($trg_select_query, null, null, $trg_link);
151 if ($target_result_set) {
153 // Fetch the row from the source server to do a comparison
154 $src_select_query = "SELECT * FROM " . PMA_backquote($src_db) . "."
155 . PMA_backquote($matching_table[$matching_table_index]) . " WHERE ";
157 if (sizeof($is_key) == 1) {
158 $src_select_query .= $is_key[0] . "='" . $source_result_set[$j] . "'";
159 } else if(sizeof($is_key) > 1){
160 for ($k=0; $k< sizeof($is_key); $k++) {
161 $src_select_query .= $is_key[$k] . "='" . $source_result_set[$j][$is_key[$k]] . "'";
162 if ($k < (sizeof($is_key) - 1)){
163 $src_select_query .= " AND ";
168 $src_result_set = PMA_DBI_fetch_result($src_select_query, null, null, $src_link);
171 * Comparing each corresponding field of the source and target matching rows.
172 * Placing the primary key, value of primary key, field to be updated, and the
173 * new value of field to be updated in each row of the update array.
175 for ($m = 0; ($m < $fields_num[$matching_table_index]) && ($starting_index == 0) ; $m++) {
176 if (isset($src_result_set[0][$fld[$m]])) {
177 if (isset($target_result_set[0][$fld[$m]])) {
178 if (($src_result_set[0][$fld[$m]] != $target_result_set[0][$fld[$m]]) && (! (in_array($fld[$m], $is_key)))) {
179 if (sizeof($is_key) == 1) {
180 if ($source_result_set[$j]) {
181 $update_array[$matching_table_index][$update_row][$is_key[0]] = $source_result_set[$j];
183 } elseif (sizeof($is_key) > 1) {
184 for ($n=0; $n < sizeof($is_key); $n++) {
185 if (isset($src_result_set[0][$is_key[$n]])) {
186 $update_array[$matching_table_index][$update_row][$is_key[$n]] = $src_result_set[0][$is_key[$n]];
191 $update_array[$matching_table_index][$update_row][$update_field] = $fld[$m];
193 $update_field++;
194 if (isset($src_result_set[0][$fld[$m]])) {
195 $update_array[$matching_table_index][$update_row][$update_field] = $src_result_set[0][$fld[$m]];
196 $update_field++;
198 $starting_index = $m;
199 $update_row++;
201 } else {
202 if (sizeof($is_key) == 1) {
203 if ($source_result_set[$j]) {
204 $update_array[$matching_table_index][$update_row][$is_key[0]] = $source_result_set[$j];
207 } elseif (sizeof($is_key) > 1) {
208 for ($n = 0; $n < sizeof($is_key); $n++) {
209 if (isset($src_result_set[0][$is_key[$n]])) {
210 $update_array[$matching_table_index][$update_row][$is_key[$n]] = $src_result_set[0][$is_key[$n]];
215 $update_array[$matching_table_index][$update_row][$update_field] = $fld[$m];
217 $update_field++;
218 if (isset($src_result_set[0][$fld[$m]])) {
219 $update_array[$matching_table_index][$update_row][$update_field] = $src_result_set[0][$fld[$m]];
220 $update_field++;
222 $starting_index = $m;
223 $update_row++;
227 for ($m = $starting_index + 1; $m < $fields_num[$matching_table_index] ; $m++)
229 if (isset($src_result_set[0][$fld[$m]])) {
230 if (isset($target_result_set[0][$fld[$m]])) {
231 if (($src_result_set[0][$fld[$m]] != $target_result_set[0][$fld[$m]]) && (!(in_array($fld[$m], $is_key)))) {
232 $update_row--;
233 $update_array[$matching_table_index][$update_row][$update_field] = $fld[$m];
234 $update_field++;
235 if ($src_result_set[0][$fld[$m]]) {
236 $update_array[$matching_table_index][$update_row][$update_field] = $src_result_set[0][$fld[$m]];
237 $update_field++;
239 $update_row++;
241 } else {
242 $update_row--;
243 $update_array[$matching_table_index][$update_row][$update_field] = $fld[$m];
244 $update_field++;
245 if ($src_result_set[0][$fld[$m]]) {
246 $update_array[$matching_table_index][$update_row][$update_field] = $src_result_set[0][$fld[$m]];
247 $update_field++;
249 $update_row++;
253 } else {
255 * Placing the primary key, and the value of primary key of the row that is to be inserted in the target table
257 if (sizeof($is_key) == 1) {
258 if (isset($source_result_set[$j])) {
259 $insert_array[$matching_table_index][$insert_row][$is_key[0]] = $source_result_set[$j];
261 } elseif (sizeof($is_key) > 1) {
262 for($l = 0; $l < sizeof($is_key); $l++) {
263 if (isset($source_result_set[$j][$matching_tables_fields[$matching_table_index][$l]])) {
264 $insert_array[$matching_table_index][$insert_row][$is_key[$l]] = $source_result_set[$j][$matching_tables_fields[$matching_table_index][$l]];
268 $insert_row++;
270 } else {
272 * Placing the primary key, and the value of primary key of the row that is to be inserted in the target table
273 * This condition is met when there is an additional column in the source table
275 if (sizeof($is_key) == 1) {
276 if (isset($source_result_set[$j])) {
277 $insert_array[$matching_table_index][$insert_row][$is_key[0]] = $source_result_set[$j];
279 } elseif (sizeof($is_key) > 1) {
280 for ($l = 0; $l < sizeof($is_key); $l++) {
281 if (isset($source_result_set[$j][$matching_tables_fields[$matching_table_index][$l]])) {
282 $insert_array[$matching_table_index][$insert_row][$is_key[$l]] = $source_result_set[$j][$matching_tables_fields[$matching_table_index][$l]];
286 $insert_row++;
288 } // for loop ends
293 * PMA_findDeleteRowsFromTargetTables finds the rows which are to be deleted from target table.
295 * @param $delete_array array containing rows that are to be deleted
296 * @param $matching_table array containing matching table names
297 * @param $matching_table_index index of a table from $matching_table array
298 * @param $trg_keys array of target table keys
299 * @param $src_keys array of source table keys
300 * @param $trg_db name of target database
301 * @param $trg_link connection established with target server
302 * @param $src_db name of source database
303 * @param $src_link connection established with source server
305 function PMA_findDeleteRowsFromTargetTables(&$delete_array, $matching_table, $matching_table_index, $trg_keys, $src_keys, $trg_db, $trg_link,$src_db, $src_link)
307 if (isset($trg_keys[$matching_table_index])) {
308 $target_key_values = PMA_get_column_values($trg_db, $matching_table[$matching_table_index], $trg_keys[$matching_table_index], $trg_link);
309 $target_row_size = sizeof($target_key_values);
311 if (isset($src_keys[$matching_table_index])) {
312 $source_key_values = PMA_get_column_values($src_db, $matching_table[$matching_table_index], $src_keys[$matching_table_index], $src_link);
313 $source_size = sizeof($source_key_values);
315 $all_keys_match = 1;
316 for ($a = 0; $a < sizeof($trg_keys[$matching_table_index]); $a++) {
317 if (isset($trg_keys[$matching_table_index][$a])) {
318 if (! (in_array($trg_keys[$matching_table_index][$a], $src_keys[$matching_table_index]))) {
319 $all_keys_match = 0;
323 if (! ($all_keys_match)) {
324 if (isset($target_key_values)) {
325 $delete_array[$matching_table_index] = $target_key_values;
328 if (isset($trg_keys[$matching_table_index])) {
329 if ((sizeof($trg_keys[$matching_table_index]) == 1) && $all_keys_match) {
330 $row = 0;
331 if (isset($target_key_values)) {
332 for ($i = 0; $i < sizeof($target_key_values); $i++) {
333 if (! (in_array($target_key_values[$i], $source_key_values))) {
334 $delete_array[$matching_table_index][$row] = $target_key_values[$i];
335 $row++;
339 } elseif ((sizeof($trg_keys[$matching_table_index]) > 1) && $all_keys_match) {
340 $row = 0;
341 if (isset($target_key_values)) {
342 for ($i = 0; $i < sizeof($target_key_values); $i++) {
343 $is_present = false;
344 for ($j = 0; $j < sizeof($source_key_values) && ($is_present == false) ; $j++) {
345 $check = true;
346 for ($k = 0; $k < sizeof($trg_keys[$matching_table_index]); $k++) {
347 if ($target_key_values[$i][$trg_keys[$matching_table_index][$k]] != $source_key_values[$j][$trg_keys[$matching_table_index][$k]]) {
348 $check = false;
351 if ($check) {
352 $is_present = true;
355 if (! ($is_present)) {
356 for ($l = 0; $l < sizeof($trg_keys[$matching_table_index]); $l++) {
357 $delete_array[$matching_table_index][$row][$trg_keys[$matching_table_index][$l]] = $target_key_values[$i][$trg_keys[$matching_table_index][$l]];
359 $row++;
368 * PMA_dataDiffInUncommonTables() finds the data difference in $source_tables_uncommon
370 * @param $source_tables_uncommon array of table names; containing table names that are in source db and not in target db
371 * @param $src_db name of source database
372 * @param $src_link connection established with source server
373 * @param $index index of a table from $matching_table array
374 * @param $row_count number of rows
376 function PMA_dataDiffInUncommonTables($source_tables_uncommon, $src_db, $src_link, $index, &$row_count)
378 $query = "SELECT COUNT(*) FROM " . PMA_backquote($src_db) . "." . PMA_backquote($source_tables_uncommon[$index]);
379 $rows = PMA_DBI_fetch_result($query, null, null, $src_link);
380 $row_count[$index] = $rows[0];
384 * PMA_updateTargetTables() sets the updated field values to target table rows using $update_array[$matching_table_index]
387 * @param $table Array containing matching tables' names
388 * @param $update_array A three dimensional array containing field
389 * value updates required for each matching table
390 * @param $src_db Name of source database
391 * @param $trg_db Name of target database
392 * @param $trg_link Connection established with target server
393 * @param $matching_table_index index of matching table in matching_table_array
394 * @param $matching_table_keys
395 * @param $display true/false value
397 function PMA_updateTargetTables($table, $update_array, $src_db, $trg_db, $trg_link, $matching_table_index, $matching_table_keys, $display)
399 if (isset($update_array[$matching_table_index])) {
400 if (sizeof($update_array[$matching_table_index])) {
402 for ($update_row = 0; $update_row < sizeof($update_array[$matching_table_index]); $update_row++) {
404 if (isset($update_array[$matching_table_index][$update_row])) {
405 $update_fields_num = sizeof($update_array[$matching_table_index][$update_row])-sizeof($matching_table_keys[$matching_table_index]);
406 if ($update_fields_num > 0) {
407 $query = "UPDATE " . PMA_backquote($trg_db) . "." .PMA_backquote($table[$matching_table_index]) . " SET ";
409 for ($update_field = 0; $update_field < $update_fields_num; $update_field = $update_field+2) {
410 if (isset($update_array[$matching_table_index][$update_row][$update_field]) && isset($update_array[$matching_table_index][$update_row][$update_field+1])) {
411 $query .= $update_array[$matching_table_index][$update_row][$update_field] . "='" . $update_array[$matching_table_index][$update_row][$update_field+1] . "'";
413 if ($update_field < ($update_fields_num - 2)) {
414 $query .= ", ";
417 $query .= " WHERE ";
418 if (isset($matching_table_keys[$matching_table_index])) {
419 for ($key = 0; $key < sizeof($matching_table_keys[$matching_table_index]); $key++)
421 if (isset($matching_table_keys[$matching_table_index][$key])) {
422 $query .= $matching_table_keys[$matching_table_index][$key] . "='" . $update_array[$matching_table_index][$update_row][$matching_table_keys[$matching_table_index][$key]] . "'";
424 if ($key < (sizeof($matching_table_keys[$matching_table_index]) - 1)) {
425 $query .= " AND ";
429 if ($display == true) {
430 echo "<p>" . $query . "</p>";
432 PMA_DBI_try_query($query, $trg_link, 0);
441 * PMA_insertIntoTargetTable() inserts missing rows in the target table using $array_insert[$matching_table_index]
443 * @param $matching_table array containing matching table names
444 * @param $src_db name of source database
445 * @param $trg_db name of target database
446 * @param $src_link connection established with source server
447 * @param $trg_link connection established with target server
448 * @param $table_fields array containing field names of a table
449 * @param $array_insert
450 * @param $matching_table_index index of matching table in matching_table_array
451 * @param $matching_tables_keys array containing field names that are keys in the matching table
452 * @param $source_columns array containing source column information
453 * @param $add_column_array array containing column names that are to be added in target table
454 * @param $criteria array containing criterias like type, null, collation, default etc
455 * @param $target_tables_keys array containing field names that are keys in the target table
456 * @param $uncommon_tables array containing table names that are present in source db but not in targt db
457 * @param $uncommon_tables_fields array containing field names of the uncommon tables
458 * @param $uncommon_cols column names that are present in target table and not in source table
459 * @param $alter_str_array array containing column names that are to be altered
460 * @param $source_indexes column names on which indexes are made in source table
461 * @param $target_indexes column names on which indexes are made in target table
462 * @param $add_indexes_array array containing column names on which index is to be added in target table
463 * @param $alter_indexes_array array containing column names whose indexes are to be altered. Only index name and uniqueness of an index can be changed
464 * @param $delete_array array containing rows that are to be deleted
465 * @param $update_array array containing rows that are to be updated in target
466 * @param $display true/false value
468 function PMA_insertIntoTargetTable($matching_table, $src_db, $trg_db, $src_link, $trg_link, $table_fields, &$array_insert, $matching_table_index,
469 $matching_tables_keys, $source_columns, &$add_column_array, $criteria, $target_tables_keys, $uncommon_tables, &$uncommon_tables_fields,$uncommon_cols,
470 &$alter_str_array,&$source_indexes, &$target_indexes, &$add_indexes_array, &$alter_indexes_array, &$delete_array, &$update_array, $display)
472 if(isset($array_insert[$matching_table_index])) {
473 if (sizeof($array_insert[$matching_table_index])) {
474 for ($insert_row = 0; $insert_row< sizeof($array_insert[$matching_table_index]); $insert_row++) {
475 if (isset($array_insert[$matching_table_index][$insert_row][$matching_tables_keys[$matching_table_index][0]])) {
477 $select_query = "SELECT * FROM " . PMA_backquote($src_db) . "." . PMA_backquote($matching_table[$matching_table_index]) . " WHERE ";
478 for ($i = 0; $i < sizeof($matching_tables_keys[$matching_table_index]); $i++) {
479 $select_query .= $matching_tables_keys[$matching_table_index][$i] . "='";
480 $select_query .= $array_insert[$matching_table_index][$insert_row][$matching_tables_keys[$matching_table_index][$i]] . "'" ;
482 if ($i < (sizeof($matching_tables_keys[$matching_table_index]) - 1)) {
483 $select_query.= " AND ";
486 $select_query .= "; ";
487 $result = PMA_DBI_fetch_result ($select_query, null, null, $src_link);
488 $insert_query = "INSERT INTO " . PMA_backquote($trg_db) . "." . PMA_backquote($matching_table[$matching_table_index]) ." (";
490 for ($field_index = 0; $field_index < sizeof($table_fields[$matching_table_index]); $field_index++)
492 $insert_query .= $table_fields[$matching_table_index][$field_index];
494 $is_fk_query = "SELECT * FROM information_schema.KEY_COLUMN_USAGE WHERE TABLE_SCHEMA = '" . $trg_db ."'
495 AND TABLE_NAME = '" . $matching_table[$matching_table_index]. "'AND COLUMN_NAME = '" .
496 $table_fields[$matching_table_index][$field_index] . "' AND TABLE_NAME <> REFERENCED_TABLE_NAME;" ;
498 $is_fk_result = PMA_DBI_fetch_result($is_fk_query, null, null, $trg_link);
499 if (sizeof($is_fk_result) > 0) {
500 for ($j = 0; $j < sizeof($is_fk_result); $j++)
502 $table_index = array_keys($matching_table, $is_fk_result[$j]['REFERENCED_TABLE_NAME']);
504 if (isset($alter_str_array[$table_index[0]])) {
505 PMA_alterTargetTableStructure($trg_db, $trg_link, $matching_tables, $source_columns, $alter_str_array, $matching_tables_fields,
506 $criteria, $matching_tables_keys, $target_tables_keys, $table_index[0], $display);
507 unset($alter_str_array[$table_index[0]]);
509 if (isset($uncommon_columns[$table_index[0]])) {
510 PMA_removeColumnsFromTargetTable($trg_db, $trg_link, $matching_tables, $uncommon_columns, $table_index[0], $display);
511 unset($uncommon_columns[$table_index[0]]);
513 if (isset($add_column_array[$table_index[0]])) {
514 PMA_findDeleteRowsFromTargetTables($delete_array, $matching_tables, $table_index[0], $target_tables_keys, $matching_tables_keys,
515 $trg_db, $trg_link, $src_db, $src_link);
517 if (isset($delete_array[$table_index[0]])) {
518 PMA_deleteFromTargetTable($trg_db, $trg_link, $matching_tables, $table_index[0], $target_tables_keys, $delete_array, $display);
519 unset($delete_array[$table_index[0]]);
521 PMA_addColumnsInTargetTable($src_db, $trg_db, $src_link, $trg_link, $matching_tables, $source_columns, $add_column_array,
522 $matching_tables_fields, $criteria, $matching_tables_keys, $target_tables_keys, $uncommon_tables,$uncommon_tables_fields,
523 $table_index[0], $uncommon_cols, $display);
524 unset($add_column_array[$table_index[0]]);
526 if (isset($add_indexes_array[$table_index[0]]) || isset($remove_indexes_array[$table_index[0]])
527 || isset($alter_indexes_array[$table_index[0]])) {
528 PMA_applyIndexesDiff ($trg_db, $trg_link, $matching_tables, $source_indexes, $target_indexes, $add_indexes_array, $alter_indexes_array,
529 $remove_indexes_array, $table_index[0], $display);
531 unset($add_indexes_array[$table_index[0]]);
532 unset($alter_indexes_array[$table_index[0]]);
533 unset($remove_indexes_array[$table_index[0]]);
535 if (isset($update_array[$table_index[0]])) {
536 PMA_updateTargetTables($matching_tables, $update_array, $src_db, $trg_db, $trg_link, $table_index[0], $matching_table_keys,
537 $display);
538 unset($update_array[$table_index[0]]);
540 if (isset($array_insert[$table_index[0]])) {
541 PMA_insertIntoTargetTable($matching_table, $src_db, $trg_db, $src_link, $trg_link, $table_fields, $array_insert,
542 $table_index[0], $matching_tables_keys, $source_columns, $add_column_array, $criteria, $target_tables_keys, $uncommon_tables,
543 $uncommon_tables_fields, $uncommon_cols, $alter_str_array, $source_indexes, $target_indexes, $add_indexes_array,
544 $alter_indexes_array, $delete_array, $update_array, $display);
545 unset($array_insert[$table_index[0]]);
549 if ($field_index < sizeof($table_fields[$matching_table_index])-1) {
550 $insert_query .= ", ";
553 $insert_query .= ") VALUES(";
554 if (sizeof($table_fields[$matching_table_index]) == 1) {
555 $insert_query .= "'" . PMA_sqlAddSlashes($result[0]) . "'";
556 } else {
557 for ($field_index = 0; $field_index < sizeof($table_fields[$matching_table_index]); $field_index++) {
558 if (isset($result[0][$table_fields[$matching_table_index][$field_index]])) {
559 $insert_query .= "'" . PMA_sqlAddSlashes($result[0][$table_fields[$matching_table_index][$field_index]]) . "'";
560 } else {
561 $insert_query .= "'NULL'";
563 if ($field_index < (sizeof($table_fields[$matching_table_index])) - 1) {
564 $insert_query .= " ," ;
568 $insert_query .= ");";
569 if ($display == true) {
570 PMA_displayQuery($insert_query);
572 PMA_DBI_try_query($insert_query, $trg_link, 0);
580 * PMA_createTargetTables() Create the missing table $uncommon_table in target database
582 * @param $src_db name of source database
583 * @param $trg_db name of target database
584 * @param $src_link connection established with source server
585 * @param $trg_link connection established with target server
586 * @param $uncommon_tables names of tables present in source but not in target
587 * @param $table_index index of table in $uncommon_tables array
588 * @param $uncommon_tables_fields field names of the uncommon table
589 * @param $display true/false value
591 function PMA_createTargetTables($src_db, $trg_db, $src_link, $trg_link, &$uncommon_tables, $table_index, &$uncommon_tables_fields, $display)
593 if (isset($uncommon_tables[$table_index])) {
594 $fields_result = PMA_DBI_get_columns($src_db, $uncommon_tables[$table_index], true, $src_link);
595 $fields = array();
596 foreach ($fields_result as $each_field) {
597 $field_name = $each_field['Field'];
598 $fields[] = $field_name;
600 $uncommon_tables_fields[$table_index] = $fields;
602 $Create_Query = PMA_DBI_fetch_value("SHOW CREATE TABLE " . PMA_backquote($src_db) . '.' . PMA_backquote($uncommon_tables[$table_index]), 0, 1, $src_link);
604 // Replace the src table name with a `dbname`.`tablename`
605 $Create_Table_Query = preg_replace('/' . preg_quote(PMA_backquote($uncommon_tables[$table_index]), '/') . '/',
606 PMA_backquote($trg_db) . '.' .PMA_backquote($uncommon_tables[$table_index]),
607 $Create_Query,
608 $limit = 1
611 $is_fk_query = "SELECT * FROM information_schema.KEY_COLUMN_USAGE WHERE TABLE_SCHEMA = '" . $src_db . "'
612 AND TABLE_NAME = '" . $uncommon_tables[$table_index] . "' AND TABLE_NAME <> REFERENCED_TABLE_NAME;" ;
614 $is_fk_result = PMA_DBI_fetch_result($is_fk_query, null, null, $src_link);
615 if (sizeof($is_fk_result) > 0) {
616 for ($j = 0; $j < sizeof($is_fk_result); $j++)
618 if (in_array($is_fk_result[$j]['REFERENCED_TABLE_NAME'], $uncommon_tables)) {
619 $table_index = array_keys($uncommon_tables, $is_fk_result[$j]['REFERENCED_TABLE_NAME']);
620 PMA_createTargetTables($src_db, $trg_db, $trg_link, $src_link, $uncommon_tables, $table_index[0], $uncommon_tables_fields, $display);
621 unset($uncommon_tables[$table_index[0]]);
625 if ($display == true) {
626 echo '<p>' . $Create_Table_Query . '</p>';
628 PMA_DBI_try_query($Create_Table_Query, $trg_link, 0);
632 * PMA_populateTargetTables() inserts data into uncommon tables after they have been created
634 * @param $src_db name of source database
635 * @param $trg_db name of target database
636 * @param $src_link connection established with source server
637 * @param $trg_link connection established with target server
638 * @param $uncommon_tables array containing uncommon table names (table names that are present in source but not in target db)
639 * @param $table_index index of table in matching_table_array
640 * @param $uncommon_tables_fields field names of the uncommon table
641 * @param $display true/false value
643 * FIXME: This turns NULL values into '' (empty string)
645 function PMA_populateTargetTables($src_db, $trg_db, $src_link, $trg_link, $uncommon_tables, $table_index, $uncommon_tables_fields, $display)
647 $display = false; // todo: maybe display some of the queries if they are not too numerous
648 $unbuffered_result = PMA_DBI_try_query('SELECT * FROM ' . PMA_backquote($src_db) . '.' . PMA_backquote($uncommon_tables[$table_index]), $src_link, PMA_DBI_QUERY_UNBUFFERED);
649 if (false !== $unbuffered_result) {
650 $insert_query = 'INSERT INTO ' . PMA_backquote($trg_db) . '.' .PMA_backquote($uncommon_tables[$table_index]) . ' VALUES';
651 while ($one_row = PMA_DBI_fetch_row($unbuffered_result)) {
652 $insert_query .= '(';
653 $key_of_last_value = count($one_row) - 1;
654 foreach($one_row as $key => $value) {
655 $insert_query .= "'" . PMA_sqlAddSlashes($value) . "'";
656 if ($key < $key_of_last_value) {
657 $insert_query .= ",";
660 $insert_query .= '),';
662 $insert_query = substr($insert_query, 0, -1);
663 $insert_query .= ';';
664 if ($display == true) {
665 PMA_displayQuery($insert_query);
667 PMA_DBI_try_query($insert_query, $trg_link, 0);
672 * PMA_deleteFromTargetTable() delete rows from target table
674 * @param $trg_db name of target database
675 * @param $trg_link connection established with target server
676 * @param $matching_tables array containing matching table names
677 * @param $table_index index of table in matching_table_array
678 * @param $target_tables_keys primary key names of the target tables
679 * @param $delete_array array containing the key values of rows that are to be deleted
680 * @param $display true/false value
682 function PMA_deleteFromTargetTable($trg_db, $trg_link, $matching_tables, $table_index, $target_tables_keys, $delete_array, $display)
684 for($i = 0; $i < sizeof($delete_array[$table_index]); $i++) {
685 if (isset($target_tables_keys[$table_index])) {
686 $delete_query = 'DELETE FROM ' . PMA_backquote($trg_db) . '.' .PMA_backquote($matching_tables[$table_index]) . ' WHERE ';
687 for($y = 0; $y < sizeof($target_tables_keys[$table_index]); $y++) {
688 $delete_query .= $target_tables_keys[$table_index][$y] . " = '";
690 if (sizeof($target_tables_keys[$table_index]) == 1) {
691 $delete_query .= $delete_array[$table_index][$i] . "'";
692 } elseif (sizeof($target_tables_keys[$table_index]) > 1) {
693 $delete_query .= $delete_array[$table_index][$i][$target_tables_keys[$table_index][$y]] . "'";
695 if ($y < (sizeof($target_tables_keys[$table_index]) - 1)) {
696 $delete_query .= ' AND ';
698 $pk_query = "SELECT * FROM information_schema.KEY_COLUMN_USAGE WHERE REFERENCED_TABLE_SCHEMA = '" . $trg_db . "'
699 AND REFERENCED_TABLE_NAME = '" . $matching_tables[$table_index]."' AND REFERENCED_COLUMN_NAME = '"
700 . $target_tables_keys[$table_index][$y] . "' AND TABLE_NAME <> REFERENCED_TABLE_NAME;";
702 $pk_query_result = PMA_DBI_fetch_result($pk_query, null, null, $trg_link);
703 $result_size = sizeof($pk_query_result);
705 if ($result_size > 0) {
706 for ($b = 0; $b < $result_size; $b++) {
707 $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] . ";";
708 PMA_DBI_try_query($drop_pk_query, $trg_link, 0);
713 if ($display == true) {
714 echo '<p>' . $delete_query . '</p>';
716 PMA_DBI_try_query($delete_query, $trg_link, 0);
721 * PMA_structureDiffInTables() Gets all the column information for source and target table.
722 * Compare columns on their names.
723 * If column exists in target then compare Type, Null, Collation, Key, Default and Comment for that column.
724 * If column does not exist in target table then it is placed in $add_column_array.
725 * If column exists in target table but criteria is different then it is palced in $alter_str_array.
726 * If column does not exist in source table but is present in target table then it is placed in $uncommon_columns.
727 * Keys for all the source tables that have a corresponding target table are placed in $matching_tables_keys.
728 * Keys for all the target tables that have a corresponding source table are placed in $target_tables_keys.
730 * @param $src_db name of source database
731 * @param $trg_db name of target database
732 * @param $src_link connection established with source server
733 * @param $trg_link connection established with target server
734 * @param $matching_tables array containing names of matching tables
735 * @param $source_columns array containing columns information of the source tables
736 * @param $target_columns array containing columns information of the target tables
737 * @param $alter_str_array three dimensional associative array first index being the matching table index, second index being column name for which target
738 * column have some criteria different and third index containing the criteria which is different.
739 * @param $add_column_array two dimensional associative array, first index of the array contain the matching table number and second index contain the
740 * column name which is to be added in the target table
741 * @param $uncommon_columns array containing the columns that are present in the target table but not in the source table
742 * @param $criteria array containing the criterias which are to be checked for field that is present in source table and target table
743 * @param $target_tables_keys array containing the field names which is key in the target table
744 * @param $matching_table_index integer number of the matching table
746 function PMA_structureDiffInTables($src_db, $trg_db, $src_link, $trg_link, $matching_tables, &$source_columns, &$target_columns, &$alter_str_array,
747 &$add_column_array, &$uncommon_columns, $criteria, &$target_tables_keys, $matching_table_index)
749 //Gets column information for source and target table
750 $source_columns[$matching_table_index] = PMA_DBI_get_columns_full($src_db, $matching_tables[$matching_table_index], null, $src_link);
751 $target_columns[$matching_table_index] = PMA_DBI_get_columns_full($trg_db, $matching_tables[$matching_table_index], null, $trg_link);
752 foreach ($source_columns[$matching_table_index] as $column_name => $each_column) {
753 if (isset($target_columns[$matching_table_index][$column_name]['Field'])) {
754 //If column exists in target table then matches criterias like type, null, collation, key, default, comment of the column
755 for ($i = 0; $i < sizeof($criteria); $i++) {
756 if ($source_columns[$matching_table_index][$column_name][$criteria[$i]] != $target_columns[$matching_table_index][$column_name][$criteria[$i]]) {
757 if (($criteria[$i] == 'Default') && ($source_columns[$matching_table_index][$column_name][$criteria[$i]] == '' )) {
758 $alter_str_array[$matching_table_index][$column_name][$criteria[$i]] = 'None';
759 } else {
760 if (! (($criteria[$i] == 'Key') && (($source_columns[$matching_table_index][$column_name][$criteria[$i]] == 'MUL')
761 || ($target_columns[$matching_table_index][$column_name][$criteria[$i]] == 'MUL')
762 || ($source_columns[$matching_table_index][$column_name][$criteria[$i]] == 'UNI')
763 || ($target_columns[$matching_table_index][$column_name][$criteria[$i]] == 'UNI')))) {
764 $alter_str_array[$matching_table_index][$column_name][$criteria[$i]] = $source_columns[$matching_table_index][$column_name][$criteria[$i]];
769 } else {
770 $add_column_array[$matching_table_index][$column_name]= $column_name;
773 //Finds column names that are present in target table but not in source table
774 foreach ($target_columns[$matching_table_index] as $fld_name => $each_column) {
775 if (! (isset($source_columns[$matching_table_index][$fld_name]['Field']))) {
776 $fields_uncommon[] = $fld_name;
778 if ($target_columns[$matching_table_index][$fld_name]['Key'] == 'PRI') {
779 $keys[] = $fld_name;
782 if (isset($fields_uncommon)) {
783 $uncommon_columns[$matching_table_index] = $fields_uncommon;
785 if (isset($keys)) {
786 $target_tables_keys[$matching_table_index] = $keys;
790 * PMA_addColumnsInTargetTable() adds column that are present in source table but not in target table
792 * @param $src_db name of source database
793 * @param $trg_db name of target database
794 * @param $src_link connection established with source server
795 * @param $trg_link connection established with target server
796 * @param $matching_tables array containing names of matching tables
797 * @param $source_columns array containing columns information of the source tables
798 * @param $add_column_array array containing the names of the column(field) that are to be added in the target
799 * @param $matching_tables_fields
800 * @param $criteria array containing the criterias
801 * @param $matching_tables_keys array containing the field names which is key in the source table
802 * @param $target_tables_keys array containing the field names which is key in the target table
803 * @param $uncommon_tables array containing the table names that are present in source db and not in target db
804 * @param $uncommon_tables_fields array containing the names of the fields of the uncommon tables
805 * @param $table_counter integer number of the matching table
806 * @param $uncommon_cols
807 * @param $display true/false value
809 function PMA_addColumnsInTargetTable($src_db, $trg_db, $src_link, $trg_link, $matching_tables, $source_columns, &$add_column_array, $matching_tables_fields,
810 $criteria, $matching_tables_keys, $target_tables_keys, $uncommon_tables, &$uncommon_tables_fields, $table_counter, $uncommon_cols, $display)
812 for ($i = 0; $i < sizeof($matching_tables_fields[$table_counter]); $i++) {
813 if (isset($add_column_array[$table_counter][$matching_tables_fields[$table_counter][$i]])) {
814 $query = "ALTER TABLE " . PMA_backquote($trg_db) . '.' . PMA_backquote($matching_tables[$table_counter]). " ADD COLUMN " .
815 $add_column_array[$table_counter][$matching_tables_fields[$table_counter][$i]] . " " . $source_columns[$table_counter][$matching_tables_fields[$table_counter][$i]]['Type'];
817 if($source_columns[$table_counter][$matching_tables_fields[$table_counter][$i]]['Null'] == 'NO') {
818 $query .= ' Not Null ';
819 } elseif ($source_columns[$table_counter][$matching_tables_fields[$table_counter][$i]]['Null'] == 'YES') {
820 $query .= ' Null ';
822 if ($source_columns[$table_counter][$matching_tables_fields[$table_counter][$i]]['Collation'] != '') {
823 $query .= ' COLLATE ' . $source_columns[$table_counter][$matching_tables_fields[$table_counter][$i]]['Collation'];
825 if ($source_columns[$table_counter][$matching_tables_fields[$table_counter][$i]]['Default'] != '') {
826 $query .= " DEFAULT " . $source_columns[$table_counter][$matching_tables_fields[$table_counter][$i]]['Default'];
828 if ($source_columns[$table_counter][$matching_tables_fields[$table_counter][$i]]['Comment'] != '') {
829 $query .= " COMMENT " . $source_columns[$table_counter][$matching_tables_fields[$table_counter][$i]]['Comment'];
831 if ($source_columns[$table_counter][$matching_tables_fields[$table_counter][$i]]['Key'] == 'PRI' ) {
832 $trg_key_size = sizeof($target_tables_keys[$table_counter]);
833 if ($trg_key_size) {
834 $check = true;
835 for ($a = 0; ($a < $trg_key_size) && ($check); $a++) {
836 if (! (in_array($target_tables_keys[$table_counter], $uncommon_cols))) {
837 $check = false;
840 if (! $check) {
841 $query .= " ,DROP PRIMARY KEY " ;
844 $query .= " , ADD PRIMARY KEY (";
845 for ($t = 0; $t < sizeof($matching_tables_keys[$table_counter]); $t++) {
846 $query .= $matching_tables_keys[$table_counter][$t];
847 if ($t < (sizeof($matching_tables_keys[$table_counter]) - 1)) {
848 $query .= " , " ;
851 $query .= ")";
854 $query .= ";";
855 if ($display == true) {
856 echo '<p>' . $query . '</p>';
858 PMA_DBI_try_query($query, $trg_link, 0);
860 //Checks if column to be added is a foreign key or not
861 $is_fk_query = "SELECT * FROM information_schema.KEY_COLUMN_USAGE WHERE TABLE_SCHEMA = '" . $trg_db . "' AND TABLE_NAME = '"
862 . $matching_tables[$table_counter] . "' AND COLUMN_NAME ='" . $add_column_array[$table_counter][$matching_tables_fields[$table_counter][$i]] .
863 "' AND TABLE_NAME <> REFERENCED_TABLE_NAME;";
865 $is_fk_result = PMA_DBI_fetch_result($is_fk_query, null, null, $src_link);
867 //If column is a foreign key then it is checked that referenced table exist in target db. If referenced table does not exist in target db then
868 //it is created first.
869 if (isset($is_fk_result)) {
870 if (in_array($is_fk_result[0]['REFERENCED_TABLE_NAME'], $uncommon_tables)) {
871 $table_index = array_keys($uncommon_tables, $is_fk_result[0]['REFERENCED_TABLE_NAME']);
872 PMA_checkForeignKeys($src_db, $src_link, $trg_db, $trg_link, $is_fk_result[0]['REFERENCED_TABLE_NAME'], $uncommon_tables, $uncommon_tables_fields);
873 PMA_createTargetTables($src_db, $trg_db, $trg_link, $src_link, $uncommon_tables, $table_index[0], $uncommon_tables_fields);
874 unset($uncommon_tables[$table_index[0]]);
876 $fk_query = "ALTER TABLE " . PMA_backquote($trg_db) . '.' . PMA_backquote($matching_tables[$table_counter]) .
877 "ADD CONSTRAINT FOREIGN KEY " . $add_column_array[$table_counter][$matching_tables_fields[$table_counter][$i]] . "
878 (" . $add_column_array[$table_counter][$matching_tables_fields[$table_counter][$i]] . ") REFERENCES " . PMA_backquote($trg_db) .
879 '.' . PMA_backquote($is_fk_result[0]['REFERENCED_TABLE_NAME']) . " (" . $is_fk_result[0]['REFERENCED_COLUMN_NAME'] . ");";
881 PMA_DBI_try_query($fk_query, $trg_link, null);
887 * PMA_checkForeignKeys() checks if the referenced table have foreign keys.
888 * uses PMA_createTargetTables()
890 * @param $src_db name of source database
891 * @param $src_link connection established with source server
892 * @param $trg_db name of target database
893 * @param $trg_link connection established with target server
894 * @param $referenced_table table whose column is a foreign key in another table
895 * @param $uncommon_tables array containing names that are uncommon
896 * @param $uncommon_tables_fields field names of the uncommon table
897 * @param $display true/false value
899 function PMA_checkForeignKeys($src_db, $src_link, $trg_db, $trg_link ,$referenced_table, &$uncommon_tables, &$uncommon_tables_fields, $display)
901 $is_fk_query = "SELECT * FROM information_schema.KEY_COLUMN_USAGE WHERE TABLE_SCHEMA = '" . $src_db . "'
902 AND TABLE_NAME = '" . $referenced_table . "' AND TABLE_NAME <> REFERENCED_TABLE_NAME;";
904 $is_fk_result = PMA_DBI_fetch_result($is_fk_query, null, null, $src_link);
905 if (sizeof($is_fk_result) > 0) {
906 for ($j = 0; $j < sizeof($is_fk_result); $j++) {
907 if (in_array($is_fk_result[$j]['REFERENCED_TABLE_NAME'], $uncommon_tables)) {
908 $table_index = array_keys($uncommon_tables, $is_fk_result[$j]['REFERENCED_TABLE_NAME']);
909 PMA_checkForeignKeys($src_db, $src_link, $trg_db, $trg_link, $is_fk_result[$j]['REFERENCED_TABLE_NAME'], $uncommon_tables,
910 $uncommon_tables_fields, $display);
911 PMA_createTargetTables($src_db, $trg_db, $trg_link, $src_link, $uncommon_tables, $table_index[0], $uncommon_tables_fields, $display);
912 unset($uncommon_tables[$table_index[0]]);
918 * PMA_alterTargetTableStructure() alters structure of the target table using $alter_str_array
920 * @param $trg_db name of target database
921 * @param $trg_link connection established with target server
922 * @param $matching_tables array containing names of matching tables
923 * @param $source_columns array containing columns information of the source table
924 * @param $alter_str_array array containing the column name and criteria which is to be altered for the targert table
925 * @param $matching_tables_fields array containing the name of the fields for the matching table
926 * @param $criteria array containing the criterias
927 * @param $matching_tables_keys array containing the field names which is key in the source table
928 * @param $target_tables_keys array containing the field names which is key in the target table
929 * @param $matching_table_index integer number of the matching table
930 * @param $display true/false value
932 function PMA_alterTargetTableStructure($trg_db, $trg_link, $matching_tables, &$source_columns, &$alter_str_array, $matching_tables_fields, $criteria,
933 &$matching_tables_keys, &$target_tables_keys, $matching_table_index, $display)
935 $check = true;
936 $sql_query = '';
937 $found = false;
939 //Checks if the criteria to be altered is primary key
940 for ($v = 0; $v < sizeof($matching_tables_fields[$matching_table_index]); $v++) {
941 if (isset($alter_str_array[$matching_table_index][$matching_tables_fields[$matching_table_index][$v]]['Key'])) {
942 if ($alter_str_array[$matching_table_index][$matching_tables_fields[$matching_table_index][$v]]['Key'] == 'PRI' ) {
943 $check = false;
947 $pri_query;
948 if (! $check) {
949 $pri_query = "ALTER TABLE " . PMA_backquote($trg_db) . '.' . PMA_backquote($matching_tables[$matching_table_index]);
950 if (sizeof($target_tables_keys[$matching_table_index]) > 0) {
951 $pri_query .= " DROP PRIMARY KEY ," ;
953 $pri_query .= " ADD PRIMARY KEY (";
954 for ($z = 0; $z < sizeof($matching_tables_keys[$matching_table_index]); $z++) {
955 $pri_query .= $matching_tables_keys[$matching_table_index][$z];
956 if ($z < (sizeof($matching_tables_keys[$matching_table_index]) - 1)) {
957 $pri_query .= " , " ;
960 $pri_query .= ");";
963 if (isset($pri_query)) {
964 if ($display == true) {
965 echo '<p>' . $pri_query . '</p>';
967 PMA_DBI_try_query($pri_query, $trg_link, 0);
969 for ($t = 0; $t < sizeof($matching_tables_fields[$matching_table_index]); $t++) {
970 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)) {
971 $sql_query = 'ALTER TABLE ' . PMA_backquote($trg_db) . '.' . PMA_backquote($matching_tables[$matching_table_index]) . ' MODIFY ' .
972 $matching_tables_fields[$matching_table_index][$t] . ' ' . $source_columns[$matching_table_index][$matching_tables_fields[$matching_table_index][$t]]['Type'];
973 $found = false;
974 for ($i = 0; $i < sizeof($criteria); $i++)
976 if (isset($alter_str_array[$matching_table_index][$matching_tables_fields[$matching_table_index][$t]][$criteria[$i]]) && $criteria[$i] != 'Key') {
977 $found = true;
978 if (($criteria[$i] == 'Type') && (! isset($alter_str_array[$matching_table_index][$matching_tables_fields[$matching_table_index][$t]][$criteria[$i+1]]))) {
979 if ($source_columns[$matching_table_index][$matching_tables_fields[$matching_table_index][$t]][$criteria[$i + 1]] == 'NO') {
980 $sql_query .= " Not Null" ;
981 } elseif ($source_columns[$matching_table_index][$matching_tables_fields[$matching_table_index][$t]][$criteria[$i + 1]] == 'YES') {
982 $sql_query .= " Null" ;
985 if (($criteria[$i] == 'Null') && ( $alter_str_array[$matching_table_index][$matching_tables_fields[$matching_table_index][$t]][$criteria[$i]] == 'NO')) {
986 $sql_query .= " Not Null " ;
987 } elseif (($criteria[$i] == 'Null') && ($alter_str_array[$matching_table_index][$matching_tables_fields[$matching_table_index][$t]][$criteria[$i]] == 'YES')) {
988 $sql_query .= " Null " ;
990 if ($criteria[$i] == 'Collation') {
991 if( !(isset($alter_str_array[$matching_table_index][$matching_tables_fields[$matching_table_index][$t]][$criteria[2]]))) {
992 $sql_query .= " Not Null " ;
994 $sql_query .= " COLLATE " . $alter_str_array[$matching_table_index][$matching_tables_fields[$matching_table_index][$t]][$criteria[$i]] ;
996 if (($criteria[$i] == 'Default') && ($alter_str_array[$matching_table_index][$matching_tables_fields[$matching_table_index][$t]][$criteria[$i]] == 'None')) {
997 if( !(isset($alter_str_array[$matching_table_index][$matching_tables_fields[$matching_table_index][$t]][$criteria[2]]))) {
998 $sql_query .= " Not Null " ;
1000 } elseif($criteria[$i] == 'Default') {
1001 if(! (isset($alter_str_array[$matching_table_index][$matching_tables_fields[$matching_table_index][$t]][$criteria[2]]))) {
1002 $sql_query .= " Not Null " ;
1004 if (is_string($alter_str_array[$matching_table_index][$matching_tables_fields[$matching_table_index][$t]][$criteria[$i]])) {
1005 if ($source_columns[$matching_table_index][$matching_tables_fields[$matching_table_index][$t]]['Type'] != 'timestamp') {
1006 $sql_query .= " DEFAULT '" . $alter_str_array[$matching_table_index][$matching_tables_fields[$matching_table_index][$t]][$criteria[$i]] . "'";
1007 } elseif($source_columns[$matching_table_index][$matching_tables_fields[$matching_table_index][$t]]['Type'] == 'timestamp') {
1008 $sql_query .= " DEFAULT " . $alter_str_array[$matching_table_index][$matching_tables_fields[$matching_table_index][$t]][$criteria[$i]];
1010 } elseif (is_numeric($alter_str_array[$matching_table_index][$matching_tables_fields[$matching_table_index][$t]][$criteria[$i]])) {
1011 $sql_query .= " DEFAULT " . $alter_str_array[$matching_table_index][$matching_tables_fields[$matching_table_index][$t]][$criteria[$i]];
1014 if ($criteria[$i] == 'Comment') {
1015 if( !(isset($alter_str_array[$matching_table_index][$matching_tables_fields[$matching_table_index][$t]][$criteria[2]]))) {
1016 $sql_query .= " Not Null " ;
1018 $sql_query .= " COMMENT '" . $alter_str_array[$matching_table_index][$matching_tables_fields[$matching_table_index][$t]][$criteria[$i]] . "'" ;
1023 $sql_query .= ";";
1024 if ($found) {
1025 if ($display == true) {
1026 echo '<p>' . $sql_query . '</p>';
1028 PMA_DBI_try_query($sql_query, $trg_link, 0);
1031 $check = false;
1032 $query = "ALTER TABLE " . PMA_backquote($trg_db) . '.' . PMA_backquote($matching_tables[$matching_table_index]);
1033 for($p = 0; $p < sizeof($matching_tables_keys[$matching_table_index]); $p++) {
1034 if ((isset($alter_str_array[$matching_table_index][$matching_tables_keys[$matching_table_index][$p]]['Key']))) {
1035 $check = true;
1036 $query .= ' MODIFY ' . $matching_tables_keys[$matching_table_index][$p] . ' '
1037 . $source_columns[$matching_table_index][$matching_tables_fields[$matching_table_index][$p]]['Type'] . ' Not Null ';
1038 if ($p < (sizeof($matching_tables_keys[$matching_table_index]) - 1)) {
1039 $query .= ', ';
1043 if ($check) {
1044 if ($display == true) {
1045 echo '<p>' . $query . '</p>';
1047 PMA_DBI_try_query($query, $trg_link, 0);
1052 * PMA_removeColumnsFromTargetTable() removes the columns which are present in target table but not in source table.
1054 * @param $trg_db name of target database
1055 * @param $trg_link connection established with target server
1056 * @param $matching_tables array containing names of matching tables
1057 * @param $uncommon_columns array containing the names of the column which are to be dropped from the target table
1058 * @param $table_counter index of the matching table as in $matchiing_tables array
1059 * @param $display true/false value
1061 function PMA_removeColumnsFromTargetTable($trg_db, $trg_link, $matching_tables, $uncommon_columns, $table_counter, $display)
1063 if (isset($uncommon_columns[$table_counter])) {
1064 $drop_query = "ALTER TABLE " . PMA_backquote($trg_db) . "." . PMA_backquote($matching_tables[$table_counter]);
1065 for ($a = 0; $a < sizeof($uncommon_columns[$table_counter]); $a++) {
1066 //Checks if column to be removed is a foreign key in any table
1067 $pk_query = "SELECT * FROM information_schema.KEY_COLUMN_USAGE WHERE REFERENCED_TABLE_SCHEMA = '" . $trg_db . "'
1068 AND REFERENCED_TABLE_NAME = '" . $matching_tables[$table_counter]."' AND REFERENCED_COLUMN_NAME = '"
1069 . $uncommon_columns[$table_counter][$a] . "' AND TABLE_NAME <> REFERENCED_TABLE_NAME;";
1071 $pk_query_result = PMA_DBI_fetch_result($pk_query, null, null, $trg_link);
1072 $result_size = sizeof($pk_query_result);
1074 if ($result_size > 0) {
1075 for ($b = 0; $b < $result_size; $b++) {
1076 $drop_pk_query = "ALTER TABLE " . PMA_backquote($pk_query_result[$b]['TABLE_SCHEMA']) . "." . PMA_backquote($pk_query_result[$b]['TABLE_NAME']) . "
1077 DROP FOREIGN KEY " . $pk_query_result[$b]['CONSTRAINT_NAME'] . ", DROP COLUMN " . $pk_query_result[$b]['COLUMN_NAME'] . ";";
1078 PMA_DBI_try_query($drop_pk_query, $trg_link, 0);
1081 $query = "SELECT * FROM information_schema.KEY_COLUMN_USAGE WHERE TABLE_SCHEMA = '" . $trg_db . "' AND TABLE_NAME = '"
1082 . $matching_tables[$table_counter]. "' AND COLUMN_NAME = '" . $uncommon_columns[$table_counter][$a] . "'
1083 AND TABLE_NAME <> REFERENCED_TABLE_NAME;";
1085 $result = PMA_DBI_fetch_result($query, null, null, $trg_link);
1087 if (sizeof($result) > 0) {
1088 $drop_query .= " DROP FOREIGN KEY " . $result[0]['CONSTRAINT_NAME'] . ",";
1090 $drop_query .= " DROP COLUMN " . $uncommon_columns[$table_counter][$a];
1091 if ($a < (sizeof($uncommon_columns[$table_counter]) - 1)) {
1092 $drop_query .= " , " ;
1095 $drop_query .= ";" ;
1097 if ($display == true) {
1098 echo '<p>' . $drop_query . '</p>';
1100 PMA_DBI_try_query($drop_query, $trg_link, 0);
1105 * PMA_indexesDiffInTables() compares the source table indexes with target table indexes and keep the indexes to be added in target table in $add_indexes_array
1106 * indexes to be altered in $alter_indexes_array and indexes to be removed from target table in $remove_indexes_array.
1107 * Only keyname and uniqueness characteristic of the indexes are altered.
1109 * @param $src_db name of source database
1110 * @param $trg_db name of target database
1111 * @param $src_link connection established with source server
1112 * @param $trg_link connection established with target server
1113 * @param $matching_tables array containing the matching tables name
1114 * @param $source_indexes array containing the indexes of the source table
1115 * @param $target_indexes array containing the indexes of the target table
1116 * @param $add_indexes_array array containing the name of the column on which the index is to be added in the target table
1117 * @param $alter_indexes_array array containing the key name which needs to be altered
1118 * @param $remove_indexes_array array containing the key name of the index which is to be removed from the target table
1119 * @param $table_counter number of the matching table
1121 function PMA_indexesDiffInTables($src_db, $trg_db, $src_link, $trg_link, $matching_tables, &$source_indexes, &$target_indexes, &$add_indexes_array,
1122 &$alter_indexes_array, &$remove_indexes_array, $table_counter)
1124 //Gets indexes information for source and target table
1125 $source_indexes[$table_counter] = PMA_get_table_indexes($src_db, $matching_tables[$table_counter],$src_link);
1126 $target_indexes[$table_counter] = PMA_get_table_indexes($trg_db, $matching_tables[$table_counter],$trg_link);
1127 for ($a = 0; $a < sizeof($source_indexes[$table_counter]); $a++) {
1128 $found = false;
1129 $z = 0;
1130 //Compares key name and non_unique characteristic of source indexes with target indexes
1132 * @todo compare the length of each sub part
1134 while (($z <= sizeof($target_indexes[$table_counter])) && ($found == false))
1136 if (isset($source_indexes[$table_counter][$a]) && isset($target_indexes[$table_counter][$z]) && $source_indexes[$table_counter][$a]['Key_name'] == $target_indexes[$table_counter][$z]['Key_name']) {
1137 $found = true;
1138 if (($source_indexes[$table_counter][$a]['Column_name'] != $target_indexes[$table_counter][$z]['Column_name']) || ($source_indexes[$table_counter][$a]['Non_unique'] != $target_indexes[$table_counter][$z]['Non_unique'])) {
1139 if (! (($source_indexes[$table_counter][$a]['Key_name'] == "PRIMARY") || ($target_indexes[$table_counter][$z]['Key_name'] == 'PRIMARY'))) {
1140 $alter_indexes_array[$table_counter][] = $source_indexes[$table_counter][$a]['Key_name'];
1144 $z++;
1146 if ($found === false) {
1147 if(! ($source_indexes[$table_counter][$a]['Key_name'] == 'PRIMARY')) {
1148 $add_indexes_array [$table_counter][] = $source_indexes[$table_counter][$a]['Column_name'];
1153 //Finds indexes that exist on target table but not on source table
1154 for ($b = 0; $b < sizeof($target_indexes[$table_counter]); $b++) {
1155 $found = false;
1156 $c = 0;
1157 while (($c <= sizeof($source_indexes[$table_counter])) && ($found == false))
1159 if ($target_indexes[$table_counter][$b]['Column_name'] == $source_indexes[$table_counter][$c]['Column_name']) {
1160 $found = true;
1162 $c++;
1164 if ($found === false) {
1165 $remove_indexes_array[$table_counter][] = $target_indexes[$table_counter][$b]['Key_name'];
1171 * PMA_applyIndexesDiff() create indexes, alters indexes and remove indexes.
1173 * @param $trg_db name of target database
1174 * @param $trg_link connection established with target server
1175 * @param $matching_tables array containing the matching tables name
1176 * @param $source_indexes array containing the indexes of the source table
1177 * @param $target_indexes array containing the indexes of the target table
1178 * @param $add_indexes_array array containing the column names on which indexes are to be created in target table
1179 * @param $alter_indexes_array array containing the column names for which indexes are to be altered
1180 * @param $remove_indexes_array array containing the key name of the indexes which are to be removed from the target table
1181 * @param $table_counter number of the matching table
1182 * @param $display true/false value
1184 function PMA_applyIndexesDiff ($trg_db, $trg_link, $matching_tables, $source_indexes, $target_indexes, $add_indexes_array, $alter_indexes_array,
1185 $remove_indexes_array, $table_counter, $display)
1187 //Adds indexes on target table
1188 if (isset($add_indexes_array[$table_counter])) {
1189 $sql = "ALTER TABLE " . PMA_backquote($trg_db) . "." . PMA_backquote($matching_tables[$table_counter]) . " ADD" ;
1190 for ($a = 0; $a < sizeof($source_indexes[$table_counter]); $a++) {
1191 if (isset($add_indexes_array[$table_counter][$a])) {
1192 for ($b = 0; $b < sizeof($source_indexes[$table_counter]); $b++) {
1193 if ($source_indexes[$table_counter][$b]['Column_name'] == $add_indexes_array[$table_counter][$a]) {
1194 if ($source_indexes[$table_counter][$b]['Non_unique'] == '0') {
1195 $sql .= " UNIQUE ";
1197 $sql .= " INDEX " . $source_indexes[$table_counter][$b]['Key_name'] . " (" . $add_indexes_array[$table_counter][$a] . " );";
1198 if ($display == true) {
1199 echo '<p>' . $sql . '</p>';
1201 PMA_DBI_try_query($sql, $trg_link, 0);
1207 //Alter indexes of target table
1209 if (isset($alter_indexes_array[$table_counter])) {
1210 $query = "ALTER TABLE " . PMA_backquote($trg_db) . "." . PMA_backquote($matching_tables[$table_counter]);
1211 for ($a = 0; $a < sizeof($alter_indexes_array[$table_counter]); $a++) {
1212 if (isset($alter_indexes_array[$table_counter][$a])) {
1213 $query .= ' DROP INDEX ' . PMA_backquote($alter_indexes_array[$table_counter][$a]) . " , ADD ";
1214 $got_first_index_column = false;
1215 for ($z = 0; $z < sizeof($source_indexes[$table_counter]); $z++) {
1216 if ($source_indexes[$table_counter][$z]['Key_name'] == $alter_indexes_array[$table_counter][$a]) {
1217 if (! $got_first_index_column) {
1218 if ($source_indexes[$table_counter][$z]['Non_unique'] == '0') {
1219 $query .= " UNIQUE ";
1221 $query .= " INDEX " . PMA_backquote($source_indexes[$table_counter][$z]['Key_name']) . " (" . PMA_backquote($source_indexes[$table_counter][$z]['Column_name']);
1222 $got_first_index_column = true;
1223 } else {
1224 // another column for this index
1225 $query .= ', ' . PMA_backquote($source_indexes[$table_counter][$z]['Column_name']);
1229 $query .= " )";
1232 if ($display == true) {
1233 echo '<p>' . $query . '</p>';
1235 PMA_DBI_try_query($query, $trg_link, 0);
1237 //Removes indexes from target table
1238 if (isset($remove_indexes_array[$table_counter])) {
1239 $drop_index_query = "ALTER TABLE " . PMA_backquote($trg_db) . "." . PMA_backquote($matching_tables[$table_counter]);
1240 for ($a = 0; $a < sizeof($target_indexes[$table_counter]); $a++) {
1241 if (isset($remove_indexes_array[$table_counter][$a])) {
1242 $drop_index_query .= " DROP INDEX " . $remove_indexes_array[$table_counter][$a];
1244 if ($a < (sizeof($remove_indexes_array[$table_counter]) - 1)) {
1245 $drop_index_query .= " , " ;
1248 $drop_index_query .= " ; " ;
1249 if ($display == true) {
1250 echo '<p>' . $drop_index_query . '</p>';
1252 PMA_DBI_try_query($drop_index_query, $trg_link, 0);
1257 * PMA_displayQuery() displays a query, taking the maximum display size
1258 * into account
1260 * @param $query the query to display
1262 function PMA_displayQuery($query) {
1263 if (strlen($query) > $GLOBALS['cfg']['MaxCharactersInDisplayedSQL']) {
1264 $query = substr($query, 0, $GLOBALS['cfg']['MaxCharactersInDisplayedSQL']) . '[...]';
1266 echo '<p>' . htmlspecialchars($query) . '</p>';
1270 * PMA_syncDisplayHeaderSource() shows the header for source database
1272 * @param string $src_db source db name
1274 function PMA_syncDisplayHeaderSource($src_db) {
1275 echo '<div id="serverstatus" style = "overflow: auto; width: 1020px; height: 220px; border-left: 1px gray solid; border-bottom: 1px gray solid; padding:0; margin-bottom: 1em "> ';
1277 echo '<table id="serverstatusconnections" class="data" width="55%">';
1278 echo '<tr>';
1279 echo '<th>' . __('Source database') . ': ' . $src_db . '<br />(';
1280 if ('cur' == $_SESSION['src_type']) {
1281 echo __('Current server');
1282 } else {
1283 echo __('Remote server') . ' ' . $_SESSION['src_server']['host'];
1285 echo ')</th>';
1286 echo '<th>' . __('Difference') . '</th>';
1287 echo '</tr>';
1291 * PMA_syncDisplayHeaderTargetAndMatchingTables() shows the header for target database and the matching tables
1293 * @param string $trg_db target db name
1294 * @param array $matching_tables
1295 * @return boolean $odd_row current value of this toggle
1297 function PMA_syncDisplayHeaderTargetAndMatchingTables($trg_db, $matching_tables) {
1298 echo '<table id="serverstatusconnections" class="data" width="43%">';
1299 echo '<tr>';
1300 echo '<th>' . __('Target database') . ': '. $trg_db . '<br />(';
1301 if ('cur' == $_SESSION['trg_type']) {
1302 echo __('Current server');
1303 } else {
1304 echo __('Remote server') . ' ' . $_SESSION['trg_server']['host'];
1306 echo ')</th>';
1307 echo '</tr>';
1308 $odd_row = false;
1309 foreach ($matching_tables as $tbl_name) {
1310 $odd_row = PMA_syncDisplayBeginTableRow($odd_row);
1311 echo '<td> ' . htmlspecialchars($tbl_name) . '</td>';
1312 echo '</tr>';
1314 return $odd_row;
1318 * PMA_syncDisplayBeginTableRow() displays the TR tag for alternating colors
1320 * @param boolean $odd_row current status of the toggle
1321 * @return boolean $odd_row final status of the toggle
1323 function PMA_syncDisplayBeginTableRow($odd_row) {
1324 $odd_row = ! $odd_row;
1325 echo '<tr height="32" class=" ';
1326 echo $odd_row ? 'odd' : 'even';
1327 echo '">';
1328 return $odd_row;
1332 * array PMA_get_column_values (string $database, string $table, string $column , mysql db link $link = null)
1334 * @param string $database name of database
1335 * @param string $table name of table to retrieve columns from
1336 * @param string $column name of the column to retrieve data from
1337 * @param mixed $link mysql link resource
1338 * @return array $field_values
1340 function PMA_get_column_values($database, $table, $column, $link = null)
1342 $query = 'SELECT ';
1343 for($i=0; $i< sizeof($column); $i++)
1345 $query.= PMA_backquote($column[$i]);
1346 if($i < (sizeof($column)-1))
1348 $query.= ', ';
1351 $query.= ' FROM ' . PMA_backquote($database) . '.' . PMA_backquote($table);
1352 $field_values = PMA_DBI_fetch_result($query, null, null, $link);
1354 if (! is_array($field_values) || count($field_values) < 1) {
1355 return false;
1357 return $field_values;
1361 * array PMA_get_table_indexes($database, $table, $link = null)
1363 * @param string $database name of database
1364 * @param string $table name of the table whose indexes are to be retreived
1365 * @param mixed $link mysql link resource
1366 * @return array $indexes
1368 function PMA_get_table_indexes($database, $table, $link = null)
1371 $indexes = PMA_DBI_fetch_result(
1372 'SHOW INDEXES FROM ' .PMA_backquote($database) . '.' . PMA_backquote($table),
1373 null, null, $link);
1375 if (! is_array($indexes) || count($indexes) < 1) {
1376 return false;
1378 return $indexes;