Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

with_param #159

Open
wants to merge 5 commits into
base: main
Choose a base branch
from
Open

with_param #159

wants to merge 5 commits into from

Conversation

serprex
Copy link
Member

@serprex serprex commented Sep 24, 2024

Small wrapper around with_option for impl Serialize + Bind

@serprex serprex force-pushed the with-param branch 2 times, most recently from 8891fb7 to 32382c9 Compare September 24, 2024 13:46
@serprex serprex changed the title With param with_param Sep 24, 2024
@serprex serprex force-pushed the with-param branch 2 times, most recently from edbd788 to abf96dd Compare September 24, 2024 13:55
@slvrtrn
Copy link
Contributor

slvrtrn commented Sep 24, 2024

Aside from the simpler types, the following needs testing:

  • Arrays, Maps, Tuples (e.g., common container types that make sense to use in this context).
  • DateTime/DateTime64 (IIRC, it requires a specific format like 2022-05-02 13:25:55.123456789, i.e. not exactly ISO timestamp)
  • Numeric and named enums
  • \t, \r, \n, single quotes, backslashes in the bound strings
  • Strings with UTF8 symbols
  • Nulls

I'd maybe also not mix it with .with_option method; the proposal from @mshustov looks better IMO (see this comment) - we can add the param_ prefix ourselves internally.

@serprex
Copy link
Member Author

serprex commented Sep 24, 2024

So already found issue with basic tests. String Bind serializes to 'string' with quotes which is correct when interpolating into query string, but for server side parameters the string should be unquoted

Note once a string is contained it is expected to be quoted in params: ClickHouse/clickhouse-connect#159

@@ -160,6 +161,12 @@ impl Client {
self
}

pub fn with_param(self, name: &str, value: impl Serialize) -> Result<Self, String> {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What's the purpose of Client::with_param? Parameters are specific for queries, not the whole client

Copy link
Member Author

@serprex serprex Sep 29, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Generally, but if someone wants to share parameter between queries queries they can put it on the client

For example, in a multi tenant app, tenantid could be put as a parameter in the function creating client

@loyd
Copy link
Collaborator

loyd commented Sep 29, 2024

We also need tests for UUID (probably, with the human-readable mode it works already). Also, for some implementation of DecimalX (the fixnum crate for instance).

And need to add some examples.

@@ -195,6 +195,12 @@ impl Query {
self.client.add_option(name, value);
self
}

pub fn with_param(self, name: &str, value: impl Serialize) -> Result<Self, String> {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm still trying to figure out the naming here. This one is consistent with with_option, but that name emphasizes the connection with Client::with_option.

For instance, bind mimics sqlx bind.
A simple param() would be more concise. Yep, it's inconsistent for now, but once Client::builder() is introduced, we can have fn option() instead of with_option.

@@ -195,6 +195,12 @@ impl Query {
self.client.add_option(name, value);
self
}

pub fn with_param(self, name: &str, value: impl Serialize) -> Result<Self, String> {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Result<> can be very inconvenient. It can be used in a concise way only inside -> Result functions (q.with_param("a", 42)?.with_param("b", 43)?), but sometimes you want explicitly handle errors with matching inside the function that doesn't return Result, so ? (via the Try trait) cannot be used.

So, I suggest delayed errors in the same way as in the bind() method.

@@ -195,6 +195,12 @@ impl Query {
self.client.add_option(name, value);
self
}

pub fn with_param(self, name: &str, value: impl Serialize) -> Result<Self, String> {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This method must be well documented. Also, we should specify the difference between param() and bind() (probably in a dedicated section on Query and provide links to that section in docs of both methods).

src/query.rs Show resolved Hide resolved
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants