public int SaveChanges() { bool result = false; object[] params1 = { "AddDirectly".ToCharArray() }; //en başta çünkü child'lara bunun id'si yerleştirilecek object[] params2 = { "AddChilds".ToCharArray() }; //childlar eklendikten sonra parentlar güncellenecek object[] params3 = { "Update".ToCharArray() }; //güncelleme object[] params4 = { "Remove".ToCharArray() }; object[] params5 = { "AddOTM".ToCharArray() }; //en sonda çünkü önce parent eklenmeli, id'ye sahip olmalı. object[] params6 = { "UpdateAfterRemoval".ToCharArray() }; object[][] parameters = { params1, params2, params3, params4, params5, params6 }; foreach (object[] parameter in parameters) { foreach (object o in dbsets) { MethodInfo method = o.GetType().GetMethod("SaveChanges"); result |= (bool)method.Invoke(o, parameter); } } this.removed = new Dictionary <Type, List <int> >(); if (result) //if any changes happened, they may have triggered new changes. { SaveChanges(); } //Mtm kayıtlarını hallet. String s; var kpList = MTMToWrite.ToList(); while (kpList.Count() > 0) { s = kpList[0].Key; String filepath = Path.Combine(this.DatabaseFolder, kpList[0].Key); Stream mtmStream; mtmStream = File.OpenWrite(filepath + ".dat"); //Streami ata, aç foreach (Tuple <BaseClass, BaseClass> mtmRecBase in kpList[0].Value) { MTMRec mtmRec = new MTMRec(mtmRecBase.Item1.id, mtmRecBase.Item2.id); int fk = FileOps.Add(mtmStream, new List <int>(), mtmRec); mtmRec.id = fk; //Streami kapa, sil listeden. } mtmStream.Close(); kpList.RemoveAt(0); MTMToWrite.Remove(s); } return(0); }
/// <summary> /// Completes changes in the dbset chosen by the command. /// </summary> /// <param name="command">AddDirectly, AddChilds, Update, Remove</param> public bool SaveChanges(char[] com) { String command = new string(com); bool result = false; if (command == "AddOTM") { while (recordsToAddAsOTM.Count() != 0) { Tuple <object, object, PropertyInfo> rec = recordsToAddAsOTM[0]; OpenMainWrite(); rec.Item3.SetValue(rec.Item2, rec.Item1); int fk = FileOps.Add(mainFile, removedIndexes, (BaseClass)rec.Item2); rec.Item2.GetType().GetProperty(rec.Item3.Name).SetValue(rec.Item2, fk); result = true; } } else if (command == "AddDirectly") { //0. elemanlar ile işlem yapma nedeni: //Bir ekleme işlemi yapılırken, içinde çocuk nesne var ise, bir şekilde üzerinde çalışılan //listenin sonuna yeni kayıt eklenmesine neden olabilir. OpenMainWrite(); while (recordsToAddDirectly.Count() != 0) { result = true; int id = FileOps.Add(mainFile, removedIndexes, recordsToAddDirectly[0]); recordsToAddDirectly[0].id = id; allRecords.Add(recordsToAddDirectly[0]); recordsToAddDirectly.RemoveAt(0); } mainFile.Close(); } else if (command == "AddChilds") { OpenMainWrite(); while (recordsToAddAsOTO.Count() != 0) { result = true; //item1=parent, item2=child T record = (T)recordsToAddAsOTO[0].Item2; int fk = FileOps.Add(mainFile, removedIndexes, record); record.id = fk; Type ownerType = recordsToAddAsOTO[0].Item1.GetType(); Type ownerDbSetType = ctx.dbsetTypes.Where(a => a.ToString().Equals(ownerType.ToString())).ElementAt(0); object ownerDbSet = ctx.GetDBSetByType(ownerDbSetType); object[] parameters = new object[1]; parameters[0] = (BaseClass)recordsToAddAsOTO[0].Item1; //Explained under recordsToAddAsOTO declaration. ownerDbSet.GetType().GetMethod("Update").Invoke(ownerDbSet, parameters); allRecords.Add(record); recordsToAddAsOTO.RemoveAt(0); } mainFile.Close(); } else if (command == "Update") { OpenMainWrite(); while (Updates.Count() != 0) { result = true; T record = Updates[0]; FileOps.Overwrite(mainFile, record); Updates.RemoveAt(0); } mainFile.Close(); } else if (command == "Remove") { OpenMainWrite(); while (recordsToRemove.Count() != 0) { result = true; recordsToRemove[0].GetType().GetProperty("removed").SetValue(recordsToRemove[0], true); FileOps.Remove(mainFile, metaFile, recordsToRemove[0]); if (!ctx.removed.Any(kp => kp.Key == typeof(T))) { ctx.removed.Add(typeof(T), new List <int>()); } ctx.removed.FirstOrDefault(kp => kp.Key == typeof(T)).Value .Add((int)recordsToRemove[0].id); recordsToRemove.RemoveAt(0); } mainFile.Close(); } else if (command == "UpdateAfterRemoval") //silinmiş bir kayda referans olanların foreign keylerini -1 yapacak { List <PropertyInfo> toChangeColumns = new List <PropertyInfo>(); Dictionary <int, List <int> > toChange = new Dictionary <int, List <int> >(); foreach (KeyValuePair <Type, List <int> > kp in ctx.removed) { List <PropertyInfo> props = this.GetType().GetGenericArguments()[0].GetProperties().ToList(); foreach (PropertyInfo info in props) { if (!info.PropertyType.IsGenericType && info.PropertyType.Name.Equals(kp.Key.Name)) { toChangeColumns.Add(info); foreach (int id in kp.Value) //null yapılacak id { toChange.Add(id, new List <int>()); } } else if (info.PropertyType.IsGenericType && info.PropertyType.GetGenericArguments()[0].Name == kp.Key.Name) { //remove from mtm } } } foreach (T rec in allRecords) { foreach (PropertyInfo column in toChangeColumns) { foreach (KeyValuePair <int, List <int> > kp in toChange) { BaseClass child_rec = (BaseClass)rec.GetType().GetProperty(column.Name).GetValue(rec); if (child_rec.id == kp.Key) { rec.GetType().GetProperty(column.Name).SetValue(rec, null); kp.Value.Add(rec.id); } } } } foreach (PropertyInfo column in toChangeColumns) { OpenMainWrite(); FileOps.MakeReferenceNull(mainFile, column, typeof(T), toChange); mainFile.Close(); } } return(result); }