-
Notifications
You must be signed in to change notification settings - Fork 143
Add support for raw queries without going through query builder (#234) #243
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
base: main
Are you sure you want to change the base?
Changes from all commits
39e36b9
b12ad8f
6bfcf50
2a3e0f8
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -23,13 +23,23 @@ use crate::headers::with_authentication; | |
| pub struct Query { | ||
| client: Client, | ||
| sql: SqlBuilder, | ||
| raw: bool, | ||
| } | ||
|
|
||
| impl Query { | ||
| pub(crate) fn new(client: &Client, template: &str) -> Self { | ||
| Self { | ||
| client: client.clone(), | ||
| sql: SqlBuilder::new(template), | ||
| raw: false, | ||
| } | ||
| } | ||
| /// Creates a new query that will be used as-is without any processing. | ||
| pub(crate) fn raw(client: &Client, query: &str) -> Self { | ||
| Self { | ||
| client: client.clone(), | ||
| sql: SqlBuilder::raw(query), | ||
| raw: true, | ||
| } | ||
| } | ||
|
|
||
|
|
@@ -53,6 +63,9 @@ impl Query { | |
| /// [`Identifier`]: crate::sql::Identifier | ||
| #[track_caller] | ||
| pub fn bind(mut self, value: impl Bind) -> Self { | ||
| if self.raw { | ||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. In general, I'm fine with this implementation. However, it's slightly strange to check in runtime properties that can easily be checked at compile time: and, obviously, a trait (sealed) to turn anyway up to @slvrtrn
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Oh, it's probably duplicate of #243 (comment)
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. We could also try to explore an option of using QueryRaw as a building block for Query, as @serprex suggested. But perhaps current impl is good enough to start with, so let's not change it. |
||
| panic!("bind() cannot be used with raw queries"); | ||
| } | ||
| self.sql.bind_arg(value); | ||
| self | ||
| } | ||
|
|
@@ -84,7 +97,10 @@ impl Query { | |
| /// # Ok(()) } | ||
| /// ``` | ||
| pub fn fetch<T: Row>(mut self) -> Result<RowCursor<T>> { | ||
| self.sql.bind_fields::<T>(); | ||
| // skip binding if raw query | ||
| if !self.raw { | ||
| self.sql.bind_fields::<T>(); | ||
| } | ||
|
|
||
| let validation = self.client.get_validation(); | ||
| if validation { | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -177,6 +177,7 @@ mod ip; | |
| mod mock; | ||
| mod nested; | ||
| mod query; | ||
| mod query_raw; | ||
| mod rbwnat; | ||
| mod time; | ||
| mod user_agent; | ||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think we should provide more documentation on this difference to help newcomers choose the right constructor