Exemple #1
0
        /// <summary>
        /// Adds a touched value block.
        /// </summary>
        /// <returns></returns>
        protected TouchedValueCollection AddTouchedValueBlock()
        {
            TouchedValueCollection block = new TouchedValueCollection();

            this.TouchedValueBlocks.Add(block);

            // return...
            return(block);
        }
 /// <summary>
 /// Adds a set of TouchedValue instances to the collection.
 /// </summary>
 /// <param name="item">The item to add.</param>
 public void AddRange(TouchedValueCollection items)
 {
     if (items == null)
     {
         throw new ArgumentNullException("items");
     }
     for (int index = 0; index < items.Count; index++)
     {
         Add(items[index]);
     }
 }
Exemple #3
0
        /// <summary>
        /// Appends the insert portion to the query.
        /// </summary>
        /// <param name="statement"></param>
        /// <param name="builder"></param>
        private void AppendInsertPortion(SqlStatement statement, StringBuilder builder)
        {
            if (statement == null)
            {
                throw new ArgumentNullException("statement");
            }
            if (builder == null)
            {
                throw new ArgumentNullException("builder");
            }

            // create a block...
            TouchedValueCollection block = this.AddTouchedValueBlock();

            EntityField[] autoIncrementFields = GetAutoIncrementFields();
            if (autoIncrementFields.Length > 0 && statement.Dialect.LastInsertedIdMode == LastInsertedIdMode.Parameter)
            {
                statement.Parameters.Add(statement.Dialect.GetLastInsertedIdParameter());
            }

            // append...
            builder.Append(statement.Dialect.InsertIntoKeyword);
            builder.Append(" ");
            builder.Append(statement.Dialect.FormatNativeName(this.EntityType.NativeName));
            builder.Append(" (");
            EntityField[] fields = this.GetNonAutoIncrementFields();
            for (int index = 0; index < fields.Length; index++)
            {
                if (index > 0)
                {
                    builder.Append(statement.Dialect.IdentifierSeparator);
                }
                builder.Append(statement.Dialect.FormatNativeName(fields[index].NativeName));
            }
            builder.Append(") ");
            builder.Append(statement.Dialect.ValuesKeyword);
            builder.Append(" (");
            for (int index = 0; index < fields.Length; index++)
            {
                // create the param...
                SqlStatementParameter param = this.CreateParameterForField(fields[index]);
                statement.Parameters.Add(param);
                param.RelatedField = fields[index];

                // add...
                if (index > 0)
                {
                    builder.Append(statement.Dialect.IdentifierSeparator);
                }
                builder.Append(statement.Dialect.FormatVariableNameForQueryText(param.Name));

                // mbr - 2007-04-02 - touched value...
                block.Add(new TouchedValue(fields[index], param.Value));
            }
            builder.Append(")");
            if (autoIncrementFields.Length > 0 && statement.Dialect.LastInsertedIdMode == LastInsertedIdMode.Parameter)
            {
                builder.Append(statement.Dialect.AppendLastInsertedIdParameterAccess(GetAutoIncrementFields()));
            }
            builder.Append(statement.Dialect.StatementSeparator);
        }
