querybuilder icon indicating copy to clipboard operation
querybuilder copied to clipboard

Defined variables are replaced in Sql

Open vvopenka opened this issue 6 years ago • 3 comments

I'm a bit confused by the Define function implemented based on this issue #184. If I understand this comment: https://github.com/sqlkata/querybuilder/issues/184#issuecomment-490371881 correctly than this code:

Query query = new Query()
                   .Define("@var1", 10)
                   .From("Test")
                   .Select("col1")
                   .Where("col1", ">", new Variable("@var1"));

Should generate this Sql: SELECT [col1] FROM [Test] WHERE [col1] > @var1 With named bindings as: {"@var1": 10}

Where as it generates this: SELECT [col1] FROM [Test] WHERE [col1] > @p0

We are planning to use this library to generate big report quieries and having named parameters really helps with debugging such queries on db.

Is it a bug in the Define implementation or is it by design? I have no problem fixing it and sending a pull request. I just need to find out if I should fix the current implementation or add a support for named parameters.

vvopenka avatar May 28 '20 19:05 vvopenka

@vvopenka no this is by design, at the beginning the intention was to implement the native variable declaration in each database vendor, but this was not the case since it was more complex than we expected, the benefit here is to define the variable once (at the level of the QueryFactory for example) and use it in all your queries, such the TenantId or something similar.

So in this feature the focus was more on the variable values, more than variable names, but I am open for discussion if this is something needed.

ahmad-moussawi avatar May 28 '20 19:05 ahmad-moussawi

I will close due to no response, feel free to reopen if you have any feedback.

ahmad-moussawi avatar Jun 08 '20 07:06 ahmad-moussawi

@ahmad-moussawi I am in a similar situation to @vvopenka. I am using SqlKata to strictly build sql queries and the execution of that query is handled by a different service. For that reason I am restricted to using a specific naming convention which doesn't follow p[n].

tanercura avatar May 14 '21 13:05 tanercura