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());
}