-
-
Notifications
You must be signed in to change notification settings - Fork 192
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
issues-347 Add SqlWriter trait #436
issues-347 Add SqlWriter trait #436
Conversation
src/prepare.rs
Outdated
pub trait SqlWriter: Write { | ||
fn push_param(&mut self, value: Value, query_builder: &dyn QueryBuilder); | ||
fn result(self) -> String; | ||
} |
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.
Hey @ikrivosheev, I think we don't need the result()
method. The SqlWriter
trait is only responsible for writing SQL and push parameters.
The to_string
alike method can be place in struct SqlStringWriter
and SqlWriterObj
.
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.
@billy1624 hello!
trait SqlWriter: Writer + ToString
- remove
SqlStringWriter
, useString
andimpl SqlWriter for String
- Rename
SqlWriterObj
toSqlWriterValues
A Small feature grew into big refactoring! |
2a84a16
to
fbd5bb5
Compare
@billy1624 @tyt2y3 can you review this PR? |
impl ToString for SqlWriterValues { | ||
fn to_string(&self) -> String { | ||
self.string.clone() | ||
} | ||
} |
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.
Is this necessary? I think it'd be better if to_string returns the version with parameters injected rather than just a clone of the SQL with placeholder
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.
My thoughts were: injection parameters are expensive. If someone needs an inject parameter into sql query it is better to call it explicit.
Do I need to fix that?
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.
Umm I think we have two options 1) remove this impl 2) make it also inject parameters. I am okay with either.
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.
@tyt2y3 I have problems:
- I cannot remove this impl, because I need it, for example: https://github.com/SeaQL/sea-query/blob/master/src/query/traits.rs#L21
- I cannot make
inject_parameters
because I need:query_builder
but I didnot store it in SqlWriter...
@@ -64,14 +64,14 @@ impl IndexDropStatement { | |||
|
|||
impl SchemaStatementBuilder for IndexDropStatement { | |||
fn build<T: SchemaBuilder>(&self, schema_builder: T) -> String { | |||
let mut sql = SqlWriter::new(); | |||
let mut sql = String::with_capacity(256); |
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 feel like we want to extract this as a common function, or at least extract the default string size as a constant
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.
Thank you for the big effort. Can you also draft the Changelog for a summary of these changes?
PR Info
Adds
SqlWriter
impl SqlWriter for String
- write sql to String with inlineValue
impl SqlWriter for SqlWriterValues
- write sql to String with storeValue
intoVec
Breaking Changes
struct SqlWriter
collector
from QueryBuilder and other backend traitsSqlWriter
parameter get&mut dyn SqlWriter
Context: #428 (comment)