Skip to content

Commit

Permalink
add SeparatedByComma/NewLine
Browse files Browse the repository at this point in the history
  • Loading branch information
olmobrutall committed Aug 9, 2022
1 parent d16aa89 commit 94401cd
Show file tree
Hide file tree
Showing 12 changed files with 357 additions and 38 deletions.
8 changes: 4 additions & 4 deletions Signum.Engine/DynamicQuery/DQueryable.cs
Original file line number Diff line number Diff line change
Expand Up @@ -149,7 +149,7 @@ static LambdaExpression SelectTupleConstructor(BuildExpressionContext context, H
if (str.HasText())
throw new ApplicationException(str);

List<Expression> expressions = tokens.Select(t => t.BuildExpression(context)).ToList();
List<Expression> expressions = tokens.Select(t => t.BuildExpression(context, searchToArray: true)).ToList();
Expression ctor = TupleReflection.TupleChainConstructor(expressions);

var pe = Expression.Parameter(ctor.Type);
Expand Down Expand Up @@ -865,15 +865,15 @@ static LambdaExpression ResultSelectSelectorAndContext(BuildExpressionContext co
if (isQueryable)
{
var tempContext = new BuildExpressionContext(keyTupleType, pk, rootKeyTokens.Select((kqt, i) => KeyValuePair.Create(kqt, new ExpressionBox(TupleReflection.TupleChainProperty(pk, i)))).ToDictionary());
resultExpressions.AddRange(redundantKeyTokens.Select(t => KeyValuePair.Create(t, t.BuildExpression(tempContext))));
resultExpressions.AddRange(redundantKeyTokens.Select(t => KeyValuePair.Create(t, t.BuildExpression(tempContext, searchToArray: true))));
}
else
{
var first = Expression.Call(miFirstE.MakeGenericMethod(typeof(object)), pe);

resultExpressions.AddRange(redundantKeyTokens.Select(t =>
{
var exp = t.BuildExpression(context);
var exp = t.BuildExpression(context, searchToArray: true);
var replaced = ExpressionReplacer.Replace(exp,
new Dictionary<ParameterExpression, Expression>
{
Expand Down Expand Up @@ -1101,7 +1101,7 @@ public static ResultTable ToResultTable<T>(this DEnumerableCount<T> collection,
var rc = new ResultColumn(c, array);
if (c.Token.Type.IsLite() || isMultiKeyGrupping && c.Token is not AggregateToken)
if ((c.Token.Type.IsLite() || isMultiKeyGrupping && c.Token is not AggregateToken) && c.Token.HasToArray() == null)
rc.CompressUniqueValues = true;
return rc;
Expand Down
2 changes: 1 addition & 1 deletion Signum.Engine/Json/FilterJsonConverter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -130,7 +130,7 @@ public class ColumnTS

public Column ToColumn(QueryDescription qd, bool canAggregate)
{
var queryToken = QueryUtils.Parse(token, qd, SubTokensOptions.CanElement | (canAggregate ? SubTokensOptions.CanAggregate : SubTokensOptions.CanOperation));
var queryToken = QueryUtils.Parse(token, qd, SubTokensOptions.CanElement | SubTokensOptions.CanToArray | (canAggregate ? SubTokensOptions.CanAggregate : SubTokensOptions.CanOperation));

return new Column(queryToken, displayName ?? queryToken.NiceName());
}
Expand Down
4 changes: 4 additions & 0 deletions Signum.Entities/DynamicQuery/QueryUtils.cs
Original file line number Diff line number Diff line change
Expand Up @@ -427,6 +427,9 @@ public static LambdaExpression CreateOrderLambda(QueryToken token, BuildExpressi
if (QueryToken.IsCollection(token.Type))
return "Collections can not be ordered";

if (token.HasToArray() != null)
return "ToArray can not be ordered";

if (token.HasAllOrAny())
return "'{0}', '{1}', '{2}' or '{3}' can not be ordered".FormatWith(
CollectionAnyAllType.All.NiceToString(),
Expand Down Expand Up @@ -599,4 +602,5 @@ public enum SubTokensOptions
CanAnyAll = 2,
CanElement = 4,
CanOperation = 8,
CanToArray = 16,
}
Original file line number Diff line number Diff line change
Expand Up @@ -127,7 +127,7 @@ protected override Expression BuildExpressionInternal(BuildExpressionContext con
public static List<CollectionElementToken> GetElements(HashSet<QueryToken> allTokens)
{
return allTokens
.SelectMany(t => t.Follow(tt => tt.Parent))
.SelectMany(t => (t.HasToArray() ?? t).Follow(tt => tt.Parent))
.OfType<CollectionElementToken>()
.Distinct()
.OrderBy(a => a.FullKey().Length)
Expand Down
225 changes: 225 additions & 0 deletions Signum.Entities/DynamicQuery/Tokens/CollectionToArrayToken.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,225 @@
using Signum.Entities.Reflection;
using Signum.Utilities.Reflection;
using System.ComponentModel;

namespace Signum.Entities.DynamicQuery;

public class CollectionToArrayToken : QueryToken
{
public CollectionToArrayType ToArrayType { get; private set; }

QueryToken parent;
public override QueryToken? Parent => parent;

readonly Type elementType;
internal CollectionToArrayToken(QueryToken parent, CollectionToArrayType toArrayType)
{
elementType = parent.Type.ElementType()!;
if (elementType == null)
throw new InvalidOperationException("not a collection");

this.ToArrayType = toArrayType;

this.parent = parent ?? throw new ArgumentNullException(nameof(parent));
}

public override Type Type
{
get { return elementType.BuildLiteNullifyUnwrapPrimaryKey(new[] { this.GetPropertyRoute()! }); }
}

public override string ToString()
{
return ToArrayType.NiceToString();
}

public override string Key
{
get { return ToArrayType.ToString(); }
}

protected override List<QueryToken> SubTokensOverride(SubTokensOptions options)
{
var st = SubTokensBase(Type, options, GetImplementations());

var ept = MListElementPropertyToken.AsMListEntityProperty(this.parent);
if (ept != null)
{
var mleType = MListElementPropertyToken.MListElementType(ept);

st.Add(new MListElementPropertyToken(this, mleType.GetProperty("RowId")!, ept.PropertyRoute, "RowId", () => QueryTokenMessage.RowId.NiceToString()) { Priority = -5 });

if (MListElementPropertyToken.HasAttribute(ept.PropertyRoute, typeof(PreserveOrderAttribute)))
st.Add(new MListElementPropertyToken(this, mleType.GetProperty("RowOrder")!, ept.PropertyRoute, "RowOrder", () => QueryTokenMessage.RowOrder.NiceToString()) { Priority = -5 });
}

return st;
}

public override Implementations? GetImplementations()
{
return parent.GetElementImplementations();
}

public override string? Format
{
get
{

if (Parent is ExtensionToken et && et.IsProjection)
return et.ElementFormat;

return parent.Format;
}
}

public override string? Unit
{
get
{

if (Parent is ExtensionToken et && et.IsProjection)
return et.ElementUnit;

return parent.Unit;
}
}

public override string? IsAllowed()
{
return parent.IsAllowed();
}


public override CollectionToArrayToken? HasToArray() => this;

public override PropertyRoute? GetPropertyRoute()
{
if (parent is ExtensionToken et && et.IsProjection)
return et.GetElementPropertyRoute();

PropertyRoute? pr = this.parent!.GetPropertyRoute();
if (pr != null && pr.Type.ElementType() != null)
return pr.Add("Item");

return pr;
}

public override string NiceName()
{
return this.parent.NiceName();
}

public override QueryToken Clone()
{
return new CollectionToArrayToken(parent.Clone(), ToArrayType);
}

protected override Expression BuildExpressionInternal(BuildExpressionContext context)
{
throw new InvalidOperationException("ToArrayToken should have a replacement at this stage");
}


public override string TypeColor
{
get { return "#0000FF"; }
}

public static int MaxToArrayValues = 100;

static MethodInfo miToArray = ReflectionTools.GetMethodInfo(() => Enumerable.ToArray<int>(null!)).GetGenericMethodDefinition();
static MethodInfo miToDistict = ReflectionTools.GetMethodInfo(() => Enumerable.Distinct<int>(null!)).GetGenericMethodDefinition();
static MethodInfo miSelect = ReflectionTools.GetMethodInfo(() => Enumerable.Select<int, long>(null!, a => a)).GetGenericMethodDefinition();
static MethodInfo miTake = ReflectionTools.GetMethodInfo(() => Enumerable.Take<int>(null!, 0)).GetGenericMethodDefinition();
static MethodInfo miSelectMany = ReflectionTools.GetMethodInfo(() => Enumerable.SelectMany<string, char>(null!, a => a)).GetGenericMethodDefinition();

internal static Expression BuildToArrayExpression(QueryToken token, CollectionToArrayToken cta, BuildExpressionContext context)
{
var ept = MListElementPropertyToken.AsMListEntityProperty(cta.Parent!);

BuildExpressionContext subCtx;
Expression query;
if(ept != null)
{
var collection = MListElementPropertyToken.BuildMListElements(ept, context);
query = collection;
Type mleType = collection.Type.ElementType()!;
var param = Expression.Parameter(mleType, mleType.Name.Substring(0, 1).ToLower());
subCtx = new BuildExpressionContext(mleType, param, new Dictionary<QueryToken, ExpressionBox>
{
{ cta, new ExpressionBox(param, mlistElementRoute: cta.GetPropertyRoute()) }
});
}
else
{
var collection = cta.Parent!.BuildExpression(context);
query = collection;
Type elemeType = collection.Type.ElementType()!;
var param = Expression.Parameter(elemeType, elemeType.Name.Substring(0, 1).ToLower());
subCtx = new BuildExpressionContext(elemeType, param, new Dictionary<QueryToken, ExpressionBox>()
{
{ cta, new ExpressionBox(param.BuildLiteNullifyUnwrapPrimaryKey(new[] { cta.GetPropertyRoute()! }))}
});
}

Expression body;
if (token is CollectionToArrayToken)
{
body = subCtx.Parameter.BuildLite();
}
else
{
var cets = token.Follow(a => a.Parent).TakeWhile(a => a != cta).OfType<CollectionElementToken>().Reverse().ToList();
foreach (var ce in cets)
{
var ept2 = MListElementPropertyToken.AsMListEntityProperty(ce.Parent!);
if (ept2 != null)
{
var collection = MListElementPropertyToken.BuildMListElements(ept2, subCtx);
Type mleType = collection.Type.ElementType()!;
query = Expression.Call(miSelectMany.MakeGenericMethod(query.Type.ElementType()!, mleType), query, Expression.Lambda(collection, subCtx.Parameter));
var param = Expression.Parameter(mleType, mleType.Name.Substring(0, 1).ToLower());
subCtx = new BuildExpressionContext(mleType, param, new Dictionary<QueryToken, ExpressionBox>()
{
{ ce, new ExpressionBox(param, mlistElementRoute: ce.GetPropertyRoute())}
});
}
else
{
var collection = ce.Parent!.BuildExpression(subCtx);
Type elementType = collection.Type.ElementType()!;
query = Expression.Call(miSelectMany.MakeGenericMethod(query.Type.ElementType()!, elementType), query, Expression.Lambda(collection, subCtx.Parameter));

var param = Expression.Parameter(elementType, elementType.Name.Substring(0, 1).ToLower());
subCtx = new BuildExpressionContext(elementType, param, new Dictionary<QueryToken, ExpressionBox>()
{
{ ce, new ExpressionBox(param.BuildLiteNullifyUnwrapPrimaryKey(new[] { ce.GetPropertyRoute()! }))}
});
}
}

body = token.BuildExpression(subCtx);
}

if (body != subCtx.Parameter)
query = Expression.Call(miSelect.MakeGenericMethod(query.Type.ElementType()!, body.Type), query, Expression.Lambda(body, subCtx.Parameter));

if (cta.ToArrayType == CollectionToArrayType.SeparatedByCommaDistict ||
cta.ToArrayType == CollectionToArrayType.SeparatedByNewLineDistict)
query = Expression.Call(miToDistict.MakeGenericMethod(token.Type), query);

query = Expression.Call(miTake.MakeGenericMethod(token.Type), query, Expression.Constant(MaxToArrayValues));

return Expression.Call(miToArray.MakeGenericMethod(token.Type), query);
}
}

[DescriptionOptions(DescriptionOptions.Members)]
public enum CollectionToArrayType
{
SeparatedByComma,
SeparatedByCommaDistict,
SeparatedByNewLine,
SeparatedByNewLineDistict,
}
6 changes: 3 additions & 3 deletions Signum.Entities/DynamicQuery/Tokens/DateToken.cs
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ public override string? Unit

public override Type Type
{
get { return typeof(DateTime?); }
get { return typeof(DateOnly?); }
}

public override string Key
Expand All @@ -47,13 +47,13 @@ protected override List<QueryToken> SubTokensOverride(SubTokensOptions options)
return new List<QueryToken>();
}

static PropertyInfo miDate = ReflectionTools.GetPropertyInfo((DateTime d) => d.Date);
static MethodInfo miDate = ReflectionTools.GetMethodInfo((DateTime d) => d.ToDateOnly());

protected override Expression BuildExpressionInternal(BuildExpressionContext context)
{
var exp = parent.BuildExpression(context);

return Expression.Property(exp.UnNullify(), miDate).Nullify();
return Expression.Call(miDate, exp.UnNullify()).Nullify();
}

public override PropertyRoute? GetPropertyRoute()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ public class MListElementPropertyToken : QueryToken
internal MListElementPropertyToken(QueryToken parent, PropertyInfo pi, PropertyRoute pr, string key, Func<string> nicePropertyName)
{
this.parent = parent ?? throw new ArgumentNullException(nameof(parent));
if (parent is not (CollectionAnyAllToken or CollectionElementToken))
if (parent is not (CollectionAnyAllToken or CollectionElementToken or CollectionToArrayToken))
throw new InvalidOperationException("Unexpected parent");

this.PropertyInfo = pi ?? throw new ArgumentNullException(nameof(pi));
Expand Down
30 changes: 25 additions & 5 deletions Signum.Entities/DynamicQuery/Tokens/QueryToken.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,11 @@
using Signum.Entities.Reflection;
using System.ComponentModel;
using System.Collections.Concurrent;
using System.Diagnostics;

namespace Signum.Entities.DynamicQuery;

[DebuggerDisplay("{FullKey(),nq}")]
public abstract class QueryToken : IEquatable<QueryToken>
{
public int Priority = 0;
Expand Down Expand Up @@ -74,11 +76,18 @@ public Func<object, T> GetAccessor<T>(BuildExpressionContext context)
return Expression.Lambda<Func<object, T>>(this.BuildExpression(context), context.Parameter).Compile();
}

public Expression BuildExpression(BuildExpressionContext context)
public Expression BuildExpression(BuildExpressionContext context, bool searchToArray = false)
{
if (context.Replacements != null && context.Replacements.TryGetValue(this, out var result))
if(context.Replacements.TryGetValue(this, out var result))
return result.GetExpression();

if (searchToArray)
{
var cta = this.HasToArray();
if (cta != null)
return CollectionToArrayToken.BuildToArrayExpression(this, cta, context);
}

return BuildExpressionInternal(context);
}

Expand Down Expand Up @@ -352,19 +361,30 @@ public static List<QueryToken> StepTokens(QueryToken parent, int decimals)
public static List<QueryToken> CollectionProperties(QueryToken parent, SubTokensOptions options)
{
if (parent.HasAllOrAny())
options &= ~SubTokensOptions.CanElement;
options &= ~(SubTokensOptions.CanElement | SubTokensOptions.CanToArray | SubTokensOptions.CanOperation | SubTokensOptions.CanAggregate);

if (parent.HasToArray() != null)
options &= ~(SubTokensOptions.CanAnyAll | SubTokensOptions.CanToArray | SubTokensOptions.CanOperation | SubTokensOptions.CanAggregate);

List<QueryToken> tokens = new List<QueryToken>() { new CountToken(parent) };

if ((options & SubTokensOptions.CanElement) == SubTokensOptions.CanElement)
if (options.HasFlag(SubTokensOptions.CanElement))
tokens.AddRange(EnumExtensions.GetValues<CollectionElementType>().Select(cet => new CollectionElementToken(parent, cet)));

if ((options & SubTokensOptions.CanAnyAll) == SubTokensOptions.CanAnyAll)
if (options.HasFlag(SubTokensOptions.CanAnyAll))
tokens.AddRange(EnumExtensions.GetValues<CollectionAnyAllType>().Select(caat => new CollectionAnyAllToken(parent, caat)));

if (options.HasFlag(SubTokensOptions.CanToArray))
tokens.AddRange(EnumExtensions.GetValues<CollectionToArrayType>().Select(ctat => new CollectionToArrayToken(parent, ctat)));

return tokens;
}

public virtual CollectionToArrayToken? HasToArray()
{
return Parent?.HasToArray();
}

public virtual bool HasAllOrAny()
{
return Parent != null && Parent.HasAllOrAny();
Expand Down
Loading

2 comments on commit 94401cd

@olmobrutall
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Presenting SeparatedByNewLine / SeparatedByComma

This commit finishes a very welcome feature to the SearchControl: The ability to add lists of values separated by comma or by new line directly in a result cell for any collection.

The way it works is that any collection has now four new sub-tokens available.

image

  • Separated by new line: Shows each elements verically aligned
  • Separated by comma: Shows the elements in the same line separated by comma (with wrapping if needed).

The elements can be anything, strings, lites, dates, values... the propper formatter will be used.

What about Distict?

The feature was originally intended for small lists, (like telephone numbers of a company) but thanks to the distict and the ability to continue the expression is also usefull for relationships with more entities potentially associated (operation logs, or products in an order).

For example:

image

With this sequence of query tokens que can see what distict categories of producst are associated with each order.

What about Top?

There is an implicit limitation to show up to 100 elements on each cell. This calue can be globally configured if needed.

Enjoy!

@MehdyKarimpour
Copy link
Contributor

@MehdyKarimpour MehdyKarimpour commented on 94401cd Aug 14, 2022 via email

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please sign in to comment.