querybuilder icon indicating copy to clipboard operation
querybuilder copied to clipboard

Fix places where the `parameterPlaceholder` is hardcoded to a `?`

Open toburger opened this issue 1 year ago • 2 comments

There are places where the parameterPlaceholder variable isn't used and the ? (question mark) is hardcoded in code.

This prevents the redefinition of the parameterPlaceholder to another value.

Practical usage:

It is not possible to define JSON (Postgres) specific queries because some of the keywords use the question mark.

Example:

query.WhereRaw(@$"data->>'Tagging'::jsonb @? '$.{tagkey}\[*\] ? (@.Id == ""{tag}"")'")

The ? gets interpreted as a parameter and throws an exception.

With the changes in place the parameterPlaceholder can be redefined to use another value, for example the $$ value can be used insetad by inheriting a new class from e.g. PostgresCompiler:

class MyPostgresCompiler : PostgresCompiler
{
    public MyPostgresCompiler()
    {
        parameterPlaceholder = "$$";
    }
}

Now the query works as expected after all desired occurrences of ? are replaced with $$ in your code.

toburger avatar Aug 02 '22 10:08 toburger

Workaround for issue #356

toburger avatar Aug 02 '22 10:08 toburger

Same problem here :)

I have a json column containing an object with question mark field like this:

{ "json_param": "question mark string ?" }

if I create a sqlkata WhereRaw query like :

query.WhereRaw("my_json_column @> '{"json_param" : "question mark string ?"}'");

I get the stack:

System.IndexOutOfRangeException: Index was outside the bounds of the array. at SqlKata.Helper.<>c__DisplayClass6_0.<ExpandParameters>b__0(Int32 i) at SqlKata.Helper.<>c__DisplayClass4_0.<ReplaceAll>b__0(String item, Int32 index) at System.Linq.Enumerable.SelectIterator[TSource,TResult](IEnumerable1 source, Func3 selector)+MoveNext() at System.Linq.Enumerable.Aggregate[TSource,TAccumulate](IEnumerable1 source, TAccumulate seed, Func3 func) at SqlKata.Helper.ReplaceAll(String subject, String match, Func`2 callback) at SqlKata.Compilers.Compiler.CompileRaw(Query query) at SqlKata.Compilers.Compiler.Compile(Query query)

@ahmad-moussawi , can you take a look at it ? thanks

kamisoft-fr avatar Sep 19 '22 12:09 kamisoft-fr

@toburger I see that there is still some reference of the ? still hardcoded, in the Firebird and SQLiteCompiler, https://github.com/sqlkata/querybuilder/commit/2e58816329162689746d792af76c76f35b028a90 can you push an update to address those? or alternatively give me the permission to modify this PR https://docs.github.com/en/pull-requests/collaborating-with-pull-requests/working-with-forks/allowing-changes-to-a-pull-request-branch-created-from-a-fork

ahmad-moussawi avatar Sep 28 '22 09:09 ahmad-moussawi

Sorry, don't know what happened. It has closed the PR automatically. I've made a new PR here with your changes applied: #616

toburger avatar Sep 28 '22 10:09 toburger

Hi, just created the PR #628 for the SqlResult class still containing the question mark hardcoded I know that I made a breaking change on the class, but I did't found another way for the moment...

kamisoft-fr avatar Nov 03 '22 10:11 kamisoft-fr