public static void WriteRows(StreamWriter s, ISheet sheet, Language language, IEnumerable <int> colIndices, bool writeRaw) { if (sheet.Header.Variant == 1) { WriteRowsCore(s, sheet.Cast <Ex.IRow>(), language, colIndices, writeRaw, WriteRowKey); } else { var rows = sheet.Cast <XivRow>().Select(_ => (Ex.Variant2.DataRow)_.SourceRow); foreach (var parentRow in rows.OrderBy(_ => _.Key)) { WriteRowsCore(s, parentRow.SubRows, language, colIndices, writeRaw, WriteSubRowKey); } } }
private void WriteVairant2Rows(ISheet sheet, StringBuilder sb) { var rows = sheet.Cast <XivSubRow>(); var cols = new List <string>(); cols.Add("`_Key`"); cols.Add("`_SubKey`"); foreach (var col in sheet.Header.Columns.Cast <RelationalColumn>()) { string name = string.IsNullOrEmpty(col.Name) ? $"unk{col.Index}" : col.Name; cols.Add($"`{name}`"); } sb.AppendLine($"INSERT INTO {GetTableName(sheet)} ({string.Join(", ", cols)}) VALUES "); foreach (var row in rows) { var data = new List <string>(); data.Add(row.ParentRow.Key.ToString()); data.Add(row.Key.ToString()); DoRowData(sheet, row, data, sb); } sb.Remove(sb.Length - 3, 3); sb.AppendLine(";"); }
private static IEnumerable <IChange> CompareVariant2(ISheet previousSheet, ISheet updatedSheet, Language language, ColumnMap[] columns) { var prevRows = previousSheet.Cast <Variant2.RelationalDataRow>().SelectMany(r => r.SubRows).ToArray(); var updatedRows = updatedSheet.Cast <Variant2.RelationalDataRow>().SelectMany(r => r.SubRows).ToArray(); var updatedRowIndex = updatedRows.ToDictionary(r => r.FullKey); foreach (var prevRow in prevRows) { if (!updatedRowIndex.TryGetValue(prevRow.FullKey, out var updatedRow)) { continue; } foreach (var col in columns) { var prevVal = prevRow[col.PreviousIndex]; var upVal = updatedRow[col.NewIndex]; if (!Comparer.IsMatch(prevVal, upVal)) { yield return (new FieldChanged(updatedSheet.Header.Name, language, col.Name, updatedRow.Key, prevVal, upVal)); } } } }
private static IEnumerable <IChange> Compare(ISheet previousSheet, ISheet updatedSheet, Language language, ColumnMap[] columns) { var prevRows = previousSheet.Cast <IRow>().ToArray(); var updatedRows = updatedSheet.Cast <IRow>().ToDictionary(_ => _.Key, _ => _); foreach (var prevRow in prevRows) { if (!updatedRows.ContainsKey(prevRow.Key)) { continue; } var updatedRow = updatedRows[prevRow.Key]; foreach (var col in columns) { var prevVal = prevRow[col.PreviousIndex]; var upVal = updatedRow[col.NewIndex]; if (!Comparer.IsMatch(prevVal, upVal)) { yield return (new FieldChanged(updatedSheet.Header.Name, language, col.Name, updatedRow.Key, prevVal, upVal)); } } } }
private static IEnumerable <IChange> Compare(ISheet previousSheet, ISheet updatedSheet, Language language, ColumnMap[] columns) { if (previousSheet.Header.Variant == 2) { foreach (IChange result in CompareVariant2(previousSheet, updatedSheet, language, columns)) { yield return(result); } } else { IRow[] prevRows = previousSheet.Cast <IRow>().ToArray(); Dictionary <int, IRow> updatedRows = updatedSheet.Cast <IRow>().ToDictionary(_ => _.Key, _ => _); foreach (IRow prevRow in prevRows) { if (!updatedRows.ContainsKey(prevRow.Key)) { continue; } IRow updatedRow = updatedRows[prevRow.Key]; foreach (ColumnMap col in columns) { object prevVal = prevRow[col.PreviousIndex]; object upVal = updatedRow[col.NewIndex]; if (!Comparer.IsMatch(prevVal, upVal)) { yield return (new FieldChanged(updatedSheet.Header.Name, language, col.Name, updatedRow.Key, prevVal, upVal)); } } } } }
public static void WriteRows(StreamWriter s, ISheet sheet, Language language, IEnumerable<int> colIndices, bool writeRaw) { foreach (var row in sheet.Cast<Ex.IRow>().OrderBy(_ => _.Key)) { var useRow = row; if (useRow is IXivRow) useRow = ((IXivRow)row).SourceRow; var multiRow = useRow as IMultiRow; s.Write(useRow.Key); foreach (var col in colIndices) { object v; if (language == Language.None || multiRow == null) v = writeRaw ? useRow.GetRaw(col) : useRow[col]; else v = writeRaw ? multiRow.GetRaw(col, language) : multiRow[col, language]; if (v == null) s.Write(","); else if (v is IDictionary<int, object>) WriteDict(s, v as IDictionary<int, object>); else if (IsUnescaped(v)) s.Write(",{0}", v); else { // This prevents new lines from appearing in CSV // which results in invalid CSV output. string nv = v.ToString().Replace("\"", "\"\""); nv = Regex.Replace(nv, @"\t|\n|\r", ""); s.Write(",\"{0}\"", nv); } } s.WriteLine(); s.Flush(); } }