ClickHouse / clickhouse-cpp

C++ client library for ClickHouse
Apache License 2.0
301 stars 157 forks source link

Parameters in queries #153

Open AndreyAlifanov opened 2 years ago

AndreyAlifanov commented 2 years ago

Does this client support parameterized queries? I see that command line client can do this, but it uses it`s own implementation.

Enmk commented 2 years ago

Hi Andey! Thank you for asking, right now clickhouse-cpp doesn't support parametrized queries. You are wellcome to submit any of (or all) a PR/API/implementation draft.

huotianyu commented 2 years ago

Hi Andey! Thank you for asking, right now clickhouse-cpp doesn't support parametrized queries. You are wellcome to submit any of (or all) a PR/API/implementation draft.

when support parameterized queries? when I excuted client.Select, I can not deal data.

Enmk commented 1 year ago

Hi @huotianyu and sorry for a late reply. There are no solid date on this, seems like there is not enough interest from community.

To get things started, could you propose an API for such a feature?

tsarchghs commented 1 year ago

Hi @Enmk, @AndreyAlifanov.

I was thinking maybe we could introduce parameters in queries in format:

AndreyAlifanov commented 1 year ago

Hi guys!

I think about it in terms of usual syntax for all DBMS, I know. Something like this: select * from my_table where id = ? and key > ?. Here ? are parameters, that one can substitute with real values while preparing query.

Proposal of @gjergjk71 is good too, but I would prefer standard SQL-syntax.

tsarchghs commented 1 year ago

@AndreyAlifanov maybe we can introduce parameterized queries with an implementation that makes use of std::variant?

std::variant<int, std::string> params[10] = { 2, "one" };
client.Execute("SELECT id, name FROM default.numbers WHERE id = ? AND name = ?", params);

Maybe @Enmk has another idea? Otherwise if this is a good addition, I could look more into it.

AndreyAlifanov commented 1 year ago

@gjergjk71 it's good idea, I think.

Enmk commented 1 year ago

I strongly believe that we need to utilize CH built-in mechanism for query parameters, but there are few prerequisite steps for that: https://github.com/ClickHouse/clickhouse-cpp/pull/260#issuecomment-1517792737

I see utility in bringing ? - based query parameters, but it is just a syntax sugar and might not benefit from any fancy features implemented on server side (like query caching). And it brings some problems with it: client would have to parse SQL query on client side to understand where to replace '?' with a value and where - not (strings, setting values, anything else)

OlegGalizin commented 3 weeks ago

Pull request for parameters in query. https://github.com/ClickHouse/clickhouse-cpp/pull/394