diff --git a/GRDB/Core/DatabaseValueConvertible.swift b/GRDB/Core/DatabaseValueConvertible.swift index 5421fbdf9..6125e4660 100644 --- a/GRDB/Core/DatabaseValueConvertible.swift +++ b/GRDB/Core/DatabaseValueConvertible.swift @@ -1,3 +1,32 @@ +// MARK: - SQLExpressible + +/// 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 _SQLExpressible { + + /// This property is an implementation detail of the query interface. + /// Do not use it directly. + /// + /// See https://github.com/groue/GRDB.swift/#the-query-interface + var sqlExpression: _SQLExpression { get } +} + +/// The protocol for all types that can be turned into an SQL expression. +/// +/// It is adopted by protocols like DatabaseValueConvertible, and types +/// like Column. +/// +/// Do not declare adoption of SQLExpressible on any type: you would have to +/// use semi-private APIs and types, whose name start with an underscore, that +/// are subject to change as GRDB evolves. +public protocol SQLExpressible : _SQLExpressible { +} + + +// MARK: - DatabaseValueConvertible + /// Types that adopt DatabaseValueConvertible can be initialized from /// database values. /// @@ -14,7 +43,7 @@ /// String.fetchOne(statement, arguments:...) // String? /// /// DatabaseValueConvertible is adopted by Bool, Int, String, etc. -public protocol DatabaseValueConvertible : _SQLExpressible { +public protocol DatabaseValueConvertible : SQLExpressible { /// Returns a value that can be stored in the database. var databaseValue: DatabaseValue { get } @@ -23,6 +52,19 @@ public protocol DatabaseValueConvertible : _SQLExpressible { } +// SQLExpressible adoption +extension DatabaseValueConvertible { + + /// This property is an implementation detail of the query interface. + /// Do not use it directly. + /// + /// See https://github.com/groue/GRDB.swift/#the-query-interface + public var sqlExpression: _SQLExpression { + return .value(self) + } +} + + /// DatabaseValueConvertible comes with built-in methods that allow to fetch /// sequences, arrays, or single values: /// diff --git a/GRDB/QueryInterface/QueryInterfaceRequest.swift b/GRDB/QueryInterface/QueryInterfaceRequest.swift index 8b16cd031..53aad647b 100644 --- a/GRDB/QueryInterface/QueryInterfaceRequest.swift +++ b/GRDB/QueryInterface/QueryInterfaceRequest.swift @@ -112,7 +112,7 @@ extension QueryInterfaceRequest { /// Returns a new QueryInterfaceRequest with the provided *predicate* added to the /// eventual set of already applied predicates. - public func filter(_ predicate: _SQLExpressible) -> QueryInterfaceRequest { + public func filter(_ predicate: SQLExpressible) -> QueryInterfaceRequest { var query = self.query if let whereExpression = query.whereExpression { query.whereExpression = .infixOperator("AND", whereExpression, predicate.sqlExpression) @@ -129,12 +129,12 @@ extension QueryInterfaceRequest { } /// Returns a new QueryInterfaceRequest grouped according to *expressions*. - public func group(_ expressions: _SQLExpressible...) -> QueryInterfaceRequest { + public func group(_ expressions: SQLExpressible...) -> QueryInterfaceRequest { return group(expressions) } /// Returns a new QueryInterfaceRequest grouped according to *expressions*. - public func group(_ expressions: [_SQLExpressible]) -> QueryInterfaceRequest { + public func group(_ expressions: [SQLExpressible]) -> QueryInterfaceRequest { var query = self.query query.groupByExpressions = expressions.map { $0.sqlExpression } return QueryInterfaceRequest(query: query) @@ -147,7 +147,7 @@ extension QueryInterfaceRequest { /// Returns a new QueryInterfaceRequest with the provided *predicate* added to the /// eventual set of already applied predicates. - public func having(_ predicate: _SQLExpressible) -> QueryInterfaceRequest { + public func having(_ predicate: SQLExpressible) -> QueryInterfaceRequest { var query = self.query if let havingExpression = query.havingExpression { query.havingExpression = (havingExpression && predicate).sqlExpression @@ -219,7 +219,7 @@ extension QueryInterfaceRequest { /// Returns an SQL expression that checks the inclusion of a value in /// the results of another request. - public func contains(_ element: _SQLExpressible) -> _SQLExpression { + public func contains(_ element: SQLExpressible) -> _SQLExpression { return .inSubQuery(query, element.sqlExpression) } @@ -256,7 +256,7 @@ extension TableMapping { } /// Returns a QueryInterfaceRequest with the provided *predicate*. - public static func filter(_ predicate: _SQLExpressible) -> QueryInterfaceRequest { + public static func filter(_ predicate: SQLExpressible) -> QueryInterfaceRequest { return all().filter(predicate) } diff --git a/GRDB/QueryInterface/SQLCollation.swift b/GRDB/QueryInterface/SQLCollation.swift index fb3450961..59c135201 100644 --- a/GRDB/QueryInterface/SQLCollation.swift +++ b/GRDB/QueryInterface/SQLCollation.swift @@ -7,7 +7,7 @@ public struct _SQLCollatedExpression { let collationName: String } -extension _SQLCollatedExpression : _SQLExpressible { +extension _SQLCollatedExpression : SQLExpressible { /// This property is an implementation detail of the query interface. /// Do not use it directly. @@ -80,14 +80,14 @@ extension _SpecificSQLExpressible { /// Returns an SQL expression that compares two values. /// /// See https://github.com/groue/GRDB.swift/#sql-operators -public func == (lhs: _SQLCollatedExpression, rhs: _SQLExpressible?) -> _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: _SQLExpressible?, rhs: _SQLCollatedExpression) -> _SQLExpression { +public func == (lhs: SQLExpressible?, rhs: _SQLCollatedExpression) -> _SQLExpression { return .collate(lhs == rhs.baseExpression, rhs.collationName) } @@ -97,14 +97,14 @@ public func == (lhs: _SQLExpressible?, rhs: _SQLCollatedExpression) -> _SQLExpre /// Returns an SQL expression that compares two values. /// /// See https://github.com/groue/GRDB.swift/#sql-operators -public func != (lhs: _SQLCollatedExpression, rhs: _SQLExpressible?) -> _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: _SQLExpressible?, rhs: _SQLCollatedExpression) -> _SQLExpression { +public func != (lhs: SQLExpressible?, rhs: _SQLCollatedExpression) -> _SQLExpression { return .collate(lhs != rhs.baseExpression, rhs.collationName) } @@ -114,14 +114,14 @@ public func != (lhs: _SQLExpressible?, rhs: _SQLCollatedExpression) -> _SQLExpre /// Returns an SQL expression that compares two values. /// /// See https://github.com/groue/GRDB.swift/#sql-operators -public func < (lhs: _SQLCollatedExpression, rhs: _SQLExpressible) -> _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: _SQLExpressible, rhs: _SQLCollatedExpression) -> _SQLExpression { +public func < (lhs: SQLExpressible, rhs: _SQLCollatedExpression) -> _SQLExpression { return .collate(lhs < rhs.baseExpression, rhs.collationName) } @@ -131,14 +131,14 @@ public func < (lhs: _SQLExpressible, rhs: _SQLCollatedExpression) -> _SQLExpress /// Returns an SQL expression that compares two values. /// /// See https://github.com/groue/GRDB.swift/#sql-operators -public func <= (lhs: _SQLCollatedExpression, rhs: _SQLExpressible) -> _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: _SQLExpressible, rhs: _SQLCollatedExpression) -> _SQLExpression { +public func <= (lhs: SQLExpressible, rhs: _SQLCollatedExpression) -> _SQLExpression { return .collate(lhs <= rhs.baseExpression, rhs.collationName) } @@ -148,14 +148,14 @@ public func <= (lhs: _SQLExpressible, rhs: _SQLCollatedExpression) -> _SQLExpres /// Returns an SQL expression that compares two values. /// /// See https://github.com/groue/GRDB.swift/#sql-operators -public func > (lhs: _SQLCollatedExpression, rhs: _SQLExpressible) -> _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: _SQLExpressible, rhs: _SQLCollatedExpression) -> _SQLExpression { +public func > (lhs: SQLExpressible, rhs: _SQLCollatedExpression) -> _SQLExpression { return .collate(lhs > rhs.baseExpression, rhs.collationName) } @@ -165,21 +165,21 @@ public func > (lhs: _SQLExpressible, rhs: _SQLCollatedExpression) -> _SQLExpress /// Returns an SQL expression that compares two values. /// /// See https://github.com/groue/GRDB.swift/#sql-operators -public func >= (lhs: _SQLCollatedExpression, rhs: _SQLExpressible) -> _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: _SQLExpressible, rhs: _SQLCollatedExpression) -> _SQLExpression { +public func >= (lhs: SQLExpressible, rhs: _SQLCollatedExpression) -> _SQLExpression { return .collate(lhs >= rhs.baseExpression, rhs.collationName) } // MARK: - Operator BETWEEN COLLATE -extension Range where Bound: _SQLExpressible { +extension Range where Bound: SQLExpressible { /// Returns an SQL expression that compares the inclusion of a value in /// a range. /// @@ -189,7 +189,7 @@ extension Range where Bound: _SQLExpressible { } } -extension ClosedRange where Bound: _SQLExpressible { +extension ClosedRange where Bound: SQLExpressible { /// Returns an SQL expression that compares the inclusion of a value in /// a range. /// @@ -202,7 +202,7 @@ extension ClosedRange where Bound: _SQLExpressible { // MARK: - Operator IN COLLATE -extension Sequence where Self.Iterator.Element: _SQLExpressible { +extension Sequence where Self.Iterator.Element: SQLExpressible { /// Returns an SQL expression that compares the inclusion of a value in /// a sequence. /// @@ -218,14 +218,14 @@ extension Sequence where Self.Iterator.Element: _SQLExpressible { /// Returns an SQL expression that compares two values. /// /// See https://github.com/groue/GRDB.swift/#sql-operators -public func === (lhs: _SQLCollatedExpression, rhs: _SQLExpressible?) -> _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: _SQLExpressible?, rhs: _SQLCollatedExpression) -> _SQLExpression { +public func === (lhs: SQLExpressible?, rhs: _SQLCollatedExpression) -> _SQLExpression { return .collate(lhs == rhs.baseExpression, rhs.collationName) } @@ -235,13 +235,13 @@ public func === (lhs: _SQLExpressible?, 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: _SQLExpressible?) -> _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: _SQLExpressible?, rhs: _SQLCollatedExpression) -> _SQLExpression { +public func !== (lhs: SQLExpressible?, rhs: _SQLCollatedExpression) -> _SQLExpression { return .collate(lhs !== rhs.baseExpression, rhs.collationName) } diff --git a/GRDB/QueryInterface/SQLFunction.swift b/GRDB/QueryInterface/SQLFunction.swift index 3453a246b..e17fc8b14 100644 --- a/GRDB/QueryInterface/SQLFunction.swift +++ b/GRDB/QueryInterface/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: _SQLExpressible...) -> _SQLExpression { + public func apply(_ arguments: SQLExpressible...) -> _SQLExpression { return .function(name, arguments.map { $0.sqlExpression }) } } @@ -55,14 +55,14 @@ public func count(distinct value: _SpecificSQLExpressible) -> _SQLExpression { /// Returns an SQL expression. /// /// See https://github.com/groue/GRDB.swift/#sql-functions -public func ?? (lhs: _SpecificSQLExpressible, rhs: _SQLExpressible) -> _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: _SQLExpressible?, rhs: _SpecificSQLExpressible) -> _SQLExpression { +public func ?? (lhs: SQLExpressible?, rhs: _SpecificSQLExpressible) -> _SQLExpression { if let lhs = lhs { return .function("IFNULL", [lhs.sqlExpression, rhs.sqlExpression]) } else { diff --git a/GRDB/QueryInterface/SQLOperator.swift b/GRDB/QueryInterface/SQLOperator.swift index 36df94013..ac4c441ac 100644 --- a/GRDB/QueryInterface/SQLOperator.swift +++ b/GRDB/QueryInterface/SQLOperator.swift @@ -3,7 +3,7 @@ /// Returns an SQL expression that compares two values. /// /// See https://github.com/groue/GRDB.swift/#sql-operators -public func == (lhs: _SpecificSQLExpressible, rhs: _SQLExpressible?) -> _SQLExpression { +public func == (lhs: _SpecificSQLExpressible, rhs: SQLExpressible?) -> _SQLExpression { return .equalOperator(lhs.sqlExpression, rhs?.sqlExpression ?? .value(nil)) } @@ -25,7 +25,7 @@ public func == (lhs: _SpecificSQLExpressible, rhs: Bool?) -> _SQLExpression { /// Returns an SQL expression that compares two values. /// /// See https://github.com/groue/GRDB.swift/#sql-operators -public func == (lhs: _SQLExpressible?, rhs: _SpecificSQLExpressible) -> _SQLExpression { +public func == (lhs: SQLExpressible?, rhs: _SpecificSQLExpressible) -> _SQLExpression { return .equalOperator(lhs?.sqlExpression ?? .value(nil), rhs.sqlExpression) } @@ -57,7 +57,7 @@ public func == (lhs: _SpecificSQLExpressible, rhs: _SpecificSQLExpressible) -> _ /// Returns an SQL expression that compares two values. /// /// See https://github.com/groue/GRDB.swift/#sql-operators -public func != (lhs: _SpecificSQLExpressible, rhs: _SQLExpressible?) -> _SQLExpression { +public func != (lhs: _SpecificSQLExpressible, rhs: SQLExpressible?) -> _SQLExpression { return .notEqualOperator(lhs.sqlExpression, rhs?.sqlExpression ?? .value(nil)) } @@ -79,7 +79,7 @@ public func != (lhs: _SpecificSQLExpressible, rhs: Bool?) -> _SQLExpression { /// Returns an SQL expression that compares two values. /// /// See https://github.com/groue/GRDB.swift/#sql-operators -public func != (lhs: _SQLExpressible?, rhs: _SpecificSQLExpressible) -> _SQLExpression { +public func != (lhs: SQLExpressible?, rhs: _SpecificSQLExpressible) -> _SQLExpression { return .notEqualOperator(lhs?.sqlExpression ?? .value(nil), rhs.sqlExpression) } @@ -111,14 +111,14 @@ public func != (lhs: _SpecificSQLExpressible, rhs: _SpecificSQLExpressible) -> _ /// Returns an SQL expression that compares two values. /// /// See https://github.com/groue/GRDB.swift/#sql-operators -public func < (lhs: _SpecificSQLExpressible, rhs: _SQLExpressible) -> _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: _SQLExpressible, rhs: _SpecificSQLExpressible) -> _SQLExpression { +public func < (lhs: SQLExpressible, rhs: _SpecificSQLExpressible) -> _SQLExpression { return .infixOperator("<", lhs.sqlExpression, rhs.sqlExpression) } @@ -135,14 +135,14 @@ public func < (lhs: _SpecificSQLExpressible, rhs: _SpecificSQLExpressible) -> _S /// Returns an SQL expression that compares two values. /// /// See https://github.com/groue/GRDB.swift/#sql-operators -public func <= (lhs: _SpecificSQLExpressible, rhs: _SQLExpressible) -> _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: _SQLExpressible, rhs: _SpecificSQLExpressible) -> _SQLExpression { +public func <= (lhs: SQLExpressible, rhs: _SpecificSQLExpressible) -> _SQLExpression { return .infixOperator("<=", lhs.sqlExpression, rhs.sqlExpression) } @@ -159,14 +159,14 @@ public func <= (lhs: _SpecificSQLExpressible, rhs: _SpecificSQLExpressible) -> _ /// Returns an SQL expression that compares two values. /// /// See https://github.com/groue/GRDB.swift/#sql-operators -public func > (lhs: _SpecificSQLExpressible, rhs: _SQLExpressible) -> _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: _SQLExpressible, rhs: _SpecificSQLExpressible) -> _SQLExpression { +public func > (lhs: SQLExpressible, rhs: _SpecificSQLExpressible) -> _SQLExpression { return .infixOperator(">", lhs.sqlExpression, rhs.sqlExpression) } @@ -183,14 +183,14 @@ public func > (lhs: _SpecificSQLExpressible, rhs: _SpecificSQLExpressible) -> _S /// Returns an SQL expression that compares two values. /// /// See https://github.com/groue/GRDB.swift/#sql-operators -public func >= (lhs: _SpecificSQLExpressible, rhs: _SQLExpressible) -> _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: _SQLExpressible, rhs: _SpecificSQLExpressible) -> _SQLExpression { +public func >= (lhs: SQLExpressible, rhs: _SpecificSQLExpressible) -> _SQLExpression { return .infixOperator(">=", lhs.sqlExpression, rhs.sqlExpression) } @@ -207,14 +207,14 @@ public func >= (lhs: _SpecificSQLExpressible, rhs: _SpecificSQLExpressible) -> _ /// Returns an SQL arithmetic expression. /// /// See https://github.com/groue/GRDB.swift/#sql-operators -public func * (lhs: _SpecificSQLExpressible, rhs: _SQLExpressible) -> _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: _SQLExpressible, rhs: _SpecificSQLExpressible) -> _SQLExpression { +public func * (lhs: SQLExpressible, rhs: _SpecificSQLExpressible) -> _SQLExpression { return .infixOperator("*", lhs.sqlExpression, rhs.sqlExpression) } @@ -231,14 +231,14 @@ public func * (lhs: _SpecificSQLExpressible, rhs: _SpecificSQLExpressible) -> _S /// Returns an SQL arithmetic expression. /// /// See https://github.com/groue/GRDB.swift/#sql-operators -public func / (lhs: _SpecificSQLExpressible, rhs: _SQLExpressible) -> _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: _SQLExpressible, rhs: _SpecificSQLExpressible) -> _SQLExpression { +public func / (lhs: SQLExpressible, rhs: _SpecificSQLExpressible) -> _SQLExpression { return .infixOperator("/", lhs.sqlExpression, rhs.sqlExpression) } @@ -255,14 +255,14 @@ public func / (lhs: _SpecificSQLExpressible, rhs: _SpecificSQLExpressible) -> _S /// Returns an SQL arithmetic expression. /// /// See https://github.com/groue/GRDB.swift/#sql-operators -public func + (lhs: _SpecificSQLExpressible, rhs: _SQLExpressible) -> _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: _SQLExpressible, rhs: _SpecificSQLExpressible) -> _SQLExpression { +public func + (lhs: SQLExpressible, rhs: _SpecificSQLExpressible) -> _SQLExpression { return .infixOperator("+", lhs.sqlExpression, rhs.sqlExpression) } @@ -289,14 +289,14 @@ public prefix func - (value: _SpecificSQLExpressible) -> _SQLExpression { /// Returns an SQL arithmetic expression. /// /// See https://github.com/groue/GRDB.swift/#sql-operators -public func - (lhs: _SpecificSQLExpressible, rhs: _SQLExpressible) -> _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: _SQLExpressible, rhs: _SpecificSQLExpressible) -> _SQLExpression { +public func - (lhs: SQLExpressible, rhs: _SpecificSQLExpressible) -> _SQLExpression { return .infixOperator("-", lhs.sqlExpression, rhs.sqlExpression) } @@ -313,14 +313,14 @@ public func - (lhs: _SpecificSQLExpressible, rhs: _SpecificSQLExpressible) -> _S /// Returns an SQL logical expression. /// /// See https://github.com/groue/GRDB.swift/#sql-operators -public func && (lhs: _SpecificSQLExpressible, rhs: _SQLExpressible) -> _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: _SQLExpressible, rhs: _SpecificSQLExpressible) -> _SQLExpression { +public func && (lhs: SQLExpressible, rhs: _SpecificSQLExpressible) -> _SQLExpression { return .infixOperator("AND", lhs.sqlExpression, rhs.sqlExpression) } @@ -334,8 +334,8 @@ public func && (lhs: _SpecificSQLExpressible, rhs: _SpecificSQLExpressible) -> _ // MARK: - Operator BETWEEN -extension Range where Bound: _SQLExpressible { - /// Returns an SQL expression that compares the inclusion of a value in +extension Range where Bound: SQLExpressible { + /// Returns an SQL expression that checks the inclusion of a value in /// a range. /// /// See https://github.com/groue/GRDB.swift/#sql-operators @@ -344,9 +344,9 @@ extension Range where Bound: _SQLExpressible { } } -extension ClosedRange where Bound: _SQLExpressible { - /// Returns an SQL expression that compares the inclusion of a value in - /// a range. +extension ClosedRange where Bound: SQLExpressible { + /// Returns an SQL expression that checks the inclusion of a value in + /// an range. /// /// See https://github.com/groue/GRDB.swift/#sql-operators public func contains(_ element: _SpecificSQLExpressible) -> _SQLExpression { @@ -354,8 +354,8 @@ extension ClosedRange where Bound: _SQLExpressible { } } -extension CountableRange where Bound: _SQLExpressible { - /// Returns an SQL expression that compares the inclusion of a value in +extension CountableRange where Bound: SQLExpressible { + /// Returns an SQL expression that checks the inclusion of a value in /// a range. /// /// See https://github.com/groue/GRDB.swift/#sql-operators @@ -364,8 +364,8 @@ extension CountableRange where Bound: _SQLExpressible { } } -extension CountableClosedRange where Bound: _SQLExpressible { - /// Returns an SQL expression that compares the inclusion of a value in +extension CountableClosedRange where Bound: SQLExpressible { + /// Returns an SQL expression that checks the inclusion of a value in /// a range. /// /// See https://github.com/groue/GRDB.swift/#sql-operators @@ -377,7 +377,7 @@ extension CountableClosedRange where Bound: _SQLExpressible { // MARK: - Operator IN -extension Sequence where Self.Iterator.Element: _SQLExpressible { +extension Sequence where Self.Iterator.Element: SQLExpressible { /// Returns an SQL expression that checks the inclusion of a value in /// a sequence. /// @@ -393,14 +393,14 @@ extension Sequence where Self.Iterator.Element: _SQLExpressible { /// Returns an SQL expression that compares two values. /// /// See https://github.com/groue/GRDB.swift/#sql-operators -public func === (lhs: _SpecificSQLExpressible, rhs: _SQLExpressible?) -> _SQLExpression { +public func === (lhs: _SpecificSQLExpressible, rhs: SQLExpressible?) -> _SQLExpression { return .isOperator(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: _SQLExpressible?, rhs: _SpecificSQLExpressible) -> _SQLExpression { +public func === (lhs: SQLExpressible?, rhs: _SpecificSQLExpressible) -> _SQLExpression { return .isOperator(lhs?.sqlExpression ?? .value(nil), rhs.sqlExpression) } @@ -417,14 +417,14 @@ public func === (lhs: _SpecificSQLExpressible, rhs: _SpecificSQLExpressible) -> /// Returns an SQL expression that compares two values. /// /// See https://github.com/groue/GRDB.swift/#sql-operators -public func !== (lhs: _SpecificSQLExpressible, rhs: _SQLExpressible?) -> _SQLExpression { +public func !== (lhs: _SpecificSQLExpressible, rhs: SQLExpressible?) -> _SQLExpression { return .isNotOperator(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: _SQLExpressible?, rhs: _SpecificSQLExpressible) -> _SQLExpression { +public func !== (lhs: SQLExpressible?, rhs: _SpecificSQLExpressible) -> _SQLExpression { return .isNotOperator(lhs?.sqlExpression ?? .value(nil), rhs.sqlExpression) } @@ -441,14 +441,14 @@ public func !== (lhs: _SpecificSQLExpressible, rhs: _SpecificSQLExpressible) -> /// Returns an SQL logical expression. /// /// See https://github.com/groue/GRDB.swift/#sql-operators -public func || (lhs: _SpecificSQLExpressible, rhs: _SQLExpressible) -> _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: _SQLExpressible, rhs: _SpecificSQLExpressible) -> _SQLExpression { +public func || (lhs: SQLExpressible, rhs: _SpecificSQLExpressible) -> _SQLExpression { return .infixOperator("OR", lhs.sqlExpression, rhs.sqlExpression) } diff --git a/GRDB/QueryInterface/SQLSelectQuery.swift b/GRDB/QueryInterface/SQLSelectQuery.swift index 40946f7ea..bacc9d00e 100644 --- a/GRDB/QueryInterface/SQLSelectQuery.swift +++ b/GRDB/QueryInterface/SQLSelectQuery.swift @@ -285,39 +285,18 @@ struct _SQLLimit { } -// MARK: - _SQLExpressible - -public protocol _SQLExpressible { - - /// This property is an implementation detail of the query interface. - /// Do not use it directly. - /// - /// See https://github.com/groue/GRDB.swift/#the-query-interface - var sqlExpression: _SQLExpression { get } -} - -// Conformance to _SQLExpressible -extension DatabaseValueConvertible { - - /// This property is an implementation detail of the query interface. - /// Do not use it directly. - /// - /// See https://github.com/groue/GRDB.swift/#the-query-interface - public var sqlExpression: _SQLExpression { - return .value(self) - } -} +// MARK: - _SpecificSQLExpressible /// 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 _SpecificSQLExpressible : _SQLExpressible { - // _SQLExpressible can be adopted by Swift standard types, and user - // types, through the DatabaseValueConvertible protocol, which inherits - // from _SQLExpressible. +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 + // For example, Int adopts SQLExpressible through // DatabaseValueConvertible. // // _SpecificSQLExpressible, on the other side, is not adopted by any @@ -328,8 +307,8 @@ public protocol _SpecificSQLExpressible : _SQLExpressible { // spill out. The three declarations below have no chance overloading a // Swift-defined operator, or a user-defined operator: // - // - ==(_SQLExpressible, _SpecificSQLExpressible) - // - ==(_SpecificSQLExpressible, _SQLExpressible) + // - ==(SQLExpressible, _SpecificSQLExpressible) + // - ==(_SpecificSQLExpressible, SQLExpressible) // - ==(_SpecificSQLExpressible, _SpecificSQLExpressible) } diff --git a/GRDB/QueryInterface/SQLTableBuilder.swift b/GRDB/QueryInterface/SQLTableBuilder.swift index 649cec883..359041ccc 100644 --- a/GRDB/QueryInterface/SQLTableBuilder.swift +++ b/GRDB/QueryInterface/SQLTableBuilder.swift @@ -114,7 +114,7 @@ extension Database { /// - ifNotExists: If false, no error is thrown if index already exists. /// - condition: If not nil, creates a partial index /// (see https://www.sqlite.org/partialindex.html). - public func create(index name: String, on table: String, columns: [String], unique: Bool = false, ifNotExists: Bool = false, condition: _SQLExpressible? = nil) throws { + public func create(index name: String, on table: String, columns: [String], unique: Bool = false, ifNotExists: Bool = false, condition: SQLExpressible? = nil) throws { let definition = IndexDefinition(name: name, table: table, columns: columns, unique: unique, ifNotExists: ifNotExists, condition: condition?.sqlExpression) let sql = definition.sql() try execute(sql) @@ -254,7 +254,7 @@ public final class TableDefinition { /// See https://www.sqlite.org/lang_createtable.html#ckconst /// /// - parameter condition: The checked condition - public func check(_ condition: _SQLExpressible) { + public func check(_ condition: SQLExpressible) { checkConstraints.append(condition.sqlExpression) } @@ -500,7 +500,7 @@ public final class ColumnDefinition { /// - parameter condition: A closure whose argument is an Column that /// represents the defined column, and returns the expression to check. /// - returns: Self so that you can further refine the column definition. - @discardableResult public func check(_ condition: @noescape (Column) -> _SQLExpressible) -> Self { + @discardableResult public func check(_ condition: @noescape (Column) -> SQLExpressible) -> Self { checkExpression = condition(Column(name)).sqlExpression return self }