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

added PostgreSQL Interval value variant #709

Open
wants to merge 4 commits into
base: master
Choose a base branch
from

Conversation

yasamoka
Copy link

@yasamoka yasamoka commented Oct 2, 2023

First of all, thank you for this feature-filled query builder.

This PR adds the Value::Interval enum variant containing a PgInterval provided by SQLx. It requires this change in SQLx and is required for these changes in SeaORM.

If this PR is considered relevant to the project, then I need some help with how to improve it in some aspects. I will mention the various points in further review requests.

Cargo.toml Outdated
@@ -44,6 +44,7 @@ time = { version = "0.3", default-features = false, optional = true, features =
ipnetwork = { version = "0.20", default-features = false, optional = true }
mac_address = { version = "1.1", default-features = false, optional = true }
ordered-float = { version = "3.4", default-features = false, optional = true }
sqlx = { git = "https://github.com/yasamoka/sqlx", branch = "pg-interval-hash", default-features = false, optional = true }
Copy link
Author

Choose a reason for hiding this comment

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

There was previously no dependency on SQLx. However, PgInterval is provided by SQLx. Is this fine?

Cargo.toml Outdated
@@ -60,7 +61,7 @@ derive = ["sea-query-derive"]
attr = ["sea-query-attr"]
hashable-value = ["derivative", "ordered-float"]
postgres-array = []
postgres-interval = ["proc-macro2", "quote"]
postgres-interval = ["proc-macro2", "quote", "sqlx/postgres"]
Copy link
Author

Choose a reason for hiding this comment

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

Is this the preferred way of adding this feature dependency?

@@ -42,6 +42,7 @@ with-time = ["sqlx?/time", "sea-query/with-time", "time"]
with-ipnetwork = ["sqlx?/ipnetwork", "sea-query/with-ipnetwork", "ipnetwork"]
with-mac_address = ["sqlx?/mac_address", "sea-query/with-mac_address", "mac_address"]
postgres-array = ["sea-query/postgres-array"]
postgres-interval = ["sqlx-postgres", "sqlx/chrono"]
Copy link
Author

Choose a reason for hiding this comment

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

Is this the preferred way of adding this feature dependency?

@@ -73,6 +73,8 @@ impl sqlx::IntoArguments<'_, sqlx::mysql::MySql> for SqlxValues {
Value::ChronoDateTimeWithTimeZone(t) => {
args.add(Value::ChronoDateTimeWithTimeZone(t).chrono_as_naive_utc_in_string());
}
#[cfg(feature = "postgres-interval")]
Value::Interval(_) => {}
Copy link
Author

Choose a reason for hiding this comment

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

Is there a better of doing this other than splitting the query builder or throwing an error?

@@ -73,6 +73,8 @@ impl<'q> sqlx::IntoArguments<'q, sqlx::sqlite::Sqlite> for SqlxValues {
Value::ChronoDateTimeWithTimeZone(t) => {
args.add(t.map(|t| *t));
}
#[cfg(feature = "postgres-interval")]
Value::Interval(_) => {}
Copy link
Author

Choose a reason for hiding this comment

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

Is there a better of doing this other than splitting the query builder or throwing an error?

}

write!(s, "'::interval").unwrap();
}
Copy link
Author

Choose a reason for hiding this comment

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

Shall I make the units abbreviated or is this better for clarity when inspecting the built SQL statement?

src/value.rs Outdated
}

fn column_type() -> ColumnType {
ColumnType::Interval(None, None)
Copy link
Author

Choose a reason for hiding this comment

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

What do I pass to ColumnType::Interval, if anything?

src/value.rs Outdated
@@ -1423,6 +1477,8 @@ pub fn sea_value_to_json_value(value: &Value) -> Json {
Value::ChronoDateTimeUtc(_) => CommonSqlQueryBuilder.value_to_string(value).into(),
#[cfg(feature = "with-chrono")]
Value::ChronoDateTimeLocal(_) => CommonSqlQueryBuilder.value_to_string(value).into(),
#[cfg(feature = "postgres-interval")]
Value::Interval(_) => CommonSqlQueryBuilder.value_to_string(value).into(),
Copy link
Author

Choose a reason for hiding this comment

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

Is this correct?

@tyt2y3
Copy link
Member

tyt2y3 commented Oct 2, 2023

Thank you. I will put this on my pipeline.

Copy link
Member

@tyt2y3 tyt2y3 left a comment

Choose a reason for hiding this comment

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

Hi, thank you for your contribution. However, I don't think we should pull in sqlx as a dependency in the root crate.
What we can do is:

  1. define our own PgInterval inside SeaQuery
  2. add a variant to Value (which is now done)
  3. support it in sea-query-binder

that way, in the future we can also support other driver libraries

@tyt2y3
Copy link
Member

tyt2y3 commented Oct 4, 2023

Also, we already have the https://docs.rs/sea-query/latest/sea_query/table/enum.PgInterval.html column type.
Edit: I mis-clicked the close button.

@tyt2y3 tyt2y3 closed this Oct 4, 2023
@tyt2y3 tyt2y3 reopened this Oct 4, 2023
@yasamoka
Copy link
Author

yasamoka commented Oct 10, 2023

Hi, thank you for your contribution. However, I don't think we should pull in sqlx as a dependency in the root crate. What we can do is:

1. define our own `PgInterval` inside SeaQuery

2. add a variant to `Value` (which is now done)

3. support it in `sea-query-binder`

that way, in the future we can also support other driver libraries

Done :)

I named it PgIntervalValue so that it does not conflict with PgInterval field type. Is that alright? And is it placed in the proper location?

for (interval, formatted) in VALUES {
let query = Query::select().expr(interval).to_owned();
assert_eq!(
query.to_string(PostgresQueryBuilder),
Copy link
Author

Choose a reason for hiding this comment

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

What shall we do about MysqlQueryBuolder and SqliteQueryBuilder?

@@ -105,6 +107,10 @@ impl sqlx::IntoArguments<'_, sqlx::postgres::Postgres> for SqlxValues {
Value::TimeDateTimeWithTimeZone(t) => {
args.add(t.as_deref());
}
#[cfg(feature = "postgres-interval")]
Value::Interval(t) => {
args.add(t.as_deref());
Copy link
Author

@yasamoka yasamoka Oct 10, 2023

Choose a reason for hiding this comment

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

How would I implement sqlx::Encode<'_, Postgres> for PgIntervalValue without adding sqlx as a dependency for sea-query?

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.

2 participants