Example #1
0
 /// <summary>
 ///     Save all Changes -- Insert, Update, or Delete
 /// </summary>
 /// <param name="entityObjects" type="System.Collections.ICollection">
 ///     Collection of object instances to perform action on
 /// </param>
 /// <param name="persistDepth" type="Wilson.ORMapper.PersistDepth">
 ///     The depth at which to persist child objects
 /// </param>
 public void PersistChanges(ICollection entityObjects, PersistDepth persistDepth)
 {
     foreach (object entityObject in entityObjects)
     {
         Internals.Instance instance = this.context[entityObject];
         // Jeff Lanning ([email protected]): Added null check for better error message.
         if (instance == null)
         {
             throw new ORMapperException("Entity object '" + entityObject.GetType().ToString() + "' is not being tracked.");
         }
         instances.Add(new PersistOptions(instance, persistDepth));
         instance.PersistChanges(this, persistDepth);
     }
 }
Example #2
0
        private static int InnerSave(Transaction transaction, ICollection instances, bool includeChildren)
        {
            PersistDepth depth = (includeChildren ? PersistDepth.ObjectGraph : PersistDepth.SingleObject);

            if (transaction == null)
            {
                DataProvider.ObjectSpace.PersistChanges(instances, depth);
            }
            else
            {
                transaction.PersistChanges(instances, depth);
            }

            ICollection <T> coll = instances as ICollection <T>;

            return(coll != null ? coll.Count : -1);
        }
Example #3
0
        private static int InnerSave(Transaction transaction, T instance, bool includeChildren)
        {
            if (instance.IsReadOnly)
            {
                throw new DataProviderException("Can not save read-only instances.");
            }

            PersistDepth depth = (includeChildren ? PersistDepth.ObjectGraph : PersistDepth.SingleObject);

            if (transaction == null)
            {
                DataProvider.ObjectSpace.PersistChanges(instance, depth);
            }
            else
            {
                transaction.PersistChanges(instance, depth);
            }

            return(1);
        }
Example #4
0
 private void RollbackChildren(PersistDepth persistDepth)
 {
     if (persistDepth == PersistDepth.ObjectGraph) {
         for (int index = 0; index < this.entity.RelationCount; index++) {
             if (this.entity.Relation(index).Relationship == Relationship.Child) {
                 IList children = (IList)this.GetField(this.entity.Relation(index).Member);
                 foreach (object entityChild in children) {
                     try {
                         this.context[entityChild].RollbackChanges(persistDepth);
                     }
                     catch {
                         // Do Nothing
                     }
                 }
             }
         }
     }
 }
