diff --git a/CHANGELOG.md b/CHANGELOG.md index 6521ccf45..0d8075a0d 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,6 +1,13 @@ Release Notes ============= +## Next version + +**New** + +- `Row` adopts Equatable + + ## 0.51.2 Released March 14, 2016 diff --git a/GRDB/Core/Row.swift b/GRDB/Core/Row.swift index 5e6b5218d..5c4c70c06 100644 --- a/GRDB/Core/Row.swift +++ b/GRDB/Core/Row.swift @@ -629,8 +629,33 @@ extension Row : CollectionType { } } +extension Row : Equatable { } -// MARK: - CustomStringConvertible +/// 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 { + return true + } + + guard lhs.count == rhs.count else { + return false + } + + var lgen = lhs.generate() + var rgen = rhs.generate() + + while let (lcol, lval) = lgen.next(), let (rcol, rval) = rgen.next() { + guard lcol == rcol else { + return false + } + guard lval == rval else { + return false + } + } + + return true +} /// Row adopts CustomStringConvertible. extension Row: CustomStringConvertible { diff --git a/GRDB/Migrations/Migration.swift b/GRDB/Migrations/Migration.swift index 6a8431d4d..9e7da319f 100644 --- a/GRDB/Migrations/Migration.swift +++ b/GRDB/Migrations/Migration.swift @@ -59,7 +59,7 @@ struct Migration { // // Let's turn any violation into an SQLITE_CONSTRAINT // error, and rollback the transaction. - throw DatabaseError(code: SQLITE_CONSTRAINT, message: "FOREIGN KEY constraint failed", sql: "PRAGMA foreign_key_check", arguments: nil) + throw DatabaseError(code: SQLITE_CONSTRAINT, message: "FOREIGN KEY constraint failed", sql: nil, arguments: nil) } // > 11. Commit the transaction started in step 2. diff --git a/README.md b/README.md index 6b3e0b17c..ebc3208eb 100644 --- a/README.md +++ b/README.md @@ -410,7 +410,7 @@ You can fetch database rows, plain values, and custom models aka "records". **Rows** are the raw results of SQL queries: ```swift -for row in Row.fetchAll(db, "SELECT * FROM wines") { +for row in Row.fetch(db, "SELECT * FROM wines") { let name: String = row.value(named: "name") let color: Color = row.value(named: "color") print(name, color) diff --git a/TODO.md b/TODO.md index 004e8e3cf..c62704b56 100644 --- a/TODO.md +++ b/TODO.md @@ -1,3 +1,5 @@ +- [ ] Write an app like http://www.appcoda.com/swiftydb/ ? +- [ ] Since commit e6010e334abdf98eb9f62c1d6abbb2a9e8cd7d19, one can not use the raw SQLite API without importing the SQLite module for the platform. We need to document that. - [ ] Read https://sqlite.org/sharedcache.html - [ ] Query builder - [ ] SELECT readers.*, books.* FROM ... JOIN ... diff --git a/Tests/Public/Migrations/DatabaseMigratorTests.swift b/Tests/Public/Migrations/DatabaseMigratorTests.swift index 5fa776016..0cb5b8ac6 100644 --- a/Tests/Public/Migrations/DatabaseMigratorTests.swift +++ b/Tests/Public/Migrations/DatabaseMigratorTests.swift @@ -110,20 +110,31 @@ class DatabaseMigratorTests : GRDBTestCase { do { try migrator.migrate(dbQueue) XCTFail("Expected error") - } catch { + } catch let error as DatabaseError { // Migration 1 and 2 should be committed. // Migration 3 should not be committed. + XCTAssertEqual(error.code, 19) // SQLITE_CONSTRAINT + XCTAssertEqual(error.message!, "FOREIGN KEY constraint failed") + XCTAssertTrue(error.sql == nil) + XCTAssertEqual(error.description, "SQLite error 19: FOREIGN KEY constraint failed") + // Arthur inserted (migration 1), Barbara (migration 3) not inserted. - let rows = dbQueue.inDatabase { db in - Row.fetchAll(db, "SELECT * FROM persons") - } + var rows = Row.fetchAll(dbQueue, "SELECT * FROM persons") XCTAssertEqual(rows.count, 1) - let row = rows.first! + var row = rows.first! XCTAssertEqual(row.value(named: "name") as String, "Arthur") // persons table has no "tmp" column (migration 2) XCTAssertEqual(Array(row.columnNames), ["id", "name"]) + + // Bobby inserted (migration 1), not deleted by migration 2. + rows = Row.fetchAll(dbQueue, "SELECT * FROM pets") + XCTAssertEqual(rows.count, 1) + row = rows.first! + XCTAssertEqual(row.value(named: "name") as String, "Bobby") + } catch { + XCTFail("Error: \(error)") } } }