Ejemplo n.º 1
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();
                     }
                 }
             }
         }
     }
 }