InternetException

HLinq 0.15.0 released - query validation, math beyond select and joining strings

· 619 words · ~3 min read · in .NET, ASP.NET, Hamster Wheel

#HLinq #LINQ #NuGet #API design

HLinq is a library that turns a query string into a LINQ expression and runs it against your database. Version 0.15.0 is out on nuget.org . It adds query validation, math operations in select, where, orderBy and thenBy. It is possible to join string values. Numbers of different types can be combined the same way as in C#, and more. Here is what is new.

Validating a query without running it

Until now the only way to find out a query was wrong was to run it. Now you can validate the request:

var errors = HLinqQuery<Person>.Validate(dependenciesBag, "where[x.Nope==1].select[x.Texto]");

dependenciesBag is the HLinqBinderDependenciesBag and can be obtained from DI. If the query was already bound to an HLinqQuery<T> you can just run query.Validate(). You can also validate a query against any object or collection with obj.ValidateHLinq(query).

A validation endpoint of an API is two lines:

app.MapPost("/persons/validate", (ValidateRequest request, HLinqBinderDependenciesBag dependenciesBag) =>
    Results.Ok(HLinqQuery<Person>.Validate(dependenciesBag, request.Query)));

Such a response returns every error, not just the first: where[x.Nope==1&&x.Texto==2&&x.Into>3] returns three errors at once. Each one contains errorCode (unknown-member, impossible-conversion, invalid-comparison, …) with examples of what was possible - so a client can use that for debugging. The same list is returned on the Errors property of the exception an invalid query throws inside the API, and over HTTP (in its errors property).

A caller of your API can now check a saved or hand-built query before using it, without going through “bad request”.

Math beyond select

Math was a select feature. Now filtering and ordering allow it too:

GET /data/persons?where[x.Total-x.Paid>5]
GET /data/persons?orderBy[x.Total-x.Paid]
GET /data/persons?orderBy[(x.A%2Bx.B)*2]
GET /data/persons?where[x.Name.ToUpper()%2Bx.Name==q]

The result of such a calculation is compared and ordered exactly like a plain property - in SQL it becomes WHERE (Total - Paid) > 5. If you want to be explicit about how the calculation should be done, you can use parentheses: where[x.Date<=(2022-12-31)]. A minus - outside them can be part of a written value (a GUID, a date): select[Date=2023-10-12] selects a date, select[N=(2022-12-31)] selects the number 1979.

Joining strings in select

The + in a select is also string concatenation:

GET /data/persons?select[FullName=x.Name%2B%22%20%22%2Bx.Surname]
[{ "FullName": "Ann Kowalska" }]

This is translated to a database select (SELECT name || ' ' || surname ...), so sorting and paging over it work like over any column.

Auto casting

Two numbers of different types used in a calculation - x.Decimal + x.Double, x.Int == x.Long - are cast automatically, or an exception is thrown if that is not possible. It works for an enum and a number too - cast to the smallest type that holds both of them - exactly how C# handles them.

It works in calculations and comparisons, so where[x.Decimal+x.Double>1] is resolved the same way. Some pairs still do not work - for example a signed number and a ulong have no overlapping type, so no comparison and no math is possible on those two. It also works for nullable and non-nullable versions of the same type.

A group’s rows, shaped explicitly

You can now select the set of properties a groupBy returns:

GET /data/persons?groupBy[x.Status].select[Rows=x.Rows.select(a.Name,x.Key)]

The row properties are selected by Rows=x.Rows.select(...) and can be referenced later by those property names, for example: x.Name is the person’s name. The results of aggregate methods can be used beside them - select[C=x.Count(),Rows=x.Rows.select(a.Name)].

Fixes worth mentioning

  • Duplicated property names (select[x.Name,Name=x.Surname]) throw during parsing.
  • A misspelled member is refused with an error naming it the way the query spelled it, in the language the query was written in.
  • ISO-8601 durations (P1DT2H) are supported.
  • An incorrect query throws as early as possible.

Try it

You can try it now yourself:

<PackageReference Include="HamsterWheel.HLinq.AspNet" Version="0.15.0" />

The source lives in mirror on GitHub , and the README documents every feature shown above.

Happy Querying!