Octonica / ClickHouseClient

ClickHouse .NET Core driver

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Dapper par error

znyet opened this issue · comments

using Dapper;
using Octonica.ClickHouseClient;

using var conn = new ClickHouseConnection(".....");
conn.Open();
var sql = "insert into people values(@id,@name)";
conn.Execute(sql, new { id = 1, name = "lina" });
var data = conn.Query("select * from people");

Octonica.ClickHouseClient.Exceptions.ClickHouseServerException:“DB::Exception: Cannot parse expression of type Int32 here: @id,@name)”

Hello,
maybe your clickhouse id column is not .net int32 compatible? please try to rewrite your query like,

select toInt32(id), name from people

if you want to know actual type of id column you can use clickhouse toTypeName function, also see column types for details.

If it will not work, please submit stacktrace

using Dapper;
using Octonica.ClickHouseClient;

using var conn = new ClickHouseConnection(".....");
conn.Open();

conn.Execute("create table if not exists people(id Int32,name String,time Date)ENGINE=MergeTree(time,(id),8192)");

var sql = "insert into people values(@id,@name)";
conn.Execute(sql, new { id = 1, name = "lina" }); //error

Octonica.ClickHouseClient.Exceptions.ClickHouseServerException:“DB::Exception: Cannot parse expression of type Int32 here: @id,@name)”

conn.Execute(sql, new { id = 1, name = "lina" }); //when use dapper,error from here

your par is "INSERT INTO table_you_just_created SELECT {id}, {dt}"

but ado.net is "insert into people values(@id,@name)"

dapper not support?

can support @id but not {id}

{id} Nonstandard!

ah, now I see.
I think it was select failing, but it is insert.

AFAIK we don't support mssql style parameter prefixes.

@victor-sushko

Hello!

The default format of ClickHouse parameters is {<name>:<data type>}, as specified in Queries with Parameters section. However, the type of the parameter can be inferred from the settings of this parameter, so it's allowed to omit :<data type> part.

I added support for parameters in the format @<name> (mssql-like parameters) in queries. It's available in the version 1.1.9 of Octonica.ClickHouseClient.

Unfortunately, ClickHouse server doesn't support parameters in VALUES (...). But it's possible to rewrite the query:

insert into people select @id,@name

Here is a short example

  class Program
    {
        record Person (int Id, string Name);
        static async Task Main(string[] args)
        {
            using var conn = new ClickHouseConnection("Host=click;Port=9000;Database=default;User=default;");
            conn.Open();
            using var cmd = conn.CreateCommand("CREATE TABLE IF NOT EXISTS dappertest(id Int32, name String) engine Memory");
            await cmd.ExecuteNonQueryAsync();

            //insert data
            conn.Execute("insert into dappertest select @id, @name", new { id = 1, name = "hello dapper" });

            //select data
            var result = await conn.QueryAsync<Person>("select * from dappertest");
        }
    }

Here is a short example

  class Program
    {
        record Person (int Id, string Name);
        static async Task Main(string[] args)
        {
            using var conn = new ClickHouseConnection("Host=click;Port=9000;Database=default;User=default;");
            conn.Open();
            using var cmd = conn.CreateCommand("CREATE TABLE IF NOT EXISTS dappertest(id Int32, name String) engine Memory");
            await cmd.ExecuteNonQueryAsync();

            //insert data
            conn.Execute("insert into dappertest select @id, @name", new { id = 1, name = "hello dapper" });

            //select data
            var result = await conn.QueryAsync<Person>("select * from dappertest");
        }
    }

Is Dapper.SimpleCRUD or Dapper.Contrib support ??

Hello, @timiil!

Here is a list of dialects supported by Dapper.SimpleCRUD:
https://github.com/ericdc1/Dapper.SimpleCRUD/blob/3fcd129fa9289acd7d2ebc9a801572d8661fc1f4/Dapper.SimpleCRUD/SimpleCRUD.cs#L1002-L1013

And here is a list of dialects supported by Dapper.Contrib:
https://github.com/DapperLib/Dapper.Contrib/blob/cf24f6bdc577b1e071c3764ddfb2cf3382531405/src/Dapper.Contrib/SqlMapperExtensions.cs#L63-L72

As far as I can tell, neither Dapper.SimpleCRUD nor Dapper.Contrib provide support for ClickHouse SQL dialect.