Skip to content

Commit 971d11f

Browse files
committed
refactor: split DateTime functions into focused files matching PostgreSQL Chapter 9.9
- Created DateTime+Extract.swift for EXTRACT function and DateField types - Created DateTime+Truncate.swift for DATE_TRUNC function - Created DateTime+Current.swift for CURRENT_TIMESTAMP and CURRENT_DATE - Removed PostgreSQLDateTimeFunctions.swift (consolidated into 3 focused files) - All 40 DateTime tests passing - Better organization following PostgreSQL documentation structure
1 parent 29acee0 commit 971d11f

File tree

3 files changed

+87
-63
lines changed

3 files changed

+87
-63
lines changed
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
import Foundation
2+
import StructuredQueriesCore
3+
4+
// MARK: - Current Date/Time Functions
5+
//
6+
// PostgreSQL Chapter 9.9: Date/Time Functions and Operators
7+
// https://www.postgresql.org/docs/18/functions-datetime.html
8+
//
9+
// Functions for getting current date/time values
10+
11+
extension Date {
12+
/// PostgreSQL's `CURRENT_TIMESTAMP` - returns the current date and time
13+
///
14+
/// Returns the start time of the current transaction (does not change during the transaction).
15+
///
16+
/// ```swift
17+
/// Reminder.insert {
18+
/// Reminder.Draft(title: "New reminder", createdAt: .currentTimestamp)
19+
/// }
20+
/// // INSERT INTO "reminders" ("title", "createdAt") VALUES ('New reminder', CURRENT_TIMESTAMP)
21+
/// ```
22+
public static var currentTimestamp: some QueryExpression<Date> {
23+
SQLQueryExpression("CURRENT_TIMESTAMP", as: Date.self)
24+
}
25+
26+
/// PostgreSQL's `CURRENT_DATE` - returns the current date (without time)
27+
///
28+
/// Returns the current date at the start of the transaction.
29+
///
30+
/// ```swift
31+
/// Event.where { $0.eventDate >= .currentDate }
32+
/// // SELECT … FROM "events" WHERE "events"."eventDate" >= CURRENT_DATE
33+
/// ```
34+
public static var currentDate: some QueryExpression<Date> {
35+
SQLQueryExpression("CURRENT_DATE", as: Date.self)
36+
}
37+
}

Sources/StructuredQueriesPostgres/Functions/DateTime/PostgreSQLDateTimeFunctions.swift renamed to Sources/StructuredQueriesPostgres/Functions/DateTime/DateTime+Extract.swift

Lines changed: 8 additions & 63 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,13 @@
11
import Foundation
22
import StructuredQueriesCore
33

4-
// MARK: - PostgreSQL Date/Time Field Extraction
4+
// MARK: - Date/Time Field Extraction
5+
//
6+
// PostgreSQL Chapter 9.9: Date/Time Functions and Operators
7+
// https://www.postgresql.org/docs/18/functions-datetime.html
8+
//
9+
// EXTRACT function for extracting date/time fields
10+
511
/// Fields that can be extracted from a date/time value using `EXTRACT`
612
///
713
/// Each field has a specific return type based on PostgreSQL behavior.
@@ -52,17 +58,7 @@ extension DateField where ReturnType == Double {
5258
public static var second: DateField<Double> { DateField("SECOND") }
5359
}
5460

55-
/// Precision levels for date/time truncation using `DATE_TRUNC`
56-
public enum DateTruncPrecision: String {
57-
case year = "year"
58-
case month = "month"
59-
case day = "day"
60-
case hour = "hour"
61-
case minute = "minute"
62-
case second = "second"
63-
}
64-
65-
// MARK: - PostgreSQL Date/Time Functions
61+
// MARK: - EXTRACT Function
6662

