Inheritance: SqlBaseExpression
		protected override Expression VisitInsertInto(SqlInsertIntoExpression insertIntoExpression)
		{
			var projection = insertIntoExpression.Source as SqlProjectionExpression;

			if (projection == null)
			{
				return insertIntoExpression;
			}

			if (projection.Select.From.NodeType != (ExpressionType)SqlExpressionType.Table)
			{
				throw new NotSupportedException();
			}

			var table = (SqlTableExpression)projection.Select.From;
			var alias = table.Alias;
			var where = AliasReferenceReplacer.Replace(projection.Select.Where, alias, table.Name);

			if (where != null)
			{
				throw new InvalidOperationException("Inserts must only be performed on pure tables");
			}

			return new SqlInsertIntoExpression(table, insertIntoExpression.ColumnNames, insertIntoExpression.ReturningAutoIncrementColumnNames, insertIntoExpression.ValueExpressions);
		}
		protected override Expression VisitInsertInto(SqlInsertIntoExpression expression)
		{
			if (!expression.RequiresIdentityInsert)
			{
				return base.VisitInsertInto(expression);
			}

			var list = new List<Expression>
			{
				new SqlSetCommandExpression("IDENTITY_INSERT", expression.Source, new SqlKeywordExpression("ON")),
				base.VisitInsertInto(expression),
				new SqlSetCommandExpression("IDENTITY_INSERT", expression.Source, new SqlKeywordExpression("OFF")),
			};

			return new SqlStatementListExpression(list);
		}
        protected override Expression VisitInsertInto(SqlInsertIntoExpression expression)
        {
            if (expression.ReturningAutoIncrementColumnNames != null && expression.ReturningAutoIncrementColumnNames.Count == 1)
            {
                var returningColumnName = expression.ReturningAutoIncrementColumnNames[0];
                var index = expression.ColumnNames.IndexOf(returningColumnName);

                if (index > 0)
                {
                    var newValueExpressions = new List<Expression>(expression.ValueExpressions);

                    newValueExpressions[index] = new SqlFunctionCallExpression(newValueExpressions[index].Type, "LAST_INSERT_ID", newValueExpressions[index]);

                    return new SqlInsertIntoExpression(expression.Table, expression.ColumnNames, expression.ReturningAutoIncrementColumnNames, newValueExpressions);
                }
            }

            return expression;
        }
        protected override void WriteInsertIntoReturning(SqlInsertIntoExpression expression)
        {
            if (expression.ReturningAutoIncrementColumnNames == null
                || expression.ReturningAutoIncrementColumnNames.Count == 0)
            {
                return;
            }

            this.Write(" OUTPUT ");
            this.WriteDeliminatedListOfItems<string>(expression.ReturningAutoIncrementColumnNames, c =>
            {
                this.WriteQuotedIdentifier("INSERTED");
                this.Write(".");
                this.WriteQuotedIdentifier(c);
            }, ",");
            this.Write("");
        }
 protected override Expression VisitInsertInto(SqlInsertIntoExpression expression)
 {
     this.hashCode ^= expression.RequiresIdentityInsert ? 2044950846 : 0;
     return base.VisitInsertInto(expression);
 }
        protected override Expression VisitInsertInto(SqlInsertIntoExpression expression)
        {
            SqlInsertIntoExpression current;
            if (!TryGetCurrent(expression, out current))
            {
                return expression;
            }

            if (!(this.result &= current.RequiresIdentityInsert == expression.RequiresIdentityInsert))
            {
                return expression;
            }

            if (!(this.result &= current.NodeType == expression.NodeType))
            {
                return expression;
            }

            if (!(this.result &= current.Type == expression.Type))
            {
                return expression;
            }

            this.currentObject = current.Source;
            this.Visit(expression.Source);
            if (!this.result)
            {
                return expression;
            }

            this.currentObject = current.WithExpression;
            this.Visit(expression.WithExpression);
            if (!this.result)
            {
                return expression;
            }

            this.currentObject = current.ValuesExpression;
            this.Visit(expression.ValuesExpression);
            if (!this.result)
            {
                return expression;
            }

            this.currentObject = current.ColumnNames;
            this.VisitObjectList(expression.ColumnNames);
            if (!this.result)
            {
                return expression;
            }

            this.currentObject = current.ValueExpressions;
            this.VisitExpressionList(expression.ValueExpressions);
            if (!this.result)
            {
                return expression;
            }

            this.currentObject = current.ReturningAutoIncrementColumnNames;
            this.VisitObjectList(expression.ReturningAutoIncrementColumnNames);
            if (!this.result)
            {
                return expression;
            }

            this.currentObject = current;
            return expression;
        }
