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.12k stars 502 forks source link

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

Closed toburger closed 2 years ago

toburger commented 2 years ago

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 commented 2 years ago

Workaround for issue #356

kamisoft-fr commented 2 years ago

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.<>cDisplayClass6_0.b0(Int32 i) at SqlKata.Helper.<>cDisplayClass4_0.b0(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

ahmad-moussawi commented 2 years ago

@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

toburger commented 2 years ago

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

kamisoft-fr commented 2 years ago

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...