Ejemplo n.º 1
0
        /// <summary>
        /// Gets the delete work units.
        /// </summary>
        /// <param name="entity"></param>
        /// <param name="et"></param>
        /// <param name="modifiedFields"></param>
        /// <returns></returns>
        public override IWorkUnit[] GetDeleteWorkUnits(object entity, EntityType et, EntityField[] modifiedFields)
        {
            if (entity == null)
            {
                throw new ArgumentNullException("entity");
            }
            if (et == null)
            {
                throw new ArgumentNullException("et");
            }
            if (modifiedFields == null)
            {
                throw new ArgumentNullException("modifiedFields");
            }

            // create...
            WorkUnitCollection units = new WorkUnitCollection();

            if (FlatTableWorkUnit.DoesExtendedTableExist(et))
            {
                // Create a delete work unit for extended properties, we may not have any field changes
                // get all of the fields...
                EntityField[] keyFields = et.GetKeyFields();
                object[]      values    = et.Storage.GetValues(entity, keyFields);

                // add...
                units.Add(new FlatTableDeleteAllWorkUnit(et, entity, keyFields, values));
            }

            // return...
            return(units.ToArray());
        }
Ejemplo n.º 2
0
        /// <summary>
        /// Gets the insert work units.
        /// </summary>
        /// <param name="entity"></param>
        /// <param name="et"></param>
        /// <param name="modifiedFields"></param>
        /// <returns></returns>
        public override IWorkUnit[] GetInsertWorkUnits(object entity, EntityType et, EntityField[] modifiedFields)
        {
            // results...
            WorkUnitCollection units = new WorkUnitCollection();

            // check...
            if (FlatTableWorkUnit.DoesExtendedTableExist(et))
            {
                // keys...
                EntityField[] keyFields = et.GetKeyFields();
                if (keyFields == null)
                {
                    throw new InvalidOperationException("keyFields is null.");
                }

                // create...
                foreach (EntityField field in modifiedFields)
                {
                    ArrayList unitFields = new ArrayList(keyFields);
                    unitFields.Add(field);

                    EntityField[] workUnitFields = (EntityField[])unitFields.ToArray(typeof(EntityField));
                    object[]      values         = et.Storage.GetValues(entity, workUnitFields);

                    // create...
                    units.Add(new FlatTableInsertWorkUnit(et, entity, workUnitFields, values));
                }
            }

            // return...
            return(units.ToArray());
        }
Ejemplo n.º 3
0
        /// <summary>
        /// Gets the work units to use with an update.
        /// </summary>
        /// <param name="entity"></param>
        /// <param name="et"></param>
        /// <param name="modifiedFields"></param>
        /// <returns></returns>
        public override IWorkUnit[] GetUpdateWorkUnits(object entity, EntityType et, EntityField[] modifiedFields)
        {
            if (entity == null)
            {
                throw new ArgumentNullException("entity");
            }
            if (et == null)
            {
                throw new ArgumentNullException("et");
            }
            if (modifiedFields == null)
            {
                throw new ArgumentNullException("modifiedFields");
            }

            // key fields...
            EntityField[] keyFields = et.GetKeyFields();
            if (keyFields == null)
            {
                throw new InvalidOperationException("'keyFields' is null.");
            }
            if (keyFields.Length == 0)
            {
                throw new InvalidOperationException("'keyFields' is zero-length.");
            }

            // get...
            WorkUnitCollection units = new WorkUnitCollection();

            if (FlatTableWorkUnit.DoesExtendedTableExist(et))
            {
                // create...
                foreach (EntityField field in modifiedFields)
                {
                    // mbr - 17-03-2006 - check that we don't actually have null...  if we do, we actually need to delete the row...
                    if (et.Storage.IsDBNull(entity, field))
                    {
                        // add...
                        object[] keyValues = et.Storage.GetValues(entity, keyFields);
                        units.Add(new FlatTableDeleteOneWorkUnit(et, entity, keyFields, keyValues, field.NativeName.Name));;
                    }
                    else
                    {
                        ArrayList unitFields = new ArrayList(keyFields);
                        unitFields.Add(field);

                        EntityField[] workUnitFields = (EntityField[])unitFields.ToArray(typeof(EntityField));
                        object[]      values         = et.Storage.GetValues(entity, workUnitFields);

                        // create...
                        units.Add(new FlatTableUpdateWorkUnit(et, entity, workUnitFields, values));
                    }
                }
            }

            // return...
            return(units.ToArray());
        }
