protected override Expression VisitUpdate(SqlUpdateExpression expression) { try { var newSource = Visit(expression.Source); this.inUpdate = true; this.updateTableName = (expression.Source as SqlTableExpression).Name; var newWhere = Visit(expression.Where); var newAssignments = VisitExpressionList(expression.Assignments); if (newSource != expression.Source || newWhere != expression.Where || newAssignments != expression.Assignments) { return(new SqlUpdateExpression(newSource, newAssignments, newWhere)); } return(expression); } finally { this.inUpdate = false; } }
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)); }
protected virtual Expression VisitUpdate(SqlUpdateExpression expression) { var newWhere = this.Visit(expression.Where); var newAssignments = this.VisitExpressionList(expression.Assignments); if (newWhere != expression.Where || newAssignments != expression.Assignments) { return(new SqlUpdateExpression(expression.Table, newAssignments, newWhere)); } return(expression); }
protected virtual Expression VisitUpdate(SqlUpdateExpression expression) { var newSource = Visit(expression.Source); var newWhere = Visit(expression.Where); var newAssignments = VisitExpressionList(expression.Assignments); if (newSource != expression.Source || newWhere != expression.Where || newAssignments != expression.Assignments) { return(new SqlUpdateExpression(newSource, newAssignments, newWhere, expression.RequiresIdentityInsert)); } return(expression); }
protected override Expression VisitUpdate(SqlUpdateExpression expression) { this.Write("UPDATE "); this.Visit(expression.Table); this.Write(" SET "); this.WriteDeliminatedListOfItems(expression.Assignments, c => this.Visit(c)); if (expression.Where == null) { this.Write(";"); } this.Write(" WHERE "); this.Visit(expression.Where); this.Write(";"); return(expression); }
protected override Expression VisitUpdate(SqlUpdateExpression updateExpression) { if (!(updateExpression.Source is SqlProjectionExpression projection)) { return(updateExpression); } 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); return(new SqlUpdateExpression(table, updateExpression.Assignments, where)); }
protected override Expression VisitUpdate(SqlUpdateExpression updateExpression) { return(updateExpression); }
protected virtual IDbCommand BuildUpdateCommand(TypeDescriptor typeDescriptor, DataAccessObject dataAccessObject) { IDbCommand command; SqlCommandValue sqlCommandValue; var updatedProperties = dataAccessObject.GetAdvanced().GetChangedPropertiesFlattened(); if (updatedProperties.Count == 0) { return(null); } var primaryKeys = dataAccessObject.GetAdvanced().GetPrimaryKeysForUpdateFlattened(); var commandKey = new SqlCommandKey(dataAccessObject.GetType(), updatedProperties); if (this.TryGetUpdateCommand(commandKey, out sqlCommandValue)) { command = this.CreateCommand(); command.CommandText = sqlCommandValue.commandText; this.FillParameters(command, updatedProperties, primaryKeys); return(command); } var assignments = updatedProperties.Select(c => (Expression) new SqlAssignExpression(new SqlColumnExpression(c.PropertyType, null, c.PersistedName), Expression.Constant(c.Value))).ToReadOnlyList(); Expression where = null; var i = 0; Debug.Assert(primaryKeys.Length > 0); foreach (var primaryKey in primaryKeys) { var currentExpression = Expression.Equal(new SqlColumnExpression(primaryKey.PropertyType, null, primaryKey.PersistedName), Expression.Constant(primaryKey.Value)); if (where == null) { where = currentExpression; } else { where = Expression.And(where, currentExpression); } i++; } var expression = new SqlUpdateExpression(new SqlTableExpression(typeDescriptor.PersistedName), assignments, where); expression = (SqlUpdateExpression)SqlObjectOperandComparisonExpander.Expand(expression); var result = this.SqlDatabaseContext.SqlQueryFormatterManager.Format(expression, SqlQueryFormatterOptions.Default & ~SqlQueryFormatterOptions.OptimiseOutConstantNulls); command = this.CreateCommand(); command.CommandText = result.CommandText; this.CacheUpdateCommand(commandKey, new SqlCommandValue() { commandText = command.CommandText }); this.FillParameters(command, updatedProperties, primaryKeys); return(command); }