Esempio n. 1
0
 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 (SQLHelper ORMObject = new SQLHelper(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));
                     }
                 }
                 if (JoinCommands.Count > 0)
                 {
                     ORMObject.Batch().AddCommands(JoinCommands.ToArray());
                     ORMObject.ExecuteNonQuery();
                 }
                 foreach (IProperty Property in Mapping.Properties)
                 {
                     if (Property.Cascade)
                     {
                         ((IProperty <ObjectType>)Property).CascadeDelete(Object, ORMObject);
                     }
                 }
                 ORMObject.Delete <ObjectType>(Object);
             }
         }
     }
 }
Esempio n. 2
0
 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 (SQLHelper ORMObject = new SQLHelper(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.Save <ObjectType, 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));
                     }
                 }
                 if (JoinCommands.Count > 0)
                 {
                     for (int x = 0; x < (JoinCommands.Sum(y => y.Parameters.Count) / 100) + 1; ++x)
                     {
                         ORMObject.Batch().AddCommands(JoinCommands.ElementsBetween(x * 100, (x * 100) + 100).ToArray());
                         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));
                     }
                 }
                 if (JoinCommands.Count > 0)
                 {
                     for (int x = 0; x < (JoinCommands.Sum(y => y.Parameters.Count) / 100) + 1; ++x)
                     {
                         ORMObject.Batch().AddCommands(JoinCommands.ElementsBetween(x * 100, (x * 100) + 100).ToArray());
                         ORMObject.ExecuteNonQuery();
                     }
                 }
             }
         }
     }
 }
Esempio n. 3
0
 /// <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();
                 }
             }
         }
     }
 }