Merge branch 'master' of ssh://phpmyadmin.git.sourceforge.net/gitroot/phpmyadmin...
[phpmyadmin.git] / libraries / server_synchronize.lib.php
bloba862a60dbf5e1592a1c47a5c7f6269ef6568d59a
1 <?php
2 /* vim: set expandtab sw=4 ts=4 sts=4: */
3 /**
4 * Server synchronisation functions.
6 * @package phpMyAdmin
7 */
9 /**
10 * Places matching tables in source and target databases in $matching_tables
11 * array whereas $uncommon_source_tables array gets the tables present in
12 * source database but are absent from target database. Criterion for
13 * matching tables is just comparing their names.
15 * @param array $trg_tables array of target database table names,
16 * @param array $src_tables array of source database table names,
17 * @param array &$matching_tables empty array passed by reference to save
18 * names of matching tables,
19 * @param array &$uncommon_source_tables empty array passed by reference to save
20 * names of tables present in source database
21 * but absent from target database
23 function PMA_getMatchingTables($trg_tables, $src_tables, &$matching_tables, &$uncommon_source_tables)
25 for ($k=0; $k< sizeof($src_tables); $k++) {
26 $present_in_target = false;
27 for ($l=0; $l < sizeof($trg_tables); $l++) {
28 if ($src_tables[$k] === $trg_tables[$l]) {
29 $present_in_target = true;
30 $matching_tables[] = $src_tables[$k];
33 if ($present_in_target === false) {
34 $uncommon_source_tables[] = $src_tables[$k];
39 /**
40 * Places tables present in target database but are absent from source database
42 * @param array $trg_tables array of target database table names,
43 * @param array $matching_tables matching tables array containing names
44 * of matching tables,
45 * @param array &$uncommon_target_tables empty array passed by reference to save
46 * names of tables presnet in target database
47 * but absent from source database
49 function PMA_getNonMatchingTargetTables($trg_tables, $matching_tables, &$uncommon_target_tables)
51 for ($c=0; $c<sizeof($trg_tables); $c++) {
52 $match = false;
53 for ($d=0; $d < sizeof($matching_tables); $d++) {
54 if ($trg_tables[$c] === $matching_tables[$d]) {
55 $match=true;
58 if ($match === false) {
59 $uncommon_target_tables[] = $trg_tables[$c];
64 /**
65 * Finds the difference in source and target matching tables by
66 * first comparing source table's primary key entries with target table enteries.
67 * It gets the field names for the matching table also for comparisons.
68 * If the entry is found in target table also then it is checked for the remaining
69 * field values also, in order to check whether update is required or not.
70 * If update is required, it is placed in $update_array
71 * Otherwise that entry is placed in the $insert_array.
73 * @param string $src_db name of source database
74 * @param string $trg_db name of target database
75 * @param db_link $src_link connection established with source server
76 * @param db_link $trg_link connection established with target server
77 * @param array &$matching_table array containing matching table names
78 * @param array &$matching_tables_fields A two dimensional array passed by reference to contain names of fields for each matching table
79 * @param array &$update_array A three dimensional array passed by reference to
80 * contain updates required for each matching table
81 * @param array &$insert_array A three dimensional array passed by reference to
82 * contain inserts required for each matching table
83 * @param array &$delete_array Unused
84 * @param array &$fields_num A two dimensional array passed by reference to
85 * contain number of fields for each matching table
86 * @param int $matching_table_index Index of a table from $matching_table array
87 * @param array &$matching_tables_keys A two dimensional array passed by reference to contain names of keys for each matching table
89 function PMA_dataDiffInTables($src_db, $trg_db, $src_link, $trg_link, &$matching_table, &$matching_tables_fields,
90 &$update_array, &$insert_array, &$delete_array, &$fields_num, $matching_table_index, &$matching_tables_keys)
92 if (isset($matching_table[$matching_table_index])) {
93 $fld = array();
94 $fld_results = PMA_DBI_get_columns($src_db, $matching_table[$matching_table_index], true, $src_link);
95 $is_key = array();
96 if (isset($fld_results)) {
97 foreach ($fld_results as $each_field) {
98 $field_name = $each_field['Field'];
99 if ($each_field['Key'] == 'PRI') {
100 $is_key[] = $field_name;
102 $fld[] = $field_name;
105 $matching_tables_fields[$matching_table_index] = $fld;
106 $fields_num[$matching_table_index] = sizeof($fld);
107 $matching_tables_keys[$matching_table_index] = $is_key;
109 $source_result_set = PMA_get_column_values($src_db, $matching_table[$matching_table_index], $is_key, $src_link);
110 $source_size = sizeof($source_result_set);
112 $trg_fld_results = PMA_DBI_get_columns($trg_db, $matching_table[$matching_table_index], true, $trg_link);
113 $all_keys_match = true;
114 $trg_keys = array();
116 if (isset($trg_fld_results)) {
117 foreach ($trg_fld_results as $each_field) {
118 if ($each_field['Key'] == 'PRI') {
119 $trg_keys[] = $each_field['Field'];
120 if (! (in_array($each_field['Field'], $is_key))) {
121 $all_keys_match = false;
126 $update_row = 0;
127 $insert_row = 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 } elseif (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];
206 } elseif (sizeof($is_key) > 1) {
207 for ($n = 0; $n < sizeof($is_key); $n++) {
208 if (isset($src_result_set[0][$is_key[$n]])) {
209 $update_array[$matching_table_index][$update_row][$is_key[$n]] = $src_result_set[0][$is_key[$n]];
214 $update_array[$matching_table_index][$update_row][$update_field] = $fld[$m];
216 $update_field++;
217 if (isset($src_result_set[0][$fld[$m]])) {
218 $update_array[$matching_table_index][$update_row][$update_field] = $src_result_set[0][$fld[$m]];
219 $update_field++;
221 $starting_index = $m;
222 $update_row++;
226 for ($m = $starting_index + 1; $m < $fields_num[$matching_table_index] ; $m++) {
227 if (isset($src_result_set[0][$fld[$m]])) {
228 if (isset($target_result_set[0][$fld[$m]])) {
229 if (($src_result_set[0][$fld[$m]] != $target_result_set[0][$fld[$m]]) && (!(in_array($fld[$m], $is_key)))) {
230 $update_row--;
231 $update_array[$matching_table_index][$update_row][$update_field] = $fld[$m];
232 $update_field++;
233 if ($src_result_set[0][$fld[$m]]) {
234 $update_array[$matching_table_index][$update_row][$update_field] = $src_result_set[0][$fld[$m]];
235 $update_field++;
237 $update_row++;
239 } else {
240 $update_row--;
241 $update_array[$matching_table_index][$update_row][$update_field] = $fld[$m];
242 $update_field++;
243 if ($src_result_set[0][$fld[$m]]) {
244 $update_array[$matching_table_index][$update_row][$update_field] = $src_result_set[0][$fld[$m]];
245 $update_field++;
247 $update_row++;
251 } else {
253 * Placing the primary key, and the value of primary key of the row that is to be inserted in the target table
255 if (sizeof($is_key) == 1) {
256 if (isset($source_result_set[$j])) {
257 $insert_array[$matching_table_index][$insert_row][$is_key[0]] = $source_result_set[$j];
259 } elseif (sizeof($is_key) > 1) {
260 for ($l = 0; $l < sizeof($is_key); $l++) {
261 if (isset($source_result_set[$j][$matching_tables_fields[$matching_table_index][$l]])) {
262 $insert_array[$matching_table_index][$insert_row][$is_key[$l]] = $source_result_set[$j][$matching_tables_fields[$matching_table_index][$l]];
266 $insert_row++;
268 } else {
270 * Placing the primary key, and the value of primary key of the row that is to be inserted in the target table
271 * This condition is met when there is an additional column in the source table
273 if (sizeof($is_key) == 1) {
274 if (isset($source_result_set[$j])) {
275 $insert_array[$matching_table_index][$insert_row][$is_key[0]] = $source_result_set[$j];
277 } elseif (sizeof($is_key) > 1) {
278 for ($l = 0; $l < sizeof($is_key); $l++) {
279 if (isset($source_result_set[$j][$matching_tables_fields[$matching_table_index][$l]])) {
280 $insert_array[$matching_table_index][$insert_row][$is_key[$l]] = $source_result_set[$j][$matching_tables_fields[$matching_table_index][$l]];
284 $insert_row++;
286 } // for loop ends
291 * Finds the rows which are to be deleted from target table.
293 * @param array &$delete_array array containing rows that are to be deleted
294 * @param array $matching_table array containing matching table names
295 * @param int $matching_table_index index of a table from $matching_table array
296 * @param array $trg_keys array of target table keys
297 * @param array $src_keys array of source table keys
298 * @param string $trg_db name of target database
299 * @param db_link $trg_link connection established with target server
300 * @param string $src_db name of source database
301 * @param db_link $src_link connection established with source server
303 function PMA_findDeleteRowsFromTargetTables(&$delete_array, $matching_table, $matching_table_index, $trg_keys, $src_keys, $trg_db, $trg_link, $src_db, $src_link)
305 if (isset($trg_keys[$matching_table_index])) {
306 $target_key_values = PMA_get_column_values($trg_db, $matching_table[$matching_table_index], $trg_keys[$matching_table_index], $trg_link);
308 if (isset($src_keys[$matching_table_index])) {
309 $source_key_values = PMA_get_column_values($src_db, $matching_table[$matching_table_index], $src_keys[$matching_table_index], $src_link);
311 $all_keys_match = 1;
312 for ($a = 0; $a < sizeof($trg_keys[$matching_table_index]); $a++) {
313 if (isset($trg_keys[$matching_table_index][$a])) {
314 if (! (in_array($trg_keys[$matching_table_index][$a], $src_keys[$matching_table_index]))) {
315 $all_keys_match = 0;
319 if (! ($all_keys_match)) {
320 if (isset($target_key_values)) {
321 $delete_array[$matching_table_index] = $target_key_values;
324 if (isset($trg_keys[$matching_table_index])) {
325 if ((sizeof($trg_keys[$matching_table_index]) == 1) && $all_keys_match) {
326 $row = 0;
327 if (isset($target_key_values)) {
328 for ($i = 0; $i < sizeof($target_key_values); $i++) {
329 if (! (in_array($target_key_values[$i], $source_key_values))) {
330 $delete_array[$matching_table_index][$row] = $target_key_values[$i];
331 $row++;
335 } elseif ((sizeof($trg_keys[$matching_table_index]) > 1) && $all_keys_match) {
336 $row = 0;
337 if (isset($target_key_values)) {
338 for ($i = 0; $i < sizeof($target_key_values); $i++) {
339 $is_present = false;
340 for ($j = 0; $j < sizeof($source_key_values) && ($is_present == false) ; $j++) {
341 $check = true;
342 for ($k = 0; $k < sizeof($trg_keys[$matching_table_index]); $k++) {
343 if ($target_key_values[$i][$trg_keys[$matching_table_index][$k]] != $source_key_values[$j][$trg_keys[$matching_table_index][$k]]) {
344 $check = false;
347 if ($check) {
348 $is_present = true;
351 if (! ($is_present)) {
352 for ($l = 0; $l < sizeof($trg_keys[$matching_table_index]); $l++) {
353 $delete_array[$matching_table_index][$row][$trg_keys[$matching_table_index][$l]] = $target_key_values[$i][$trg_keys[$matching_table_index][$l]];
355 $row++;
364 * PMA_dataDiffInUncommonTables() finds the data difference in $source_tables_uncommon
366 * @param array $source_tables_uncommon table names that are in source db and not in target db
367 * @param string $src_db name of source database
368 * @param mixed $src_link connection established with source server
369 * @param int $index index of a table from $matching_table array
370 * @param array $row_count number of rows
372 function PMA_dataDiffInUncommonTables($source_tables_uncommon, $src_db, $src_link, $index, &$row_count)
374 $query = "SELECT COUNT(*) FROM " . PMA_backquote($src_db) . "." . PMA_backquote($source_tables_uncommon[$index]);
375 $rows = PMA_DBI_fetch_result($query, null, null, $src_link);
376 $row_count[$index] = $rows[0];
380 * PMA_updateTargetTables() sets the updated field values to target table rows using $update_array[$matching_table_index]
383 * @param array $table Matching tables' names
384 * @param array $update_array A three dimensional array containing field
385 * value updates required for each matching table
386 * @param string $src_db Name of source database
387 * @param string $trg_db Name of target database
388 * @param mixed $trg_link Connection established with target server
389 * @param int $matching_table_index index of matching table in matching_table_array
390 * @param array $matching_table_keys
391 * @param boolean $display
393 function PMA_updateTargetTables($table, $update_array, $src_db, $trg_db, $trg_link, $matching_table_index, $matching_table_keys, $display)
395 if (isset($update_array[$matching_table_index])) {
396 if (sizeof($update_array[$matching_table_index])) {
398 for ($update_row = 0; $update_row < sizeof($update_array[$matching_table_index]); $update_row++) {
400 if (isset($update_array[$matching_table_index][$update_row])) {
401 $update_fields_num = sizeof($update_array[$matching_table_index][$update_row])-sizeof($matching_table_keys[$matching_table_index]);
402 if ($update_fields_num > 0) {
403 $query = "UPDATE " . PMA_backquote($trg_db) . "." .PMA_backquote($table[$matching_table_index]) . " SET ";
405 for ($update_field = 0; $update_field < $update_fields_num; $update_field = $update_field+2) {
406 if (isset($update_array[$matching_table_index][$update_row][$update_field]) && isset($update_array[$matching_table_index][$update_row][$update_field+1])) {
407 $query .= $update_array[$matching_table_index][$update_row][$update_field] . "='" . $update_array[$matching_table_index][$update_row][$update_field+1] . "'";
409 if ($update_field < ($update_fields_num - 2)) {
410 $query .= ", ";
413 $query .= " WHERE ";
414 if (isset($matching_table_keys[$matching_table_index])) {
415 for ($key = 0; $key < sizeof($matching_table_keys[$matching_table_index]); $key++)
417 if (isset($matching_table_keys[$matching_table_index][$key])) {
418 $query .= $matching_table_keys[$matching_table_index][$key] . "='" . $update_array[$matching_table_index][$update_row][$matching_table_keys[$matching_table_index][$key]] . "'";
420 if ($key < (sizeof($matching_table_keys[$matching_table_index]) - 1)) {
421 $query .= " AND ";
425 if ($display == true) {
426 echo "<p>" . $query . "</p>";
428 PMA_DBI_try_query($query, $trg_link, 0);
437 * PMA_insertIntoTargetTable() inserts missing rows in the target table using $array_insert[$matching_table_index]
439 * @param array $matching_table matching table names
440 * @param string $src_db name of source database
441 * @param string $trg_db name of target database
442 * @param mixed $src_link connection established with source server
443 * @param mixed $trg_link connection established with target server
444 * @param array $table_fields field names of a table
445 * @param array &$array_insert
446 * @param int $matching_table_index index of matching table in matching_table_array
447 * @param array $matching_tables_keys field names that are keys in the matching table
448 * @param array $source_columns source column information
449 * @param array &$add_column_array column names that are to be added in target table
450 * @param array $criteria criteria like type, null, collation, default etc
451 * @param array $target_tables_keys field names that are keys in the target table
452 * @param array $uncommon_tables table names that are present in source db but not in targt db
453 * @param array &$uncommon_tables_fields field names of the uncommon tables
454 * @param array $uncommon_cols column names that are present in target table and not in source table
455 * @param array &$alter_str_array column names that are to be altered
456 * @param array &$source_indexes column names on which indexes are made in source table
457 * @param array &$target_indexes column names on which indexes are made in target table
458 * @param array &$add_indexes_array column names on which index is to be added in target table
459 * @param array &$alter_indexes_array column names whose indexes are to be altered. Only index name and uniqueness of an index can be changed
460 * @param array &$delete_array rows that are to be deleted
461 * @param array &$update_array rows that are to be updated in target
462 * @param bool $display
464 function PMA_insertIntoTargetTable($matching_table, $src_db, $trg_db, $src_link, $trg_link, $table_fields, &$array_insert, $matching_table_index,
465 $matching_tables_keys, $source_columns, &$add_column_array, $criteria, $target_tables_keys, $uncommon_tables, &$uncommon_tables_fields, $uncommon_cols,
466 &$alter_str_array,&$source_indexes, &$target_indexes, &$add_indexes_array, &$alter_indexes_array, &$delete_array, &$update_array, $display)
468 if (isset($array_insert[$matching_table_index])) {
469 if (sizeof($array_insert[$matching_table_index])) {
470 for ($insert_row = 0; $insert_row< sizeof($array_insert[$matching_table_index]); $insert_row++) {
471 if (isset($array_insert[$matching_table_index][$insert_row][$matching_tables_keys[$matching_table_index][0]])) {
473 $select_query = "SELECT * FROM " . PMA_backquote($src_db) . "." . PMA_backquote($matching_table[$matching_table_index]) . " WHERE ";
474 for ($i = 0; $i < sizeof($matching_tables_keys[$matching_table_index]); $i++) {
475 $select_query .= $matching_tables_keys[$matching_table_index][$i] . "='";
476 $select_query .= $array_insert[$matching_table_index][$insert_row][$matching_tables_keys[$matching_table_index][$i]] . "'" ;
478 if ($i < (sizeof($matching_tables_keys[$matching_table_index]) - 1)) {
479 $select_query.= " AND ";
482 $select_query .= "; ";
483 $result = PMA_DBI_fetch_result ($select_query, null, null, $src_link);
484 $insert_query = "INSERT INTO " . PMA_backquote($trg_db) . "." . PMA_backquote($matching_table[$matching_table_index]) ." (";
486 for ($field_index = 0; $field_index < sizeof($table_fields[$matching_table_index]); $field_index++)
488 $insert_query .= $table_fields[$matching_table_index][$field_index];
490 $is_fk_query = "SELECT * FROM information_schema.KEY_COLUMN_USAGE WHERE TABLE_SCHEMA = '" . $trg_db ."'
491 AND TABLE_NAME = '" . $matching_table[$matching_table_index]. "'AND COLUMN_NAME = '" .
492 $table_fields[$matching_table_index][$field_index] . "' AND TABLE_NAME <> REFERENCED_TABLE_NAME;" ;
494 $is_fk_result = PMA_DBI_fetch_result($is_fk_query, null, null, $trg_link);
495 if (sizeof($is_fk_result) > 0) {
496 for ($j = 0; $j < sizeof($is_fk_result); $j++)
498 $table_index = array_keys($matching_table, $is_fk_result[$j]['REFERENCED_TABLE_NAME']);
500 if (isset($alter_str_array[$table_index[0]])) {
501 PMA_alterTargetTableStructure($trg_db, $trg_link, $matching_tables, $source_columns, $alter_str_array, $matching_tables_fields,
502 $criteria, $matching_tables_keys, $target_tables_keys, $table_index[0], $display);
503 unset($alter_str_array[$table_index[0]]);
505 if (isset($uncommon_columns[$table_index[0]])) {
506 PMA_removeColumnsFromTargetTable($trg_db, $trg_link, $matching_tables, $uncommon_columns, $table_index[0], $display);
507 unset($uncommon_columns[$table_index[0]]);
509 if (isset($add_column_array[$table_index[0]])) {
510 PMA_findDeleteRowsFromTargetTables($delete_array, $matching_tables, $table_index[0], $target_tables_keys, $matching_tables_keys,
511 $trg_db, $trg_link, $src_db, $src_link);
513 if (isset($delete_array[$table_index[0]])) {
514 PMA_deleteFromTargetTable($trg_db, $trg_link, $matching_tables, $table_index[0], $target_tables_keys, $delete_array, $display);
515 unset($delete_array[$table_index[0]]);
517 PMA_addColumnsInTargetTable($src_db, $trg_db, $src_link, $trg_link, $matching_tables, $source_columns, $add_column_array,
518 $matching_tables_fields, $criteria, $matching_tables_keys, $target_tables_keys, $uncommon_tables,$uncommon_tables_fields,
519 $table_index[0], $uncommon_cols, $display);
520 unset($add_column_array[$table_index[0]]);
522 if (isset($add_indexes_array[$table_index[0]]) || isset($remove_indexes_array[$table_index[0]])
523 || isset($alter_indexes_array[$table_index[0]])) {
524 PMA_applyIndexesDiff ($trg_db, $trg_link, $matching_tables, $source_indexes, $target_indexes, $add_indexes_array, $alter_indexes_array,
525 $remove_indexes_array, $table_index[0], $display);
527 unset($add_indexes_array[$table_index[0]]);
528 unset($alter_indexes_array[$table_index[0]]);
529 unset($remove_indexes_array[$table_index[0]]);
531 if (isset($update_array[$table_index[0]])) {
532 PMA_updateTargetTables($matching_tables, $update_array, $src_db, $trg_db, $trg_link, $table_index[0], $matching_table_keys,
533 $display);
534 unset($update_array[$table_index[0]]);
536 if (isset($array_insert[$table_index[0]])) {
537 PMA_insertIntoTargetTable($matching_table, $src_db, $trg_db, $src_link, $trg_link, $table_fields, $array_insert,
538 $table_index[0], $matching_tables_keys, $source_columns, $add_column_array, $criteria, $target_tables_keys, $uncommon_tables,
539 $uncommon_tables_fields, $uncommon_cols, $alter_str_array, $source_indexes, $target_indexes, $add_indexes_array,
540 $alter_indexes_array, $delete_array, $update_array, $display);
541 unset($array_insert[$table_index[0]]);
545 if ($field_index < sizeof($table_fields[$matching_table_index])-1) {
546 $insert_query .= ", ";
549 $insert_query .= ") VALUES(";
550 if (sizeof($table_fields[$matching_table_index]) == 1) {
551 $insert_query .= "'" . PMA_sqlAddSlashes($result[0]) . "'";
552 } else {
553 for ($field_index = 0; $field_index < sizeof($table_fields[$matching_table_index]); $field_index++) {
554 if (isset($result[0][$table_fields[$matching_table_index][$field_index]])) {
555 $insert_query .= "'" . PMA_sqlAddSlashes($result[0][$table_fields[$matching_table_index][$field_index]]) . "'";
556 } else {
557 $insert_query .= "'NULL'";
559 if ($field_index < (sizeof($table_fields[$matching_table_index])) - 1) {
560 $insert_query .= " ," ;
564 $insert_query .= ");";
565 if ($display == true) {
566 PMA_displayQuery($insert_query);
568 PMA_DBI_try_query($insert_query, $trg_link, 0);
576 * PMA_createTargetTables() Create the missing table $uncommon_table in target database
578 * @param string $src_db name of source database
579 * @param string $trg_db name of target database
580 * @param mixed $src_link connection established with source server
581 * @param mixed $trg_link connection established with target server
582 * @param array &$uncommon_tables names of tables present in source but not in target
583 * @param int $table_index index of table in $uncommon_tables array
584 * @param array &$uncommon_tables_fields field names of the uncommon table
585 * @param bool $display
587 function PMA_createTargetTables($src_db, $trg_db, $src_link, $trg_link, &$uncommon_tables, $table_index, &$uncommon_tables_fields, $display)
589 if (isset($uncommon_tables[$table_index])) {
590 $fields_result = PMA_DBI_get_columns($src_db, $uncommon_tables[$table_index], true, $src_link);
591 $fields = array();
592 foreach ($fields_result as $each_field) {
593 $field_name = $each_field['Field'];
594 $fields[] = $field_name;
596 $uncommon_tables_fields[$table_index] = $fields;
598 $Create_Query = PMA_DBI_fetch_value("SHOW CREATE TABLE " . PMA_backquote($src_db) . '.' . PMA_backquote($uncommon_tables[$table_index]), 0, 1, $src_link);
600 // Replace the src table name with a `dbname`.`tablename`
601 $Create_Table_Query = preg_replace('/' . preg_quote(PMA_backquote($uncommon_tables[$table_index]), '/') . '/',
602 PMA_backquote($trg_db) . '.' .PMA_backquote($uncommon_tables[$table_index]),
603 $Create_Query,
604 $limit = 1
607 $is_fk_query = "SELECT * FROM information_schema.KEY_COLUMN_USAGE WHERE TABLE_SCHEMA = '" . $src_db . "'
608 AND TABLE_NAME = '" . $uncommon_tables[$table_index] . "' AND TABLE_NAME <> REFERENCED_TABLE_NAME;" ;
610 $is_fk_result = PMA_DBI_fetch_result($is_fk_query, null, null, $src_link);
611 if (sizeof($is_fk_result) > 0) {
612 for ($j = 0; $j < sizeof($is_fk_result); $j++)
614 if (in_array($is_fk_result[$j]['REFERENCED_TABLE_NAME'], $uncommon_tables)) {
615 $table_index = array_keys($uncommon_tables, $is_fk_result[$j]['REFERENCED_TABLE_NAME']);
616 PMA_createTargetTables($src_db, $trg_db, $trg_link, $src_link, $uncommon_tables, $table_index[0], $uncommon_tables_fields, $display);
617 unset($uncommon_tables[$table_index[0]]);
621 if ($display == true) {
622 echo '<p>' . $Create_Table_Query . '</p>';
624 PMA_DBI_try_query($Create_Table_Query, $trg_link, 0);
628 * PMA_populateTargetTables() inserts data into uncommon tables after they have been created
630 * @param string $src_db name of source database
631 * @param string $trg_db name of target database
632 * @param mixed $src_link connection established with source server
633 * @param mixed $trg_link connection established with target server
634 * @param array $uncommon_tables uncommon table names (table names that are present in source but not in target db)
635 * @param int $table_index index of table in matching_table_array
636 * @param array $uncommon_tables_fields field names of the uncommon table
637 * @param bool $display
639 * @todo This turns NULL values into '' (empty string)
641 function PMA_populateTargetTables($src_db, $trg_db, $src_link, $trg_link, $uncommon_tables, $table_index, $uncommon_tables_fields, $display)
643 $display = false; // todo: maybe display some of the queries if they are not too numerous
644 $unbuffered_result = PMA_DBI_try_query('SELECT * FROM ' . PMA_backquote($src_db) . '.' . PMA_backquote($uncommon_tables[$table_index]), $src_link, PMA_DBI_QUERY_UNBUFFERED);
645 if (false !== $unbuffered_result) {
646 $insert_query = 'INSERT INTO ' . PMA_backquote($trg_db) . '.' .PMA_backquote($uncommon_tables[$table_index]) . ' VALUES';
647 while ($one_row = PMA_DBI_fetch_row($unbuffered_result)) {
648 $insert_query .= '(';
649 $key_of_last_value = count($one_row) - 1;
650 foreach ($one_row as $key => $value) {
651 $insert_query .= "'" . PMA_sqlAddSlashes($value) . "'";
652 if ($key < $key_of_last_value) {
653 $insert_query .= ",";
656 $insert_query .= '),';
658 $insert_query = substr($insert_query, 0, -1);
659 $insert_query .= ';';
660 if ($display == true) {
661 PMA_displayQuery($insert_query);
663 PMA_DBI_try_query($insert_query, $trg_link, 0);
668 * PMA_deleteFromTargetTable() delete rows from target table
670 * @param string $trg_db name of target database
671 * @param mixed $trg_link connection established with target server
672 * @param array $matching_tables matching table names
673 * @param int $table_index index of table in matching_table_array
674 * @param array $target_tables_keys primary key names of the target tables
675 * @param array $delete_array key values of rows that are to be deleted
676 * @param bool $display
678 function PMA_deleteFromTargetTable($trg_db, $trg_link, $matching_tables, $table_index, $target_tables_keys, $delete_array, $display)
680 for ($i = 0; $i < sizeof($delete_array[$table_index]); $i++) {
681 if (isset($target_tables_keys[$table_index])) {
682 $delete_query = 'DELETE FROM ' . PMA_backquote($trg_db) . '.' .PMA_backquote($matching_tables[$table_index]) . ' WHERE ';
683 for ($y = 0; $y < sizeof($target_tables_keys[$table_index]); $y++) {
684 $delete_query .= $target_tables_keys[$table_index][$y] . " = '";
686 if (sizeof($target_tables_keys[$table_index]) == 1) {
687 $delete_query .= $delete_array[$table_index][$i] . "'";
688 } elseif (sizeof($target_tables_keys[$table_index]) > 1) {
689 $delete_query .= $delete_array[$table_index][$i][$target_tables_keys[$table_index][$y]] . "'";
691 if ($y < (sizeof($target_tables_keys[$table_index]) - 1)) {
692 $delete_query .= ' AND ';
694 $pk_query = "SELECT * FROM information_schema.KEY_COLUMN_USAGE WHERE REFERENCED_TABLE_SCHEMA = '" . $trg_db . "'
695 AND REFERENCED_TABLE_NAME = '" . $matching_tables[$table_index]."' AND REFERENCED_COLUMN_NAME = '"
696 . $target_tables_keys[$table_index][$y] . "' AND TABLE_NAME <> REFERENCED_TABLE_NAME;";
698 $pk_query_result = PMA_DBI_fetch_result($pk_query, null, null, $trg_link);
699 $result_size = sizeof($pk_query_result);
701 if ($result_size > 0) {
702 for ($b = 0; $b < $result_size; $b++) {
703 $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] . ";";
704 PMA_DBI_try_query($drop_pk_query, $trg_link, 0);
709 if ($display == true) {
710 echo '<p>' . $delete_query . '</p>';
712 PMA_DBI_try_query($delete_query, $trg_link, 0);
717 * PMA_structureDiffInTables() Gets all the column information for source and target table.
718 * Compare columns on their names.
719 * If column exists in target then compare Type, Null, Collation, Key, Default and Comment for that column.
720 * If column does not exist in target table then it is placed in $add_column_array.
721 * If column exists in target table but criteria is different then it is palced in $alter_str_array.
722 * If column does not exist in source table but is present in target table then it is placed in $uncommon_columns.
723 * Keys for all the source tables that have a corresponding target table are placed in $matching_tables_keys.
724 * Keys for all the target tables that have a corresponding source table are placed in $target_tables_keys.
726 * @param string $src_db name of source database
727 * @param string $trg_db name of target database
728 * @param mixed $src_link connection established with source server
729 * @param mixed $trg_link connection established with target server
730 * @param array $matching_tables names of matching tables
731 * @param array &$source_columns columns information of the source tables
732 * @param array &$target_columns columns information of the target tables
733 * @param array &$alter_str_array three dimensional associative array first index being the matching table index, second index being column name for which target
734 * column have some criteria different and third index containing the criteria which is different.
735 * @param array &$add_column_array two dimensional associative array, first index of the array contain the matching table number and second index contain the
736 * column name which is to be added in the target table
737 * @param array &$uncommon_columns columns that are present in the target table but not in the source table
738 * @param array $criteria criteria which are to be checked for field that is present in source table and target table
739 * @param array &$target_tables_keys field names which is key in the target table
740 * @param int $matching_table_index number of the matching table
742 function PMA_structureDiffInTables($src_db, $trg_db, $src_link, $trg_link, $matching_tables, &$source_columns, &$target_columns, &$alter_str_array,
743 &$add_column_array, &$uncommon_columns, $criteria, &$target_tables_keys, $matching_table_index)
745 //Gets column information for source and target table
746 $source_columns[$matching_table_index] = PMA_DBI_get_columns_full($src_db, $matching_tables[$matching_table_index], null, $src_link);
747 $target_columns[$matching_table_index] = PMA_DBI_get_columns_full($trg_db, $matching_tables[$matching_table_index], null, $trg_link);
748 foreach ($source_columns[$matching_table_index] as $column_name => $each_column) {
749 if (isset($target_columns[$matching_table_index][$column_name]['Field'])) {
750 //If column exists in target table then matches criteria like type, null, collation, key, default, comment of the column
751 for ($i = 0; $i < sizeof($criteria); $i++) {
752 if ($source_columns[$matching_table_index][$column_name][$criteria[$i]] != $target_columns[$matching_table_index][$column_name][$criteria[$i]]) {
753 if (($criteria[$i] == 'Default') && ($source_columns[$matching_table_index][$column_name][$criteria[$i]] == '' )) {
754 $alter_str_array[$matching_table_index][$column_name][$criteria[$i]] = 'None';
755 } else {
756 if (! (($criteria[$i] == 'Key') && (($source_columns[$matching_table_index][$column_name][$criteria[$i]] == 'MUL')
757 || ($target_columns[$matching_table_index][$column_name][$criteria[$i]] == 'MUL')
758 || ($source_columns[$matching_table_index][$column_name][$criteria[$i]] == 'UNI')
759 || ($target_columns[$matching_table_index][$column_name][$criteria[$i]] == 'UNI')))) {
760 $alter_str_array[$matching_table_index][$column_name][$criteria[$i]] = $source_columns[$matching_table_index][$column_name][$criteria[$i]];
765 } else {
766 $add_column_array[$matching_table_index][$column_name]= $column_name;
769 //Finds column names that are present in target table but not in source table
770 foreach ($target_columns[$matching_table_index] as $fld_name => $each_column) {
771 if (! (isset($source_columns[$matching_table_index][$fld_name]['Field']))) {
772 $fields_uncommon[] = $fld_name;
774 if ($target_columns[$matching_table_index][$fld_name]['Key'] == 'PRI') {
775 $keys[] = $fld_name;
778 if (isset($fields_uncommon)) {
779 $uncommon_columns[$matching_table_index] = $fields_uncommon;
781 if (isset($keys)) {
782 $target_tables_keys[$matching_table_index] = $keys;
786 * PMA_addColumnsInTargetTable() adds column that are present in source table but not in target table
788 * @param string $src_db name of source database
789 * @param string $trg_db name of target database
790 * @param mixed $src_link connection established with source server
791 * @param mixed $trg_link connection established with target server
792 * @param array $matching_tables names of matching tables
793 * @param array $source_columns columns information of the source tables
794 * @param array &$add_column_array the names of the column(field) that are to be added in the target
795 * @param array $matching_tables_fields
796 * @param array $criteria criteria
797 * @param array $matching_tables_keys field names which is key in the source table
798 * @param array $target_tables_keys field names which is key in the target table
799 * @param array $uncommon_tables table names that are present in source db and not in target db
800 * @param array &$uncommon_tables_fields names of the fields of the uncommon tables
801 * @param int $table_counter number of the matching table
802 * @param array $uncommon_cols
803 * @param bool $display
805 function PMA_addColumnsInTargetTable($src_db, $trg_db, $src_link, $trg_link, $matching_tables, $source_columns, &$add_column_array, $matching_tables_fields,
806 $criteria, $matching_tables_keys, $target_tables_keys, $uncommon_tables, &$uncommon_tables_fields, $table_counter, $uncommon_cols, $display)
808 for ($i = 0; $i < sizeof($matching_tables_fields[$table_counter]); $i++) {
809 if (isset($add_column_array[$table_counter][$matching_tables_fields[$table_counter][$i]])) {
810 $query = "ALTER TABLE " . PMA_backquote($trg_db) . '.' . PMA_backquote($matching_tables[$table_counter]). " ADD COLUMN " .
811 $add_column_array[$table_counter][$matching_tables_fields[$table_counter][$i]] . " " . $source_columns[$table_counter][$matching_tables_fields[$table_counter][$i]]['Type'];
813 if ($source_columns[$table_counter][$matching_tables_fields[$table_counter][$i]]['Null'] == 'NO') {
814 $query .= ' Not Null ';
815 } elseif ($source_columns[$table_counter][$matching_tables_fields[$table_counter][$i]]['Null'] == 'YES') {
816 $query .= ' Null ';
818 if ($source_columns[$table_counter][$matching_tables_fields[$table_counter][$i]]['Collation'] != '') {
819 $query .= ' COLLATE ' . $source_columns[$table_counter][$matching_tables_fields[$table_counter][$i]]['Collation'];
821 if ($source_columns[$table_counter][$matching_tables_fields[$table_counter][$i]]['Default'] != '') {
822 $query .= " DEFAULT " . $source_columns[$table_counter][$matching_tables_fields[$table_counter][$i]]['Default'];
824 if ($source_columns[$table_counter][$matching_tables_fields[$table_counter][$i]]['Comment'] != '') {
825 $query .= " COMMENT " . $source_columns[$table_counter][$matching_tables_fields[$table_counter][$i]]['Comment'];
827 if ($source_columns[$table_counter][$matching_tables_fields[$table_counter][$i]]['Key'] == 'PRI' ) {
828 $trg_key_size = sizeof($target_tables_keys[$table_counter]);
829 if ($trg_key_size) {
830 $check = true;
831 for ($a = 0; ($a < $trg_key_size) && ($check); $a++) {
832 if (! (in_array($target_tables_keys[$table_counter], $uncommon_cols))) {
833 $check = false;
836 if (! $check) {
837 $query .= " ,DROP PRIMARY KEY " ;
840 $query .= " , ADD PRIMARY KEY (";
841 for ($t = 0; $t < sizeof($matching_tables_keys[$table_counter]); $t++) {
842 $query .= $matching_tables_keys[$table_counter][$t];
843 if ($t < (sizeof($matching_tables_keys[$table_counter]) - 1)) {
844 $query .= " , " ;
847 $query .= ")";
850 $query .= ";";
851 if ($display == true) {
852 echo '<p>' . $query . '</p>';
854 PMA_DBI_try_query($query, $trg_link, 0);
856 //Checks if column to be added is a foreign key or not
857 $is_fk_query = "SELECT * FROM information_schema.KEY_COLUMN_USAGE WHERE TABLE_SCHEMA = '" . $trg_db . "' AND TABLE_NAME = '"
858 . $matching_tables[$table_counter] . "' AND COLUMN_NAME ='" . $add_column_array[$table_counter][$matching_tables_fields[$table_counter][$i]] .
859 "' AND TABLE_NAME <> REFERENCED_TABLE_NAME;";
861 $is_fk_result = PMA_DBI_fetch_result($is_fk_query, null, null, $src_link);
863 //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
864 //it is created first.
865 if (isset($is_fk_result)) {
866 if (in_array($is_fk_result[0]['REFERENCED_TABLE_NAME'], $uncommon_tables)) {
867 $table_index = array_keys($uncommon_tables, $is_fk_result[0]['REFERENCED_TABLE_NAME']);
868 PMA_checkForeignKeys($src_db, $src_link, $trg_db, $trg_link, $is_fk_result[0]['REFERENCED_TABLE_NAME'], $uncommon_tables, $uncommon_tables_fields);
869 PMA_createTargetTables($src_db, $trg_db, $trg_link, $src_link, $uncommon_tables, $table_index[0], $uncommon_tables_fields);
870 unset($uncommon_tables[$table_index[0]]);
872 $fk_query = "ALTER TABLE " . PMA_backquote($trg_db) . '.' . PMA_backquote($matching_tables[$table_counter]) .
873 "ADD CONSTRAINT FOREIGN KEY " . $add_column_array[$table_counter][$matching_tables_fields[$table_counter][$i]] . "
874 (" . $add_column_array[$table_counter][$matching_tables_fields[$table_counter][$i]] . ") REFERENCES " . PMA_backquote($trg_db) .
875 '.' . PMA_backquote($is_fk_result[0]['REFERENCED_TABLE_NAME']) . " (" . $is_fk_result[0]['REFERENCED_COLUMN_NAME'] . ");";
877 PMA_DBI_try_query($fk_query, $trg_link, null);
883 * PMA_checkForeignKeys() checks if the referenced table have foreign keys.
884 * uses PMA_createTargetTables()
886 * @param string $src_db name of source database
887 * @param mixed $src_link connection established with source server
888 * @param string $trg_db name of target database
889 * @param mixed $trg_link connection established with target server
890 * @param string $referenced_table table whose column is a foreign key in another table
891 * @param array &$uncommon_tables names that are uncommon
892 * @param array &$uncommon_tables_fields field names of the uncommon table
893 * @param bool $display
895 function PMA_checkForeignKeys($src_db, $src_link, $trg_db, $trg_link, $referenced_table, &$uncommon_tables, &$uncommon_tables_fields, $display)
897 $is_fk_query = "SELECT * FROM information_schema.KEY_COLUMN_USAGE WHERE TABLE_SCHEMA = '" . $src_db . "'
898 AND TABLE_NAME = '" . $referenced_table . "' AND TABLE_NAME <> REFERENCED_TABLE_NAME;";
900 $is_fk_result = PMA_DBI_fetch_result($is_fk_query, null, null, $src_link);
901 if (sizeof($is_fk_result) > 0) {
902 for ($j = 0; $j < sizeof($is_fk_result); $j++) {
903 if (in_array($is_fk_result[$j]['REFERENCED_TABLE_NAME'], $uncommon_tables)) {
904 $table_index = array_keys($uncommon_tables, $is_fk_result[$j]['REFERENCED_TABLE_NAME']);
905 PMA_checkForeignKeys($src_db, $src_link, $trg_db, $trg_link, $is_fk_result[$j]['REFERENCED_TABLE_NAME'], $uncommon_tables,
906 $uncommon_tables_fields, $display);
907 PMA_createTargetTables($src_db, $trg_db, $trg_link, $src_link, $uncommon_tables, $table_index[0], $uncommon_tables_fields, $display);
908 unset($uncommon_tables[$table_index[0]]);
914 * PMA_alterTargetTableStructure() alters structure of the target table using $alter_str_array
916 * @param string $trg_db name of target database
917 * @param mixed $trg_link connection established with target server
918 * @param array $matching_tables names of matching tables
919 * @param array &$source_columns columns information of the source table
920 * @param array &$alter_str_array column name and criteria which is to be altered for the targert table
921 * @param array $matching_tables_fields name of the fields for the matching table
922 * @param array $criteria criteria
923 * @param array &$matching_tables_keys field names which is key in the source table
924 * @param array &$target_tables_keys field names which is key in the target table
925 * @param int $matching_table_index number of the matching table
926 * @param bool $display
928 function PMA_alterTargetTableStructure($trg_db, $trg_link, $matching_tables, &$source_columns, &$alter_str_array, $matching_tables_fields, $criteria,
929 &$matching_tables_keys, &$target_tables_keys, $matching_table_index, $display)
931 $check = true;
932 $sql_query = '';
933 $found = false;
935 //Checks if the criteria to be altered is primary key
936 for ($v = 0; $v < sizeof($matching_tables_fields[$matching_table_index]); $v++) {
937 if (isset($alter_str_array[$matching_table_index][$matching_tables_fields[$matching_table_index][$v]]['Key'])) {
938 if ($alter_str_array[$matching_table_index][$matching_tables_fields[$matching_table_index][$v]]['Key'] == 'PRI' ) {
939 $check = false;
944 $pri_query = null;
945 if (! $check) {
946 $pri_query = "ALTER TABLE " . PMA_backquote($trg_db) . '.' . PMA_backquote($matching_tables[$matching_table_index]);
947 if (sizeof($target_tables_keys[$matching_table_index]) > 0) {
948 $pri_query .= " DROP PRIMARY KEY ," ;
950 $pri_query .= " ADD PRIMARY KEY (";
951 for ($z = 0; $z < sizeof($matching_tables_keys[$matching_table_index]); $z++) {
952 $pri_query .= $matching_tables_keys[$matching_table_index][$z];
953 if ($z < (sizeof($matching_tables_keys[$matching_table_index]) - 1)) {
954 $pri_query .= " , " ;
957 $pri_query .= ");";
960 if (isset($pri_query)) {
961 if ($display == true) {
962 echo '<p>' . $pri_query . '</p>';
964 PMA_DBI_try_query($pri_query, $trg_link, 0);
966 for ($t = 0; $t < sizeof($matching_tables_fields[$matching_table_index]); $t++) {
967 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)) {
968 $sql_query = 'ALTER TABLE ' . PMA_backquote($trg_db) . '.' . PMA_backquote($matching_tables[$matching_table_index]) . ' MODIFY ' .
969 $matching_tables_fields[$matching_table_index][$t] . ' ' . $source_columns[$matching_table_index][$matching_tables_fields[$matching_table_index][$t]]['Type'];
970 $found = false;
971 for ($i = 0; $i < sizeof($criteria); $i++)
973 if (isset($alter_str_array[$matching_table_index][$matching_tables_fields[$matching_table_index][$t]][$criteria[$i]]) && $criteria[$i] != 'Key') {
974 $found = true;
975 if (($criteria[$i] == 'Type') && (! isset($alter_str_array[$matching_table_index][$matching_tables_fields[$matching_table_index][$t]][$criteria[$i+1]]))) {
976 if ($source_columns[$matching_table_index][$matching_tables_fields[$matching_table_index][$t]][$criteria[$i + 1]] == 'NO') {
977 $sql_query .= " Not Null" ;
978 } elseif ($source_columns[$matching_table_index][$matching_tables_fields[$matching_table_index][$t]][$criteria[$i + 1]] == 'YES') {
979 $sql_query .= " Null" ;
982 if (($criteria[$i] == 'Null') && ( $alter_str_array[$matching_table_index][$matching_tables_fields[$matching_table_index][$t]][$criteria[$i]] == 'NO')) {
983 $sql_query .= " Not Null " ;
984 } elseif (($criteria[$i] == 'Null') && ($alter_str_array[$matching_table_index][$matching_tables_fields[$matching_table_index][$t]][$criteria[$i]] == 'YES')) {
985 $sql_query .= " Null " ;
987 if ($criteria[$i] == 'Collation') {
988 if ( !(isset($alter_str_array[$matching_table_index][$matching_tables_fields[$matching_table_index][$t]][$criteria[2]]))) {
989 $sql_query .= " Not Null " ;
991 $sql_query .= " COLLATE " . $alter_str_array[$matching_table_index][$matching_tables_fields[$matching_table_index][$t]][$criteria[$i]] ;
993 if (($criteria[$i] == 'Default') && ($alter_str_array[$matching_table_index][$matching_tables_fields[$matching_table_index][$t]][$criteria[$i]] == 'None')) {
994 if ( !(isset($alter_str_array[$matching_table_index][$matching_tables_fields[$matching_table_index][$t]][$criteria[2]]))) {
995 $sql_query .= " Not Null " ;
997 } elseif ($criteria[$i] == 'Default') {
998 if (! (isset($alter_str_array[$matching_table_index][$matching_tables_fields[$matching_table_index][$t]][$criteria[2]]))) {
999 $sql_query .= " Not Null " ;
1001 if (is_string($alter_str_array[$matching_table_index][$matching_tables_fields[$matching_table_index][$t]][$criteria[$i]])) {
1002 if ($source_columns[$matching_table_index][$matching_tables_fields[$matching_table_index][$t]]['Type'] != 'timestamp') {
1003 $sql_query .= " DEFAULT '" . $alter_str_array[$matching_table_index][$matching_tables_fields[$matching_table_index][$t]][$criteria[$i]] . "'";
1004 } elseif ($source_columns[$matching_table_index][$matching_tables_fields[$matching_table_index][$t]]['Type'] == 'timestamp') {
1005 $sql_query .= " DEFAULT " . $alter_str_array[$matching_table_index][$matching_tables_fields[$matching_table_index][$t]][$criteria[$i]];
1007 } elseif (is_numeric($alter_str_array[$matching_table_index][$matching_tables_fields[$matching_table_index][$t]][$criteria[$i]])) {
1008 $sql_query .= " DEFAULT " . $alter_str_array[$matching_table_index][$matching_tables_fields[$matching_table_index][$t]][$criteria[$i]];
1011 if ($criteria[$i] == 'Comment') {
1012 if ( !(isset($alter_str_array[$matching_table_index][$matching_tables_fields[$matching_table_index][$t]][$criteria[2]]))) {
1013 $sql_query .= " Not Null " ;
1015 $sql_query .= " COMMENT '" . $alter_str_array[$matching_table_index][$matching_tables_fields[$matching_table_index][$t]][$criteria[$i]] . "'" ;
1020 $sql_query .= ";";
1021 if ($found) {
1022 if ($display == true) {
1023 echo '<p>' . $sql_query . '</p>';
1025 PMA_DBI_try_query($sql_query, $trg_link, 0);
1028 $check = false;
1029 $query = "ALTER TABLE " . PMA_backquote($trg_db) . '.' . PMA_backquote($matching_tables[$matching_table_index]);
1030 for ($p = 0; $p < sizeof($matching_tables_keys[$matching_table_index]); $p++) {
1031 if ((isset($alter_str_array[$matching_table_index][$matching_tables_keys[$matching_table_index][$p]]['Key']))) {
1032 $check = true;
1033 $query .= ' MODIFY ' . $matching_tables_keys[$matching_table_index][$p] . ' '
1034 . $source_columns[$matching_table_index][$matching_tables_fields[$matching_table_index][$p]]['Type'] . ' Not Null ';
1035 if ($p < (sizeof($matching_tables_keys[$matching_table_index]) - 1)) {
1036 $query .= ', ';
1040 if ($check) {
1041 if ($display == true) {
1042 echo '<p>' . $query . '</p>';
1044 PMA_DBI_try_query($query, $trg_link, 0);
1049 * PMA_removeColumnsFromTargetTable() removes the columns which are present in target table but not in source table.
1051 * @param string $trg_db name of target database
1052 * @param mixed $trg_link connection established with target server
1053 * @param array $matching_tables names of matching tables
1054 * @param array $uncommon_columns array containing the names of the column which are to be dropped from the target table
1055 * @param int $table_counter index of the matching table as in $matchiing_tables array
1056 * @param bool $display
1058 function PMA_removeColumnsFromTargetTable($trg_db, $trg_link, $matching_tables, $uncommon_columns, $table_counter, $display)
1060 if (isset($uncommon_columns[$table_counter])) {
1061 $drop_query = "ALTER TABLE " . PMA_backquote($trg_db) . "." . PMA_backquote($matching_tables[$table_counter]);
1062 for ($a = 0; $a < sizeof($uncommon_columns[$table_counter]); $a++) {
1063 //Checks if column to be removed is a foreign key in any table
1064 $pk_query = "SELECT * FROM information_schema.KEY_COLUMN_USAGE WHERE REFERENCED_TABLE_SCHEMA = '" . $trg_db . "'
1065 AND REFERENCED_TABLE_NAME = '" . $matching_tables[$table_counter]."' AND REFERENCED_COLUMN_NAME = '"
1066 . $uncommon_columns[$table_counter][$a] . "' AND TABLE_NAME <> REFERENCED_TABLE_NAME;";
1068 $pk_query_result = PMA_DBI_fetch_result($pk_query, null, null, $trg_link);
1069 $result_size = sizeof($pk_query_result);
1071 if ($result_size > 0) {
1072 for ($b = 0; $b < $result_size; $b++) {
1073 $drop_pk_query = "ALTER TABLE " . PMA_backquote($pk_query_result[$b]['TABLE_SCHEMA']) . "." . PMA_backquote($pk_query_result[$b]['TABLE_NAME']) . "
1074 DROP FOREIGN KEY " . $pk_query_result[$b]['CONSTRAINT_NAME'] . ", DROP COLUMN " . $pk_query_result[$b]['COLUMN_NAME'] . ";";
1075 PMA_DBI_try_query($drop_pk_query, $trg_link, 0);
1078 $query = "SELECT * FROM information_schema.KEY_COLUMN_USAGE WHERE TABLE_SCHEMA = '" . $trg_db . "' AND TABLE_NAME = '"
1079 . $matching_tables[$table_counter]. "' AND COLUMN_NAME = '" . $uncommon_columns[$table_counter][$a] . "'
1080 AND TABLE_NAME <> REFERENCED_TABLE_NAME;";
1082 $result = PMA_DBI_fetch_result($query, null, null, $trg_link);
1084 if (sizeof($result) > 0) {
1085 $drop_query .= " DROP FOREIGN KEY " . $result[0]['CONSTRAINT_NAME'] . ",";
1087 $drop_query .= " DROP COLUMN " . $uncommon_columns[$table_counter][$a];
1088 if ($a < (sizeof($uncommon_columns[$table_counter]) - 1)) {
1089 $drop_query .= " , " ;
1092 $drop_query .= ";" ;
1094 if ($display == true) {
1095 echo '<p>' . $drop_query . '</p>';
1097 PMA_DBI_try_query($drop_query, $trg_link, 0);
1102 * 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
1103 * indexes to be altered in $alter_indexes_array and indexes to be removed from target table in $remove_indexes_array.
1104 * Only keyname and uniqueness characteristic of the indexes are altered.
1106 * @param string $src_db name of source database
1107 * @param string $trg_db name of target database
1108 * @param mixed $src_link connection established with source server
1109 * @param mixed $trg_link connection established with target server
1110 * @param array $matching_tables matching tables name
1111 * @param array &$source_indexes indexes of the source table
1112 * @param array &$target_indexes indexes of the target table
1113 * @param array &$add_indexes_array name of the column on which the index is to be added in the target table
1114 * @param array &$alter_indexes_array key name which needs to be altered
1115 * @param array &$remove_indexes_array key name of the index which is to be removed from the target table
1116 * @param int $table_counter number of the matching table
1118 function PMA_indexesDiffInTables($src_db, $trg_db, $src_link, $trg_link, $matching_tables, &$source_indexes, &$target_indexes, &$add_indexes_array,
1119 &$alter_indexes_array, &$remove_indexes_array, $table_counter)
1121 //Gets indexes information for source and target table
1122 $source_indexes[$table_counter] = PMA_get_table_indexes($src_db, $matching_tables[$table_counter],$src_link);
1123 $target_indexes[$table_counter] = PMA_get_table_indexes($trg_db, $matching_tables[$table_counter],$trg_link);
1124 for ($a = 0; $a < sizeof($source_indexes[$table_counter]); $a++) {
1125 $found = false;
1126 $z = 0;
1127 //Compares key name and non_unique characteristic of source indexes with target indexes
1129 * @todo compare the length of each sub part
1131 while (($z <= sizeof($target_indexes[$table_counter])) && ($found == false))
1133 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']) {
1134 $found = true;
1135 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'])) {
1136 if (! (($source_indexes[$table_counter][$a]['Key_name'] == "PRIMARY") || ($target_indexes[$table_counter][$z]['Key_name'] == 'PRIMARY'))) {
1137 $alter_indexes_array[$table_counter][] = $source_indexes[$table_counter][$a]['Key_name'];
1141 $z++;
1143 if ($found === false) {
1144 if (! ($source_indexes[$table_counter][$a]['Key_name'] == 'PRIMARY')) {
1145 $add_indexes_array [$table_counter][] = $source_indexes[$table_counter][$a]['Column_name'];
1150 //Finds indexes that exist on target table but not on source table
1151 for ($b = 0; $b < sizeof($target_indexes[$table_counter]); $b++) {
1152 $found = false;
1153 $c = 0;
1154 while (($c <= sizeof($source_indexes[$table_counter])) && ($found == false))
1156 if ($target_indexes[$table_counter][$b]['Column_name'] == $source_indexes[$table_counter][$c]['Column_name']) {
1157 $found = true;
1159 $c++;
1161 if ($found === false) {
1162 $remove_indexes_array[$table_counter][] = $target_indexes[$table_counter][$b]['Key_name'];
1168 * PMA_applyIndexesDiff() create indexes, alters indexes and remove indexes.
1170 * @param string $trg_db name of target database
1171 * @param mixed $trg_link connection established with target server
1172 * @param array $matching_tables matching tables name
1173 * @param array $source_indexes indexes of the source table
1174 * @param array $target_indexes indexes of the target table
1175 * @param array $add_indexes_array column names on which indexes are to be created in target table
1176 * @param array $alter_indexes_array column names for which indexes are to be altered
1177 * @param array $remove_indexes_array key name of the indexes which are to be removed from the target table
1178 * @param int $table_counter number of the matching table
1179 * @param $display
1181 function PMA_applyIndexesDiff ($trg_db, $trg_link, $matching_tables, $source_indexes, $target_indexes, $add_indexes_array, $alter_indexes_array,
1182 $remove_indexes_array, $table_counter, $display)
1184 //Adds indexes on target table
1185 if (isset($add_indexes_array[$table_counter])) {
1186 $sql = "ALTER TABLE " . PMA_backquote($trg_db) . "." . PMA_backquote($matching_tables[$table_counter]) . " ADD" ;
1187 for ($a = 0; $a < sizeof($source_indexes[$table_counter]); $a++) {
1188 if (isset($add_indexes_array[$table_counter][$a])) {
1189 for ($b = 0; $b < sizeof($source_indexes[$table_counter]); $b++) {
1190 if ($source_indexes[$table_counter][$b]['Column_name'] == $add_indexes_array[$table_counter][$a]) {
1191 if ($source_indexes[$table_counter][$b]['Non_unique'] == '0') {
1192 $sql .= " UNIQUE ";
1194 $sql .= " INDEX " . $source_indexes[$table_counter][$b]['Key_name'] . " (" . $add_indexes_array[$table_counter][$a] . " );";
1195 if ($display == true) {
1196 echo '<p>' . $sql . '</p>';
1198 PMA_DBI_try_query($sql, $trg_link, 0);
1204 //Alter indexes of target table
1206 if (isset($alter_indexes_array[$table_counter])) {
1207 $query = "ALTER TABLE " . PMA_backquote($trg_db) . "." . PMA_backquote($matching_tables[$table_counter]);
1208 for ($a = 0; $a < sizeof($alter_indexes_array[$table_counter]); $a++) {
1209 if (isset($alter_indexes_array[$table_counter][$a])) {
1210 $query .= ' DROP INDEX ' . PMA_backquote($alter_indexes_array[$table_counter][$a]) . " , ADD ";
1211 $got_first_index_column = false;
1212 for ($z = 0; $z < sizeof($source_indexes[$table_counter]); $z++) {
1213 if ($source_indexes[$table_counter][$z]['Key_name'] == $alter_indexes_array[$table_counter][$a]) {
1214 if (! $got_first_index_column) {
1215 if ($source_indexes[$table_counter][$z]['Non_unique'] == '0') {
1216 $query .= " UNIQUE ";
1218 $query .= " INDEX " . PMA_backquote($source_indexes[$table_counter][$z]['Key_name']) . " (" . PMA_backquote($source_indexes[$table_counter][$z]['Column_name']);
1219 $got_first_index_column = true;
1220 } else {
1221 // another column for this index
1222 $query .= ', ' . PMA_backquote($source_indexes[$table_counter][$z]['Column_name']);
1226 $query .= " )";
1229 if ($display == true) {
1230 echo '<p>' . $query . '</p>';
1232 PMA_DBI_try_query($query, $trg_link, 0);
1234 //Removes indexes from target table
1235 if (isset($remove_indexes_array[$table_counter])) {
1236 $drop_index_query = "ALTER TABLE " . PMA_backquote($trg_db) . "." . PMA_backquote($matching_tables[$table_counter]);
1237 for ($a = 0; $a < sizeof($target_indexes[$table_counter]); $a++) {
1238 if (isset($remove_indexes_array[$table_counter][$a])) {
1239 $drop_index_query .= " DROP INDEX " . $remove_indexes_array[$table_counter][$a];
1241 if ($a < (sizeof($remove_indexes_array[$table_counter]) - 1)) {
1242 $drop_index_query .= " , " ;
1245 $drop_index_query .= " ; " ;
1246 if ($display == true) {
1247 echo '<p>' . $drop_index_query . '</p>';
1249 PMA_DBI_try_query($drop_index_query, $trg_link, 0);
1254 * PMA_displayQuery() displays a query, taking the maximum display size
1255 * into account
1257 * @param string $query the query to display
1259 function PMA_displayQuery($query)
1261 if (strlen($query) > $GLOBALS['cfg']['MaxCharactersInDisplayedSQL']) {
1262 $query = substr($query, 0, $GLOBALS['cfg']['MaxCharactersInDisplayedSQL']) . '[...]';
1264 echo '<p>' . htmlspecialchars($query) . '</p>';
1268 * PMA_syncDisplayHeaderCompare() shows the header for source database
1270 * @param string $src_db source db name
1271 * @param string $trg_db target db name
1273 function PMA_syncDisplayHeaderCompare($src_db, $trg_db)
1275 echo '<fieldset style="padding:0"><div style="padding:1.5em; overflow:auto; height:220px">';
1277 echo '<table class="data">';
1278 echo '<tr>';
1279 echo '<th>' . __('Source database') . ': ' . htmlspecialchars($src_db) . '<br />(';
1280 if ('cur' == $_SESSION['src_type']) {
1281 echo __('Current server');
1282 } else {
1283 echo __('Remote server') . ' ' . htmlspecialchars($_SESSION['src_server']['host']);
1285 echo ')</th>';
1286 echo '<th>' . __('Difference') . '</th>';
1287 echo '<th>' . __('Target database') . ': '. htmlspecialchars($trg_db) . '<br />(';
1288 if ('cur' == $_SESSION['trg_type']) {
1289 echo __('Current server');
1290 } else {
1291 echo __('Remote server') . ' ' . htmlspecialchars($_SESSION['trg_server']['host']);
1293 echo ')</th>';
1294 echo '</tr>';
1298 * Prints table row
1300 * $rows contains following keys:
1301 * - src_table_name - source server table name
1302 * - dst_table_name - target server table name
1303 * - btn_type - 'M' or 'U'
1304 * - btn_structure - null or arguments for showDetails in server_synchronize.js (without img_obj and table_name):
1305 * i, update_size, insert_size, remove_size, insert_index, remove_index
1307 * @param array $rows
1309 function PMA_syncDisplayDataCompare($rows)
1311 global $pmaThemeImage;
1313 $odd_row = true;
1314 foreach ($rows as $row) {
1315 echo '<tr class=" ' . ($odd_row ? 'odd' : 'even') . '">';
1316 echo '<td>' . htmlspecialchars($row['src_table_name']) . '</td><td style="text-align:center">';
1317 if (isset($row['btn_structure']) && $row['btn_structure']) {
1318 // parameters: i, update_size, insert_size, remove_size, insert_index, remove_index
1319 $p = $row['btn_structure'];
1320 $p[0] = $row['btn_type'] . 'S' . $p[0];
1321 echo '<img class="icon struct_img" src="' . $pmaThemeImage . 'new_struct.png" width="16" height="16"
1322 alt="Structure" title="' . __('Click to select') . '" style="cursor:pointer" onclick="showDetails('
1323 . "'" . implode($p, "','") . "'"
1324 . ', this, ' . "'" . PMA_escapeJsString(htmlspecialchars($row['src_table_name'])) . "'" . ')" /> ';
1326 if (isset($row['btn_data']) && $row['btn_data']) {
1327 // parameters: i, update_size, insert_size, remove_size, insert_index, remove_index
1328 $p = $row['btn_data'];
1329 $p[0] = $row['btn_type'] . 'D' . $p[0];
1330 echo '<img class="icon data_img" src="' . $pmaThemeImage . 'new_data.png" width="16" height="16"
1331 alt="Data" title="' . __('Click to select') . '" style="cursor:pointer" onclick="showDetails('
1332 . "'" . implode($p, "','") . "'"
1333 . ', this, ' . "'" . PMA_escapeJsString(htmlspecialchars($row['src_table_name'])) . "'" . ')" />';
1335 echo '</td><td>' . htmlspecialchars($row['dst_table_name']) . '</td></tr>';
1336 $odd_row = !$odd_row;
1341 * array PMA_get_column_values (string $database, string $table, string $column , mysql db link $link = null)
1343 * @param string $database name of database
1344 * @param string $table name of table to retrieve columns from
1345 * @param string $column name of the column to retrieve data from
1346 * @param mixed $link mysql link resource
1347 * @return array $field_values
1349 function PMA_get_column_values($database, $table, $column, $link = null)
1351 $query = 'SELECT ';
1352 for ($i=0; $i< sizeof($column); $i++)
1354 $query.= PMA_backquote($column[$i]);
1355 if ($i < (sizeof($column)-1))
1357 $query.= ', ';
1360 $query.= ' FROM ' . PMA_backquote($database) . '.' . PMA_backquote($table);
1361 $field_values = PMA_DBI_fetch_result($query, null, null, $link);
1363 if (! is_array($field_values) || count($field_values) < 1) {
1364 return false;
1366 return $field_values;
1370 * array PMA_get_table_indexes($database, $table, $link = null)
1372 * @param string $database name of database
1373 * @param string $table name of the table whose indexes are to be retreived
1374 * @param mixed $link mysql link resource
1375 * @return array $indexes
1377 function PMA_get_table_indexes($database, $table, $link = null)
1379 $indexes = PMA_DBI_fetch_result(
1380 'SHOW INDEXES FROM ' .PMA_backquote($database) . '.' . PMA_backquote($table),
1381 null, null, $link);
1383 if (!is_array($indexes) || count($indexes) == 0) {
1384 return null;
1386 return $indexes;