Example #5
0
 // Make sure cascade deletes bubble through all related child collection
 private void PersistChildren(Transaction transaction, PersistDepth persistDepth, bool parentDeleted)
 {
     if (persistDepth == PersistDepth.ObjectGraph) {
         object[] keyValues = new object[this.entity.KeyFields.Length];
         for (int index1 = 0; index1 < this.entity.KeyFields.Length; index1++) {
             keyValues[index1] = this.GetField(this.entity.KeyFields[index1].Member);
         }
         for (int index = 0; index < this.entity.RelationCount; index++) {
             bool cascadeDelete = (this.State == ObjectState.Deleted) && this.entity.Relation(index).Cascade;
             if (this.entity.Relation(index).Relationship == Relationship.Child) {
                 EntityMap childMap = this.context.Mappings[this.entity.Relation(index).Type];
                 string[] childMembers = new string[this.entity.Relation(index).Fields.Length];
                 for (int index2 = 0; index2 < childMembers.Length; index2++) {
                     for (int field = 0; field < childMap.FieldCount; field++) {
                         if (childMap[field].Field == this.entity.Relation(index).Fields[index2]
                             && childMap[field].PersistType == PersistType.Persist) {
                             childMembers[index2] = childMap[field].Member;
                             break;
                         }
                     }
                 }
                 IList children = (IList)this.GetField(this.entity.Relation(index).Member);
                 // Do not lazy-load lists to persist them -- Jerry Shea (http://www.RenewTek.com)
                 if (!(children is ILoadOnDemand) || (children as ILoadOnDemand).IsLoaded || cascadeDelete || parentDeleted) {
                     // Force recursive persistence if there are more child relations in this graph
                     bool subChildren = (this.context.Mappings[this.entity.Relation(index).Type].ChildRelations > 0);
                     foreach (object entityChild in children) {
                         if (subChildren || cascadeDelete || parentDeleted || this.context[entityChild].State != ObjectState.Unchanged) {
                             if (this.context[entityChild].State == ObjectState.Inserted) {
                                 for (int index3 = 0; index3 < childMembers.Length; index3++) {
                                     this.context[entityChild].SetField(childMembers[index3], keyValues[index3]);
                                 }
                             }
                             if (this.context[entityChild].State != ObjectState.Unknown) {
                                 this.context[entityChild].PersistChanges(transaction, persistDepth, cascadeDelete || parentDeleted);
                             }
                         }
                     }
                     BindingFlags flags = BindingFlags.NonPublic | BindingFlags.Instance;
                     PropertyInfo relation = children.GetType().GetProperty("Removed", flags);
                     if (relation != null) {
                         ArrayList removed = (ArrayList)relation.GetValue(children, null);
                         foreach (object removedChild in removed) {
                             if (this.context[removedChild].State != ObjectState.Unknown) {
                                 this.context[removedChild].MarkForDeletion();
                                 this.context[removedChild].PersistChanges(transaction, persistDepth, true);
                             }
                         }
                         removed.Clear();
                     }
                 }
             }
             else if (this.entity.Relation(index).Relationship == Relationship.Many) {
                 ManyMap manyMap = (ManyMap)this.entity.Relation(index);
                 IList children = (IList)this.GetField(manyMap.Member);
                 // Do not lazy-load lists to persist them
                 if (!(children is ILoadOnDemand) || (children as ILoadOnDemand).IsLoaded || cascadeDelete || parentDeleted) {
                     if (manyMap.DeleteSP == null || manyMap.DeleteSP.Length == 0) {
                         string whereClause = this.commands.GetExpression(manyMap.Table, manyMap.Source, keyValues);
                         string sqlDelete = this.commands.CreateDelete(manyMap.Table, whereClause);
                         this.context.Connection.TransactionCommand(transaction.id, this.EntityObject.GetType(), CommandInfo.ManyDelete, transaction.transaction, sqlDelete);
                     }
                     else {
                         Parameter[] parameters = new Parameter[keyValues.Length];
                         for (int index4 = 0; index4 < keyValues.Length; index4++) {
                             parameters[index4] = new Parameter(manyMap.Source[index4], keyValues[index4], null);
                         }
                         this.context.Connection.TransactionCommand(transaction.id, this.EntityObject.GetType(), CommandInfo.ManyDelete, transaction.transaction, manyMap.DeleteSP, parameters);
                     }
                     if (!(cascadeDelete || parentDeleted)) { // Bug-Fix for Multiple Many-to-Many to Avoid Re-Inserts on Deletes
                         foreach (object entityChild in children) {
                             Instance childInstance = this.context[entityChild];
                             object childKeys = EntityKey.GetObjectKey(this.context, childInstance);
                             if (manyMap.InsertSP == null || manyMap.InsertSP.Length == 0) {
                                 object[] childValues = null;
                                 if (childKeys.GetType().IsArray) {
                                     childValues = childKeys as object[];
                                 }
                                 else {
                                     childValues = new object[] { childKeys };
                                 }
                                 string sqlInsert = this.commands.InsertMany(manyMap.Table,
                                     manyMap.Source, manyMap.Dest, keyValues, childValues);
                                 this.context.Connection.TransactionCommand(transaction.id, this.EntityObject.GetType(), CommandInfo.ManyInsert, transaction.transaction, sqlInsert);
                             }
                             else {
                                 Parameter[] parameters = new Parameter[2 * keyValues.Length];
                                 for (int index5 = 0; index5 < keyValues.Length; index5++) {
                                     parameters[index5] = new Parameter(manyMap.Source[index5], keyValues[index5], null);
                                 }
                                 if (childKeys.GetType().IsArray) {
                                     for (int index6 = keyValues.Length; index6 < 2 * keyValues.Length; index6++) {
                                         parameters[index6] = new Parameter(manyMap.Dest[index6], (childKeys as object[])[index6], null);
                                     }
                                 }
                                 else {
                                     parameters[keyValues.Length] = new Parameter(manyMap.Dest[0], childKeys, null);
                                 }
                                 this.context.Connection.TransactionCommand(transaction.id, this.EntityObject.GetType(), CommandInfo.ManyInsert, transaction.transaction, manyMap.InsertSP, parameters);
                             }
                         }
                     }
                 }
             }
         }
     }
 }
