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

Add Postgre function: DATE_TRUNC #825

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 11 additions & 3 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -30,17 +30,25 @@ path = "src/lib.rs"
[dependencies]
inherent = "1.0"
sea-query-derive = { version = "0.4.2", path = "sea-query-derive", default-features = false, optional = true }
serde_json = { version = "1", default-features = false, optional = true, features = ["std"] }
chrono = { version = "0.4.27", default-features = false, optional = true, features = ["clock"] }
serde_json = { version = "1", default-features = false, optional = true, features = [
"std",
] }
chrono = { version = "0.4.27", default-features = false, optional = true, features = [
"clock",
] }
postgres-types = { version = "0", default-features = false, optional = true }
pgvector = { version = "~0.4", default-features = false, optional = true }
rust_decimal = { version = "1", default-features = false, optional = true }
bigdecimal = { version = "0.4", default-features = false, optional = true }
uuid = { version = "1", default-features = false, optional = true }
time = { version = "0.3.36", default-features = false, optional = true, features = ["macros", "formatting"] }
time = { version = "0.3.36", default-features = false, optional = true, features = [
"macros",
"formatting",
] }
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 }
strum_macros = { version = "0.26" }

[dev-dependencies]
sea-query = { path = ".", features = ["tests-cfg"] }
Expand Down
1 change: 1 addition & 0 deletions src/backend/postgres/query.rs
Original file line number Diff line number Diff line change
Expand Up @@ -124,6 +124,7 @@ impl QueryBuilder for PostgresQueryBuilder {
PgFunction::GenRandomUUID => "GEN_RANDOM_UUID",
PgFunction::JsonBuildObject => "JSON_BUILD_OBJECT",
PgFunction::JsonAgg => "JSON_AGG",
PgFunction::DateTrunc => "DATE_TRUNC",
#[cfg(feature = "postgres-array")]
PgFunction::Any => "ANY",
#[cfg(feature = "postgres-array")]
Expand Down
42 changes: 41 additions & 1 deletion src/extension/postgres/func.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
//! For calling built-in Postgres SQL functions.

use crate::{expr::*, func::*};
use crate::{expr::*, func::*, PgDateTruncUnit};

/// Functions
#[derive(Debug, Clone, PartialEq)]
Expand All @@ -16,6 +16,7 @@ pub enum PgFunction {
GenRandomUUID,
JsonBuildObject,
JsonAgg,
DateTrunc,
#[cfg(feature = "postgres-array")]
Any,
#[cfg(feature = "postgres-array")]
Expand Down Expand Up @@ -384,6 +385,45 @@ impl PgFunc {
FunctionCall::new(Function::PgFunction(PgFunction::JsonBuildObject)).args(args)
}

/// Call the `DATE_TRUNC` function. Postgres only.
///
/// # Examples
///
/// ```
/// use sea_query::{tests_cfg::*, *};
///
/// let query = Query::select()
/// .expr(PgFunc::date_trunc(
/// PgDateTruncUnit::Day,
/// Expr::val("2020-01-01"),
/// ))
/// .to_owned();
///
/// assert_eq!(
/// query.to_string(PostgresQueryBuilder),
/// r#"SELECT DATE_TRUNC('day', '2020-01-01')"#
/// );
///
/// let query = Query::select()
/// .expr(PgFunc::date_trunc(
/// PgDateTruncUnit::Microseconds,
/// Expr::val("2020-01-01"),
/// ))
/// .to_owned();
///
/// assert_eq!(
/// query.to_string(PostgresQueryBuilder),
/// r#"SELECT DATE_TRUNC('microseconds', '2020-01-01')"#
/// );
/// ```
pub fn date_trunc<T>(unit: PgDateTruncUnit, expr: T) -> FunctionCall
where
T: Into<SimpleExpr>,
{
FunctionCall::new(Function::PgFunction(PgFunction::DateTrunc))
.args([Expr::val(unit.to_string()).into(), expr.into()])
}

/// Call the `JSON_AGG` function. Postgres only.
///
/// # Examples
Expand Down
21 changes: 21 additions & 0 deletions src/table/column.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
use strum_macros::Display;

use crate::{expr::*, types::*};

/// Specification of a table column
Expand Down Expand Up @@ -200,6 +202,25 @@ pub enum PgInterval {
MinuteToSecond,
}

// All possible inputs to DATE_TRUNC (https://www.postgresql.org/docs/current/functions-datetime.html#FUNCTIONS-DATETIME-TRUNC)
#[derive(Debug, Clone, Eq, PartialEq, Display)]
#[strum(serialize_all = "lowercase")]
pub enum PgDateTruncUnit {
Microseconds,
Milliseconds,
Second,
Minute,
Hour,
Day,
Week,
Month,
Quarter,
Year,
Decade,
Century,
Millennium,
}

impl ColumnDef {
/// Construct a table column
pub fn new<T>(name: T) -> Self
Expand Down