Skip to content

added PostgreSQL Interval value variant#709

Open
yasamoka wants to merge 4 commits into
SeaQL:masterfrom
yasamoka:pg-interval-value
Open

added PostgreSQL Interval value variant#709
yasamoka wants to merge 4 commits into
SeaQL:masterfrom
yasamoka:pg-interval-value

Conversation

@yasamoka

@yasamoka yasamoka commented Oct 2, 2023

Copy link
Copy Markdown

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.

Comment thread Cargo.toml Outdated
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
Copy Markdown
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?

Comment thread Cargo.toml Outdated
hashable-value = ["derivative", "ordered-float"]
postgres-array = []
postgres-interval = ["proc-macro2", "quote"]
postgres-interval = ["proc-macro2", "quote", "sqlx/postgres"]

Copy link
Copy Markdown
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?

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
Copy Markdown
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?

Comment thread sea-query-binder/src/sqlx_mysql.rs Outdated
args.add(Value::ChronoDateTimeWithTimeZone(t).chrono_as_naive_utc_in_string());
}
#[cfg(feature = "postgres-interval")]
Value::Interval(_) => {}

Copy link
Copy Markdown
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?

Comment thread sea-query-binder/src/sqlx_sqlite.rs Outdated
args.add(t.map(|t| *t));
}
#[cfg(feature = "postgres-interval")]
Value::Interval(_) => {}

Copy link
Copy Markdown
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
Copy Markdown
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?

Comment thread src/value.rs Outdated
}

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

Copy link
Copy Markdown
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?

Comment thread src/value.rs Outdated
#[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
Copy Markdown
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

tyt2y3 commented Oct 2, 2023

Copy link
Copy Markdown
Member

Thank you. I will put this on my pipeline.

@tyt2y3 tyt2y3 left a comment

Copy link
Copy Markdown
Member

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

tyt2y3 commented Oct 4, 2023

Copy link
Copy Markdown
Member

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

yasamoka commented Oct 10, 2023

Copy link
Copy Markdown
Author

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?

Comment thread src/value.rs
for (interval, formatted) in VALUES {
let query = Query::select().expr(interval).to_owned();
assert_eq!(
query.to_string(PostgresQueryBuilder),

Copy link
Copy Markdown
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?

}
#[cfg(feature = "postgres-interval")]
Value::Interval(t) => {
args.add(t.as_deref());

@yasamoka yasamoka Oct 10, 2023

Copy link
Copy Markdown
Author

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