Replies: 9 comments
-
Hi, can you post your current snippet of sea-query, and what you imagine the API could be? |
Beta Was this translation helpful? Give feedback.
-
I haven't actually used sea-query, I'm just looking at options for this case. Currently Diesel supports this because it's an ORM. How I imagine the API may look, would be perhaps something like: let table_update = TableUpdate {
column_one: None, // Don't update column_one
column_two: Some(None), // Update column_two to be null
column_three: Some(Some(10)), // Update column_three to be 10
};
let (prepared_query, values): (String, Vec<&(dyn ToSql + Sync)>) = Query::update()
.table(MyTable::Table)
.values(&table_update)
.where(Expr::col(MyTable::Id).eq(1)
.build_prepared(PostgresQueryBuilder);
assert_eq!(
prepared_query,
"UPDATE my_table SET column_two = NULL, column_three = $1 WHERE id = $2"
);
assert_eq!(
values,
vec![Some(10), 1]
); Where This would be optimal for my situation :) |
Beta Was this translation helpful? Give feedback.
-
Thank you for the snippet. I can see your use case now. This is exactly one of the use cases we are tackling in SeaORM. To make this happen in sea_query, we'd need:
I think we'd need a procedural macro (in a separate crate anyway). Are you familiar with proc marco and interested in contributing? |
Beta Was this translation helpful? Give feedback.
-
I'd truly love to contribute to this, but unfortunately I haven't written any macros in Rust before. I'd love to start learning it soon though. |
Beta Was this translation helpful? Give feedback.
-
Actually we have a very similar piece of macro in SeaORM. I can let you take a peek on that. |
Beta Was this translation helpful? Give feedback.
-
Yeah perhaps. If you're worried about it becoming bloated, you could consider using some feature flags. I do have a use case for this in my project with an app using tokio-postgres. |
Beta Was this translation helpful? Give feedback.
-
Invited you as our early access VIP ;) |
Beta Was this translation helpful? Give feedback.
-
Now that SeaORM is out, what is your opinion on this matter? @acidic9 |
Beta Was this translation helpful? Give feedback.
-
@tyt2y3 I'll have a deeper play with it and see how seaorm solves this probelm. Will get back to you soon! |
Beta Was this translation helpful? Give feedback.
-
When creating a prepared statement in something like sqlx or tokio-postgres, I'm having trouble creating a prepared update statement which supports updating fields to be
null
).This would optimally be done with an
Option<Option<T>>
type (in my case I'd actually use it withMaybeUndefined
from the async-graphql crate.Where:
None
would not update the field at allSome(None)
would update the field to be nullSome(Some(value))
would update the field with the valueThe main issue is that the statement would include fields only where applicable.
Eg:
Or if a is
None
, b isSome(None)
and c isSome(Some(value))
:I looked through the sea-query docs but could not see anything which would provide this kind of functionality.
Is this supported? Or planned to be supported?
Beta Was this translation helpful? Give feedback.
All reactions