sqlkata / querybuilder

SQL query builder, written in c#, helps you build complex queries easily, supports SqlServer, MySql, PostgreSql, Oracle, Sqlite and Firebird
https://sqlkata.com
MIT License
3.06k stars 499 forks source link

Multiple bindings with the same value #666

Open fairking opened 1 year ago

fairking commented 1 year ago

I am trying to get some query working which includes binding value multiple times: eg.

.OrderByRaw("CASE WHEN StartDate < ? and (EndDate is null or EndDate > ?) THEN 0 WHEN StartDate > ? THEN 1 ELSE 2 END",
                        bindings: new object[] { DateTime.Today, DateTime.Today, DateTime.Today })

Is there any way to modify query in a way, so the DateTime.Today declared only ones.

Doc says nothing about that.

ahmad-moussawi commented 1 year ago

There is a hidden feature that allows you to define a variable and use it multiple times.

using static SqlKata.Expressions;

var query = new Query("Users")
.Define("Today", DateTime.Today)
.Where("CreatedAt", Variable("Today"))
.Where("PublishedAt", ">", Variable("Today"));

Check the example here%0A.Where(%22PublishedAt%22%2C%20%22%3E%22%2C%20Variable(%22Today%22))%3B).

This is not publicly documented since it may be changed or removed later, so consider this if you decide to use it.

fairking commented 1 year ago

Thanks for the answer. Would it be correct?

var query = new Query("Users")
.Define("Today", DateTime.Today)
.OrderByRaw("CASE WHEN StartDate < ? and (EndDate is null or EndDate > ?) THEN 0 WHEN StartDate > ? THEN 1 ELSE 2 END",
    bindings: new object[] { Variable("Today"), Variable("Today"), Variable("Today") })

It doesn't make sense to me.

Would be nice to have named parameters, so I can use them in raw queries like this:

var query = new Query("Users")
.Define("Today", DateTime.Today)
.WhereRaw("DATEPART('weekday', @Today) = 3")
.OrderByRaw("CASE WHEN StartDate < @Today and (EndDate is null or EndDate > @Today) THEN 0 WHEN StartDate > @Today THEN 1 ELSE 2 END")