From cb36991cb21dee04a52b18cf367bac1d40817a9f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Gwendal=20Roue=CC=81?= Date: Thu, 2 Jun 2016 08:09:44 +0200 Subject: [PATCH] Cleanup Query Interface protocols --- GRDB/Core/DatabaseValueConvertible.swift | 2 +- GRDB/FetchRequest/QueryInterfaceRequest.swift | 42 +++---- GRDB/FetchRequest/SQLCollation.swift | 44 +++---- GRDB/FetchRequest/SQLFunction.swift | 24 ++-- GRDB/FetchRequest/SQLOperator.swift | 112 +++++++++--------- GRDB/FetchRequest/SQLSelectQuery.swift | 76 ++++++++---- README.md | 4 +- 7 files changed, 164 insertions(+), 140 deletions(-) diff --git a/GRDB/Core/DatabaseValueConvertible.swift b/GRDB/Core/DatabaseValueConvertible.swift index a9d10278f..a5c258015 100644 --- a/GRDB/Core/DatabaseValueConvertible.swift +++ b/GRDB/Core/DatabaseValueConvertible.swift @@ -14,7 +14,7 @@ /// String.fetchOne(statement, arguments:...) // String? /// /// DatabaseValueConvertible is adopted by Bool, Int, String, etc. -public protocol DatabaseValueConvertible : _SQLExpressionType { +public protocol DatabaseValueConvertible : _SQLExpressible { /// Returns a value that can be stored in the database. var databaseValue: DatabaseValue { get } diff --git a/GRDB/FetchRequest/QueryInterfaceRequest.swift b/GRDB/FetchRequest/QueryInterfaceRequest.swift index e9f9a6010..622afdc1c 100644 --- a/GRDB/FetchRequest/QueryInterfaceRequest.swift +++ b/GRDB/FetchRequest/QueryInterfaceRequest.swift @@ -125,7 +125,7 @@ extension QueryInterfaceRequest { /// Returns a new QueryInterfaceRequest with the provided *predicate* added to the /// eventual set of already applied predicates. @warn_unused_result - public func filter(predicate: _SQLExpressionType) -> QueryInterfaceRequest { + public func filter(predicate: _SQLExpressible) -> QueryInterfaceRequest { var query = self.query if let whereExpression = query.whereExpression { query.whereExpression = .InfixOperator("AND", whereExpression, predicate.sqlExpression) @@ -144,13 +144,13 @@ extension QueryInterfaceRequest { /// Returns a new QueryInterfaceRequest grouped according to *expressions*. @warn_unused_result - public func group(expressions: _SQLExpressionType...) -> QueryInterfaceRequest { + public func group(expressions: _SQLExpressible...) -> QueryInterfaceRequest { return group(expressions) } /// Returns a new QueryInterfaceRequest grouped according to *expressions*. @warn_unused_result - public func group(expressions: [_SQLExpressionType]) -> QueryInterfaceRequest { + public func group(expressions: [_SQLExpressible]) -> QueryInterfaceRequest { var query = self.query query.groupByExpressions = expressions.map { $0.sqlExpression } return QueryInterfaceRequest(query: query) @@ -165,7 +165,7 @@ extension QueryInterfaceRequest { /// Returns a new QueryInterfaceRequest with the provided *predicate* added to the /// eventual set of already applied predicates. @warn_unused_result - public func having(predicate: _SQLExpressionType) -> QueryInterfaceRequest { + public func having(predicate: _SQLExpressible) -> QueryInterfaceRequest { var query = self.query if let havingExpression = query.havingExpression { query.havingExpression = (havingExpression && predicate).sqlExpression @@ -182,24 +182,24 @@ extension QueryInterfaceRequest { return having(_SQLLiteral(sql)) } - /// Returns a new QueryInterfaceRequest with the provided *sortDescriptors* added to - /// the eventual set of already applied sort descriptors. + /// Returns a new QueryInterfaceRequest with the provided *orderings* added to + /// the eventual set of already applied orderings. @warn_unused_result - public func order(sortDescriptors: _SQLSortDescriptorType...) -> QueryInterfaceRequest { - return order(sortDescriptors) + public func order(orderings: _SQLOrdering...) -> QueryInterfaceRequest { + return order(orderings) } - /// Returns a new QueryInterfaceRequest with the provided *sortDescriptors* added to - /// the eventual set of already applied sort descriptors. + /// Returns a new QueryInterfaceRequest with the provided *orderings* added to + /// the eventual set of already applied orderings. @warn_unused_result - public func order(sortDescriptors: [_SQLSortDescriptorType]) -> QueryInterfaceRequest { + public func order(orderings: [_SQLOrdering]) -> QueryInterfaceRequest { var query = self.query - query.sortDescriptors.appendContentsOf(sortDescriptors) + query.orderings.appendContentsOf(orderings) return QueryInterfaceRequest(query: query) } /// Returns a new QueryInterfaceRequest with the provided *sql* added to the - /// eventual set of already applied sort descriptors. + /// eventual set of already applied orderings. @warn_unused_result public func order(sql sql: String) -> QueryInterfaceRequest { return order([_SQLLiteral(sql)]) @@ -244,7 +244,7 @@ extension QueryInterfaceRequest { /// Returns an SQL expression that checks the inclusion of a value in /// the results of another request. - public func contains(element: _SQLExpressionType) -> _SQLExpression { + public func contains(element: _SQLExpressible) -> _SQLExpression { return .InSubQuery(query, element.sqlExpression) } @@ -286,7 +286,7 @@ extension TableMapping { /// Returns a QueryInterfaceRequest with the provided *predicate*. @warn_unused_result - public static func filter(predicate: _SQLExpressionType) -> QueryInterfaceRequest { + public static func filter(predicate: _SQLExpressible) -> QueryInterfaceRequest { return all().filter(predicate) } @@ -297,17 +297,17 @@ extension TableMapping { } /// Returns a QueryInterfaceRequest sorted according to the - /// provided *sortDescriptors*. + /// provided *orderings*. @warn_unused_result - public static func order(sortDescriptors: _SQLSortDescriptorType...) -> QueryInterfaceRequest { - return all().order(sortDescriptors) + public static func order(orderings: _SQLOrdering...) -> QueryInterfaceRequest { + return all().order(orderings) } /// Returns a QueryInterfaceRequest sorted according to the - /// provided *sortDescriptors*. + /// provided *orderings*. @warn_unused_result - public static func order(sortDescriptors: [_SQLSortDescriptorType]) -> QueryInterfaceRequest { - return all().order(sortDescriptors) + public static func order(orderings: [_SQLOrdering]) -> QueryInterfaceRequest { + return all().order(orderings) } /// Returns a QueryInterfaceRequest sorted according to *sql*. diff --git a/GRDB/FetchRequest/SQLCollation.swift b/GRDB/FetchRequest/SQLCollation.swift index 95f51cebf..93d743016 100644 --- a/GRDB/FetchRequest/SQLCollation.swift +++ b/GRDB/FetchRequest/SQLCollation.swift @@ -7,7 +7,7 @@ public struct _SQLCollatedExpression { let collationName: String } -extension _SQLCollatedExpression : _SQLExpressionType { +extension _SQLCollatedExpression : _SQLExpressible { /// This property is an implementation detail of the query interface. /// Do not use it directly. @@ -18,7 +18,7 @@ extension _SQLCollatedExpression : _SQLExpressionType { } } -extension _SQLCollatedExpression : _SQLSortDescriptorType { +extension _SQLCollatedExpression : _SQLOrdering { /// This property is an implementation detail of the query interface. /// Do not use it directly. @@ -51,7 +51,7 @@ extension _SQLCollatedExpression : _SQLSortDescriptorType { } } -extension _SQLDerivedExpressionType { +extension _SpecificSQLExpressible { /// This method is an implementation detail of the query interface. /// Do not use it directly. @@ -76,14 +76,14 @@ extension _SQLDerivedExpressionType { /// Returns an SQL expression that compares two values. /// /// See https://github.com/groue/GRDB.swift/#sql-operators -public func == (lhs: _SQLCollatedExpression, rhs: _SQLExpressionType?) -> _SQLExpression { +public func == (lhs: _SQLCollatedExpression, rhs: _SQLExpressible?) -> _SQLExpression { return .Collate(lhs.baseExpression == rhs, lhs.collationName) } /// Returns an SQL expression that compares two values. /// /// See https://github.com/groue/GRDB.swift/#sql-operators -public func == (lhs: _SQLExpressionType?, rhs: _SQLCollatedExpression) -> _SQLExpression { +public func == (lhs: _SQLExpressible?, rhs: _SQLCollatedExpression) -> _SQLExpression { return .Collate(lhs == rhs.baseExpression, rhs.collationName) } @@ -93,14 +93,14 @@ public func == (lhs: _SQLExpressionType?, rhs: _SQLCollatedExpression) -> _SQLEx /// Returns an SQL expression that compares two values. /// /// See https://github.com/groue/GRDB.swift/#sql-operators -public func != (lhs: _SQLCollatedExpression, rhs: _SQLExpressionType?) -> _SQLExpression { +public func != (lhs: _SQLCollatedExpression, rhs: _SQLExpressible?) -> _SQLExpression { return .Collate(lhs.baseExpression != rhs, lhs.collationName) } /// Returns an SQL expression that compares two values. /// /// See https://github.com/groue/GRDB.swift/#sql-operators -public func != (lhs: _SQLExpressionType?, rhs: _SQLCollatedExpression) -> _SQLExpression { +public func != (lhs: _SQLExpressible?, rhs: _SQLCollatedExpression) -> _SQLExpression { return .Collate(lhs != rhs.baseExpression, rhs.collationName) } @@ -110,14 +110,14 @@ public func != (lhs: _SQLExpressionType?, rhs: _SQLCollatedExpression) -> _SQLEx /// Returns an SQL expression that compares two values. /// /// See https://github.com/groue/GRDB.swift/#sql-operators -public func < (lhs: _SQLCollatedExpression, rhs: _SQLExpressionType) -> _SQLExpression { +public func < (lhs: _SQLCollatedExpression, rhs: _SQLExpressible) -> _SQLExpression { return .Collate(lhs.baseExpression < rhs, lhs.collationName) } /// Returns an SQL expression that compares two values. /// /// See https://github.com/groue/GRDB.swift/#sql-operators -public func < (lhs: _SQLExpressionType, rhs: _SQLCollatedExpression) -> _SQLExpression { +public func < (lhs: _SQLExpressible, rhs: _SQLCollatedExpression) -> _SQLExpression { return .Collate(lhs < rhs.baseExpression, rhs.collationName) } @@ -127,14 +127,14 @@ public func < (lhs: _SQLExpressionType, rhs: _SQLCollatedExpression) -> _SQLExpr /// Returns an SQL expression that compares two values. /// /// See https://github.com/groue/GRDB.swift/#sql-operators -public func <= (lhs: _SQLCollatedExpression, rhs: _SQLExpressionType) -> _SQLExpression { +public func <= (lhs: _SQLCollatedExpression, rhs: _SQLExpressible) -> _SQLExpression { return .Collate(lhs.baseExpression <= rhs, lhs.collationName) } /// Returns an SQL expression that compares two values. /// /// See https://github.com/groue/GRDB.swift/#sql-operators -public func <= (lhs: _SQLExpressionType, rhs: _SQLCollatedExpression) -> _SQLExpression { +public func <= (lhs: _SQLExpressible, rhs: _SQLCollatedExpression) -> _SQLExpression { return .Collate(lhs <= rhs.baseExpression, rhs.collationName) } @@ -144,14 +144,14 @@ public func <= (lhs: _SQLExpressionType, rhs: _SQLCollatedExpression) -> _SQLExp /// Returns an SQL expression that compares two values. /// /// See https://github.com/groue/GRDB.swift/#sql-operators -public func > (lhs: _SQLCollatedExpression, rhs: _SQLExpressionType) -> _SQLExpression { +public func > (lhs: _SQLCollatedExpression, rhs: _SQLExpressible) -> _SQLExpression { return .Collate(lhs.baseExpression > rhs, lhs.collationName) } /// Returns an SQL expression that compares two values. /// /// See https://github.com/groue/GRDB.swift/#sql-operators -public func > (lhs: _SQLExpressionType, rhs: _SQLCollatedExpression) -> _SQLExpression { +public func > (lhs: _SQLExpressible, rhs: _SQLCollatedExpression) -> _SQLExpression { return .Collate(lhs > rhs.baseExpression, rhs.collationName) } @@ -161,21 +161,21 @@ public func > (lhs: _SQLExpressionType, rhs: _SQLCollatedExpression) -> _SQLExpr /// Returns an SQL expression that compares two values. /// /// See https://github.com/groue/GRDB.swift/#sql-operators -public func >= (lhs: _SQLCollatedExpression, rhs: _SQLExpressionType) -> _SQLExpression { +public func >= (lhs: _SQLCollatedExpression, rhs: _SQLExpressible) -> _SQLExpression { return .Collate(lhs.baseExpression >= rhs, lhs.collationName) } /// Returns an SQL expression that compares two values. /// /// See https://github.com/groue/GRDB.swift/#sql-operators -public func >= (lhs: _SQLExpressionType, rhs: _SQLCollatedExpression) -> _SQLExpression { +public func >= (lhs: _SQLExpressible, rhs: _SQLCollatedExpression) -> _SQLExpression { return .Collate(lhs >= rhs.baseExpression, rhs.collationName) } // MARK: - Operator BETWEEN COLLATE -extension ClosedInterval where Bound: _SQLExpressionType { +extension ClosedInterval where Bound: _SQLExpressible { /// Returns an SQL expression that compares the inclusion of a value in /// an interval. /// @@ -185,7 +185,7 @@ extension ClosedInterval where Bound: _SQLExpressionType { } } -extension HalfOpenInterval where Bound: _SQLExpressionType { +extension HalfOpenInterval where Bound: _SQLExpressible { /// Returns an SQL expression that compares the inclusion of a value in /// an interval. /// @@ -198,7 +198,7 @@ extension HalfOpenInterval where Bound: _SQLExpressionType { // MARK: - Operator IN COLLATE -extension SequenceType where Self.Generator.Element: _SQLExpressionType { +extension SequenceType where Self.Generator.Element: _SQLExpressible { /// Returns an SQL expression that compares the inclusion of a value in /// a sequence. /// @@ -214,14 +214,14 @@ extension SequenceType where Self.Generator.Element: _SQLExpressionType { /// Returns an SQL expression that compares two values. /// /// See https://github.com/groue/GRDB.swift/#sql-operators -public func === (lhs: _SQLCollatedExpression, rhs: _SQLExpressionType?) -> _SQLExpression { +public func === (lhs: _SQLCollatedExpression, rhs: _SQLExpressible?) -> _SQLExpression { return .Collate(lhs.baseExpression === rhs, lhs.collationName) } /// Returns an SQL expression that compares two values. /// /// See https://github.com/groue/GRDB.swift/#sql-operators -public func === (lhs: _SQLExpressionType?, rhs: _SQLCollatedExpression) -> _SQLExpression { +public func === (lhs: _SQLExpressible?, rhs: _SQLCollatedExpression) -> _SQLExpression { return .Collate(lhs == rhs.baseExpression, rhs.collationName) } @@ -231,13 +231,13 @@ public func === (lhs: _SQLExpressionType?, rhs: _SQLCollatedExpression) -> _SQLE /// Returns an SQL expression that compares two values. /// /// See https://github.com/groue/GRDB.swift/#sql-operators -public func !== (lhs: _SQLCollatedExpression, rhs: _SQLExpressionType?) -> _SQLExpression { +public func !== (lhs: _SQLCollatedExpression, rhs: _SQLExpressible?) -> _SQLExpression { return .Collate(lhs.baseExpression !== rhs, lhs.collationName) } /// Returns an SQL expression that compares two values. /// /// See https://github.com/groue/GRDB.swift/#sql-operators -public func !== (lhs: _SQLExpressionType?, rhs: _SQLCollatedExpression) -> _SQLExpression { +public func !== (lhs: _SQLExpressible?, rhs: _SQLCollatedExpression) -> _SQLExpression { return .Collate(lhs !== rhs.baseExpression, rhs.collationName) } diff --git a/GRDB/FetchRequest/SQLFunction.swift b/GRDB/FetchRequest/SQLFunction.swift index 9d4d8f209..500dd348c 100644 --- a/GRDB/FetchRequest/SQLFunction.swift +++ b/GRDB/FetchRequest/SQLFunction.swift @@ -4,7 +4,7 @@ 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 { + public func apply(arguments: _SQLExpressible...) -> _SQLExpression { return .Function(name, arguments.map { $0.sqlExpression }) } } @@ -15,7 +15,7 @@ extension DatabaseFunction { /// Returns an SQL expression. /// /// See https://github.com/groue/GRDB.swift/#sql-functions -public func abs(value: _SQLDerivedExpressionType) -> _SQLExpression { +public func abs(value: _SpecificSQLExpressible) -> _SQLExpression { return .Function("ABS", [value.sqlExpression]) } @@ -25,7 +25,7 @@ public func abs(value: _SQLDerivedExpressionType) -> _SQLExpression { /// Returns an SQL expression. /// /// See https://github.com/groue/GRDB.swift/#sql-functions -public func average(value: _SQLDerivedExpressionType) -> _SQLExpression { +public func average(value: _SpecificSQLExpressible) -> _SQLExpression { return .Function("AVG", [value.sqlExpression]) } @@ -35,7 +35,7 @@ public func average(value: _SQLDerivedExpressionType) -> _SQLExpression { /// Returns an SQL expression. /// /// See https://github.com/groue/GRDB.swift/#sql-functions -public func count(counted: _SQLDerivedExpressionType) -> _SQLExpression { +public func count(counted: _SQLSelectable) -> _SQLExpression { return .Count(counted) } @@ -45,7 +45,7 @@ public func count(counted: _SQLDerivedExpressionType) -> _SQLExpression { /// Returns an SQL expression. /// /// See https://github.com/groue/GRDB.swift/#sql-functions -public func count(distinct value: _SQLDerivedExpressionType) -> _SQLExpression { +public func count(distinct value: _SpecificSQLExpressible) -> _SQLExpression { return .CountDistinct(value.sqlExpression) } @@ -55,14 +55,14 @@ public func count(distinct value: _SQLDerivedExpressionType) -> _SQLExpression { /// Returns an SQL expression. /// /// See https://github.com/groue/GRDB.swift/#sql-functions -public func ?? (lhs: _SQLDerivedExpressionType, rhs: _SQLExpressionType) -> _SQLExpression { +public func ?? (lhs: _SpecificSQLExpressible, rhs: _SQLExpressible) -> _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 { +public func ?? (lhs: _SQLExpressible?, rhs: _SpecificSQLExpressible) -> _SQLExpression { if let lhs = lhs { return .Function("IFNULL", [lhs.sqlExpression, rhs.sqlExpression]) } else { @@ -76,7 +76,7 @@ public func ?? (lhs: _SQLExpressionType?, rhs: _SQLDerivedExpressionType) -> _SQ /// Returns an SQL expression. /// /// See https://github.com/groue/GRDB.swift/#sql-functions -public func max(value: _SQLDerivedExpressionType) -> _SQLExpression { +public func max(value: _SpecificSQLExpressible) -> _SQLExpression { return .Function("MAX", [value.sqlExpression]) } @@ -86,7 +86,7 @@ public func max(value: _SQLDerivedExpressionType) -> _SQLExpression { /// Returns an SQL expression. /// /// See https://github.com/groue/GRDB.swift/#sql-functions -public func min(value: _SQLDerivedExpressionType) -> _SQLExpression { +public func min(value: _SpecificSQLExpressible) -> _SQLExpression { return .Function("MIN", [value.sqlExpression]) } @@ -96,14 +96,14 @@ public func min(value: _SQLDerivedExpressionType) -> _SQLExpression { /// Returns an SQL expression. /// /// See https://github.com/groue/GRDB.swift/#sql-functions -public func sum(value: _SQLDerivedExpressionType) -> _SQLExpression { +public func sum(value: _SpecificSQLExpressible) -> _SQLExpression { return .Function("SUM", [value.sqlExpression]) } // MARK: - Swift String functions -extension _SQLDerivedExpressionType { +extension _SpecificSQLExpressible { /// Returns an SQL expression that applies the Swift's built-in /// capitalizedString String property. It is NULL for non-String arguments. /// @@ -136,7 +136,7 @@ extension _SQLDerivedExpressionType { } @available(iOS 9.0, OSX 10.11, *) -extension _SQLDerivedExpressionType { +extension _SpecificSQLExpressible { /// Returns an SQL expression that applies the Swift's built-in /// localizedCapitalizedString String property. It is NULL for non-String arguments. /// diff --git a/GRDB/FetchRequest/SQLOperator.swift b/GRDB/FetchRequest/SQLOperator.swift index 50c9630f8..0f471a542 100644 --- a/GRDB/FetchRequest/SQLOperator.swift +++ b/GRDB/FetchRequest/SQLOperator.swift @@ -3,14 +3,14 @@ /// Returns an SQL expression that compares two values. /// /// See https://github.com/groue/GRDB.swift/#sql-operators -public func == (lhs: _SQLDerivedExpressionType, rhs: _SQLExpressionType?) -> _SQLExpression { +public func == (lhs: _SpecificSQLExpressible, rhs: _SQLExpressible?) -> _SQLExpression { return .Equal(lhs.sqlExpression, rhs?.sqlExpression ?? .Value(nil)) } /// Returns an SQL expression that compares two values. /// /// See https://github.com/groue/GRDB.swift/#sql-operators -public func == (lhs: _SQLDerivedExpressionType, rhs: protocol<_SQLExpressionType, BooleanType>?) -> _SQLExpression { +public func == (lhs: _SpecificSQLExpressible, rhs: protocol<_SQLExpressible, BooleanType>?) -> _SQLExpression { if let rhs = rhs { if rhs.boolValue { return lhs.sqlExpression @@ -25,14 +25,14 @@ public func == (lhs: _SQLDerivedExpressionType, rhs: protocol<_SQLExpressionType /// Returns an SQL expression that compares two values. /// /// See https://github.com/groue/GRDB.swift/#sql-operators -public func == (lhs: _SQLExpressionType?, rhs: _SQLDerivedExpressionType) -> _SQLExpression { +public func == (lhs: _SQLExpressible?, rhs: _SpecificSQLExpressible) -> _SQLExpression { return .Equal(lhs?.sqlExpression ?? .Value(nil), rhs.sqlExpression) } /// Returns an SQL expression that compares two values. /// /// See https://github.com/groue/GRDB.swift/#sql-operators -public func == (lhs: protocol<_SQLExpressionType, BooleanType>?, rhs: _SQLDerivedExpressionType) -> _SQLExpression { +public func == (lhs: protocol<_SQLExpressible, BooleanType>?, rhs: _SpecificSQLExpressible) -> _SQLExpression { if let lhs = lhs { if lhs.boolValue { return rhs.sqlExpression @@ -47,7 +47,7 @@ public func == (lhs: protocol<_SQLExpressionType, BooleanType>?, rhs: _SQLDerive /// Returns an SQL expression that compares two values. /// /// See https://github.com/groue/GRDB.swift/#sql-operators -public func == (lhs: _SQLDerivedExpressionType, rhs: _SQLDerivedExpressionType) -> _SQLExpression { +public func == (lhs: _SpecificSQLExpressible, rhs: _SpecificSQLExpressible) -> _SQLExpression { return .Equal(lhs.sqlExpression, rhs.sqlExpression) } @@ -57,14 +57,14 @@ public func == (lhs: _SQLDerivedExpressionType, rhs: _SQLDerivedExpressionType) /// Returns an SQL expression that compares two values. /// /// See https://github.com/groue/GRDB.swift/#sql-operators -public func != (lhs: _SQLDerivedExpressionType, rhs: _SQLExpressionType?) -> _SQLExpression { +public func != (lhs: _SpecificSQLExpressible, rhs: _SQLExpressible?) -> _SQLExpression { return .NotEqual(lhs.sqlExpression, rhs?.sqlExpression ?? .Value(nil)) } /// Returns an SQL expression that compares two values. /// /// See https://github.com/groue/GRDB.swift/#sql-operators -public func != (lhs: _SQLDerivedExpressionType, rhs: protocol<_SQLExpressionType, BooleanType>?) -> _SQLExpression { +public func != (lhs: _SpecificSQLExpressible, rhs: protocol<_SQLExpressible, BooleanType>?) -> _SQLExpression { if let rhs = rhs { if rhs.boolValue { return .Not(lhs.sqlExpression) @@ -79,14 +79,14 @@ public func != (lhs: _SQLDerivedExpressionType, rhs: protocol<_SQLExpressionType /// Returns an SQL expression that compares two values. /// /// See https://github.com/groue/GRDB.swift/#sql-operators -public func != (lhs: _SQLExpressionType?, rhs: _SQLDerivedExpressionType) -> _SQLExpression { +public func != (lhs: _SQLExpressible?, rhs: _SpecificSQLExpressible) -> _SQLExpression { return .NotEqual(lhs?.sqlExpression ?? .Value(nil), rhs.sqlExpression) } /// Returns an SQL expression that compares two values. /// /// See https://github.com/groue/GRDB.swift/#sql-operators -public func != (lhs: protocol<_SQLExpressionType, BooleanType>?, rhs: _SQLDerivedExpressionType) -> _SQLExpression { +public func != (lhs: protocol<_SQLExpressible, BooleanType>?, rhs: _SpecificSQLExpressible) -> _SQLExpression { if let lhs = lhs { if lhs.boolValue { return .Not(rhs.sqlExpression) @@ -101,7 +101,7 @@ public func != (lhs: protocol<_SQLExpressionType, BooleanType>?, rhs: _SQLDerive /// Returns an SQL expression that compares two values. /// /// See https://github.com/groue/GRDB.swift/#sql-operators -public func != (lhs: _SQLDerivedExpressionType, rhs: _SQLDerivedExpressionType) -> _SQLExpression { +public func != (lhs: _SpecificSQLExpressible, rhs: _SpecificSQLExpressible) -> _SQLExpression { return .NotEqual(lhs.sqlExpression, rhs.sqlExpression) } @@ -111,21 +111,21 @@ public func != (lhs: _SQLDerivedExpressionType, rhs: _SQLDerivedExpressionType) /// Returns an SQL expression that compares two values. /// /// See https://github.com/groue/GRDB.swift/#sql-operators -public func < (lhs: _SQLDerivedExpressionType, rhs: _SQLExpressionType) -> _SQLExpression { +public func < (lhs: _SpecificSQLExpressible, rhs: _SQLExpressible) -> _SQLExpression { return .InfixOperator("<", lhs.sqlExpression, rhs.sqlExpression) } /// Returns an SQL expression that compares two values. /// /// See https://github.com/groue/GRDB.swift/#sql-operators -public func < (lhs: _SQLExpressionType, rhs: _SQLDerivedExpressionType) -> _SQLExpression { +public func < (lhs: _SQLExpressible, rhs: _SpecificSQLExpressible) -> _SQLExpression { return .InfixOperator("<", lhs.sqlExpression, rhs.sqlExpression) } /// Returns an SQL expression that compares two values. /// /// See https://github.com/groue/GRDB.swift/#sql-operators -public func < (lhs: _SQLDerivedExpressionType, rhs: _SQLDerivedExpressionType) -> _SQLExpression { +public func < (lhs: _SpecificSQLExpressible, rhs: _SpecificSQLExpressible) -> _SQLExpression { return .InfixOperator("<", lhs.sqlExpression, rhs.sqlExpression) } @@ -135,21 +135,21 @@ public func < (lhs: _SQLDerivedExpressionType, rhs: _SQLDerivedExpressionType) - /// Returns an SQL expression that compares two values. /// /// See https://github.com/groue/GRDB.swift/#sql-operators -public func <= (lhs: _SQLDerivedExpressionType, rhs: _SQLExpressionType) -> _SQLExpression { +public func <= (lhs: _SpecificSQLExpressible, rhs: _SQLExpressible) -> _SQLExpression { return .InfixOperator("<=", lhs.sqlExpression, rhs.sqlExpression) } /// Returns an SQL expression that compares two values. /// /// See https://github.com/groue/GRDB.swift/#sql-operators -public func <= (lhs: _SQLExpressionType, rhs: _SQLDerivedExpressionType) -> _SQLExpression { +public func <= (lhs: _SQLExpressible, rhs: _SpecificSQLExpressible) -> _SQLExpression { return .InfixOperator("<=", lhs.sqlExpression, rhs.sqlExpression) } /// Returns an SQL expression that compares two values. /// /// See https://github.com/groue/GRDB.swift/#sql-operators -public func <= (lhs: _SQLDerivedExpressionType, rhs: _SQLDerivedExpressionType) -> _SQLExpression { +public func <= (lhs: _SpecificSQLExpressible, rhs: _SpecificSQLExpressible) -> _SQLExpression { return .InfixOperator("<=", lhs.sqlExpression, rhs.sqlExpression) } @@ -159,21 +159,21 @@ public func <= (lhs: _SQLDerivedExpressionType, rhs: _SQLDerivedExpressionType) /// Returns an SQL expression that compares two values. /// /// See https://github.com/groue/GRDB.swift/#sql-operators -public func > (lhs: _SQLDerivedExpressionType, rhs: _SQLExpressionType) -> _SQLExpression { +public func > (lhs: _SpecificSQLExpressible, rhs: _SQLExpressible) -> _SQLExpression { return .InfixOperator(">", lhs.sqlExpression, rhs.sqlExpression) } /// Returns an SQL expression that compares two values. /// /// See https://github.com/groue/GRDB.swift/#sql-operators -public func > (lhs: _SQLExpressionType, rhs: _SQLDerivedExpressionType) -> _SQLExpression { +public func > (lhs: _SQLExpressible, rhs: _SpecificSQLExpressible) -> _SQLExpression { return .InfixOperator(">", lhs.sqlExpression, rhs.sqlExpression) } /// Returns an SQL expression that compares two values. /// /// See https://github.com/groue/GRDB.swift/#sql-operators -public func > (lhs: _SQLDerivedExpressionType, rhs: _SQLDerivedExpressionType) -> _SQLExpression { +public func > (lhs: _SpecificSQLExpressible, rhs: _SpecificSQLExpressible) -> _SQLExpression { return .InfixOperator(">", lhs.sqlExpression, rhs.sqlExpression) } @@ -183,21 +183,21 @@ public func > (lhs: _SQLDerivedExpressionType, rhs: _SQLDerivedExpressionType) - /// Returns an SQL expression that compares two values. /// /// See https://github.com/groue/GRDB.swift/#sql-operators -public func >= (lhs: _SQLDerivedExpressionType, rhs: _SQLExpressionType) -> _SQLExpression { +public func >= (lhs: _SpecificSQLExpressible, rhs: _SQLExpressible) -> _SQLExpression { return .InfixOperator(">=", lhs.sqlExpression, rhs.sqlExpression) } /// Returns an SQL expression that compares two values. /// /// See https://github.com/groue/GRDB.swift/#sql-operators -public func >= (lhs: _SQLExpressionType, rhs: _SQLDerivedExpressionType) -> _SQLExpression { +public func >= (lhs: _SQLExpressible, rhs: _SpecificSQLExpressible) -> _SQLExpression { return .InfixOperator(">=", lhs.sqlExpression, rhs.sqlExpression) } /// Returns an SQL expression that compares two values. /// /// See https://github.com/groue/GRDB.swift/#sql-operators -public func >= (lhs: _SQLDerivedExpressionType, rhs: _SQLDerivedExpressionType) -> _SQLExpression { +public func >= (lhs: _SpecificSQLExpressible, rhs: _SpecificSQLExpressible) -> _SQLExpression { return .InfixOperator(">=", lhs.sqlExpression, rhs.sqlExpression) } @@ -207,21 +207,21 @@ public func >= (lhs: _SQLDerivedExpressionType, rhs: _SQLDerivedExpressionType) /// Returns an SQL arithmetic expression. /// /// See https://github.com/groue/GRDB.swift/#sql-operators -public func * (lhs: _SQLDerivedExpressionType, rhs: _SQLExpressionType) -> _SQLExpression { +public func * (lhs: _SpecificSQLExpressible, rhs: _SQLExpressible) -> _SQLExpression { return .InfixOperator("*", lhs.sqlExpression, rhs.sqlExpression) } /// Returns an SQL arithmetic expression. /// /// See https://github.com/groue/GRDB.swift/#sql-operators -public func * (lhs: _SQLExpressionType, rhs: _SQLDerivedExpressionType) -> _SQLExpression { +public func * (lhs: _SQLExpressible, rhs: _SpecificSQLExpressible) -> _SQLExpression { return .InfixOperator("*", lhs.sqlExpression, rhs.sqlExpression) } /// Returns an SQL arithmetic expression. /// /// See https://github.com/groue/GRDB.swift/#sql-operators -public func * (lhs: _SQLDerivedExpressionType, rhs: _SQLDerivedExpressionType) -> _SQLExpression { +public func * (lhs: _SpecificSQLExpressible, rhs: _SpecificSQLExpressible) -> _SQLExpression { return .InfixOperator("*", lhs.sqlExpression, rhs.sqlExpression) } @@ -231,21 +231,21 @@ public func * (lhs: _SQLDerivedExpressionType, rhs: _SQLDerivedExpressionType) - /// Returns an SQL arithmetic expression. /// /// See https://github.com/groue/GRDB.swift/#sql-operators -public func / (lhs: _SQLDerivedExpressionType, rhs: _SQLExpressionType) -> _SQLExpression { +public func / (lhs: _SpecificSQLExpressible, rhs: _SQLExpressible) -> _SQLExpression { return .InfixOperator("/", lhs.sqlExpression, rhs.sqlExpression) } /// Returns an SQL arithmetic expression. /// /// See https://github.com/groue/GRDB.swift/#sql-operators -public func / (lhs: _SQLExpressionType, rhs: _SQLDerivedExpressionType) -> _SQLExpression { +public func / (lhs: _SQLExpressible, rhs: _SpecificSQLExpressible) -> _SQLExpression { return .InfixOperator("/", lhs.sqlExpression, rhs.sqlExpression) } /// Returns an SQL arithmetic expression. /// /// See https://github.com/groue/GRDB.swift/#sql-operators -public func / (lhs: _SQLDerivedExpressionType, rhs: _SQLDerivedExpressionType) -> _SQLExpression { +public func / (lhs: _SpecificSQLExpressible, rhs: _SpecificSQLExpressible) -> _SQLExpression { return .InfixOperator("/", lhs.sqlExpression, rhs.sqlExpression) } @@ -255,21 +255,21 @@ public func / (lhs: _SQLDerivedExpressionType, rhs: _SQLDerivedExpressionType) - /// Returns an SQL arithmetic expression. /// /// See https://github.com/groue/GRDB.swift/#sql-operators -public func + (lhs: _SQLDerivedExpressionType, rhs: _SQLExpressionType) -> _SQLExpression { +public func + (lhs: _SpecificSQLExpressible, rhs: _SQLExpressible) -> _SQLExpression { return .InfixOperator("+", lhs.sqlExpression, rhs.sqlExpression) } /// Returns an SQL arithmetic expression. /// /// See https://github.com/groue/GRDB.swift/#sql-operators -public func + (lhs: _SQLExpressionType, rhs: _SQLDerivedExpressionType) -> _SQLExpression { +public func + (lhs: _SQLExpressible, rhs: _SpecificSQLExpressible) -> _SQLExpression { return .InfixOperator("+", lhs.sqlExpression, rhs.sqlExpression) } /// Returns an SQL arithmetic expression. /// /// See https://github.com/groue/GRDB.swift/#sql-operators -public func + (lhs: _SQLDerivedExpressionType, rhs: _SQLDerivedExpressionType) -> _SQLExpression { +public func + (lhs: _SpecificSQLExpressible, rhs: _SpecificSQLExpressible) -> _SQLExpression { return .InfixOperator("+", lhs.sqlExpression, rhs.sqlExpression) } @@ -279,7 +279,7 @@ public func + (lhs: _SQLDerivedExpressionType, rhs: _SQLDerivedExpressionType) - /// Returns an SQL arithmetic expression. /// /// See https://github.com/groue/GRDB.swift/#sql-operators -public prefix func - (value: _SQLDerivedExpressionType) -> _SQLExpression { +public prefix func - (value: _SpecificSQLExpressible) -> _SQLExpression { return .PrefixOperator("-", value.sqlExpression) } @@ -289,21 +289,21 @@ public prefix func - (value: _SQLDerivedExpressionType) -> _SQLExpression { /// Returns an SQL arithmetic expression. /// /// See https://github.com/groue/GRDB.swift/#sql-operators -public func - (lhs: _SQLDerivedExpressionType, rhs: _SQLExpressionType) -> _SQLExpression { +public func - (lhs: _SpecificSQLExpressible, rhs: _SQLExpressible) -> _SQLExpression { return .InfixOperator("-", lhs.sqlExpression, rhs.sqlExpression) } /// Returns an SQL arithmetic expression. /// /// See https://github.com/groue/GRDB.swift/#sql-operators -public func - (lhs: _SQLExpressionType, rhs: _SQLDerivedExpressionType) -> _SQLExpression { +public func - (lhs: _SQLExpressible, rhs: _SpecificSQLExpressible) -> _SQLExpression { return .InfixOperator("-", lhs.sqlExpression, rhs.sqlExpression) } /// Returns an SQL arithmetic expression. /// /// See https://github.com/groue/GRDB.swift/#sql-operators -public func - (lhs: _SQLDerivedExpressionType, rhs: _SQLDerivedExpressionType) -> _SQLExpression { +public func - (lhs: _SpecificSQLExpressible, rhs: _SpecificSQLExpressible) -> _SQLExpression { return .InfixOperator("-", lhs.sqlExpression, rhs.sqlExpression) } @@ -313,53 +313,53 @@ public func - (lhs: _SQLDerivedExpressionType, rhs: _SQLDerivedExpressionType) - /// Returns an SQL logical expression. /// /// See https://github.com/groue/GRDB.swift/#sql-operators -public func && (lhs: _SQLDerivedExpressionType, rhs: _SQLExpressionType) -> _SQLExpression { +public func && (lhs: _SpecificSQLExpressible, rhs: _SQLExpressible) -> _SQLExpression { return .InfixOperator("AND", lhs.sqlExpression, rhs.sqlExpression) } /// Returns an SQL logical expression. /// /// See https://github.com/groue/GRDB.swift/#sql-operators -public func && (lhs: _SQLExpressionType, rhs: _SQLDerivedExpressionType) -> _SQLExpression { +public func && (lhs: _SQLExpressible, rhs: _SpecificSQLExpressible) -> _SQLExpression { return .InfixOperator("AND", lhs.sqlExpression, rhs.sqlExpression) } /// Returns an SQL logical expression. /// /// See https://github.com/groue/GRDB.swift/#sql-operators -public func && (lhs: _SQLDerivedExpressionType, rhs: _SQLDerivedExpressionType) -> _SQLExpression { +public func && (lhs: _SpecificSQLExpressible, rhs: _SpecificSQLExpressible) -> _SQLExpression { return .InfixOperator("AND", lhs.sqlExpression, rhs.sqlExpression) } // MARK: - Operator BETWEEN -extension Range where Element: protocol<_SQLExpressionType, BidirectionalIndexType> { +extension Range where Element: protocol<_SQLExpressible, BidirectionalIndexType> { /// Returns an SQL expression that checks the inclusion of a value in /// a range. /// /// See https://github.com/groue/GRDB.swift/#sql-operators - public func contains(element: _SQLDerivedExpressionType) -> _SQLExpression { + public func contains(element: _SpecificSQLExpressible) -> _SQLExpression { return .Between(value: element.sqlExpression, min: startIndex.sqlExpression, max: endIndex.predecessor().sqlExpression) } } -extension ClosedInterval where Bound: _SQLExpressionType { +extension ClosedInterval where Bound: _SQLExpressible { /// Returns an SQL expression that checks the inclusion of a value in /// an interval. /// /// See https://github.com/groue/GRDB.swift/#sql-operators - public func contains(element: _SQLDerivedExpressionType) -> _SQLExpression { + public func contains(element: _SpecificSQLExpressible) -> _SQLExpression { return .Between(value: element.sqlExpression, min: start.sqlExpression, max: end.sqlExpression) } } -extension HalfOpenInterval where Bound: _SQLExpressionType { +extension HalfOpenInterval where Bound: _SQLExpressible { /// Returns an SQL expression that checks the inclusion of a value in /// an interval. /// /// See https://github.com/groue/GRDB.swift/#sql-operators - public func contains(element: _SQLDerivedExpressionType) -> _SQLExpression { + public func contains(element: _SpecificSQLExpressible) -> _SQLExpression { return (element >= start) && (element < end) } } @@ -367,12 +367,12 @@ extension HalfOpenInterval where Bound: _SQLExpressionType { // MARK: - Operator IN -extension SequenceType where Self.Generator.Element: _SQLExpressionType { +extension SequenceType where Self.Generator.Element: _SQLExpressible { /// Returns an SQL expression that checks the inclusion of a value in /// a sequence. /// /// See https://github.com/groue/GRDB.swift/#sql-operators - public func contains(element: _SQLDerivedExpressionType) -> _SQLExpression { + public func contains(element: _SpecificSQLExpressible) -> _SQLExpression { return .In(map { $0.sqlExpression }, element.sqlExpression) } } @@ -383,21 +383,21 @@ extension SequenceType where Self.Generator.Element: _SQLExpressionType { /// Returns an SQL expression that compares two values. /// /// See https://github.com/groue/GRDB.swift/#sql-operators -public func === (lhs: _SQLDerivedExpressionType, rhs: _SQLExpressionType?) -> _SQLExpression { +public func === (lhs: _SpecificSQLExpressible, rhs: _SQLExpressible?) -> _SQLExpression { return .Is(lhs.sqlExpression, rhs?.sqlExpression ?? .Value(nil)) } /// Returns an SQL expression that compares two values. /// /// See https://github.com/groue/GRDB.swift/#sql-operators -public func === (lhs: _SQLExpressionType?, rhs: _SQLDerivedExpressionType) -> _SQLExpression { +public func === (lhs: _SQLExpressible?, rhs: _SpecificSQLExpressible) -> _SQLExpression { return .Is(lhs?.sqlExpression ?? .Value(nil), rhs.sqlExpression) } /// Returns an SQL expression that compares two values. /// /// See https://github.com/groue/GRDB.swift/#sql-operators -public func === (lhs: _SQLDerivedExpressionType, rhs: _SQLDerivedExpressionType) -> _SQLExpression { +public func === (lhs: _SpecificSQLExpressible, rhs: _SpecificSQLExpressible) -> _SQLExpression { return .Is(lhs.sqlExpression, rhs.sqlExpression) } @@ -407,21 +407,21 @@ public func === (lhs: _SQLDerivedExpressionType, rhs: _SQLDerivedExpressionType) /// Returns an SQL expression that compares two values. /// /// See https://github.com/groue/GRDB.swift/#sql-operators -public func !== (lhs: _SQLDerivedExpressionType, rhs: _SQLExpressionType?) -> _SQLExpression { +public func !== (lhs: _SpecificSQLExpressible, rhs: _SQLExpressible?) -> _SQLExpression { return .IsNot(lhs.sqlExpression, rhs?.sqlExpression ?? .Value(nil)) } /// Returns an SQL expression that compares two values. /// /// See https://github.com/groue/GRDB.swift/#sql-operators -public func !== (lhs: _SQLExpressionType?, rhs: _SQLDerivedExpressionType) -> _SQLExpression { +public func !== (lhs: _SQLExpressible?, rhs: _SpecificSQLExpressible) -> _SQLExpression { return .IsNot(lhs?.sqlExpression ?? .Value(nil), rhs.sqlExpression) } /// Returns an SQL expression that compares two values. /// /// See https://github.com/groue/GRDB.swift/#sql-operators -public func !== (lhs: _SQLDerivedExpressionType, rhs: _SQLDerivedExpressionType) -> _SQLExpression { +public func !== (lhs: _SpecificSQLExpressible, rhs: _SpecificSQLExpressible) -> _SQLExpression { return .IsNot(lhs.sqlExpression, rhs.sqlExpression) } @@ -431,21 +431,21 @@ public func !== (lhs: _SQLDerivedExpressionType, rhs: _SQLDerivedExpressionType) /// Returns an SQL logical expression. /// /// See https://github.com/groue/GRDB.swift/#sql-operators -public func || (lhs: _SQLDerivedExpressionType, rhs: _SQLExpressionType) -> _SQLExpression { +public func || (lhs: _SpecificSQLExpressible, rhs: _SQLExpressible) -> _SQLExpression { return .InfixOperator("OR", lhs.sqlExpression, rhs.sqlExpression) } /// Returns an SQL logical expression. /// /// See https://github.com/groue/GRDB.swift/#sql-operators -public func || (lhs: _SQLExpressionType, rhs: _SQLDerivedExpressionType) -> _SQLExpression { +public func || (lhs: _SQLExpressible, rhs: _SpecificSQLExpressible) -> _SQLExpression { return .InfixOperator("OR", lhs.sqlExpression, rhs.sqlExpression) } /// Returns an SQL logical expression. /// /// See https://github.com/groue/GRDB.swift/#sql-operators -public func || (lhs: _SQLDerivedExpressionType, rhs: _SQLDerivedExpressionType) -> _SQLExpression { +public func || (lhs: _SpecificSQLExpressible, rhs: _SpecificSQLExpressible) -> _SQLExpression { return .InfixOperator("OR", lhs.sqlExpression, rhs.sqlExpression) } @@ -455,6 +455,6 @@ public func || (lhs: _SQLDerivedExpressionType, rhs: _SQLDerivedExpressionType) /// Returns an SQL logical expression. /// /// See https://github.com/groue/GRDB.swift/#sql-operators -public prefix func ! (value: _SQLDerivedExpressionType) -> _SQLExpression { +public prefix func ! (value: _SpecificSQLExpressible) -> _SQLExpression { return .Not(value.sqlExpression) } diff --git a/GRDB/FetchRequest/SQLSelectQuery.swift b/GRDB/FetchRequest/SQLSelectQuery.swift index feff6babd..44e0046d4 100644 --- a/GRDB/FetchRequest/SQLSelectQuery.swift +++ b/GRDB/FetchRequest/SQLSelectQuery.swift @@ -12,7 +12,7 @@ public struct _SQLSelectQuery { var source: _SQLSource? var whereExpression: _SQLExpression? var groupByExpressions: [_SQLExpression] - var sortDescriptors: [_SQLSortDescriptorType] + var orderings: [_SQLOrdering] var reversed: Bool var havingExpression: _SQLExpression? var limit: _SQLLimit? @@ -23,7 +23,7 @@ public struct _SQLSelectQuery { from source: _SQLSource? = nil, filter whereExpression: _SQLExpression? = nil, groupBy groupByExpressions: [_SQLExpression] = [], - orderBy sortDescriptors: [_SQLSortDescriptorType] = [], + orderBy orderings: [_SQLOrdering] = [], reversed: Bool = false, having havingExpression: _SQLExpression? = nil, limit: _SQLLimit? = nil) @@ -33,7 +33,7 @@ public struct _SQLSelectQuery { self.source = source self.whereExpression = whereExpression self.groupByExpressions = groupByExpressions - self.sortDescriptors = sortDescriptors + self.orderings = orderings self.reversed = reversed self.havingExpression = havingExpression self.limit = limit @@ -65,9 +65,9 @@ public struct _SQLSelectQuery { sql += try " HAVING " + havingExpression.sql(db, &bindings) } - var sortDescriptors = self.sortDescriptors + var orderings = self.orderings if reversed { - if sortDescriptors.isEmpty { + if orderings.isEmpty { // https://www.sqlite.org/lang_createtable.html#rowid // // > The rowid value can be accessed using one of the special @@ -80,13 +80,13 @@ public struct _SQLSelectQuery { // Here we assume that _rowid_ is not a custom column. // TODO: support for user-defined _rowid_ column. // TODO: support for WITHOUT ROWID tables. - sortDescriptors = [SQLColumn("_rowid_").desc] + orderings = [SQLColumn("_rowid_").desc] } else { - sortDescriptors = sortDescriptors.map { $0.reversedSortDescriptor } + orderings = orderings.map { $0.reversedSortDescriptor } } } - if !sortDescriptors.isEmpty { - sql += try " ORDER BY " + sortDescriptors.map { try $0.orderingSQL(db, &bindings) }.joinWithSeparator(", ") + if !orderings.isEmpty { + sql += try " ORDER BY " + orderings.map { try $0.orderingSQL(db, &bindings) }.joinWithSeparator(", ") } if let limit = limit { @@ -177,7 +177,7 @@ public struct _SQLSelectQuery { private var unorderedQuery: _SQLSelectQuery { var query = self query.reversed = false - query.sortDescriptors = [] + query.orderings = [] return query } } @@ -208,13 +208,13 @@ indirect enum _SQLSource { } -// MARK: - _SQLSortDescriptorType +// MARK: - _SQLOrdering /// This protocol is an implementation detail of the query interface. /// Do not use it directly. /// /// See https://github.com/groue/GRDB.swift/#the-query-interface -public protocol _SQLSortDescriptorType { +public protocol _SQLOrdering { var reversedSortDescriptor: _SQLSortDescriptor { get } func orderingSQL(db: Database, inout _ bindings: [DatabaseValueConvertible?]) throws -> String } @@ -228,7 +228,7 @@ public enum _SQLSortDescriptor { case Desc(_SQLExpression) } -extension _SQLSortDescriptor : _SQLSortDescriptorType { +extension _SQLSortDescriptor : _SQLOrdering { /// This property is an implementation detail of the query interface. /// Do not use it directly. @@ -274,9 +274,9 @@ struct _SQLLimit { } -// MARK: - _SQLExpressionType +// MARK: - _SQLExpressible -public protocol _SQLExpressionType { +public protocol _SQLExpressible { /// This property is an implementation detail of the query interface. /// Do not use it directly. @@ -285,7 +285,7 @@ public protocol _SQLExpressionType { var sqlExpression: _SQLExpression { get } } -// Conformance to _SQLExpressionType +// Conformance to _SQLExpressible extension DatabaseValueConvertible { /// This property is an implementation detail of the query interface. @@ -301,11 +301,28 @@ extension DatabaseValueConvertible { /// Do not use it directly. /// /// See https://github.com/groue/GRDB.swift/#the-query-interface -public protocol _SQLDerivedExpressionType : _SQLExpressionType, _SQLSortDescriptorType, _SQLSelectable { +public protocol _SpecificSQLExpressible : _SQLExpressible { + // _SQLExpressible can be adopted by Swift standard types, and user + // types, through the DatabaseValueConvertible protocol, which inherits + // from _SQLExpressible. + // + // For example, Int adopts _SQLExpressible through + // DatabaseValueConvertible. + // + // _SpecificSQLExpressible, on the other side, is not adopted by any + // Swift standard type or any user type. It is only adopted by GRDB types, + // such as SQLColumn, _SQLExpression and _SQLLiteral. + // + // This separation lets us define functions and operators that do not + // spill out. The three declarations below have no chance overloading a + // Swift-defined operator, or a user-defined operator: + // + // - ==(_SQLExpressible, _SpecificSQLExpressible) + // - ==(_SpecificSQLExpressible, _SQLExpressible) + // - ==(_SpecificSQLExpressible, _SpecificSQLExpressible) } -// Conformance to _SQLSortDescriptorType -extension _SQLDerivedExpressionType { +extension _SpecificSQLExpressible where Self: _SQLOrdering { /// This property is an implementation detail of the query interface. /// Do not use it directly. @@ -324,8 +341,7 @@ extension _SQLDerivedExpressionType { } } -// Conformance to _SQLSelectable -extension _SQLDerivedExpressionType { +extension _SpecificSQLExpressible where Self: _SQLSelectable { /// This method is an implementation detail of the query interface. /// Do not use it directly. @@ -352,7 +368,7 @@ extension _SQLDerivedExpressionType { } } -extension _SQLDerivedExpressionType { +extension _SpecificSQLExpressible { /// Returns a value that can be used as an argument to FetchRequest.order() /// @@ -386,7 +402,7 @@ public indirect enum _SQLExpression { case Literal(String) /// For example: `1` or `'foo'` - case Value(DatabaseValueConvertible?) + case Value(DatabaseValueConvertible?) // TODO: switch to DatabaseValue? /// For example: `name`, `table.name` case Identifier(identifier: String, sourceName: String?) @@ -581,7 +597,7 @@ public indirect enum _SQLExpression { } } -extension _SQLExpression : _SQLDerivedExpressionType { +extension _SQLExpression : _SpecificSQLExpressible { /// This property is an implementation detail of the query interface. /// Do not use it directly. @@ -592,6 +608,9 @@ extension _SQLExpression : _SQLDerivedExpressionType { } } +extension _SQLExpression : _SQLSelectable {} +extension _SQLExpression : _SQLOrdering {} + // MARK: - _SQLSelectable @@ -663,12 +682,15 @@ struct _SQLLiteral { } } -extension _SQLLiteral : _SQLDerivedExpressionType { +extension _SQLLiteral : _SpecificSQLExpressible { var sqlExpression: _SQLExpression { return .Literal(sql) } } +extension _SQLLiteral : _SQLSelectable {} +extension _SQLLiteral : _SQLOrdering {} + // MARK: - SQLColumn @@ -693,7 +715,7 @@ public struct SQLColumn { } } -extension SQLColumn : _SQLDerivedExpressionType { +extension SQLColumn : _SpecificSQLExpressible { /// This property is an implementation detail of the query interface. /// Do not use it directly. @@ -704,3 +726,5 @@ extension SQLColumn : _SQLDerivedExpressionType { } } +extension SQLColumn : _SQLSelectable {} +extension SQLColumn : _SQLOrdering {} diff --git a/README.md b/README.md index 96bd31db1..ff55e3111 100644 --- a/README.md +++ b/README.md @@ -2153,7 +2153,7 @@ All the methods above return another request, which you can further refine by ap .having(min(ageColumn) >= 18) ``` -- `order(sortDescriptor, ...)` sorts. +- `order(ordering, ...)` sorts. ```swift // SELECT * FROM "persons" ORDER BY "name" @@ -2163,7 +2163,7 @@ All the methods above return another request, which you can further refine by ap Person.order(scoreColumn.desc, nameColumn) ``` -- `reverse()` reverses the eventual sort descriptors. +- `reverse()` reverses the eventual orderings. ```swift // SELECT * FROM "persons" ORDER BY "score" ASC, "name" DESC