ClickHouse.Client icon indicating copy to clipboard operation
ClickHouse.Client copied to clipboard

Auto convert @param to {<name>:<data type>}

Open bnuzhouwei opened this issue 3 years ago • 6 comments

The @param is the defualt paramter format for ADO.NET, can auth covert @params to {:}, so that the application could use CH same as other databases.

bnuzhouwei avatar Nov 17 '22 04:11 bnuzhouwei

@param is not ADO.NET syntax, it is SQL Server syntax. Each database has their own syntax for parameterized queries, e.g.:

SQL Server: @param Oracle: :param PostgreSQL: $1 SQLite: ? or @param or $param ClickHouse (as you can see here): SELECT {param}

I don't think I should change SQL dynamically to match another database's syntax

DarkWanderer avatar Nov 17 '22 08:11 DarkWanderer

In @param works well in NPGSQL, System.Data.SQLite, Mysql.data.client, system.data.sqlclient, and etc. but except oracle.

implemeted in the driver, it will be easier to migration databases.

bnuzhouwei avatar Nov 17 '22 08:11 bnuzhouwei

Does any of those providers rewrite the SQL for it to work? Can you point me to examples in code?

DarkWanderer avatar Nov 17 '22 08:11 DarkWanderer

In Npgsql (ADO.NET driver for PostgreSQL), it use a function to change prefix with '@' or ':'

    internal void ChangeParameterName(string? value)
    {
        if (value == null)
            _name = TrimmedName = PositionalName;
        else if (value.Length > 0 && (value[0] == ':' || value[0] == '@'))
            TrimmedName = (_name = value).Substring(1);
        else
            _name = TrimmedName = value;
    }

bnuzhouwei avatar Nov 17 '22 12:11 bnuzhouwei

This doesn't rewrite SQL though, it just changes parameter name if you try to assign :name or @name to NpgsqlParameter.ParameterName. Is there an example which rewrites SQL, as you suggest?

DarkWanderer avatar Nov 17 '22 19:11 DarkWanderer

ProcessRawQuery in NpgsqlCommand with EnableSqlRewriting rewite the sql from '@' and ‘:’ to '$' with Query analysis

ParseRawQuery in SqlQueryParser includes doing rewriting named parameter placeholders to positional (@p => $1), and splitting the query up by semicolons.

bnuzhouwei avatar Nov 18 '22 00:11 bnuzhouwei