Represents an 'UPDATE' database query.
Inheritance: DbQueryExpression
Esempio n. 1
0
        /// <summary>
        /// Translates the <paramref name="updateExpression"/> into a string representation.
        /// </summary>
        /// <param name="updateExpression">The <see cref="DbUpdateExpression"/> to translate.</param>
        /// <returns><see cref="string"/></returns>
        protected override DbExpression VisitUpdateExpression(DbUpdateExpression updateExpression)
        {
            var sb = new StringBuilder();

            if (updateExpression.FromExpression.IsNull())
            {
                sb.AppendFormat("UPDATE {0} ", Visit(updateExpression.Target));
            }
            else
            {
                sb.AppendFormat("UPDATE {0} ", Visit(updateExpression.FromExpression));
            }


            if (!updateExpression.SetExpression.IsNull())
            {
                sb.AppendLine();
                sb.AppendFormat("SET {0} ", Visit(updateExpression.SetExpression));
            }

            if (!updateExpression.WhereExpression.IsNull())
            {
                sb.AppendLine();
                sb.AppendLine("WHERE ");
                sb.AppendFormat(1, "{0} ", Visit(updateExpression.WhereExpression));
            }

            return(ExpressionFactory.Sql(sb.ToString().Trim()));
        }
        /// <summary>
        /// Translates the <paramref name="updateExpression"/> into a string representation.
        /// </summary>
        /// <param name="updateExpression">The <see cref="DbUpdateExpression"/> to translate.</param>
        /// <returns><see cref="string"/></returns>
        protected override DbExpression VisitUpdateExpression(DbUpdateExpression updateExpression)
        {
            var  sb        = new StringBuilder();
            bool isAliased = false;

            if (updateExpression.Target.ExpressionType == DbExpressionType.Alias)
            {
                sb.AppendFormat("UPDATE {0} ", Visit(((DbAliasExpression)updateExpression.Target).Target));
                isAliased = true;
            }
            else
            {
                sb.AppendFormat("UPDATE {0} ", Visit(updateExpression.Target));
            }
            sb.AppendLine();
            if (!updateExpression.SetExpression.IsNull())
            {
                sb.AppendFormat("SET {0} ", Visit(updateExpression.SetExpression));
            }

            //If the target table is aliased, we asume that the same alias is used in the FROM clause
            if (!updateExpression.FromExpression.IsNull())
            {
                sb.AppendLine();
                sb.AppendLine("FROM ");
                sb.AppendFormat(1, "{0} ", Visit(updateExpression.FromExpression));
            }
            //If the target table is aliased we need to add a FROM clause
            else if (updateExpression.FromExpression.IsNull() && isAliased)
            {
                sb.AppendLine();
                sb.AppendLine("FROM ");
                sb.AppendFormat(1, "{0} ", Visit(updateExpression.Target));
            }

            if (!updateExpression.WhereExpression.IsNull())
            {
                sb.AppendLine();
                sb.AppendLine("WHERE ");
                sb.AppendFormat(1, "{0} ", Visit(updateExpression.WhereExpression));
            }

            return(ExpressionFactory.Sql(sb.ToString().Trim()));
        }
        /// <summary>
        /// Translates the <paramref name="updateExpression"/> into a string representation.
        /// </summary>
        /// <param name="updateExpression">The <see cref="DbUpdateExpression"/> to translate.</param>
        /// <returns><see cref="string"/></returns>
        protected override DbExpression VisitUpdateExpression(DbUpdateExpression updateExpression)
        {
            var sb = new StringBuilder();

            if (updateExpression.FromExpression.IsNull())
                sb.AppendFormat("UPDATE {0} ", Visit(updateExpression.Target));
            else
                sb.AppendFormat("UPDATE {0} ", Visit(updateExpression.FromExpression));

            if (!updateExpression.SetExpression.IsNull())
            {
                sb.AppendLine();
                sb.AppendFormat("SET {0} ", Visit(updateExpression.SetExpression));
            }

            if (!updateExpression.WhereExpression.IsNull())
            {
                sb.AppendLine();
                sb.AppendLine("WHERE ");
                sb.AppendFormat(1, "{0} ", Visit(updateExpression.WhereExpression));
            }

            return ExpressionFactory.Sql(sb.ToString().Trim());
        }
 /// <summary>
 /// Translates the <paramref name="updateExpression"/> into a string representation.
 /// </summary>
 /// <param name="updateExpression">The <see cref="DbUpdateExpression"/> to translate.</param>
 /// <returns><see cref="DbExpression"/></returns>
 protected virtual DbExpression VisitUpdateExpression(DbUpdateExpression updateExpression)
 {
     var targetExpression = Visit(updateExpression.Target);
     var fromExpression = Visit(updateExpression.FromExpression);
     var setExpression = Visit(updateExpression.SetExpression);
     var whereExpression = Visit(updateExpression.WhereExpression);
     if (!ReferenceEquals(targetExpression,updateExpression.Target))
         updateExpression.Target = targetExpression;
     if (!ReferenceEquals(fromExpression, updateExpression.FromExpression))
         updateExpression.FromExpression = fromExpression;
     if (!ReferenceEquals(setExpression, updateExpression.SetExpression))
         updateExpression.SetExpression = setExpression;
     if (!ReferenceEquals(whereExpression, updateExpression.WhereExpression))
         updateExpression.WhereExpression= whereExpression;
     return updateExpression;
 }
        /// <summary>
        /// Translates the <paramref name="updateExpression"/> into a string representation.
        /// </summary>
        /// <param name="updateExpression">The <see cref="DbUpdateExpression"/> to translate.</param>
        /// <returns><see cref="string"/></returns>
        protected override DbExpression VisitUpdateExpression(DbUpdateExpression updateExpression)
        {
            var sb = new StringBuilder();
            bool isAliased = false;
            if (updateExpression.Target.ExpressionType == DbExpressionType.Alias)
            {
                sb.AppendFormat("UPDATE {0} ", Visit(((DbAliasExpression) updateExpression.Target).Target));
                isAliased = true;
            }
            else
                sb.AppendFormat("UPDATE {0} ", Visit(updateExpression.Target));
            sb.AppendLine();
            if (!updateExpression.SetExpression.IsNull())
            {
                sb.AppendFormat("SET {0} ", Visit(updateExpression.SetExpression));
            }

            //If the target table is aliased, we asume that the same alias is used in the FROM clause
            if (!updateExpression.FromExpression.IsNull())
            {
                sb.AppendLine();
                sb.AppendLine("FROM ");
                sb.AppendFormat(1, "{0} ", Visit(updateExpression.FromExpression));
            }
            //If the target table is aliased we need to add a FROM clause
            else if (updateExpression.FromExpression.IsNull() && isAliased)
            {
                sb.AppendLine();
                sb.AppendLine("FROM ");
                sb.AppendFormat(1, "{0} ", Visit(updateExpression.Target));
            }

            if (!updateExpression.WhereExpression.IsNull())
            {
                sb.AppendLine();
                sb.AppendLine("WHERE ");
                sb.AppendFormat(1, "{0} ", Visit(updateExpression.WhereExpression));
            }

            return ExpressionFactory.Sql(sb.ToString().Trim());
        }