Example #7
0
        protected override void WriteInsertIntoReturning(SqlInsertIntoExpression expression)
        {
            if (expression.ReturningAutoIncrementColumnNames == null
                || expression.ReturningAutoIncrementColumnNames.Count == 0)
            {
                return;
            }

            this.Write("; SELECT LAST_INSERT_ID()");
        }
Example #8
0
        protected override Expression VisitInsertInto(SqlInsertIntoExpression expression)
        {
            this.Write("INSERT INTO ");
            this.Visit(expression.Table);

            if (expression.ValueExpressions == null || expression.ValueExpressions.Count == 0)
            {
                this.WriteInsertDefaultValuesSuffix();
            }
            else
            {
                this.Write("(");
                this.WriteDeliminatedListOfItems(expression.ColumnNames, this.WriteQuotedIdentifier);

                this.Write(") ");

                if (this.sqlDialect.SupportsFeature(SqlFeature.InsertOutput))
                {
                    this.WriteInsertIntoReturning(expression);
                    this.Write(" ");
                }

                this.Write("VALUES (");
                this.WriteDeliminatedListOfItems(expression.ValueExpressions, c => this.Visit(c));
                this.Write(")");
            }

            if (!this.sqlDialect.SupportsFeature(SqlFeature.InsertOutput))
            {
                this.WriteInsertIntoReturning(expression);
            }

            this.Write(";");

            return expression;
        }
Example #9
0
		protected virtual Expression VisitInsertInto(SqlInsertIntoExpression expression)
		{
			var source = this.VisitSource(expression.Source);
			var valueExpressions = this.VisitExpressionList(expression.ValueExpressions);

			if (source != expression.Source || valueExpressions != expression.ValueExpressions)
			{
				return new SqlInsertIntoExpression(source, expression.ColumnNames, expression.ReturningAutoIncrementColumnNames, valueExpressions);
			}

			return expression;
		}