Example #6
0
 // Make sure cascade deletes bubble through all related child collection
 private void PersistChanges(Transaction transaction, PersistDepth persistDepth, bool parentDeleted)
 {
     this.lastAccess = DateTime.Now;
     try {
         if (this.entity.ReadOnly) {
             throw new PersistenceException("ObjectSpace: Entity is ReadOnly - " + this.entity.Type);
         }
         else if (this.State == ObjectState.Deleted || parentDeleted) {
             if (this.entity.HasEvents) {
                 ((IObjectNotification)this.EntityObject).OnDeleting(transaction);
             }
             this.PersistChildren(transaction, persistDepth, parentDeleted);
             this.CascadeDeletes(transaction);
             if (this.State == ObjectState.Deleted) this.DeleteObject(transaction);
             if (this.entity.HasEvents) {
                 ((IObjectNotification)this.EntityObject).OnDeleted(transaction);
             }
         }
         else if (this.initial == InitialState.Inserted) {
             if (this.entity.HasEvents) {
                 ((IObjectNotification)this.EntityObject).OnCreating(transaction);
             }
             this.InsertObject(transaction);
             this.PersistChildren(transaction, persistDepth, parentDeleted);
             if (this.entity.HasEvents) {
                 ((IObjectNotification)this.EntityObject).OnCreated(transaction);
             }
         }
         else {
             if (this.entity.HasEvents) {
                 ((IObjectNotification)this.EntityObject).OnUpdating(transaction);
             }
             this.PersistChildren(transaction, persistDepth, parentDeleted);
             if (this.State == ObjectState.Updated) this.UpdateObject(transaction);
             if (this.entity.HasEvents) {
                 ((IObjectNotification)this.EntityObject).OnUpdated(transaction);
             }
         }
     }
     catch (Exception exception) {
         if (this.entity.HasEvents) {
             ((IObjectNotification)this.EntityObject).OnPersistError(transaction, exception);
         }
         throw;
     }
 }
Example #7
0
 private void CommitChildren(PersistDepth persistDepth)
 {
     if (persistDepth == PersistDepth.ObjectGraph) {
         for (int index = 0; index < this.entity.RelationCount; index++) {
             if (this.entity.Relation(index).Relationship == Relationship.Child) {
                 IList children = (IList)this.GetField(this.entity.Relation(index).Member);
                 // Do not lazy-load lists to persist them -- Jerry Shea (http://www.RenewTek.com)
                 if (!(children is ILoadOnDemand) || (children as ILoadOnDemand).IsLoaded) {
                     foreach (object entityChild in children) {
                         try {
                             if (this.context.IsTracked(entityChild)) {
                                 this.context[entityChild].CommitChanges(persistDepth);
                             }
                             else {
                                 this.context.StartTracking(entityChild, InitialState.Unchanged);
                             }
                         }
                         catch {
                             // Do Nothing
                         }
                     }
                 }
             }
         }
     }
 }
