internal override SqlStatement VisitUpdate(SqlUpdate sup)
		{
			SqlScope save = this.CurrentScope;
			this.CurrentScope = new SqlScope(sup.Select, this.CurrentScope.ContainingScope);
			base.VisitUpdate(sup);
			this.CurrentScope = save;
			return sup;
		}
Example #2
0
		internal override SqlStatement VisitUpdate(SqlUpdate update)
		{
			bool saveMakeUnique = this.makeUnique;
			this.makeUnique = false;
			bool saveUseMappedNames = this.useMappedNames;
			this.useMappedNames = true;
			SqlStatement stmt = base.VisitUpdate(update);
			this.makeUnique = saveMakeUnique;
			this.useMappedNames = saveUseMappedNames;
			return stmt;
		}
Example #3
0
 internal virtual SqlStatement VisitUpdate(SqlUpdate update) {
     update.Select = this.VisitSequence(update.Select);
     for (int i = 0, n = update.Assignments.Count; i < n; i++) {
         update.Assignments[i] = (SqlAssign)this.Visit(update.Assignments[i]);
     }
     return update;
 }
Example #4
0
		private SqlStatement VisitUpdate(Expression item, LambdaExpression check, LambdaExpression resultSelector)
		{
			if(item == null)
			{
				throw Error.ArgumentNull("item");
			}
			MetaTable metaTable = _services.Model.GetTable(item.Type);
			Expression source = _services.Context.GetTable(metaTable.RowType.Type).Expression;
			Type rowType = metaTable.RowType.Type;

			bool saveAllowDeferred = _allowDeferred;
			_allowDeferred = false;

			try
			{
				Expression seq = source;
				// construct identity predicate based on supplied item
				ParameterExpression p = Expression.Parameter(rowType, "p");
				LambdaExpression idPredicate = Expression.Lambda(Expression.Equal(p, item), p);

				// combine predicate and check expression into single find predicate
				LambdaExpression findPredicate = idPredicate;
				if(check != null)
				{
					findPredicate = Expression.Lambda(Expression.And(Expression.Invoke(findPredicate, p), Expression.Invoke(check, p)), p);
				}
				seq = Expression.Call(typeof(Enumerable), "Where", new Type[] { rowType }, seq, findPredicate);

				// source 'query' is based on table + find predicate
				SqlSelect ss = new RetypeCheckClause().VisitSelect(this.VisitSequence(seq));

				// construct update assignments from 'item' info
				List<SqlAssign> assignments = new List<SqlAssign>();
				ConstantExpression conItem = item as ConstantExpression;
				if(conItem == null)
				{
					throw Error.UpdateItemMustBeConstant();
				}
				if(conItem.Value == null)
				{
					throw Error.ArgumentNull("item");
				}
				// get changes from data services to construct update command
				Type entityType = conItem.Value.GetType();
				MetaType metaType = _services.Model.GetMetaType(entityType);
				ITable table = _services.Context.GetTable(metaType.InheritanceRoot.Type);
				foreach(ModifiedMemberInfo mmi in table.GetModifiedMembers(conItem.Value))
				{
					MetaDataMember mdm = metaType.GetDataMember(mmi.Member);
					assignments.Add(
						new SqlAssign(
							_nodeFactory.Member(ss.Selection, mmi.Member),
							new SqlValue(mdm.Type, _typeProvider.From(mdm.Type), mmi.CurrentValue, true, source),
							source
							));
				}

				SqlUpdate upd = new SqlUpdate(ss, assignments, source);

				if(resultSelector == null)
				{
					return upd;
				}

				SqlSelect select = null;

				// build select to return result
				seq = source;
				seq = Expression.Call(typeof(Enumerable), "Where", new Type[] { rowType }, seq, idPredicate);
				seq = Expression.Call(typeof(Enumerable), "Select", new Type[] { rowType, resultSelector.Body.Type }, seq, resultSelector);
				select = this.VisitSequence(seq);
				select.Where = _nodeFactory.AndAccumulate(
					_nodeFactory.Binary(SqlNodeType.GT, this.GetRowCountExpression(), _nodeFactory.ValueFromObject(0, false, _dominatingExpression)),
					select.Where
					);

				// combine update & select into statement block
				SqlBlock block = new SqlBlock(source);
				block.Statements.Add(upd);
				block.Statements.Add(select);
				return block;
			}
			finally
			{
				_allowDeferred = saveAllowDeferred;
			}
		}
		internal override SqlStatement VisitUpdate(SqlUpdate su)
		{
			_commandStringBuilder.Append("UPDATE ");
			_suppressedAliases.Add(su.Select.From);
			this.Visit(su.Select.From);
			this.NewLine();
			_commandStringBuilder.Append("SET ");

			for(int i = 0, n = su.Assignments.Count; i < n; i++)
			{
				if(i > 0) _commandStringBuilder.Append(", ");
				SqlAssign sa = su.Assignments[i];
				this.Visit(sa.LValue);
				_commandStringBuilder.Append(" = ");
				this.Visit(sa.RValue);
			}
			if(su.Select.Where != null)
			{
				this.NewLine();
				_commandStringBuilder.Append("WHERE ");
				this.Visit(su.Select.Where);
			}
			_suppressedAliases.Remove(su.Select.From);
			return su;
		}
		internal override SqlStatement VisitUpdate(SqlUpdate su)
		{
			SqlSelect ss = (SqlSelect)this.Visit(su.Select);
			List<SqlAssign> assignments = new List<SqlAssign>(su.Assignments.Count);
			foreach(SqlAssign sa in su.Assignments)
			{
				assignments.Add((SqlAssign)this.Visit(sa));
			}
			return new SqlUpdate(ss, assignments, su.SourceExpression);
		}
		internal override SqlStatement VisitUpdate(SqlUpdate sup)
		{
			bool saveTop = this.topLevel;
			this.topLevel = false;
			base.VisitUpdate(sup);
			this.topLevel = saveTop;
			return sup;
		}