Date modifiers, more julianDay(), and dateTime()

This commit is contained in:
Gwendal Roué committed 2020-06-05 20:48:12 +02:00
1 parent 3c48a434f1
commit d94fcc398b
4 files changed
+193 -11

No files matched your search

+89 -2
View File
@@ -217,6 +217,68 @@ extension SQLSpecificExpressible {
// MARK: - Date functions
/// A date modifier for SQLite date functions.
///
/// For more information, see https://www.sqlite.org/lang_datefunc.html
public enum SQLDateModifier: SQLExpression {
case day(Int)
case hour(Int)
case minute(Int)
case second(Double)
case month(Int)
case year(Int)
case startOfMonth
case startOfYear
case startOfDay
case weekday(Int)
case unixEpoch
case localTime
case utc
var rawValue: String {
switch self {
case let .day(value):
return "\(value) days"
case let .hour(value):
return "\(value) hours"
case let .minute(value):
return "\(value) minutes"
case let .second(value):
return "\(value) seconds"
case let .month(value):
return "\(value) months"
case let .year(value):
return "\(value) years"
case .startOfMonth:
return "start of month"
case .startOfYear:
return "start of year"
case .startOfDay:
return "start of day"
case let .weekday(value):
return "weekday \(value)"
case .unixEpoch:
return "unixepoch"
case .localTime:
return "localtime"
case .utc:
return "utc"
}
}
/// [**Experimental**](http://github.com/groue/GRDB.swift#what-are-experimental-features)
/// :nodoc:
public func expressionSQL(_ context: SQLGenerationContext, wrappedInParenthesis: Bool) throws -> String {
try rawValue.databaseValue.expressionSQL(context, wrappedInParenthesis: wrappedInParenthesis)
}
/// [**Experimental**](http://github.com/groue/GRDB.swift#what-are-experimental-features)
/// :nodoc:
public func qualifiedExpression(with alias: TableAlias) -> SQLExpression {
self
}
}
// MARK: JULIANDAY(...)
extension SQLFunctionName {
@@ -228,6 +290,31 @@ extension SQLFunctionName {
///
/// // JULIANDAY(date)
/// julianDay(Column("date"))
public func julianDay(_ value: SQLSpecificExpressible) -> SQLExpression {
SQLExpressionFunction(.julianDay, arguments: value)
///
/// // JULIANDAY(date, '1 days')
/// julianDay(Column("date"), .day(1))
///
/// For more information, see https://www.sqlite.org/lang_datefunc.html
public func julianDay(_ value: SQLSpecificExpressible, _ modifiers: SQLDateModifier...) -> SQLExpression {
SQLExpressionFunction(.julianDay, arguments: [value.sqlExpression] + modifiers)
}
// MARK: DATETIME(...)
extension SQLFunctionName {
/// The `DATETIME` function name
public static let dateTime = SQLFunctionName("DATETIME")
}
/// Returns an expression that evaluates the `DATETIME` SQL function.
///
/// // DATETIME(date)
/// dateTime(Column("date"))
///
/// // DATETIME(date, '1 days')
/// dateTime(Column("date"), .day(1))
///
/// For more information, see https://www.sqlite.org/lang_datefunc.html
public func dateTime(_ value: SQLSpecificExpressible, _ modifiers: SQLDateModifier...) -> SQLExpression {
SQLExpressionFunction(.dateTime, arguments: [value.sqlExpression] + modifiers)
}