diff --git a/GRDB/Core/Database.swift b/GRDB/Core/Database.swift index 9a8f4c8eb..0e0097db7 100644 --- a/GRDB/Core/Database.swift +++ b/GRDB/Core/Database.swift @@ -841,11 +841,14 @@ extension Database { self.rawValue = rawValue } + #if swift(>=4.1) + #else /// The hash value /// :nodoc: public var hashValue: Int { return rawValue.hashValue } + #endif /// The `BINARY` built-in SQL collation public static let binary = CollationName("BINARY") @@ -878,11 +881,14 @@ extension Database { self.rawValue = rawValue } + #if swift(>=4.1) + #else /// The hash value /// :nodoc: public var hashValue: Int { return rawValue.hashValue } + #endif /// The `TEXT` SQL column type public static let text = ColumnType("TEXT") diff --git a/GRDB/Core/DatabaseError.swift b/GRDB/Core/DatabaseError.swift index 6833c383f..4c6b39256 100644 --- a/GRDB/Core/DatabaseError.swift +++ b/GRDB/Core/DatabaseError.swift @@ -139,6 +139,8 @@ public struct ResultCode : RawRepresentable, Equatable, CustomStringConvertible public static let SQLITE_OK_LOAD_PERMANENTLY = ResultCode(rawValue: (SQLITE_OK.rawValue | (1<<8))) } +#if swift(>=4.1) +#else // Equatable extension ResultCode { /// :nodoc: @@ -146,6 +148,7 @@ extension ResultCode { return lhs.rawValue == rhs.rawValue } } +#endif // CustomStringConvertible extension ResultCode { diff --git a/GRDB/Core/DatabaseFunction.swift b/GRDB/Core/DatabaseFunction.swift index a0443c87d..702913cf7 100644 --- a/GRDB/Core/DatabaseFunction.swift +++ b/GRDB/Core/DatabaseFunction.swift @@ -5,12 +5,28 @@ #endif /// An SQL function or aggregate. -public final class DatabaseFunction { - public let name: String - let argumentCount: Int32? +public final class DatabaseFunction: Hashable { + // SQLite identifies functions by (name + argument count) + private struct Identity: Hashable { + let name: String + let nArg: Int32 // -1 for variadic functions + + #if swift(>=4.1) + #else + var hashValue: Int { + return name.hashValue ^ nArg.hashValue + } + + static func == (lhs: Identity, rhs: Identity) -> Bool { + return lhs.name == rhs.name && lhs.nArg == rhs.nArg + } + #endif + } + + public var name: String { return identity.name } + private let identity: Identity let pure: Bool private let kind: Kind - private var nArg: Int32 { return argumentCount ?? -1 } private var eTextRep: Int32 { return (SQLITE_UTF8 | (pure ? SQLITE_DETERMINISTIC : 0)) } /// Returns an SQL function. @@ -38,8 +54,7 @@ public final class DatabaseFunction { /// exactly *argumentCount* elements, provided *argumentCount* is /// not nil. public init(_ name: String, argumentCount: Int32? = nil, pure: Bool = false, function: @escaping ([DatabaseValue]) throws -> DatabaseValueConvertible?) { - self.name = name - self.argumentCount = argumentCount + self.identity = Identity(name: name, nArg: argumentCount ?? -1) self.pure = pure self.kind = .function{ (argc, argv) in let arguments = (0..(_ name: String, argumentCount: Int32? = nil, pure: Bool = false, aggregate: Aggregate.Type) { - self.name = name - self.argumentCount = argumentCount + self.identity = Identity(name: name, nArg: argumentCount ?? -1) self.pure = pure self.kind = .aggregate { return Aggregate() } } @@ -104,8 +118,8 @@ public final class DatabaseFunction { let code = sqlite3_create_function_v2( db.sqliteConnection, - name, - nArg, + identity.name, + identity.nArg, eTextRep, definitionP, kind.xFunc, @@ -127,8 +141,8 @@ public final class DatabaseFunction { func uninstall(in db: Database) { let code = sqlite3_create_function_v2( db.sqliteConnection, - name, - nArg, + identity.name, + identity.nArg, eTextRep, nil, nil, nil, nil, nil) @@ -309,17 +323,17 @@ public final class DatabaseFunction { } } -extension DatabaseFunction : Hashable { +extension DatabaseFunction { /// The hash value /// :nodoc: public var hashValue: Int { - return name.hashValue ^ nArg.hashValue + return identity.hashValue } /// Two functions are equal if they share the same name and arity. /// :nodoc: public static func == (lhs: DatabaseFunction, rhs: DatabaseFunction) -> Bool { - return lhs.name == rhs.name && lhs.nArg == rhs.nArg + return lhs.identity == rhs.identity } } diff --git a/GRDB/Core/DatabaseRegion.swift b/GRDB/Core/DatabaseRegion.swift index 52f67a6fb..8d4b140ad 100644 --- a/GRDB/Core/DatabaseRegion.swift +++ b/GRDB/Core/DatabaseRegion.swift @@ -255,11 +255,14 @@ private struct TableRegion: Equatable { return false } + #if swift(>=4.1) + #else static func == (lhs: TableRegion, rhs: TableRegion) -> Bool { if lhs.columns != rhs.columns { return false } if lhs.rowIds != rhs.rowIds { return false } return true } + #endif func intersection(_ other: TableRegion) -> TableRegion { let columnsIntersection: Set? diff --git a/GRDB/Core/Statement.swift b/GRDB/Core/Statement.swift index f29a1536f..dbbcf86e8 100644 --- a/GRDB/Core/Statement.swift +++ b/GRDB/Core/Statement.swift @@ -911,6 +911,8 @@ extension StatementArguments { } } +#if swift(>=4.1) +#else // Equatable extension StatementArguments { /// :nodoc: @@ -920,6 +922,7 @@ extension StatementArguments { return true } } +#endif /// A thread-unsafe statement cache struct StatementCache { diff --git a/GRDB/QueryInterface/SQLExpression+QueryInterface.swift b/GRDB/QueryInterface/SQLExpression+QueryInterface.swift index 786ec9a5d..825774fde 100644 --- a/GRDB/QueryInterface/SQLExpression+QueryInterface.swift +++ b/GRDB/QueryInterface/SQLExpression+QueryInterface.swift @@ -486,6 +486,8 @@ public struct SQLFunctionName : Hashable { self.sql = sql } + #if swift(>=4.1) + #else /// The hash value /// /// :nodoc: @@ -499,6 +501,7 @@ public struct SQLFunctionName : Hashable { public static func == (lhs: SQLFunctionName, rhs: SQLFunctionName) -> Bool { return lhs.sql == rhs.sql } + #endif } /// [**Experimental**](http://github.com/groue/GRDB.swift#what-are-experimental-features) diff --git a/GRDB/Record/FetchedRecordsController.swift b/GRDB/Record/FetchedRecordsController.swift index 03dd53490..9d23f784b 100644 --- a/GRDB/Record/FetchedRecordsController.swift +++ b/GRDB/Record/FetchedRecordsController.swift @@ -1050,8 +1050,9 @@ private final class Item : RowConvertible, Equatable { init(row: Row) { self.row = row.copy() } + + static func == (lhs: Item, rhs: Item) -> Bool { + return lhs.row == rhs.row + } } -private func == (lhs: Item, rhs: Item) -> Bool { - return lhs.row == rhs.row -} diff --git a/GRDB/Record/Persistable.swift b/GRDB/Record/Persistable.swift index 002e78175..824617966 100644 --- a/GRDB/Record/Persistable.swift +++ b/GRDB/Record/Persistable.swift @@ -977,20 +977,22 @@ final class DAO { // MARK: - InsertQuery -private struct InsertQuery { +private struct InsertQuery: Hashable { let onConflict: Database.ConflictResolution let tableName: String let insertedColumns: [String] -} - -extension InsertQuery : Hashable { + + // Not generated by Swift 4.1 var hashValue: Int { return tableName.hashValue } + #if swift(>=4.1) + #else static func == (lhs: InsertQuery, rhs: InsertQuery) -> Bool { if lhs.tableName != rhs.tableName { return false } if lhs.onConflict != rhs.onConflict { return false } return lhs.insertedColumns == rhs.insertedColumns } + #endif } extension InsertQuery { @@ -1016,22 +1018,24 @@ extension InsertQuery { // MARK: - UpdateQuery -private struct UpdateQuery { +private struct UpdateQuery: Hashable { let onConflict: Database.ConflictResolution let tableName: String let updatedColumns: [String] let conditionColumns: [String] -} - -extension UpdateQuery : Hashable { + + // Not generated by Swift 4.1 var hashValue: Int { return tableName.hashValue } + #if swift(>=4.1) + #else static func == (lhs: UpdateQuery, rhs: UpdateQuery) -> Bool { if lhs.tableName != rhs.tableName { return false } if lhs.onConflict != rhs.onConflict { return false } if lhs.updatedColumns != rhs.updatedColumns { return false } return lhs.conditionColumns == rhs.conditionColumns } + #endif } extension UpdateQuery {