Ejemplo n.º 1
0
        public static IDeleteQuery CreateDeleteQuery(string db, DbProviderType provider)
        {
            IDeleteQuery query = CreateDeleteQuery(db);

            query.DbProviderType = provider;
            return(query);
        }
Ejemplo n.º 2
0
        public override async Task <DeleteQueryResult> QueryAsync(IDeleteQuery deleteQuery)
        {
            var b   = Batch.CreateSimple(this);
            var res = b.QueryAsync(deleteQuery);
            await b.Execute();

            return(res.Result);
        }
Ejemplo n.º 3
0
        /// <summary>
        /// Creates a command to delete all records
        /// </summary>
        /// <returns></returns>
        protected IDbCommand CreateDeleteAllCommand()
        {
            IDbCommand   command = DbProviderFactory.CreateCommand(Provider);
            IDeleteQuery s       = QueryFactory.CreateDeleteQuery(Db, Provider);

            s.SetTable(Table);
            command.CommandText = s.GetText();
            return(command);
        }
Ejemplo n.º 4
0
        public override Task <DeleteQueryResult> QueryAsync(IDeleteQuery deleteQuery)
        {
            _combinedRawQuery.Append(deleteQuery.GetRawQuery());
            _combinedRawQuery.Append(";\n");
            IsEmpty = false;

            var tcs = new TaskCompletionSource <DeleteQueryResult>();

            _resultProcessors.Add(async reader => {
                tcs.SetResult(await deleteQuery.ReadResultAsync(reader));
            });

            return(tcs.Task);
        }
Ejemplo n.º 5
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));
        }
Ejemplo n.º 6
0
        public bool Delete(ITransaction transaction = null, List <Guid> saveFlags = null)
        {
            ITransaction localTransaction = transaction == null?SQLProviderFactory.GenerateTransaction() : transaction;

            try
            {
                if (!isEditable)
                {
                    throw new System.Data.ReadOnlyException("Attempt to call Delete on a Read Only Data Object");
                }

                PreValidate();
                Validate(Validator.SaveModes.Delete, saveFlags);

                if (Errors.Any())
                {
                    return(false);
                }

                fKConstraintConflicts = GetFKConstraintConflicts(localTransaction);
                if (fKConstraintConflicts.Any())
                {
                    HandleFKConstraintConflicts(fKConstraintConflicts, localTransaction);
                    if (fKConstraintConflicts.Any())
                    {
                        return(false);
                    }
                }

                if (!PreDelete(localTransaction))
                {
                    return(false);
                }

                SchemaObject schemaObject = Schema.Schema.GetSchemaObject(GetType());

                IDeleteQuery deleteQuery = SQLProviderFactory.GetDeleteQuery();
                deleteQuery.Table     = new Table(schemaObject.SchemaName, schemaObject.ObjectName);
                deleteQuery.Condition = new Condition()
                {
                    Left          = (Base.Data.Operand.Field)schemaObject.PrimaryKeyField.FieldName,
                    ConditionType = Condition.ConditionTypes.Equal,
                    Right         = new Literal(schemaObject.PrimaryKeyField.GetValue(this))
                };

                deleteQuery.Execute(localTransaction);

                if (!PostDelete(localTransaction))
                {
                    return(false);
                }

                if (transaction == null)
                {
                    localTransaction.Commit();
                }
            }
            finally
            {
                if (transaction == null && localTransaction.IsActive)
                {
                    localTransaction.Rollback();
                }
            }

            return(true);
        }
Ejemplo n.º 7
0
 public abstract Task <DeleteQueryResult> QueryAsync(IDeleteQuery deleteQuery);