mirror of
https://github.com/groue/GRDB.swift
synced 2026-11-24 04:07:47 +00:00
170 lines
5.6 KiB
Swift
170 lines
5.6 KiB
Swift
// MARK: - Custom Functions
|
|
|
|
extension DatabaseFunction {
|
|
/// Returns an SQL expression that applies the function.
|
|
///
|
|
/// See https://github.com/groue/GRDB.swift/#sql-functions
|
|
public func apply(arguments: _SQLExpressionType...) -> _SQLExpression {
|
|
return .Function(name, arguments.map { $0.sqlExpression })
|
|
}
|
|
}
|
|
|
|
|
|
// MARK: - ABS(...)
|
|
|
|
/// Returns an SQL expression.
|
|
///
|
|
/// See https://github.com/groue/GRDB.swift/#sql-functions
|
|
public func abs(value: _SQLDerivedExpressionType) -> _SQLExpression {
|
|
return .Function("ABS", [value.sqlExpression])
|
|
}
|
|
|
|
|
|
// MARK: - AVG(...)
|
|
|
|
/// Returns an SQL expression.
|
|
///
|
|
/// See https://github.com/groue/GRDB.swift/#sql-functions
|
|
public func average(value: _SQLDerivedExpressionType) -> _SQLExpression {
|
|
return .Function("AVG", [value.sqlExpression])
|
|
}
|
|
|
|
|
|
// MARK: - COUNT(...)
|
|
|
|
/// Returns an SQL expression.
|
|
///
|
|
/// See https://github.com/groue/GRDB.swift/#sql-functions
|
|
public func count(counted: _SQLDerivedExpressionType) -> _SQLExpression {
|
|
return .Count(counted)
|
|
}
|
|
|
|
|
|
// MARK: - COUNT(DISTINCT ...)
|
|
|
|
/// Returns an SQL expression.
|
|
///
|
|
/// See https://github.com/groue/GRDB.swift/#sql-functions
|
|
public func count(distinct value: _SQLDerivedExpressionType) -> _SQLExpression {
|
|
return .CountDistinct(value.sqlExpression)
|
|
}
|
|
|
|
|
|
// MARK: - IFNULL(...)
|
|
|
|
/// Returns an SQL expression.
|
|
///
|
|
/// See https://github.com/groue/GRDB.swift/#sql-functions
|
|
public func ?? (lhs: _SQLDerivedExpressionType, rhs: _SQLExpressionType) -> _SQLExpression {
|
|
return .Function("IFNULL", [lhs.sqlExpression, rhs.sqlExpression])
|
|
}
|
|
|
|
/// Returns an SQL expression.
|
|
///
|
|
/// See https://github.com/groue/GRDB.swift/#sql-functions
|
|
public func ?? (lhs: _SQLExpressionType?, rhs: _SQLDerivedExpressionType) -> _SQLExpression {
|
|
if let lhs = lhs {
|
|
return .Function("IFNULL", [lhs.sqlExpression, rhs.sqlExpression])
|
|
} else {
|
|
return rhs.sqlExpression
|
|
}
|
|
}
|
|
|
|
|
|
// MARK: - MAX(...)
|
|
|
|
/// Returns an SQL expression.
|
|
///
|
|
/// See https://github.com/groue/GRDB.swift/#sql-functions
|
|
public func max(value: _SQLDerivedExpressionType) -> _SQLExpression {
|
|
return .Function("MAX", [value.sqlExpression])
|
|
}
|
|
|
|
|
|
// MARK: - MIN(...)
|
|
|
|
/// Returns an SQL expression.
|
|
///
|
|
/// See https://github.com/groue/GRDB.swift/#sql-functions
|
|
public func min(value: _SQLDerivedExpressionType) -> _SQLExpression {
|
|
return .Function("MIN", [value.sqlExpression])
|
|
}
|
|
|
|
|
|
// MARK: - SUM(...)
|
|
|
|
/// Returns an SQL expression.
|
|
///
|
|
/// See https://github.com/groue/GRDB.swift/#sql-functions
|
|
public func sum(value: _SQLDerivedExpressionType) -> _SQLExpression {
|
|
return .Function("SUM", [value.sqlExpression])
|
|
}
|
|
|
|
|
|
// MARK: - Swift String functions
|
|
|
|
extension _SQLDerivedExpressionType {
|
|
/// Returns an SQL expression that applies the Swift's built-in
|
|
/// capitalizedString String property. It is NULL for non-String arguments.
|
|
///
|
|
/// let nameColumn = SQLColumn("name")
|
|
/// let request = Person.select(nameColumn.capitalizedString)
|
|
/// let names = String.fetchAll(dbQueue, request) // [String]
|
|
public var capitalizedString: _SQLExpression {
|
|
return DatabaseFunction.capitalizedString.apply(sqlExpression)
|
|
}
|
|
|
|
/// Returns an SQL expression that applies the Swift's built-in
|
|
/// lowercaseString String property. It is NULL for non-String arguments.
|
|
///
|
|
/// let nameColumn = SQLColumn("name")
|
|
/// let request = Person.select(nameColumn.lowercaseString)
|
|
/// let names = String.fetchAll(dbQueue, request) // [String]
|
|
public var lowercaseString: _SQLExpression {
|
|
return DatabaseFunction.lowercaseString.apply(sqlExpression)
|
|
}
|
|
|
|
/// Returns an SQL expression that applies the Swift's built-in
|
|
/// uppercaseString String property. It is NULL for non-String arguments.
|
|
///
|
|
/// let nameColumn = SQLColumn("name")
|
|
/// let request = Person.select(nameColumn.uppercaseString)
|
|
/// let names = String.fetchAll(dbQueue, request) // [String]
|
|
public var uppercaseString: _SQLExpression {
|
|
return DatabaseFunction.uppercaseString.apply(sqlExpression)
|
|
}
|
|
}
|
|
|
|
@available(iOSApplicationExtension 9.0, OSXApplicationExtension 10.11, *)
|
|
extension _SQLDerivedExpressionType {
|
|
/// Returns an SQL expression that applies the Swift's built-in
|
|
/// localizedCapitalizedString String property. It is NULL for non-String arguments.
|
|
///
|
|
/// let nameColumn = SQLColumn("name")
|
|
/// let request = Person.select(nameColumn.localizedCapitalizedString)
|
|
/// let names = String.fetchAll(dbQueue, request) // [String]
|
|
public var localizedCapitalizedString: _SQLExpression {
|
|
return DatabaseFunction.localizedCapitalizedString.apply(sqlExpression)
|
|
}
|
|
|
|
/// Returns an SQL expression that applies the Swift's built-in
|
|
/// localizedLowercaseString String property. It is NULL for non-String arguments.
|
|
///
|
|
/// let nameColumn = SQLColumn("name")
|
|
/// let request = Person.select(nameColumn.localizedLowercaseString)
|
|
/// let names = String.fetchAll(dbQueue, request) // [String]
|
|
public var localizedLowercaseString: _SQLExpression {
|
|
return DatabaseFunction.localizedLowercaseString.apply(sqlExpression)
|
|
}
|
|
|
|
/// Returns an SQL expression that applies the Swift's built-in
|
|
/// localizedUppercaseString String property. It is NULL for non-String arguments.
|
|
///
|
|
/// let nameColumn = SQLColumn("name")
|
|
/// let request = Person.select(nameColumn.localizedUppercaseString)
|
|
/// let names = String.fetchAll(dbQueue, request) // [String]
|
|
public var localizedUppercaseString: _SQLExpression {
|
|
return DatabaseFunction.localizedUppercaseString.apply(sqlExpression)
|
|
}
|
|
}
|