Exemple #4
0
        /// <summary>
        /// Creates an Delete statement.
        /// </summary>
        /// <returns></returns>
        public override SqlStatement[] GetStatements(WorkUnitProcessingContext context)
        {
            // check...
            if (EntityType == null)
            {
                throw new ArgumentNullException("EntityType");
            }
            if (Dialect == null)
            {
                throw new InvalidOperationException("Dialect is null.");
            }

            // mbr - 25-04-2007 - added touched...
            TouchedValueCollection block = this.AddTouchedValueBlock();

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

            // create...
            SqlStatement statement = new SqlStatement(this.EntityType, this.Dialect);

            statement.OriginalWorkUnit = this;

            // sql...
            StringBuilder builder = new StringBuilder();

            builder.Append(statement.Dialect.UpdateKeyword);
            builder.Append(" ");
            builder.Append(statement.Dialect.FormatNativeName(this.EntityType.NativeName));
            builder.Append(" ");
            builder.Append(statement.Dialect.SetKeyword);
            builder.Append(" ");

            // do the non-key fields...
            EntityField[] fields = this.GetNonAutoIncrementFields();
            if (fields.Length == 0)
            {
                throw new InvalidOperationException("Non-key fields are zero length.");
            }

            // walk...
            int fieldCount = 0;

            for (int index = 0; index < fields.Length; index++)
            {
                // create a parameter...
                SqlStatementParameter parameter = this.CreateParameterForField(fields[index]);
                statement.Parameters.Add(parameter);
                parameter.RelatedField = fields[index];

                // mbr - 25-04-2007 - added touched...
                object originalValue = null;
                if (this.Entity is Entity && ((Entity)this.Entity).GetOriginalValue(fields[index], ref originalValue))
                {
                    block.Add(new TouchedValue(fields[index], originalValue, parameter.Value));
                }

                // add...
                if (fieldCount > 0)
                {
                    builder.Append(",");
                }
                builder.Append(statement.Dialect.FormatNativeName(fields[index].NativeName));
                builder.Append("=");
                builder.Append(statement.Dialect.FormatVariableNameForQueryText(parameter.Name));

                fieldCount++;
            }

            // where...
            builder.Append(" ");
            builder.Append(statement.Dialect.WhereKeyword);
            builder.Append(" ");

            // key...
            fields = this.GetKeyFields();
            if (fields.Length == 0)
            {
                throw new InvalidOperationException("Key fields are zero length.");
            }

            // walk...
            StringBuilder constraints = new StringBuilder();

            for (int index = 0; index < fields.Length; index++)
            {
                // param...
                SqlStatementParameter parameter = this.CreateParameterForField(fields[index]);
                statement.Parameters.Add(parameter);
                parameter.RelatedField = fields[index];

                // add...
                if (index > 0)
                {
                    constraints.Append(" ");
                    constraints.Append(statement.Dialect.AndKeyword);
                    constraints.Append(" ");
                }
                constraints.Append(statement.Dialect.FormatNativeName(fields[index].NativeName));
                constraints.Append("=");
                constraints.Append(statement.Dialect.FormatVariableNameForQueryText(parameter.Name));
            }

            // mbr - 13-10-2005 - rejigged to handle partitioning...
            string useConstraints = constraints.ToString();

            //if(this.EntityType.SupportsPartitioning)
            //{
            //    // get the strategy....
            //    PartitioningStrategy strategy = this.EntityType.PartitioningStrategy;
            //    if(strategy == null)
            //        throw new InvalidOperationException("strategy is null.");

            //    // mbr - 04-09-2007 - for c7 - need to be able to skip the the constraint check...
            //    if(strategy.ConstrainUpdateQuery)
            //    {
            //        // get the partition SQL...  (yes, this is for read, not for write.  for write really means 'for insert'.)
            //        // mbr - 04-09-2007 - for c7 - removed 'forReading'.
            //        //				useConstraints = strategy.RebuildConstraints(statement, useConstraints, true);
            //        useConstraints = strategy.RebuildConstraints(statement, useConstraints);

            //        // we have to get something back...
            //        if(useConstraints == null)
            //            throw new InvalidOperationException("'useConstraints' is null.");

            //        // mbr - 04-09-2007 - for c7 - zero-length can be ok.
            //        if(useConstraints.Length == 0 && !(strategy.IsZeroLengthIdSetOk))
            //            throw new InvalidOperationException("'useConstraints' is zero-length.");
            //    }
            //}

            // append...
            builder.Append(useConstraints);

            // return...
            statement.CommandText = builder.ToString();
            return(new SqlStatement[] { statement });
        }