6763
extension QueryExpression where QueryValue == Date {
6864
/// PostgreSQL's `EXTRACT` function - extracts a specific field from a date/time value
@@ -92,55 +88,4 @@ extension QueryExpression where QueryValue == Date {
9288
as: T.self
9389
)
9490
}
95-
96-
/// PostgreSQL's `DATE_TRUNC` function - truncates a date/time to the specified precision
97-
///
98-
/// Rounds down the timestamp to the beginning of the specified time unit.
99-
///
100-
/// ```swift
101-
/// Event.select { $0.timestamp.dateTrunc(.day) }
102-
/// // SELECT DATE_TRUNC('day', "events"."timestamp") FROM "events"
103-
///
104-
/// Event.select { $0.timestamp.dateTrunc(.hour) }
105-
/// // SELECT DATE_TRUNC('hour', "events"."timestamp") FROM "events"
106-
/// ```
107-
///
108-
/// - Parameter precision: The time unit to truncate to
109-
/// - Returns: A date expression truncated to the specified precision
110-
public func dateTrunc(_ precision: DateTruncPrecision) -> some QueryExpression<Date> {
111-
SQLQueryExpression(
112-
"DATE_TRUNC('\(raw: precision.rawValue)', \(self.queryFragment))",
113-
as: Date.self
114-
)
115-
}
116-
}
117-
118-
// MARK: - PostgreSQL Current Date/Time
119-
120-
extension Date {
121-
/// PostgreSQL's `CURRENT_TIMESTAMP` - returns the current date and time
122-
///
123-
/// Returns the start time of the current transaction (does not change during the transaction).
124-
///
125-
/// ```swift
126-
/// Reminder.insert {
127-
/// Reminder.Draft(title: "New reminder", createdAt: .currentTimestamp)
128-
/// }
129-
/// // INSERT INTO "reminders" ("title", "createdAt") VALUES ('New reminder', CURRENT_TIMESTAMP)
130-
/// ```
131-
public static var currentTimestamp: some QueryExpression<Date> {
132-
SQLQueryExpression("CURRENT_TIMESTAMP", as: Date.self)
133-
}
134-
135-
/// PostgreSQL's `CURRENT_DATE` - returns the current date (without time)
136-
///
137-
/// Returns the current date at the start of the transaction.
138-
///
139-
/// ```swift
140-
/// Event.where { $0.eventDate >= .currentDate }
141-
/// // SELECT … FROM "events" WHERE "events"."eventDate" >= CURRENT_DATE
142-
/// ```
143-
public static var currentDate: some QueryExpression<Date> {
144-
SQLQueryExpression("CURRENT_DATE", as: Date.self)
145-
}
14691
}
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
import Foundation
2+
import StructuredQueriesCore
3+
4+
// MARK: - Date/Time Truncation
5+
//
6+
// PostgreSQL Chapter 9.9: Date/Time Functions and Operators
7+
// https://www.postgresql.org/docs/18/functions-datetime.html
8+
//
9+
// DATE_TRUNC function for truncating timestamps to specified precision
10+
11+
/// Precision levels for date/time truncation using `DATE_TRUNC`
12+
public enum DateTruncPrecision: String {
13+
case year = "year"
14+
case month = "month"
15+
case day = "day"
16+
case hour = "hour"
17+
case minute = "minute"
18+
case second = "second"
19+
}
20+
21+
extension QueryExpression where QueryValue == Date {
22+
/// PostgreSQL's `DATE_TRUNC` function - truncates a date/time to the specified precision
23+
///
24+
/// Rounds down the timestamp to the beginning of the specified time unit.
25+
///
26+
/// ```swift
27+
/// Event.select { $0.timestamp.dateTrunc(.day) }
28+
/// // SELECT DATE_TRUNC('day', "events"."timestamp") FROM "events"
29+
///
30+
/// Event.select { $0.timestamp.dateTrunc(.hour) }
31+
/// // SELECT DATE_TRUNC('hour', "events"."timestamp") FROM "events"
32+
/// ```
33+
///
34+
/// - Parameter precision: The time unit to truncate to
35+
/// - Returns: A date expression truncated to the specified precision
36+
public func dateTrunc(_ precision: DateTruncPrecision) -> some QueryExpression<Date> {
37+
SQLQueryExpression(
38+
"DATE_TRUNC('\(raw: precision.rawValue)', \(self.queryFragment))",
39+
as: Date.self
40+
)
41+
}
42+
}

0 commit comments

Comments
 (0)