From 1244b6b283f68ebc2f84f2f5550c5ae54ca62720 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Gwendal=20Roue=CC=81?= Date: Fri, 30 Sep 2016 06:00:50 +0200 Subject: [PATCH] Move == operators inside their adopting type --- GRDB/Core/Database.swift | 22 ++--- GRDB/Core/DatabaseValue.swift | 53 ++++++----- GRDB/Core/Row.swift | 94 +++++++++---------- GRDB/QueryInterface/SQLExpression.swift | 24 ++--- GRDB/Record/Persistable.swift | 26 ++--- .../FetchRequest/FetchRequestTests.swift | 7 +- 6 files changed, 118 insertions(+), 108 deletions(-) diff --git a/GRDB/Core/Database.swift b/GRDB/Core/Database.swift index c0eaef2c3..c470a277d 100644 --- a/GRDB/Core/Database.swift +++ b/GRDB/Core/Database.swift @@ -787,11 +787,11 @@ extension DatabaseFunction : Hashable { public var hashValue: Int { return name.hashValue ^ argumentCount.hashValue } -} - -/// Two functions are equal if they share the same name and argumentCount. -public func ==(lhs: DatabaseFunction, rhs: DatabaseFunction) -> Bool { - return lhs.name == rhs.name && lhs.argumentCount == rhs.argumentCount + + /// Two functions are equal if they share the same name and argumentCount. + public static func ==(lhs: DatabaseFunction, rhs: DatabaseFunction) -> Bool { + return lhs.name == rhs.name && lhs.argumentCount == rhs.argumentCount + } } @@ -869,12 +869,12 @@ extension DatabaseCollation : Hashable { // sqlite3_strnicmp SQLite function. return 0 } -} - -/// Two collations are equal if they share the same name (case insensitive) -public func ==(lhs: DatabaseCollation, rhs: DatabaseCollation) -> Bool { - // See https://www.sqlite.org/c3ref/create_collation.html - return sqlite3_stricmp(lhs.name, lhs.name) == 0 + + /// Two collations are equal if they share the same name (case insensitive) + public static func ==(lhs: DatabaseCollation, rhs: DatabaseCollation) -> Bool { + // See https://www.sqlite.org/c3ref/create_collation.html + return sqlite3_stricmp(lhs.name, lhs.name) == 0 + } } diff --git a/GRDB/Core/DatabaseValue.swift b/GRDB/Core/DatabaseValue.swift index 04242c4fc..c4222cb21 100644 --- a/GRDB/Core/DatabaseValue.swift +++ b/GRDB/Core/DatabaseValue.swift @@ -194,32 +194,41 @@ extension DatabaseValue : Hashable { return data.hashValue } } -} - -/// DatabaseValue adopts Equatable. -public func ==(lhs: DatabaseValue, rhs: DatabaseValue) -> Bool { - switch (lhs.storage, rhs.storage) { - case (.null, .null): - return true - case (.int64(let lhs), .int64(let rhs)): - return lhs == rhs - case (.double(let lhs), .double(let rhs)): - return lhs == rhs - case (.int64(let lhs), .double(let rhs)): - return int64EqualDouble(lhs, rhs) - case (.double(let lhs), .int64(let rhs)): - return int64EqualDouble(rhs, lhs) - case (.string(let lhs), .string(let rhs)): - return lhs == rhs - case (.blob(let lhs), .blob(let rhs)): - return lhs == rhs - default: - return false + + /// Returns whether two DatabaseValues are equal. + /// + /// 1.databaseValue == "foo".databaseValue // false + /// 1.databaseValue == 1.databaseValue // true + /// + /// When comparing integers and doubles, the result is true if and only + /// values are equal, and if converting one type to the other does + /// not lose information: + /// + /// 1.databaseValue == 1.0.databaseValue // true + public static func ==(lhs: DatabaseValue, rhs: DatabaseValue) -> Bool { + switch (lhs.storage, rhs.storage) { + case (.null, .null): + return true + case (.int64(let lhs), .int64(let rhs)): + return lhs == rhs + case (.double(let lhs), .double(let rhs)): + return lhs == rhs + case (.int64(let lhs), .double(let rhs)): + return int64EqualDouble(lhs, rhs) + case (.double(let lhs), .int64(let rhs)): + return int64EqualDouble(rhs, lhs) + case (.string(let lhs), .string(let rhs)): + return lhs == rhs + case (.blob(let lhs), .blob(let rhs)): + return lhs == rhs + default: + return false + } } } /// Returns true if i and d hold exactly the same value, and if converting one -/// type into the other does not lose any information. +/// type to the other does not lose any information. private func int64EqualDouble(_ i: Int64, _ d: Double) -> Bool { // TODO: wait for https://github.com/apple/swift-evolution/blob/master/proposals/0080-failable-numeric-initializers.md // Bug: https://bugs.swift.org/browse/SR-1491 diff --git a/GRDB/Core/Row.swift b/GRDB/Core/Row.swift index 6cf6bbf0d..ace42a1b5 100644 --- a/GRDB/Core/Row.swift +++ b/GRDB/Core/Row.swift @@ -782,46 +782,47 @@ extension Row : Collection { } -extension Row : Equatable { } - -/// Returns true if and only if both rows have the same columns and values, in -/// the same order. Columns are compared in a case-sensitive way. -public func ==(lhs: Row, rhs: Row) -> Bool { - if lhs === rhs { +extension Row : Equatable { + + /// Returns true if and only if both rows have the same columns and values, + /// in the same order. Columns are compared in a case-sensitive way. + public static func ==(lhs: Row, rhs: Row) -> Bool { + if lhs === rhs { + return true + } + + guard lhs.count == rhs.count else { + return false + } + + var liter = lhs.makeIterator() + var riter = rhs.makeIterator() + + while let (lcol, lval) = liter.next(), let (rcol, rval) = riter.next() { + guard lcol == rcol else { + return false + } + guard lval == rval else { + return false + } + } + + let lscopeNames = lhs.impl.scopeNames + let rscopeNames = rhs.impl.scopeNames + guard lscopeNames == rscopeNames else { + return false + } + + for name in lscopeNames { + let lscope = lhs.scoped(on: name) + let rscope = rhs.scoped(on: name) + guard lscope == rscope else { + return false + } + } + return true } - - guard lhs.count == rhs.count else { - return false - } - - var liter = lhs.makeIterator() - var riter = rhs.makeIterator() - - while let (lcol, lval) = liter.next(), let (rcol, rval) = riter.next() { - guard lcol == rcol else { - return false - } - guard lval == rval else { - return false - } - } - - let lscopeNames = lhs.impl.scopeNames - let rscopeNames = rhs.impl.scopeNames - guard lscopeNames == rscopeNames else { - return false - } - - for name in lscopeNames { - let lscope = lhs.scoped(on: name) - let rscope = rhs.scoped(on: name) - guard lscope == rscope else { - return false - } - } - - return true } /// Row adopts CustomStringConvertible. @@ -843,15 +844,14 @@ extension Row: CustomStringConvertible { public struct RowIndex : Comparable { let index: Int init(_ index: Int) { self.index = index } -} - -/// Equatable implementation for RowIndex -public func ==(lhs: RowIndex, rhs: RowIndex) -> Bool { - return lhs.index == rhs.index -} - -public func <(lhs: RowIndex, rhs: RowIndex) -> Bool { - return lhs.index < rhs.index + + public static func ==(lhs: RowIndex, rhs: RowIndex) -> Bool { + return lhs.index == rhs.index + } + + public static func <(lhs: RowIndex, rhs: RowIndex) -> Bool { + return lhs.index < rhs.index + } } diff --git a/GRDB/QueryInterface/SQLExpression.swift b/GRDB/QueryInterface/SQLExpression.swift index 7716dd045..84072e85a 100644 --- a/GRDB/QueryInterface/SQLExpression.swift +++ b/GRDB/QueryInterface/SQLExpression.swift @@ -309,10 +309,10 @@ public struct SQLUnaryOperator : Hashable { public var hashValue: Int { return sql.hashValue } -} - -public func ==(lhs: SQLUnaryOperator, rhs: SQLUnaryOperator) -> Bool { - return lhs.sql == rhs.sql + + public static func ==(lhs: SQLUnaryOperator, rhs: SQLUnaryOperator) -> Bool { + return lhs.sql == rhs.sql + } } /// This type is an implementation detail of the query interface. @@ -397,10 +397,10 @@ public struct SQLBinaryOperator : Hashable { } return SQLBinaryOperator(negatedSQL, negated: sql) } -} - -public func ==(lhs: SQLBinaryOperator, rhs: SQLBinaryOperator) -> Bool { - return lhs.sql == rhs.sql + + public static func ==(lhs: SQLBinaryOperator, rhs: SQLBinaryOperator) -> Bool { + return lhs.sql == rhs.sql + } } /// This type is an implementation detail of the query interface. @@ -611,10 +611,10 @@ public struct SQLFunctionName : Hashable { public var hashValue: Int { return sql.hashValue } -} - -public func ==(lhs: SQLFunctionName, rhs: SQLFunctionName) -> Bool { - return lhs.sql == rhs.sql + + public static func ==(lhs: SQLFunctionName, rhs: SQLFunctionName) -> Bool { + return lhs.sql == rhs.sql + } } /// This type is an implementation detail of the query interface. diff --git a/GRDB/Record/Persistable.swift b/GRDB/Record/Persistable.swift index 3e0cde56c..ad65ec509 100644 --- a/GRDB/Record/Persistable.swift +++ b/GRDB/Record/Persistable.swift @@ -700,12 +700,12 @@ private struct InsertQuery { extension InsertQuery : Hashable { var hashValue: Int { return tableName.hashValue } -} - -private 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 + + 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 + } } extension InsertQuery { @@ -740,13 +740,13 @@ private struct UpdateQuery { extension UpdateQuery : Hashable { var hashValue: Int { return tableName.hashValue } -} - -private 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 + + 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 + } } extension UpdateQuery { diff --git a/Tests/Public/FetchRequest/FetchRequestTests.swift b/Tests/Public/FetchRequest/FetchRequestTests.swift index 275e485c7..082875ab8 100644 --- a/Tests/Public/FetchRequest/FetchRequestTests.swift +++ b/Tests/Public/FetchRequest/FetchRequestTests.swift @@ -37,9 +37,10 @@ private struct CustomStruct: DatabaseValueConvertible { return CustomStruct(number: number) } } -extension CustomStruct: Equatable { } -private func ==(lhs: CustomStruct, rhs: CustomStruct) -> Bool { - return lhs.number == rhs.number +extension CustomStruct: Equatable { + static func ==(lhs: CustomStruct, rhs: CustomStruct) -> Bool { + return lhs.number == rhs.number + } } class FetchRequestTests: GRDBTestCase {