Skip to content

Commit

Permalink
SE-0206
Browse files Browse the repository at this point in the history
  • Loading branch information
groue committed Aug 28, 2018
1 parent d43d998 commit c0c97e8
Show file tree
Hide file tree
Showing 8 changed files with 73 additions and 34 deletions.
9 changes: 5 additions & 4 deletions GRDB/Core/Database+Schema.swift
Original file line number Diff line number Diff line change
Expand Up @@ -577,9 +577,10 @@ struct SchemaInfo: Equatable {
var name: String
var tbl_name: String?

// TODO: remove when Hashable conformance is synthesized
var hashValue: Int {
return type.hashValue ^ name.hashValue ^ (tbl_name?.hashValue ?? 0)
}
#if !swift(>=4.2)
var hashValue: Int {
return type.hashValue ^ name.hashValue ^ (tbl_name?.hashValue ?? 0)
}
#endif
}
}
21 changes: 14 additions & 7 deletions GRDB/Core/DatabaseCollation.swift
Original file line number Diff line number Diff line change
Expand Up @@ -32,17 +32,24 @@ public final class DatabaseCollation {
}
}

extension DatabaseCollation : Hashable {
extension DatabaseCollation: Hashable {
// Collation equality is based on the sqlite3_strnicmp SQLite function.
// (see https://www.sqlite.org/c3ref/create_collation.html). Computing
// a hash value that honors the Swift Hashable contract (value equality
// implies hash equality) is thus non trivial. But it's not that
// important, since this hashValue is only used when one adds
// or removes a collation from a database connection.
#if swift(>=4.2)
/// :nodoc:
public func hash(into hasher: inout Hasher) {
hasher.combine(0)
}
#else
/// :nodoc:
public var hashValue: Int {
// Collation equality is based on the sqlite3_strnicmp SQLite function.
// (see https://www.sqlite.org/c3ref/create_collation.html). Computing
// a hash value that honors the Swift Hashable contract (value equality
// implies hash equality) is thus non trivial. But it's not that
// important, since this hashValue is only used when one adds
// or removes a collation from a database connection.
return 0
}
#endif

/// Two collations are equal if they share the same name (case insensitive)
/// :nodoc:
Expand Down
8 changes: 7 additions & 1 deletion GRDB/Core/DatabaseFunction.swift
Original file line number Diff line number Diff line change
Expand Up @@ -313,11 +313,17 @@ public final class DatabaseFunction: Hashable {
}

extension DatabaseFunction {
/// The hash value
#if swift(>=4.2)
/// :nodoc:
public func hash(into hasher: inout Hasher) {
hasher.combine(identity)
}
#else
/// :nodoc:
public var hashValue: Int {
return identity.hashValue
}
#endif

/// Two functions are equal if they share the same name and arity.
/// :nodoc:
Expand Down
20 changes: 19 additions & 1 deletion GRDB/Core/DatabaseValue.swift
Original file line number Diff line number Diff line change
Expand Up @@ -149,7 +149,24 @@ public struct DatabaseValue: Hashable, CustomStringConvertible, DatabaseValueCon
// Hashable
extension DatabaseValue {

/// The hash value
#if swift(>=4.2)
/// :nodoc:
public func hash(into hasher: inout Hasher) {
switch storage {
case .null:
hasher.combine(0)
case .int64(let int64):
// 1 == 1.0, hence 1 and 1.0 must have the same hash:
hasher.combine(Double(int64))
case .double(let double):
hasher.combine(double)
case .string(let string):
hasher.combine(string)
case .blob(let data):
hasher.combine(data)
}
}
#else
/// :nodoc:
public var hashValue: Int {
switch storage {
Expand All @@ -166,6 +183,7 @@ extension DatabaseValue {
return data.hashValue
}
}
#endif

/// Returns whether two DatabaseValues are equal.
///
Expand Down
12 changes: 11 additions & 1 deletion GRDB/Core/Row.swift
Original file line number Diff line number Diff line change
Expand Up @@ -985,12 +985,22 @@ extension Row {

// Hashable
extension Row {
/// The hash value
#if swift(>=4.2)
/// :nodoc:
public func hash(into hasher: inout Hasher) {
hasher.combine(count)
for (column, dbValue) in self {
hasher.combine(column)
hasher.combine(dbValue)
}
}
#else
/// :nodoc:
public var hashValue: Int {
return columnNames.reduce(0) { (acc, column) in acc ^ column.hashValue } ^
databaseValues.reduce(0) { (acc, dbValue) in acc ^ dbValue.hashValue }
}
#endif
}

// CustomStringConvertible & CustomDebugStringConvertible
Expand Down
24 changes: 6 additions & 18 deletions GRDB/QueryInterface/SQLExpression+QueryInterface.swift
Original file line number Diff line number Diff line change
Expand Up @@ -121,20 +121,6 @@ public struct SQLUnaryOperator : Hashable {
self.sql = sql
self.needsRightSpace = needsRightSpace
}

/// The hash value
///
/// :nodoc:
public var hashValue: Int {
return sql.hashValue
}

/// Equality operator
///
/// :nodoc:
public static func == (lhs: SQLUnaryOperator, rhs: SQLUnaryOperator) -> Bool {
return lhs.sql == rhs.sql
}
}

/// [**Experimental**](http://github.com/groue/GRDB.swift#what-are-experimental-features)
Expand Down Expand Up @@ -227,15 +213,17 @@ public struct SQLBinaryOperator : Hashable {
return SQLBinaryOperator(negatedSQL, negated: sql)
}

/// The hash value
#if !swift(>=4.2)
/// :nodoc:
public var hashValue: Int {
return sql.hashValue
return sql.hashValue ^ (negatedSQL?.hashValue ?? 0)
}

/// Equality operator
/// :nodoc:
public static func == (lhs: SQLBinaryOperator, rhs: SQLBinaryOperator) -> Bool {
return lhs.sql == rhs.sql
return lhs.sql == rhs.sql && lhs.negatedSQL == rhs.negatedSQL
}
#endif
}

/// [**Experimental**](http://github.com/groue/GRDB.swift#what-are-experimental-features)
Expand Down
7 changes: 7 additions & 0 deletions GRDB/QueryInterface/SQLGenerationContext.swift
Original file line number Diff line number Diff line change
Expand Up @@ -235,10 +235,17 @@ public class TableAlias: Hashable {
return expression.qualifiedExpression(with: self)
}

#if swift(>=4.2)
/// :nodoc:
public func hash(into hasher: inout Hasher) {
hasher.combine(ObjectIdentifier(root))
}
#else
/// :nodoc:
public var hashValue: Int {
return ObjectIdentifier(root).hashValue
}
#endif

/// :nodoc:
public static func == (lhs: TableAlias, rhs: TableAlias) -> Bool {
Expand Down
6 changes: 4 additions & 2 deletions GRDB/Record/PersistableRecord.swift
Original file line number Diff line number Diff line change
Expand Up @@ -1185,8 +1185,9 @@ private struct InsertQuery: Hashable {
let tableName: String
let insertedColumns: [String]

// TODO: remove when Swift can generate it
#if !swift(>=4.2)
var hashValue: Int { return tableName.hashValue }
#endif
}

extension InsertQuery {
Expand Down Expand Up @@ -1218,8 +1219,9 @@ private struct UpdateQuery: Hashable {
let updatedColumns: [String]
let conditionColumns: [String]

// TODO: remove when Swift can generate it
#if !swift(>=4.2)
var hashValue: Int { return tableName.hashValue }
#endif
}

extension UpdateQuery {
Expand Down

0 comments on commit c0c97e8

Please sign in to comment.