querybuilder icon indicating copy to clipboard operation
querybuilder copied to clipboard

Allow quoting identifiers

Open yankyhoffman opened this issue 1 year ago • 6 comments

Currently, SqlKata allows consumers to quote identifiers only within ?Raw methods.

Due to that when working with identifiers which need to be quoted (whether being keywords or due to casing) separate logic needs to be implemented when calling a non-Raw vs a Raw method.

This PR allows any identifier to be quoted within the non-Raw methods as well.

yankyhoffman avatar Jul 18 '22 17:07 yankyhoffman

If you wrap your quoted identifiers in your raw expressions with { and } , SqlKata should automatically replace those with the provider specific identifiers.

JordanMarr avatar Jul 18 '22 18:07 JordanMarr

If you wrap your quoted identifiers in your raw expressions with { and } , SqlKata should automatically replace those with the provider specific identifiers.

The documentation state that square brackets ([]) be used within Raw expressions.

The current PR adds the ability to quote in non-Raw expressions as well.

yankyhoffman avatar Jul 18 '22 19:07 yankyhoffman

Interesting. I am using { and } and it is still replaced (not sure where I read to use curly braces), but I tried with [ and ] and that also works.

JordanMarr avatar Jul 18 '22 19:07 JordanMarr

I found where I read that you can use { } or [ ]: https://github.com/sqlkata/querybuilder/issues/407#issuecomment-682377675

By default, SqlKata uses the {} and [] to identify tables and columns identifiers in the raw expressions.

So you should probably handle both in your PR.

JordanMarr avatar Jul 18 '22 21:07 JordanMarr

It's not clear for me why quoting in non raw methods is needed?

ahmad-moussawi avatar Sep 11 '22 07:09 ahmad-moussawi

Sorry for my belated response...

Addressing your question Ahmed, indeed it is not needed to quote identifiers in the non-raw methods, but why not support them?

To give some more context and an actual use-case, I wrote a small abstraction over your beautiful library which translates classes and constructs a SqlKata query with Selects/Wheres etc, due to the nature of possible filters (or transformations (UPPER, DATE_PART etc) possible I needed to not only branch and use the raw vs non-raw version of the methods, I also had to branch and conditionally quote identifiers based on whether the end result was passed to a raw vs non-raw method.

This PR aims to simplify and allow consumers of the library to pass pre-quoted identifiers and not worry about getting triple quoted identifiers out as does it simplify the need of the consumer to manually quote non-raw identifiers with the concrete quotation character (i.e. the consumer passes the values to the compiler and doesn't have to worry whether to quote non-raw identifiers with double quotes (") or brackets ([]) or backticks (`).

yankyhoffman avatar Sep 19 '22 16:09 yankyhoffman