public SQL_XML_IO_statuses DeleteLearningItem(LearningItem learningitem) { if (learningitem == null) return SQL_XML_IO_statuses.OtherError; RefTree.Clear(); DBConnect dbconnect = new DBConnect(); dbconnect.Open(); //logging = true; //FindAllReferences_("LearningItems",learningitem.id); if(ViewModelLocator.Instance.AppSetting.CurrentLearningItem.id == learningitem.id) { ViewModelLocator.Instance.AppSetting.CurrentLearningItem = null; } EFDbContext.SaveChgs(); // Rules schema.ClearRules(); schema.GetTableByName(AppSetting.TableName).DontProcessing = true; schema.GetTableByName(Dictionary.TableName).DontProcessing = true; schema.GetTableByName(Language.TableName).DontProcessing = true; schema.GetTableByName(WordOfDictionary.TableName).DontProcessing = true; // optimization for speed DBConnect.Query query = new DBConnect.Query(); query.sql = SQLqueries.Delete_Update_Subtitle_Items_Words; query.AddParameter("id", learningitem.id); dbconnect.ExecuteSQL(query); query.sql = SQLqueries.Delete_Subtitile_Items_Words; query.AddParameter("id", learningitem.id); dbconnect.ExecuteSQL(query); query.sql = SQLqueries.Delete_Subtitle_Items; query.AddParameter("id", learningitem.id); dbconnect.ExecuteSQL(query); PathList.Clear(); DeleteTable(dbconnect,"LearningItems",learningitem.id,true); dbconnect.Close(); ((IObjectContextAdapter)ViewModelLocator.Instance.DataBase).ObjectContext.Detach(learningitem); EFDbContext.SaveChgs(); return SQL_XML_IO_statuses.Ok; }
public void DeleteTable(DBConnect dbconnect,string table_name,Guid id,bool remove,string path = "",int parent_key_number =0) { string sql; if (path == "") path = table_name; if (!PathList.Contains(path)) PathList.Add(path); if (RefTreeContains(table_name, id)) return; AddToRefList(table_name, id,path); Dictionary<string,Ref> ForwardRefList = new Dictionary<string,Ref>(); // find forward references var foreign_keys = schema.ForeignKey.Where((x) => x.Table_str == table_name && x.number != parent_key_number); if(foreign_keys.Count() > 0) { foreach (ForeignKey key in foreign_keys) { if (key.field.DontProcessing) continue; if (key.PrimaryTable.DontProcessing) continue; DBConnect.Query query = new DBConnect.Query(); query.sql = "SELECT [" + key.field_str + "] as id FROM [" + key.Table_str + "] where id=@id"; query.AddParameter("id", id); Guid? res = dbconnect.GetGuid("SELECT [" + key.field_str + "] as id FROM [" + key.Table_str + "] where id='"+id.ToString()+"'"); if (res == null) continue; Guid id_ = (Guid)res; // is it a forward reference if (!RefTreeContains(key.PrimaryTable_str, id_)){ ForwardRefList.Add(key.field_str,new Ref {tablename = key.PrimaryTable_str,guid = id_,number = key.number}); } } } // Clearing the reference fields if(ForwardRefList.Count > 0 && remove) { sql = "UPDATE ["+table_name+"] SET"+Environment.NewLine; bool FirstElement = true; foreach(var elm in ForwardRefList) { sql += (FirstElement ? "" : ",") + "[" + elm.Key + "]=null"+Environment.NewLine; FirstElement = false; } sql += "WHERE id='"+id+"'"; dbconnect.ExecuteSQL(sql); } // delete objects by forward references foreach(var elm in ForwardRefList) { DeleteTable(dbconnect,elm.Value.tablename, elm.Value.guid,remove,path + "->" +elm.Value.tablename,elm.Value.number); } // delete objects by backward references foreign_keys = schema.ForeignKey.Where((x) => x.PrimaryTable_str == table_name && x.number != parent_key_number); if(foreign_keys.Count() > 0) { foreach (ForeignKey key in foreign_keys) { if (key.Table.DontProcessing) continue; List<Guid> id_list = dbconnect.GetGuidList("SELECT id FROM [" + key.Table_str + "] where [" + key.field_str + "]='"+id.ToString()+"'"); string ids = ""; foreach (Guid id_ in id_list) { ids += (ids.Count() == 0 ? "":",")+id_; if (!RefTreeContains(key.Table_str, id_)) { DeleteTable(dbconnect,key.Table_str, id_,remove, path + "->" + key.Table_str, key.number); } } } } if (remove) { dbconnect.ExecuteSQL("DELETE FROM ["+table_name+"] WHERE id='"+id.ToString()+"'"); } }