Open
Description
Right now there is no way to dynamically order queries.
This makes the library unsuitable for applications that let the user sort a paged table of records.
That's a lot of applications!
This should be solvable without giving up type safety. Here's the design I have in mind:
Any command that consists of a single SELECT
statement which is not known to be a single-row SELECT should be dynamically orderable.
e.g.
type MyQuery = SQL<"select Column1, 1+1 as Column2 from SomeTable">
This means that it gets the following extra stuff in its generated type:
- A nested type
MyQuery.By
with a private constructor and an overrided ToString(). - Static getters
MyQuery.By.Column1
andMyQuery.By.Column2
, which return instances ofMyQuery.By
- A static method
MyQuery.By.OfString("Column1")
which checks that the passed string is one of the statically known output column names.MyQuery.By.OfString(MyQuery.By.Column2.ToString())
should work. - A static
OrderBy
method taking aMyQuery.By
and a DUAscending|Descending
and returning aMyQuery.OrderedQuery
MyQuery.OrderedQuery
has an instance methodThenBy
taking another by+direction,Page
taking limit+offsetMyQuery.OrderedQuery
has an instanceCommand
method which is like the regularMyQuery.Command(param1 = ...)
method, but includes the orderings applied so far
Hypothetical usage:
type MyQuery = SQL<"select Column1, 1+1 as Column2 from SomeTable where Name like @name">
let example (conn : ConnectionContext) =
MyQuery
.OrderBy(MyQuery.By.Column1, Ascending)
.ThenBy(MyQuery.By.Column2, Descending)
.Page(25, 50)
.Command(name = "%a%")
.Execute(conn)
I think it would be OK to limit to one ThenBy
clause. Orderings more complicated than that don't make much sense to generate dynamically.
Thoughts?