Ejemplo n.º 4
0
 /// <summary>
 /// Adds a set of IWorkUnit instances to the collection.
 /// </summary>
 /// <param name="item">The item to add.</param>
 public void AddRange(WorkUnitCollection items)
 {
     if (items == null)
     {
         throw new ArgumentNullException("items");
     }
     for (int index = 0; index < items.Count; index++)
     {
         Add(items[index]);
     }
 }
Ejemplo n.º 5
0
        /// <summary>
        /// Populates the sync step based on the schema.
        /// </summary>
        internal void Initialize(SqlSchema entitySchema, SqlSchema databaseSchema)
        {
            if (entitySchema == null)
            {
                throw new ArgumentNullException("entitySchema");
            }
            if (databaseSchema == null)
            {
                throw new ArgumentNullException("databaseSchema");
            }

            // get...
            WorkUnitCollection units = entitySchema.GetSchemaWorkUnits(databaseSchema);

            if (units == null)
            {
                throw new InvalidOperationException("units is null.");
            }

            // set...
            _workUnits = units;
        }
Ejemplo n.º 6
0
        /// <summary>
        /// Processes the given work units.
        /// </summary>
        /// <param name="units"></param>
        public void Process(WorkUnitCollection units, bool trace = false, IOperationItem operation = null, bool outsideTransaction = false, ITimingBucket timings = null)
        {
            if (units == null)
            {
                throw new ArgumentNullException("units");
            }
            if (units.Count == 0)
            {
                return;
            }

            // operation...
            if (timings == null)
            {
                timings = NullTimingBucket.Instance;
            }
            if (operation == null)
            {
                operation = NullOperationItem.Instance;
            }

            // mbr - 02-10-2007 - case 827 - notify outside...
            // mbr - 2014-11-30 - changed this to load up the statements...
            var touchedEts          = new List <EntityType>();
            var anyNeedsTransaction = units.Count > 1;
            var context             = new WorkUnitProcessingContext(timings);

            using (timings.GetTimer("Count"))
            {
                foreach (IWorkUnit unit in units)
                {
                    ISaveChangesNotification notification = unit.Entity as ISaveChangesNotification;
                    if (notification != null)
                    {
                        notification.BeforeSaveChangesOutTransaction(unit);
                    }

                    // add...
                    if (!(touchedEts.Contains(unit.EntityType)))
                    {
                        touchedEts.Add(unit.EntityType);
                    }

                    // add...
                    if (unit.NeedsTransaction && !(anyNeedsTransaction))
                    {
                        anyNeedsTransaction = true;
                    }
                }
            }

            // mbr - 26-11-2007 - get a manager for this thread...
            IWorkUnitTransactionManager manager = null;

            using (timings.GetTimer("Initialize manager"))
            {
                if (outsideTransaction)
                {
                    manager = new OutsideTransactionWorkUnitProcessorTransactionManager();
                }
                else
                {
                    //manager = WorkUnitProcessorTransactionManager.Current;

                    // mbr - 2014-11-30 - we only want to use transactions if we are in a tranaction. the old
                    // approach created a transaction just for single row inserts, which turned out created
                    // quite a slowdown...
                    if (anyNeedsTransaction)
                    {
                        manager = WorkUnitProcessorTransactionManager.Current;
                    }
                    else
                    {
                        manager = WorkUnitProcessorNullTransactionManager.Current;
                    }
                }
                if (manager == null)
                {
                    throw new InvalidOperationException("manager is null.");
                }

                // initialise the manager...
                manager.Initialize();
            }

            try
            {
                // mbr - 26-11-2007 - now done in the manager...
//				if(connection == null)
//					connection = Database.CreateConnection(units[0].EntityType.DatabaseName);

                // mbr - 26-11-2007 - don't do transactions here (the manager will do it)...
//				connection.BeginTransaction();
                try
                {
                    // reset the bag...
                    using (timings.GetTimer("Reset"))
                        context.ResetBag();

                    // run...
                    operation.ProgressMaximum = units.Count;
                    operation.ProgressValue   = 0;

                    // mbr - 06-09-2007 - for c7 - the "before" event can replace the work unit.  (done by changing the entity and regenerating.)
                    //foreach(IWorkUnit unit in units)
                    for (int index = 0; index < units.Count; index++)
                    {
                        using (var child = timings.GetChildBucket("Execute"))
                        {
                            // get a connection to use here...
                            IConnection connection = null;
                            using (child.GetTimer("Connect"))
                            {
                                connection = manager.GetConnection(units[index]);
                                if (connection == null)
                                {
                                    throw new InvalidOperationException("connection is null.");
                                }

                                // set...
                                context.SetConnection(connection);
                            }

                            // get the unit...
                            IWorkUnit unit = units[index];
                            try
                            {
                                if (trace)
                                {
                                    this.LogInfo(() => string.Format("Running unit '{0}'...", unit));
                                }

                                ISaveChangesNotification notification = unit.Entity as ISaveChangesNotification;
                                using (child.GetTimer("NotifyPre"))
                                {
                                    // before...
                                    if (notification != null)
                                    {
                                        // mbr - 02-10-2007 - case 827 - changed interface.
                                        //								IWorkUnit newUnit = notification.BeforeSaveChanges(unit);
                                        IWorkUnit newUnit = notification.BeforeSaveChangesInTransaction(unit, connection);

                                        // patch it..
                                        if (newUnit != unit)
                                        {
                                            units[index] = newUnit;
                                            unit         = newUnit;
                                        }
                                    }
                                }

                                // run it...
                                using (var childChild = child.GetChildBucket("Process"))
                                    unit.Process(context, childChild);

                                // set...
                                using (child.GetTimer("Results"))
                                    unit.SetResultsBag(context.Bag);

                                // mbr - 10-10-2007 - for c7 - do reconciliation here...
                                using (child.GetTimer("Reconcile"))
                                {
                                    WorkUnitCollection forReconciliation = new WorkUnitCollection();
                                    forReconciliation.Add(unit);
                                    unit.EntityType.Persistence.ReconcileWorkUnitProcessorResults(forReconciliation);
                                }

                                // mbr - 02-10-2007 - case 827 - call...
                                using (child.GetTimer("NotifyPost"))
                                {
                                    if (notification != null)
                                    {
                                        notification.AfterSaveChangesInTransaction(unit, connection);
                                    }
                                }
                            }
                            catch (Exception ex)
                            {
                                throw new InvalidOperationException(string.Format(Cultures.Exceptions, "Failed when processing '{0}'.", unit), ex);
                            }

                            // next...
                            operation.IncrementProgress();
                        }
                    }

                    // mbr - 26-11-2007 - it's the manager that needs to commit...
//					connection.Commit();

                    using (timings.GetTimer("Commit"))
                        manager.Commit();
                }
                catch (Exception ex)
                {
                    // rollback...
                    try
                    {
                        // mbr - 26-11-2007 - it's the manager that needs to rollback...
//						connection.Rollback();
                        manager.Rollback(ex);
                    }
                    catch (Exception rollbackEx)
                    {
                        // mbr - 26-11-2007 - added.
                        if (this.Log.IsWarnEnabled)
                        {
                            this.Log.Warn("A further exception occurred whilst rolling back the transaction.", rollbackEx);
                        }
                    }

                    // throw...
                    throw new InvalidOperationException("Failed to process work units.", ex);
                }
            }
            finally
            {
                // mbr - 26-11-2007 - get the manager to tear down...
//				if(connection != null)
//					connection.Dispose();
                using (timings.GetTimer("Dispose"))
                    manager.Dispose();

                // mbr - 2010-04-19 - invalidate the caches...
                //EntityCache.Invalidate(touchedEts);

                // flag...
                foreach (IWorkUnit unit in units)
                {
                    ISaveChangesNotification notification = unit.Entity as ISaveChangesNotification;
                    if (notification != null)
                    {
                        notification.AfterSaveChangesOutTransaction(unit);
                    }
                }
            }
        }
Ejemplo n.º 7
0
        ///// <summary>
        ///// Processes the given work units.
        ///// </summary>
        ///// <param name="units"></param>
        //public void Process(WorkUnitCollection units)
        //{
        //    this.Process(units, null);
        //}

        void IWorkUnitProcessor.Process(WorkUnitCollection units)
        {
            this.Process(units);
        }