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

Debug Query as SQL string. #155

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

Conversation

pravic
Copy link

@pravic pravic commented Sep 18, 2024

Summary

Allow to print the current SQL query, whether it's bound or not. Useful for logging and diagnostics.

Note: I chose Debug over Display because the latter is too implicit and somebody might accidentally pass a query into a string.

Albeit, something like fn Query::into_string(self) -> String can be useful on its own - as a poor man's query builder:

let q = client.query("SELECT id from system.users WHERE name = ?");
let q = q.bind("default");

// store the query string for later 
let query_string = q.into_string(); // returns `q.sql.finish()` inernally

// then execute it:
client.query(&query_string).fetch();

closes #146

Checklist

Delete items not relevant to your PR:

Allow to print the current SQL query,
whether it's bound or not.

Useful for logging and diagnostics.
Allow to print the current SQL query,
whether it's bound or not.

Useful for logging and diagnostics.
serprex
serprex previously approved these changes Sep 19, 2024
src/query.rs Outdated
@@ -22,6 +23,12 @@ pub struct Query {
sql: SqlBuilder,
}

impl fmt::Debug for Query {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
write!(f, "{sql}", sql = self.sql)
Copy link
Collaborator

@loyd loyd Sep 20, 2024

Choose a reason for hiding this comment

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

Query can easily include more than client + sql in the future and Debug (despite Display) shouldn't be so restrictive.

I think, we should leave Query { client: Client, sql: "blabla" } here and don't pretend it's a simple str-like struct.

Another way is to provide fn sql_display(&self) -> &impl Display which also covers into_string() but in more much reliable way for future changes

Copy link
Author

Choose a reason for hiding this comment

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

Another way is to provide fn sql_display(&self) -> &impl Display

@loyd Do I understand it right, that we:

  • keep the impl Display for SqlBuilder in this PR
  • remove impl Debug for Query
  • instead, add fn Query::sql_display

Correct?

Copy link
Author

Choose a reason for hiding this comment

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

Also, sql_display or as_sql?

Copy link
Author

Choose a reason for hiding this comment

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

@loyd Removed Debug in f4da92b.

Copy link
Collaborator

@loyd loyd left a comment

Choose a reason for hiding this comment

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

Only misleading Debug impl, all other changes are fine

@CLAassistant
Copy link

CLAassistant commented Sep 20, 2024

CLA assistant check
All committers have signed the CLA.

`Debug` for `Query` isn't a good idea.
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.

Implement std::fmt::Display for Query
4 participants