/// <summary> /// /// </summary> /// <param name="database"></param> public void Remove(Database database) { if (database == null) return; database.PropertyChanged -= DatabaseObserver; Verbose("Remove Database: " + database.Name); if (database.Columns != null) { Remove(database.Columns); } if (database.Records != null) { Remove(database.Records); } }
/// <summary> /// /// </summary> /// <param name="path"></param> /// <param name="database"></param> public void UpdateData(string path, Database database) { try { if (_csvReader == null) return; var db = _csvReader.Read(path); if (db.Columns.Length <= 1) return; // check for the Id column if (db.Columns[0].Name != "Id") return; // skip Id column for update if (db.Columns.Length - 1 != database.Columns.Length) return; // check column names for (int i = 1; i < db.Columns.Length; i++) { if (db.Columns[i].Name != database.Columns[i - 1].Name) return; } bool isDirty = false; var recordsBuilder = database.Records.ToBuilder(); for (int i = 0; i < database.Records.Length; i++) { var record = database.Records[i]; var result = db.Records.FirstOrDefault(r => r.Id == record.Id); if (result != null) { // update existing record for (int j = 1; j < result.Values.Length; j++) { var valuesBuilder = record.Values.ToBuilder(); valuesBuilder[j - 1] = result.Values[j]; record.Values = valuesBuilder.ToImmutable(); } isDirty = true; } else { var r = db.Records[i]; // use existing columns r.Columns = database.Columns; // skip Id column r.Values = r.Values.Skip(1).ToImmutableArray(); recordsBuilder.Add(r); isDirty = true; } } if (isDirty) { var builder = _editor.Project.Databases.ToBuilder(); var index = builder.IndexOf(database); database.Records = recordsBuilder.ToImmutable(); builder[index] = database; var previous = _editor.Project.Databases; var next = builder.ToImmutable(); _editor.History.Snapshot(previous, next, (p) => _editor.Project.Databases = p); _editor.Project.Databases = next; } } catch (Exception ex) { if (_editor.Log != null) { _editor.Log.LogError("{0}{1}{2}", ex.Message, Environment.NewLine, ex.StackTrace); } } }
/// <summary> /// /// </summary> /// <param name="database"></param> public void Add(Database database) { if (database == null) return; database.PropertyChanged += DatabaseObserver; Verbose("Add Database: " + database.Name); if (database.Columns != null) { Add(database.Columns); } if (database.Records != null) { Add(database.Records); } }
/// <summary> /// /// </summary> /// <param name="path"></param> /// <param name="database"></param> public void ExportData(string path, Database database) { try { if (_csvWriter == null) return; _csvWriter.Write(path, database); } catch (Exception ex) { if (_editor.Log != null) { _editor.Log.LogError("{0}{1}{2}", ex.Message, Environment.NewLine, ex.StackTrace); } } }