Example #8
0
 public void RollbackChanges(PersistDepth persistDepth)
 {
     this.RollbackChildren(persistDepth);
     if (this.initial == InitialState.Inserted && this.entity.KeyType == KeyType.Auto) {
         // Tracking Bug-Fix by Gerrod Thomas (http://www.Gerrod.com)
         this.context.EndTracking(this.EntityObject);
         FieldMap autoKeyMember = this.entity.AutoKeyMember();
         this.SetField(autoKeyMember, this.GetField(autoKeyMember));
         this.context.StartTracking(this);
     }
 }
Example #9
0
 public void PersistChanges(Transaction transaction, PersistDepth persistDepth)
 {
     this.PersistChanges(transaction, persistDepth, false);
 }
Example #10
0
 public void CommitChanges(PersistDepth persistDepth)
 {
     this.CommitChildren(persistDepth);
     if (this.State == ObjectState.Deleted) {
         this.instance = null;
     }
     else {
         if (this.initial == InitialState.Inserted && this.entity.KeyType == KeyType.Auto) {
             // InitialState Bug-Fix by Gerrod Thomas (http://www.Gerrod.com)
             this.initial = InitialState.Unchanged;
             EntityKey entityKey = new EntityKey(this.context, this.EntityObject, this.IsPersisted);
             this.context.StartTracking(entityKey, this);
         }
         this.StartTracking(InitialState.Unchanged);
     }
 }
Example #11
0
 internal PersistOptions(Instance instance, PersistDepth depth)
 {
     this.instance = instance;
     this.depth = depth;
     this.entityObject = instance.EntityObject;
 }
Example #12
0
 /// <summary>
 ///     Save all Changes -- Insert, Update, or Delete
 /// </summary>
 /// <param name="entityObjects" type="System.Collections.ICollection">
 ///     Collection of object instances to perform action on
 /// </param>
 /// <param name="persistDepth" type="Wilson.ORMapper.PersistDepth">
 ///     The depth at which to persist child objects
 /// </param>
 public void PersistChanges(ICollection entityObjects, PersistDepth persistDepth)
 {
     foreach (object entityObject in entityObjects) {
         Internals.Instance instance = this.context[entityObject];
         // Jeff Lanning ([email protected]): Added null check for better error message.
         if (instance == null) {
             throw new ORMapperException("Entity object '" + entityObject.GetType().ToString() + "' is not being tracked.");
         }
         instances.Add(new PersistOptions(instance, persistDepth));
         instance.PersistChanges(this, persistDepth);
     }
 }
Example #13
0
 /// <summary>
 ///     Save all Changes -- Insert, Update, or Delete
 /// </summary>
 /// <param name="entityObject" type="object">
 ///     Object instance to perform action on
 /// </param>
 /// <param name="persistDepth" type="Wilson.ORMapper.PersistDepth">
 ///    The depth at which to persist child objects
 /// </param>
 public void PersistChanges(object entityObject, PersistDepth persistDepth)
 {
     object[] entityObjects = new object[] {entityObject};
     this.PersistChanges(entityObjects, persistDepth);
 }
Example #14
0
 internal PersistOptions(Instance instance, PersistDepth depth)
 {
     this.instance     = instance;
     this.depth        = depth;
     this.entityObject = instance.EntityObject;
 }
Example #15
0
 /// <summary>
 ///     Save all Changes -- Insert, Update, or Delete
 /// </summary>
 /// <param name="entityObject" type="object">
 ///     Object instance to perform action on
 /// </param>
 /// <param name="persistDepth" type="Wilson.ORMapper.PersistDepth">
 ///    The depth at which to persist child objects
 /// </param>
 public void PersistChanges(object entityObject, PersistDepth persistDepth)
 {
     object[] entityObjects = new object[] { entityObject };
     this.PersistChanges(entityObjects, persistDepth);
 }