public override void JoinsDelete(ClassType Object, MicroORM MicroORM) { if (Object == null) { return; } IEnumerable <DataType> List = CompiledExpression(Object); if (List == null) { return; } foreach (DataType Item in List) { if (Item != null) { IParameter CurrentIDParameter = ((IProperty <ClassType>)Mapping.IDProperty).GetAsParameter(Object); CurrentIDParameter.ID = "ID"; MicroORM.Command = "DELETE FROM " + TableName + " WHERE " + Mapping.TableName + Mapping.IDProperty.FieldName + "=@ID"; MicroORM.CommandType = CommandType.Text; CurrentIDParameter.AddParameter(MicroORM); MicroORM.ExecuteNonQuery(); } } }
public override void JoinsSave(ClassType Object, MicroORM MicroORM) { if (Object == null) { return; } IEnumerable <DataType> List = CompiledExpression(Object); if (List == null) { return; } foreach (DataType Item in List) { if (Item != null) { IParameter CurrentIDParameter = ((IProperty <ClassType>)Mapping.IDProperty).GetAsParameter(Object); IMapping ForeignMapping = Mapping.Manager.Mappings[typeof(DataType)].First(x => x.DatabaseConfigType == Mapping.DatabaseConfigType); IParameter ForeignIDParameter = ((IProperty <DataType>)ForeignMapping.IDProperty).GetAsParameter(Item); CurrentIDParameter.ID = "ID1"; ForeignIDParameter.ID = "ID2"; MicroORM.Command = "INSERT INTO " + TableName + "(" + Mapping.TableName + Mapping.IDProperty.FieldName + "," + ForeignMapping.TableName + ForeignMapping.IDProperty.FieldName + ") VALUES (@ID1,@ID2)"; MicroORM.CommandType = CommandType.Text; CurrentIDParameter.AddParameter(MicroORM); ForeignIDParameter.AddParameter(MicroORM); MicroORM.ExecuteNonQuery(); } } }
/// <summary> /// Deletes the specified object from the database /// </summary> /// <typeparam name="ObjectType">Object type</typeparam> /// <param name="Object">Object to delete</param> public virtual void Delete <ObjectType>(ObjectType Object) where ObjectType : class, new() { foreach (IDatabase Database in Mappings.Keys.Where(x => x.Writable).OrderBy(x => x.Order)) { IMapping Mapping = Mappings[Database].FirstOrDefault(x => x.ObjectType == typeof(ObjectType)); if (Mapping != null) { using (MicroORM ORMObject = new MicroORM(Database.Name)) { System.Collections.Generic.List <Command> JoinCommands = new System.Collections.Generic.List <Command>(); foreach (IProperty Property in Mapping.Properties) { if (!Property.Cascade && Mapping.ObjectType == Property.Type) { JoinCommands.AddIfUnique(((IProperty <ObjectType>)Property).JoinsDelete(Object, ORMObject)); } if (Property.Cascade) { JoinCommands.AddIfUnique(((IProperty <ObjectType>)Property).CascadeJoinsDelete(Object, ORMObject)); } } foreach (Command JoinCommand in JoinCommands) { ORMObject.ChangeCommand(JoinCommand); ORMObject.ExecuteNonQuery(); } foreach (IProperty Property in Mapping.Properties) { if (Property.Cascade) { ((IProperty <ObjectType>)Property).CascadeDelete(Object, ORMObject); } } ORMObject.Map <ObjectType>().Delete(Object); } } } }
/// <summary> /// Saves an object to the database /// </summary> /// <typeparam name="ObjectType">Object type</typeparam> /// <typeparam name="PrimaryKeyType">Primary key type</typeparam> /// <param name="Object">Object to save</param> /// <param name="Parameters">Extra parameters used in saving the object</param> public virtual void Save <ObjectType, PrimaryKeyType>(ObjectType Object, params IParameter[] Parameters) where ObjectType : class, new() { foreach (IDatabase Database in Mappings.Keys.Where(x => x.Writable).OrderBy(x => x.Order)) { IMapping Mapping = Mappings[Database].FirstOrDefault(x => x.ObjectType == typeof(ObjectType)); if (Mapping != null) { using (MicroORM ORMObject = new MicroORM(Database.Name)) { foreach (IProperty Property in Mapping.Properties) { if (Property.Cascade) { ((IProperty <ObjectType>)Property).CascadeSave(Object, ORMObject); } } System.Collections.Generic.List <IParameter> Params = Parameters.ToList(); foreach (IProperty Property in Mapping.Properties) { IParameter Parameter = ((IProperty <ObjectType>)Property).GetAsParameter(Object); if (Parameter != null) { Params.Add(Parameter); } } ORMObject.Map <ObjectType>().Save <PrimaryKeyType>(Object, Params.ToArray()); System.Collections.Generic.List <Command> JoinCommands = new System.Collections.Generic.List <Command>(); foreach (IProperty Property in Mapping.Properties) { if (!Property.Cascade && (Property is IManyToMany || Property is IManyToOne || Property is IIEnumerableManyToOne || Property is IListManyToMany || Property is IListManyToOne)) { JoinCommands.AddIfUnique(((IProperty <ObjectType>)Property).JoinsDelete(Object, ORMObject)); } if (Property.Cascade) { JoinCommands.AddIfUnique(((IProperty <ObjectType>)Property).CascadeJoinsDelete(Object, ORMObject)); } } foreach (Command JoinCommand in JoinCommands) { ORMObject.ChangeCommand(JoinCommand); ORMObject.ExecuteNonQuery(); } JoinCommands = new System.Collections.Generic.List <Command>(); foreach (IProperty Property in Mapping.Properties) { if (!Property.Cascade && (Property is IManyToMany || Property is IManyToOne || Property is IIEnumerableManyToOne || Property is IListManyToMany || Property is IListManyToOne)) { JoinCommands.AddIfUnique(((IProperty <ObjectType>)Property).JoinsSave(Object, ORMObject)); } if (Property.Cascade) { JoinCommands.AddIfUnique(((IProperty <ObjectType>)Property).CascadeJoinsSave(Object, ORMObject)); } } foreach (Command JoinCommand in JoinCommands) { ORMObject.ChangeCommand(JoinCommand); ORMObject.ExecuteNonQuery(); } } } } }