Example #10
0
		protected virtual bool WriteInsertIntoAfterSource(SqlInsertIntoExpression expression)
		{
			return false;
		}
		protected override Expression VisitInsertInto(SqlInsertIntoExpression insertIntoExpression)
		{
			return insertIntoExpression;
		}
		protected override bool WriteInsertIntoAfterSource(SqlInsertIntoExpression expression)
		{
			var tableHintExpression = expression.WithExpression as SqlTableHintExpression;

			if (tableHintExpression?.TableLock == true)
			{
				this.Write(" WITH (TABLOCK) ");
			}

			return true;
		}
		protected override Expression VisitUpdate(SqlUpdateExpression expression)
		{
			if (!expression.RequiresIdentityInsert)
			{
				return base.VisitUpdate(expression);
			}

			var tableName = ((SqlTableExpression)expression.Source).Name;

			var typeDescriptor = this
				.typeDescriptorProvider
				.GetTypeDescriptors()
				.Single(c => c.PersistedName == tableName);

			var insertedColumns = expression
				.Assignments
				.OfType<SqlAssignExpression>()
				.Select(c => new { name = ((SqlColumnExpression)c.Target).Name, value = c.Value, propertyDescriptor = typeDescriptor.GetPropertyDescriptorByColumnName(((SqlColumnExpression)c.Target).Name) })
				.ToList();

			var columnInfos = QueryBinder
				.GetColumnInfos(this.typeDescriptorProvider, typeDescriptor.PersistedProperties.Where(c => insertedColumns.All(d => d.propertyDescriptor != c)))
				.ToList();

			var visitedUpdated = (SqlUpdateExpression)base.VisitUpdate(expression);
			var selectIntoExpression = new SqlSelectExpression
			(
				typeof(void),
				null,
				columnInfos.Select
				(
					c => new SqlColumnDeclaration
					(
						null, new SqlColumnExpression(c.DefinitionProperty.PropertyType, null, c.GetColumnName())
					)
				)
				.Concat(insertedColumns.Select(d => d.value.Type.GetUnwrappedNullableType() == typeof(bool) ? new SqlColumnDeclaration(d.name, new BitBooleanExpression(d.value)) : new SqlColumnDeclaration(d.name, d.value)))
				.ToReadOnlyCollection(),
				visitedUpdated.Source,
				visitedUpdated.Where,
				null, null, false, null, null, false, false, new SqlTableExpression("#TEMP")
			);

			var selectExpression = new SqlSelectExpression(typeof(void), null, null, selectIntoExpression.Into, null, null);
			var insertExpression = new SqlInsertIntoExpression(visitedUpdated.Source, columnInfos.Select(c => c.GetColumnName()).Concat(insertedColumns.Select(c => c.name)).ToReadOnlyCollection(), null, selectExpression, null, true);
			var deleteExpression = new SqlDeleteExpression(visitedUpdated.Source, visitedUpdated.Where);
			
			var list = new List<Expression>
			{
				selectIntoExpression,
				deleteExpression,
				new SqlSetCommandExpression("IDENTITY_INSERT", visitedUpdated.Source, new SqlKeywordExpression("ON")),
				insertExpression,
				new SqlSetCommandExpression("IDENTITY_INSERT", visitedUpdated.Source, new SqlKeywordExpression("OFF")),
			};

			return new SqlStatementListExpression(list);
		}
Example #14
0
		protected override Expression VisitInsertInto(SqlInsertIntoExpression expression)
		{
			this.Write("INSERT INTO ");
			this.Visit(expression.Source);

			if ((expression.ValueExpressions == null || expression.ValueExpressions.Count == 0) && expression.ValuesExpression == null)
			{
				this.WriteInsertDefaultValuesSuffix();
			}
			else
			{
				this.WriteInsertIntoAfterSource(expression);

				this.Write("(");
				this.WriteDeliminatedListOfItems(expression.ColumnNames, this.WriteQuotedIdentifier);

				this.Write(") ");

				if (this.sqlDialect.SupportsCapability(SqlCapability.InsertOutput))
				{
					this.WriteInsertIntoReturning(expression);
					this.Write(" ");
				}


				if (expression.ValuesExpression != null)
				{
					this.Visit(expression.ValuesExpression);
				}
				else
				{
					this.Write("VALUES (");
					this.WriteDeliminatedListOfItems (expression.ValueExpressions, c =>
					{
						this.Write("(");
						this.Visit(c);
						this.Write(")");
					});
					this.Write(")");
				}
			}

			if (!this.sqlDialect.SupportsCapability(SqlCapability.InsertOutput))
			{
				this.WriteInsertIntoReturning(expression);
			}

			this.Write(";");

			return expression;
		}
        protected override void WriteInsertIntoReturning(SqlInsertIntoExpression expression)
        {
            if (expression.ReturningAutoIncrementColumnNames == null
                || expression.ReturningAutoIncrementColumnNames.Count == 0)
            {
                return;
            }

            this.Write(" RETURNING ");
            this.WriteDeliminatedListOfItems<string>(expression.ReturningAutoIncrementColumnNames, (Func<string, string>)this.WriteQuotedIdentifier, ",");
        }
Example #16
0
 protected virtual Expression VisitInsertInto(SqlInsertIntoExpression expression)
 {
     return expression;
 }
Example #17
0
 protected override Expression VisitInsertInto(SqlInsertIntoExpression expression)
 {
     this.hashCode ^= expression.RequiresIdentityInsert ? 2044950846 : 0;
     return(base.VisitInsertInto(expression));
 }
Example #18
0
 protected virtual Expression VisitInsertInto(SqlInsertIntoExpression expression)
 {
     return(expression);
 }