/// <inheritdoc/> protected override SqlFragment GenerateForUpdateContent(GenerateContext context, DbExpression content) { var data = (UpdateContent)context.Data; data.SetConcurrencyExpectCount(1); RegisterExpressionForCommit(context, content, data.CommitObject); UpdateFragment update = null; if (data.Items.Count == 1) { update = data.UpdateByKeys(data.Unit, data.TargetName); } else { var values = new ValuesFragment(context, data.CommitObject, data.Items, data.Table); RegisterExpressionForCommit(context, content, values); update = data.UpdateByTemptable(data.Unit, values, data.TargetName); } if (data.ReturnMembers.Any()) { foreach (var member in data.ReturnMembers) { var metadata = member.Metadata; update.ReturnMembers.Add(update.Target.GetMember(metadata)); } data.GenerateOutput(); } return(update); }
/// <inheritdoc/> protected override SqlFragment GenerateForDeleteContent(GenerateContext context, DbExpression content) { var data = (DeleteContent)context.Data; data.SetConcurrencyExpectCount(1); var metadata = data.Table; if (data.Items.Count > 1 && metadata.Keys.Length > 1) { var values = new ValuesFragment(context, data.CommitObject, data.Items, data.Table); var filterMembers = data.UnionConcurrencyMembers(metadata, metadata.Keys); var delete = new DeleteFragment(context, metadata); var current = delete.Target; delete.AddSource(values); current.Join(values, filterMembers); return(delete); } return(base.GenerateForDeleteContent(context, content)); }
/// <inheritdoc/> protected override SqlFragment GenerateForRelation(GenerateContext context, DbExpression content) { var data = (RelationContent)context.Data; if (data.Items.Count > 1) { var metadata = data.Items.Navigate; if (data.IsAddRelation && !metadata.IsComposite) { var columns = data.Source.Keys.Concat(metadata.Pairs.Select(a => a.ForeignKey)); var values = new ValuesFragment(context, data.CommitObject, data.Items); data.Source.Keys.ForEach(key => values.SetValue(key)); if (data.IsAddRelation) { metadata.Pairs.ForEach( pair => values.SetValue(pair.ForeignKey, data.CommitObject.GetMember(pair.PrincipalKey))); } var update = new UpdateFragment(context, data.Table); update.AddSource(update.Target, values); foreach (var pair in metadata.Pairs) { update.SetValue(pair.ForeignKey, values.GetMember(pair.ForeignKey)); } update.Target.Join(values, data.Table.Keys); return(update); } else if (!data.IsAddRelation && metadata.IsComposite) { var composite = (CompositeNavigateMetadata)data.Items.Navigate; var values = new ValuesFragment(context, data.CommitObject, data.Items); composite.Pairs.ForEach(pair => values.SetValue(pair.ForeignKey, data.CommitObject.GetMember(pair.PrincipalKey))); composite.CompositePairs.ForEach(pair => values.SetValue(pair.ForeignKey, data.CommitObject.GetMember(pair.PrincipalKey))); var target = new TableFragment(context, data.Table); var delete = new DeleteFragment(context, target); delete.AddSource(values); target.Join(values, composite.Pairs.Select(a => a.ForeignKey).Concat(composite.CompositePairs.Select(a => a.ForeignKey))); return(delete); } } return(base.GenerateForRelation(context, content)); }