From 3293a33f385e30fbc3a6c5ddb0f1616a29e70507 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Gwendal=20Roue=CC=81?= Date: Wed, 16 Mar 2016 08:36:55 +0100 Subject: [PATCH 1/7] Row adopts Equatable --- GRDB/Core/Row.swift | 27 ++++++++++++++++++++++++++- 1 file changed, 26 insertions(+), 1 deletion(-) diff --git a/GRDB/Core/Row.swift b/GRDB/Core/Row.swift index ee5beece7..a52eaf357 100644 --- a/GRDB/Core/Row.swift +++ b/GRDB/Core/Row.swift @@ -619,8 +619,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 false +} /// Row adopts CustomStringConvertible. extension Row: CustomStringConvertible { From cc6f2c00dff5ccdb96de01d895a1e898cdb1fffe Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Gwendal=20Roue=CC=81?= Date: Wed, 16 Mar 2016 14:09:29 +0100 Subject: [PATCH 2/7] Fix ==(Row, Row) --- GRDB/Core/Row.swift | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/GRDB/Core/Row.swift b/GRDB/Core/Row.swift index a52eaf357..be71b81e2 100644 --- a/GRDB/Core/Row.swift +++ b/GRDB/Core/Row.swift @@ -644,7 +644,7 @@ public func ==(lhs: Row, rhs: Row) -> Bool { } } - return false + return true } /// Row adopts CustomStringConvertible. From 2d7a035297b7d7a59a16b071ba22965ee4f91299 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Gwendal=20Roue=CC=81?= Date: Wed, 16 Mar 2016 15:49:04 +0100 Subject: [PATCH 3/7] TODO.md --- TODO.md | 1 + 1 file changed, 1 insertion(+) diff --git a/TODO.md b/TODO.md index 004e8e3cf..c3041c6fc 100644 --- a/TODO.md +++ b/TODO.md @@ -1,3 +1,4 @@ +- [ ] Write an app like http://www.appcoda.com/swiftydb/ ? - [ ] Read https://sqlite.org/sharedcache.html - [ ] Query builder - [ ] SELECT readers.*, books.* FROM ... JOIN ... From 4f13055a45b79120d6312c07e0558fbf5309d0f1 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Gwendal=20Roue=CC=81?= Date: Fri, 18 Mar 2016 06:20:25 +0100 Subject: [PATCH 4/7] README.md --- README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) 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) From 5c8526d19759c6f069fe51ddb8b9716e7dfdef7b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Gwendal=20Roue=CC=81?= Date: Fri, 18 Mar 2016 06:26:40 +0100 Subject: [PATCH 5/7] TODO.md --- TODO.md | 1 + 1 file changed, 1 insertion(+) diff --git a/TODO.md b/TODO.md index c3041c6fc..c62704b56 100644 --- a/TODO.md +++ b/TODO.md @@ -1,4 +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 ... From 4e5d5aac8c348b28513c3f8af8354f4e26b60d33 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Gwendal=20Roue=CC=81?= Date: Fri, 18 Mar 2016 06:26:47 +0100 Subject: [PATCH 6/7] CHANGELOG.md --- CHANGELOG.md | 7 +++++++ 1 file changed, 7 insertions(+) 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 From af538c4febd74d37294723caa4af500d69474d44 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Gwendal=20Roue=CC=81?= Date: Fri, 18 Mar 2016 06:44:24 +0100 Subject: [PATCH 7/7] Improve test for migration with disabled foreign key checks --- GRDB/Migrations/Migration.swift | 2 +- .../Migrations/DatabaseMigratorTests.swift | 21 ++++++++++++++----- 2 files changed, 17 insertions(+), 6 deletions(-) diff --git a/GRDB/Migrations/Migration.swift b/GRDB/Migrations/Migration.swift index 5132fc6ef..e47bef4ab 100644 --- a/GRDB/Migrations/Migration.swift +++ b/GRDB/Migrations/Migration.swift @@ -49,7 +49,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/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)") } } }