From 53181b2182ff238013fb442f491e48a6c9aaa855 Mon Sep 17 00:00:00 2001 From: Ordanis Sanchez Date: Tue, 5 Nov 2024 10:17:27 -0400 Subject: [PATCH] Impl current timestamp/date/time for expr --- Cargo.lock | 2 +- Cargo.toml | 2 +- python/sea_query/_internal.pyi | 6 ++++++ src/expr.rs | 15 +++++++++++++++ tests/test_table_columns.py | 12 ++++++++++++ 5 files changed, 35 insertions(+), 2 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index acb2722..63e7772 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -243,7 +243,7 @@ dependencies = [ [[package]] name = "sea-query" -version = "0.2.4" +version = "0.2.5" dependencies = [ "chrono", "pyo3", diff --git a/Cargo.toml b/Cargo.toml index 85aaa48..c2cf2b8 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -1,6 +1,6 @@ [package] name = "sea-query" -version = "0.2.4" +version = "0.2.5" edition = "2021" # See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html diff --git a/python/sea_query/_internal.pyi b/python/sea_query/_internal.pyi index 536b550..927bcb3 100644 --- a/python/sea_query/_internal.pyi +++ b/python/sea_query/_internal.pyi @@ -74,6 +74,12 @@ class Expr: def count_distinct(self) -> SimpleExpr: ... def if_null(self, value: ValueType) -> SimpleExpr: ... @staticmethod + def current_timestamp() -> Expr: ... + @staticmethod + def current_date() -> Expr: ... + @staticmethod + def current_time() -> Expr: ... + @staticmethod def exists(query: SelectStatement) -> SimpleExpr: ... @staticmethod def case() -> CaseStatement: ... diff --git a/src/expr.rs b/src/expr.rs index 2c71f33..acb546c 100644 --- a/src/expr.rs +++ b/src/expr.rs @@ -163,6 +163,21 @@ impl Expr { SimpleExpr(self.take().if_null(&value)) } + #[staticmethod] + fn current_timestamp() -> Expr { + Expr(Some(SeaExpr::current_timestamp())) + } + + #[staticmethod] + fn current_date() -> Expr { + Expr(Some(SeaExpr::current_date())) + } + + #[staticmethod] + fn current_time() -> Expr { + Expr(Some(SeaExpr::current_time())) + } + #[staticmethod] fn exists(query: SelectStatement) -> SimpleExpr { SimpleExpr(SeaExpr::exists(query.0)) diff --git a/tests/test_table_columns.py b/tests/test_table_columns.py index 55a378f..fd41790 100644 --- a/tests/test_table_columns.py +++ b/tests/test_table_columns.py @@ -447,6 +447,18 @@ def test_col_auto_increment(): "\"created_at\" timestamp without time zone DEFAULT 'now()'", ), (Column("data").json().default(Expr.value("{}")), "\"data\" json DEFAULT '{}'"), + ( + Column("datetime").timestamp().default(Expr.current_timestamp()), + '"datetime" timestamp DEFAULT CURRENT_TIMESTAMP', + ), + ( + Column("date").date().default(Expr.current_date()), + '"date" date DEFAULT CURRENT_DATE', + ), + ( + Column("time").time().default(Expr.current_time()), + '"time" time DEFAULT CURRENT_TIME', + ), ], ) def test_col_default(column, expected):