/** * if in_both = false, what rows in A are not in B ? * * if in_both = true, what rows are in A and B ? * - when a is empty, all of b's are returned * - a's row members are the ones returned when in_both rows are found * this is important when comparing tables whose rows are the same but have added columns * * @return void */ public static function table_data_rows_compare($table_a, $table_b, $in_both, &$a_rows, &$b_rows, &$changes) { $a_rows = array(); $b_rows = array(); $changes = array(); $table_a_data_rows = dbx::get_table_rows($table_a); $table_b_data_rows = dbx::get_table_rows($table_b); // data_row_overlay_key_search() needs these to do the matching $table_a_data_rows_columns = preg_split("/[\\,\\s]+/", $table_a_data_rows['columns'], -1, PREG_SPLIT_NO_EMPTY); $table_b_data_rows_columns = preg_split("/[\\,\\s]+/", $table_b_data_rows['columns'], -1, PREG_SPLIT_NO_EMPTY); if ($table_a_data_rows == null || $table_a_data_rows_columns == null) { // table_a has no data rows if ($in_both) { // what rows are in A and B? none in A to compare } else { // what rows in A are not in B? none to list } } else { if ($table_b_data_rows == null || $table_b_data_rows_columns == null) { // table_b has no data rows if ($in_both) { // what rows are in A and B? none in B to compare } else { // what rows in A are not in B? omg, all of them! $a_rows = $table_a_data_rows->row; } } else { $table_b_primary_keys = mysql5_table::primary_key_columns($table_b); // @NOTICE: in the case of table row data comparising for DIFFING, the primary keys to use are table B // but the table B and A columns are reversed compared to data_rows_overlay() as the comparison is B vs A not base vs overlay (AvB) // so the columns to match as base and overlay are reversed, comared to other calls to data_row_overlay_primary_key_index() $primary_key_index = xml_parser::data_row_overlay_primary_key_index($table_b_primary_keys, $table_b_data_rows_columns, $table_a_data_rows_columns); $base_pklookup = array(); $i = 0; if (count($primary_key_index['base'])) { foreach ($table_b_data_rows->row as $base_row) { $s = ''; foreach ($primary_key_index['base'] as $index) { $s .= ':' . $base_row->col[$index]; } $base_pklookup[$s] = $i++; } } $table_b_index = 0; foreach ($table_a_data_rows->row as $table_a_data_row) { $s = ''; foreach ($primary_key_index['overlay'] as $index) { $s .= ':' . $table_a_data_row->col[$index]; } if (array_key_exists($s, $base_pklookup)) { $match = TRUE; $table_b_index = $base_pklookup[$s]; } else { $match = FALSE; } if ($match) { // we found a match //echo "rows match\n"; var_dump($table_a_data_row); var_dump($table_b_data_row); $table_b_data_row =& $table_b_data_rows->row[$table_b_index]; if ($in_both) { // looking for rows in both // is the row marked for delete in A? if (self::table_data_row_deleted($table_a_data_row)) { // don't return changes, we are looking for rows in_both } else { if (self::table_data_row_deleted($table_b_data_row)) { // don't return changes, we are looking for rows in_both } else { // do table data row diff, add rows to return by reference rows for both A and B $changed_columns = array(); static::table_data_row_diff($table_a_data_rows_columns, $table_a_data_row, $table_b_data_rows_columns, $table_b_data_row, $changed_columns); $a_rows[] = $table_a_data_row; $b_rows[] = $table_b_data_row; $changes[] = $changed_columns; } } } else { // is the A row marked for delete? if (self::table_data_row_deleted($table_a_data_row)) { // there was a match, but we are looking for A not in B, A row is marked deleted, don't return it } else { if (self::table_data_row_deleted($table_b_data_row)) { // there was a match // A is not deleted // we are looking for A not in B // B is deleted $a_rows[] = $table_a_data_row; } } } } else { // echo "rows don't match\n"; var_dump($table_a_data_row); var_dump($table_b_data_row); // no match if (!$in_both) { // looking for A not in B if (self::table_data_row_deleted($table_a_data_row)) { // but the A row is marked deleted, don't return it } else { $a_rows[] = $table_a_data_row; } } } } } } }
/** * determine SQL clause expression to match for data_row primary keys * * @return string */ public static function primary_key_expression($db_doc, $node_schema, $node_table, $data_row_columns, $data_row) { $primary_keys = format_table::primary_key_columns($node_table); $primary_key_index = xml_parser::data_row_overlay_primary_key_index($primary_keys, $data_row_columns, $data_row_columns); $primary_key_index = $primary_key_index['base']; // figure out the primary key expression $primary_key_expression = array(); foreach ($primary_keys as $primary_column_name) { if (!isset($primary_key_index[$primary_column_name])) { throw new exception("primary key column named {$primary_column_name} not found in primary_key_index"); } $column_index = $primary_key_index[$primary_column_name]; // get the type of the column, chasing foreign keys if necessary $node_column = dbx::get_table_column($node_table, $primary_column_name); $value_type = format_column::column_type($db_doc, $node_schema, $node_table, $node_column, $foreign); $primary_key_expression[] = format::get_quoted_column_name($primary_column_name) . ' = ' . format::value_escape($value_type, $data_row->col[$column_index]); } if (count($primary_key_expression) == 0) { throw new exception($node_table['name'] . " primary_key_expression is empty, determinate loop failed"); } return implode(' AND ', $primary_key_expression); }