Represents a SQL DELETE command that deletes a single row of data from a table.
상속: BaseInsertDeleteCommand
        private static void AppendDeleteCommand(Script script, DeleteCommand command, ref int paramIndex)
        {
            var operation = command.Operation;
            if (operation.ValueMetadata != null)
            {
                if (null != operation.OwnerPropertyMetadata
                    && operation.OwnerPropertyMetadata.HasAttribute<ManyToManyAttribute>())
                {
                    //  Remove record in link table; don't touch either entity table

                    script.Buffer.Append(string.Format(
                        @"DELETE FROM {0}
WHERE [{1}] = ", 
                        operation.OwnerPropertyMetadata.GetAttribute<ManyToManyAttribute>().SchemaQualifiedLinkTableName,
                        operation.OwnerPrimaryKeyColumn));

                    FormatWithParameter(script, "{0} AND ", ref paramIndex, new Func<object>(() => operation.OwnerPrimaryKeyAsObject));

                    script.Buffer.Append(string.Format("[{0}] = ", operation.ValueMetadata.PrimaryKey.Prop.Name));

                    FormatWithParameter(script, @"{0};
", ref paramIndex, new Func<object>(() => operation.ValueMetadata.GetPrimaryKeyValueAsObject(operation.Value)));
                }
                else if (operation.OwnerPropertyMetadata == null
                    || ((operation.OwnerPropertyMetadata.HasAttribute<OneToManyAttribute>()
                        || operation.OwnerPropertyMetadata.HasAttribute<OneToOneAttribute>())
                    && !operation.ValueMetadata.HasAttribute<ReferenceDataAttribute>()
                    && !operation.OwnerPropertyMetadata.HasAttribute<ReferenceDataAttribute>()))
                {
                    //  DELETE the value from the other table

                    script.Buffer.Append(string.Format(
                        @"DELETE FROM {0}
WHERE [{1}] = ",
                        operation.ValueMetadata.TableName,
                        operation.ValueMetadata.PrimaryKey.Prop.Name));
                    FormatWithParameter(script, @"{0};
", ref paramIndex, new Func<object>(() => operation.ValueMetadata.GetPrimaryKeyValueAsObject(operation.Value)));
                }
            }
            else
            {
                throw new ArgumentException(
                    string.Format(
                        "Invalid DELETE command: {0}",
                        JsonConvert.SerializeObject(command)),
                    "command");
            }
        }
예제 #2
0
        private static void AppendDeleteCommand(Script script, DeleteCommand command, ref int paramIndex)
        {
            var operation = command.Operation;

            if (operation.ValueMetadata != null)
            {
                if (null != operation.OwnerPropertyMetadata &&
                    operation.OwnerPropertyMetadata.HasAttribute <ManyToManyAttribute>())
                {
                    //  Remove record in link table; don't touch either entity table

                    script.Buffer.Append(string.Format(
                                             @"DELETE FROM {0}
WHERE [{1}] = ",
                                             operation.OwnerPropertyMetadata.GetAttribute <ManyToManyAttribute>().SchemaQualifiedLinkTableName,
                                             operation.OwnerPrimaryKeyColumn));

                    FormatWithParameter(
                        script,
                        "{0} AND ",
                        ref paramIndex,
                        null,   //  No wire up required for DELETE from link table.
                        new Func <object>(() => operation.OwnerPrimaryKeyAsObject));

                    script.Buffer.Append(string.Format("[{0}] = ", operation.ValueMetadata.PrimaryKey.Prop.Name));

                    FormatWithParameter(script, @"{0};
",
                                        ref paramIndex,
                                        null, //  No wire up required for DELETE from link table.
                                        new Func <object>(() => operation.ValueMetadata.GetPrimaryKeyValueAsObject(operation.Value)));
                }
                else if (operation.OwnerPropertyMetadata == null ||
                         ((operation.OwnerPropertyMetadata.HasAttribute <OneToManyAttribute>() ||
                           operation.OwnerPropertyMetadata.HasAttribute <OneToOneAttribute>()) &&
                          !operation.ValueMetadata.HasAttribute <ReferenceDataAttribute>() &&
                          !operation.OwnerPropertyMetadata.HasAttribute <ReferenceDataAttribute>()))
                {
                    //  DELETE the value from the other table

                    script.Buffer.Append(string.Format(
                                             @"DELETE FROM {0}
WHERE [{1}] = ",
                                             operation.ValueMetadata.TableName,
                                             operation.ValueMetadata.PrimaryKey.Prop.Name));
                    FormatWithParameter(script, @"{0};
",
                                        ref paramIndex,
                                        null, //  Methinks no wire-up required here either, although with 1:1 relationships where the FK is on the
                                              //  child there's always the possibility we might need to set the column value on the parent to null.
                                              //  Possible TODO
                                        new Func <object>(() => operation.ValueMetadata.GetPrimaryKeyValueAsObject(operation.Value)));
                }
            }
            else
            {
                throw new ArgumentException(
                          string.Format(
                              "Invalid DELETE command: {0}",
                              JsonConvert.SerializeObject(command)),
                          "command");
            }
        }
        private static void AppendDeleteCommand(Script script, DeleteCommand command, ref int paramIndex)
        {
            var operation = command.Operation;
            if (operation.ValueMetadata != null)
            {
                if (null != operation.OwnerPropertyMetadata
                    && operation.OwnerPropertyMetadata.HasAttribute<ManyToManyAttribute>())
                {
                    //  Remove record in link table; don't touch either entity table

                    script.Buffer.Append(string.Format(
                        @"DELETE FROM {0}
WHERE [{1}] = ", 
                        operation.OwnerPropertyMetadata.GetAttribute<ManyToManyAttribute>().SchemaQualifiedLinkTableName,
                        operation.OwnerPrimaryKeyColumn));

                    FormatWithParameter(
                        script,
                        "{0} AND ",
                        ref paramIndex,
                        null,   //  No wire up required for DELETE from link table.
                        new Func<object>(() => operation.OwnerPrimaryKeyAsObject));

                    script.Buffer.Append(string.Format("[{0}] = ", operation.ValueMetadata.PrimaryKey.Prop.Name));

                    FormatWithParameter(script, @"{0};
",
                        ref paramIndex,
                        null,   //  No wire up required for DELETE from link table.
                        new Func<object>(() => operation.ValueMetadata.GetPrimaryKeyValueAsObject(operation.Value)));
                }
                else if (operation.OwnerPropertyMetadata == null
                    || ((operation.OwnerPropertyMetadata.HasAttribute<OneToManyAttribute>()
                        || operation.OwnerPropertyMetadata.HasAttribute<OneToOneAttribute>())
                    && !operation.ValueMetadata.HasAttribute<ReferenceDataAttribute>()
                    && !operation.OwnerPropertyMetadata.HasAttribute<ReferenceDataAttribute>()))
                {
                    //  DELETE the value from the other table

                    script.Buffer.Append(string.Format(
                        @"DELETE FROM {0}
WHERE [{1}] = ",
                        operation.ValueMetadata.TableName,
                        operation.ValueMetadata.PrimaryKey.Prop.Name));
                    FormatWithParameter(script, @"{0};
",
                        ref paramIndex,
                        null,   //  Methinks no wire-up required here either, although with 1:1 relationships where the FK is on the
                                //  child there's always the possibility we might need to set the column value on the parent to null.
                                //  Possible TODO
                        new Func<object>(() => operation.ValueMetadata.GetPrimaryKeyValueAsObject(operation.Value)));
                }
            }
            else
            {
                throw new ArgumentException(
                    string.Format(
                        "Invalid DELETE command: {0}",
                        JsonConvert.SerializeObject(command)),
                    "command");
            }
        }