jchristn / DatabaseWrapper

Simple database wrapper for Microsoft SQL Server, MySQL, PostgreSQL, and Sqlite written in C# supporting dynamic query building and nesting using expressions.
MIT License
97 stars 26 forks source link

Support for between operator #2

Closed twobytescy closed 4 years ago

twobytescy commented 4 years ago

Utilizing GreaterThanOrEqualTo and LessThanOrEqualTo operators

jchristn commented 4 years ago

Thank you so much @twobytescy I'm pushing this to NuGet as we speak as version 3.0.1. Cheers, Joel

jchristn commented 4 years ago

Hi @twobytescy in doing some testing I'm running into cases where the correct Expression constructor isn't being used. I'm going to merge the explicit 'Between' constructor into the normal constructor and re-work this a bit. Stay tuned.

jchristn commented 4 years ago

This seems to work:

            List<string> returnFields = new List<string> { "firstName", "lastName", "age" };
            List<object> vals = new List<object>();
            vals.Add(10);
            vals.Add(20);
            Expression e = new Expression("id", Operators.Between, vals);
            Console.WriteLine("Expression: " + e.ToString());
            _Database.Select("person", null, null, returnFields, e, "ORDER BY age DESC");

But this gets pulled into the regular public Expression(object left, Operators oper, object right) constructor instead of the desired public Expression(object left, Operators oper, List<object> right) constructor:

            List<string> returnFields = new List<string> { "firstName", "lastName", "age" }; 
            Expression e = new Expression("id", Operators.Between, new List<int> { 10, 20 });
            Console.WriteLine("Expression: " + e.ToString());
            _Database.Select("person", null, null, returnFields, e, "ORDER BY age DESC");
jchristn commented 4 years ago

So we may be stuck with the caller explicitly using List<object> even though they may want to use int, decimal, string, etc...

https://stackoverflow.com/questions/62184219/multiple-constructors-using-generics-and-lists

Will watch this and see if there is a better solution, but for now, I think the one you've created is the most appropriate. Caveat being the caller has to explicitly use List<object>, but it works great when that's what they use.

Cheers, Joel

jchristn commented 4 years ago

Hi @twobytescy given the confusion a consumer of the library may encounter when trying to do Between, would you be opposed to me creating a static factory for this?

i.e.

public static Expression Between(object left, List<object> right)
{
  ...
}

This would keep people from tripping over the issue of their Expressions not being built correctly because they use strongly-typed List<> instead of List for the right term.

Let me know what you think. Thanks! ~Joel

twobytescy commented 4 years ago

Hello.

Well this was my initial implementation and I had it changed to be consistent with your other constructors.

I totally agree with you

S

On Thu, 4 Jun 2020 at 6:55 AM, Joel Christner notifications@github.com wrote:

Hi @twobytescy https://github.com/twobytescy given the confusion a consumer of the library may encounter when trying to do Between, would you be opposed to me creating a static factory for this?

i.e.

public static Expression Between(object left, List right) { ... }

This would keep people from tripping over the issue of their Expressions not being built correctly because they use strongly-typed List<> instead of List for the right term.

Let me know what you think. Thanks! ~Joel

— You are receiving this because you were mentioned. Reply to this email directly, view it on GitHub https://github.com/jchristn/DatabaseWrapper/pull/2#issuecomment-638587271, or unsubscribe https://github.com/notifications/unsubscribe-auth/AL6JJMX6JSBL7TPUNGD622LRU4LMRANCNFSM4NPRPJDA .

jchristn commented 4 years ago

My apologies @twobytescy i will get this changed and fixed on my end. Thanks!