Exemple #1
0
        private bool SaveUpdate(ITransaction transaction)
        {
            SchemaObject schemaObject = Schema.Schema.GetSchemaObject(GetType());

            IUpdateQuery updateQuery = SQLProviderFactory.GetUpdateQuery();

            updateQuery.Table = new Table(schemaObject.SchemaName, schemaObject.ObjectName);

            foreach (Schema.Field field in schemaObject.GetFields().Where(f => f != schemaObject.PrimaryKeyField))
            {
                FieldValue fieldValue = new FieldValue();
                fieldValue.FieldName = field.FieldName;
                fieldValue.Value     = field.GetValue(this);
                updateQuery.FieldValueList.Add(fieldValue);
            }

            updateQuery.Condition = new Condition()
            {
                Left          = (Base.Data.Operand.Field)schemaObject.PrimaryKeyField.FieldName,
                ConditionType = Condition.ConditionTypes.Equal,
                Right         = new Literal(schemaObject.PrimaryKeyField.GetValue(this))
            };

            updateQuery.Execute(transaction);

            return(true);
        }
Exemple #2
0
        private void HandleFKConstraintConflicts(List <FKConstraintConflict> conflicts, ITransaction transaction)
        {
            HashSet <FKConstraintConflict> handled = new HashSet <FKConstraintConflict>();
            IEnumerable <IGrouping <Type, FKConstraintConflict> > constraintsByType = conflicts.GroupBy(conf => conf.ConflictType);

            foreach (IGrouping <Type, FKConstraintConflict> constraintGroup in constraintsByType)
            {
                SchemaObject foreignSchemaObject = Schema.Schema.GetSchemaObject(constraintGroup.Key);
                foreach (IGrouping <string, FKConstraintConflict> constraintGroupByField in constraintGroup.GroupBy(fk => fk.ForeignKeyName))
                {
                    List <long> autoDeleteReferenceKeys = new List <long>();
                    List <long> autoRemoveReferenceKeys = new List <long>();

                    foreach (FKConstraintConflict fKConstraintConflict in constraintGroup)
                    {
                        switch (fKConstraintConflict.ActionType)
                        {
                        case FKConstraintConflict.ActionTypes.AutoDeleteReference:
                            autoDeleteReferenceKeys.Add(fKConstraintConflict.ForeignKey.Value);
                            handled.Add(fKConstraintConflict);
                            break;

                        case FKConstraintConflict.ActionTypes.AutoRemoveReference:
                            autoRemoveReferenceKeys.Add(fKConstraintConflict.ForeignKey.Value);
                            handled.Add(fKConstraintConflict);
                            break;
                        }
                    }

                    if (autoDeleteReferenceKeys.Any())
                    {
                        IDeleteQuery deleteQuery = SQLProviderFactory.GetDeleteQuery();
                        deleteQuery.Table     = new Table(foreignSchemaObject.SchemaName, foreignSchemaObject.ObjectName);
                        deleteQuery.Condition = new Condition()
                        {
                            Left          = (Base.Data.Operand.Field)foreignSchemaObject.PrimaryKeyField.FieldName,
                            ConditionType = Condition.ConditionTypes.List,
                            Right         = (CSV <long>)autoDeleteReferenceKeys
                        };

                        deleteQuery.Execute(transaction);
                    }

                    if (autoRemoveReferenceKeys.Any())
                    {
                        IUpdateQuery updateQuery = SQLProviderFactory.GetUpdateQuery();
                        updateQuery.Table = new Table(foreignSchemaObject.SchemaName, foreignSchemaObject.ObjectName);
                        updateQuery.FieldValueList.Add(new FieldValue()
                        {
                            FieldName = constraintGroupByField.Key,
                            Value     = null
                        });
                        updateQuery.Condition = new Condition()
                        {
                            Left          = (Base.Data.Operand.Field)foreignSchemaObject.PrimaryKeyField.FieldName,
                            ConditionType = Condition.ConditionTypes.List,
                            Right         = (CSV <long>)autoRemoveReferenceKeys
                        };

                        updateQuery.Execute(transaction);
                    }
                }
            }

            conflicts.RemoveAll(fk => handled.Contains(fk));
        }