Skip to content

Commit

Permalink
allow sync of VirtualMList
Browse files Browse the repository at this point in the history
  • Loading branch information
JafarMirzaie committed Jul 22, 2022
1 parent 0619750 commit 2293a2c
Show file tree
Hide file tree
Showing 3 changed files with 185 additions and 82 deletions.
13 changes: 9 additions & 4 deletions Signum.Engine/Engine/SqlPreCommand.cs
Original file line number Diff line number Diff line change
Expand Up @@ -368,14 +368,19 @@ public SqlPreCommandSimple AddComment(string? comment)
return this;
}

public SqlPreCommandSimple ReplaceFirstParameter(string? variableName)
internal SqlPreCommandSimple ReplaceFirstParameter(string? variableName)
{
if (variableName == null)
return this;

var first = Parameters!.FirstEx();
Sql = Regex.Replace(Sql, $@"(?<toReplace>{first.ParameterName})(\b|$)", variableName); //HACK
Parameters!.Remove(first);
this.ReplaceParameter(this.Parameters!.FirstEx(), variableName);
return this;
}

internal SqlPreCommandSimple ReplaceParameter(DbParameter param, string variableName)
{
Sql = Regex.Replace(Sql, $@"(?<toReplace>{param.ParameterName})(\b|$)", variableName); //HACK
Parameters!.Remove(param);
return this;
}

Expand Down
26 changes: 24 additions & 2 deletions Signum.Engine/Schema/Schema.Delete.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using Signum.Engine.Linq;
using Signum.Utilities.Reflection;

namespace Signum.Engine.Maps;

Expand All @@ -19,13 +20,34 @@ public SqlPreCommand DeleteSqlSync<T>(T entity, Expression<Func<T, bool>>? where
select new SqlPreCommandSimple("DELETE FROM {0} WHERE {1} = {2}; --{3}"
.FormatWith(tml.Name, tml.BackReference.Name.SqlEscape(isPostgres), variableOrId, comment ?? entity.ToString()))).Combine(Spacing.Simple);

var vmis = VirtualMList.RegisteredVirtualMLists.TryGetC(this.Type);

SqlPreCommand? virtualMList = vmis?.Values
.Select(vmi => giDeleteVirtualMListSync.GetInvoker(this.Type, vmi.BackReferenceRoute.RootType)(entity, vmi))
.Combine(Spacing.Double);

var main = new SqlPreCommandSimple("DELETE FROM {0} WHERE {1} = {2}; --{3}"
.FormatWith(Name, this.PrimaryKey.Name.SqlEscape(isPostgres), variableOrId, comment ?? entity.ToString()));

if (isPostgres && declaration != null)
return PostgresDoBlock(entity.Id.VariableName!, declaration, SqlPreCommand.Combine(Spacing.Simple, pre, collections, main)!);
return PostgresDoBlock(entity.Id.VariableName!, declaration, SqlPreCommand.Combine(Spacing.Simple, pre, collections, virtualMList, main)!);

return SqlPreCommand.Combine(Spacing.Simple, declaration, pre, collections, virtualMList, main)!;
}

static GenericInvoker<Func<Entity, VirtualMListInfo, SqlPreCommand?>> giDeleteVirtualMListSync = new GenericInvoker<Func<Entity, VirtualMListInfo, SqlPreCommand?>>(
(e, vmi) => DeleteVirtualMListSync<Entity, Entity>(e, vmi));
static SqlPreCommand? DeleteVirtualMListSync<T, E>(T entity, VirtualMListInfo vmli)
where T : Entity
where E : Entity
{
var table = Schema.Current.Table(typeof(E));

var backRef = vmli.BackReferenceRoute.GetLambdaExpression<E, Lite<T>>(safeNullAccess: false);

var delete = Administrator.UnsafeDeletePreCommand(Database.Query<E>().Where(e => backRef.Evaluate(e).Is(entity)));

return SqlPreCommand.Combine(Spacing.Simple, declaration, pre, collections, main)!;
return delete;
}

int parameterIndex;
Expand Down
Loading

0 comments on commit 2293a2c

Please sign in to comment.