From ffde18dbfc15d1ff16a0c937d29422a4a1dc398d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Gwendal=20Roue=CC=81?= Date: Sun, 5 Jul 2015 20:00:33 +0200 Subject: [PATCH] PrimaryKey cases renaming --- .../RowModel/PrimaryKeyMultipleTests.swift | 2 +- .../RowModel/PrimaryKeySingleTests.swift | 2 +- GRDB/RowModel.swift | 20 +++++++++---------- Playground.playground/Contents.swift | 2 +- README.md | 17 ++++++++++------ 5 files changed, 24 insertions(+), 19 deletions(-) diff --git a/GRDB Tests/RowModel/PrimaryKeyMultipleTests.swift b/GRDB Tests/RowModel/PrimaryKeyMultipleTests.swift index d020c7fa1..efd28ef53 100644 --- a/GRDB Tests/RowModel/PrimaryKeyMultipleTests.swift +++ b/GRDB Tests/RowModel/PrimaryKeyMultipleTests.swift @@ -35,7 +35,7 @@ class Citizenship: RowModel { } override class var databasePrimaryKey: PrimaryKey { - return .Multiple(["personID", "countryName"]) + return .Columns(["personID", "countryName"]) } override var databaseDictionary: [String: SQLiteValueConvertible?] { diff --git a/GRDB Tests/RowModel/PrimaryKeySingleTests.swift b/GRDB Tests/RowModel/PrimaryKeySingleTests.swift index 9a114c908..7ddfee52f 100644 --- a/GRDB Tests/RowModel/PrimaryKeySingleTests.swift +++ b/GRDB Tests/RowModel/PrimaryKeySingleTests.swift @@ -35,7 +35,7 @@ class Pet: RowModel { } override class var databasePrimaryKey: PrimaryKey { - return .Single("UUID") + return .Column("UUID") } override var databaseDictionary: [String: SQLiteValueConvertible?] { diff --git a/GRDB/RowModel.swift b/GRDB/RowModel.swift index 663fd57e8..25ab86bf9 100644 --- a/GRDB/RowModel.swift +++ b/GRDB/RowModel.swift @@ -29,8 +29,8 @@ public class RowModel { public enum PrimaryKey { case None case RowID(String) - case Single(String) - case Multiple([String]) + case Column(String) + case Columns([String]) } public class var databaseTableName : String? { @@ -186,7 +186,7 @@ public class RowModel { saveIsUpdate = false } - case .Single(let column): + case .Column(let column): if let value = databaseDictionary[column]! { // Update if and only if the primary key exists in the database. @@ -198,7 +198,7 @@ public class RowModel { saveIsUpdate = false } - case .Multiple(let columns): + case .Columns(let columns): let databaseDictionary = self.databaseDictionary for column in columns { if databaseDictionary[column]! == nil { @@ -249,9 +249,9 @@ public class RowModel { return nil case .RowID(let column): return [column: dictionary[column]!] - case .Single(let column): + case .Column(let column): return [column: dictionary[column]!] - case .Multiple(let columns): + case .Columns(let columns): var dic = [String: SQLiteValueConvertible?]() for column in columns { dic[column] = dictionary[column]! @@ -327,9 +327,9 @@ extension Database { keyDictionary = bindings.dictionary(defaultColumnNames: nil) case .RowID(let column): keyDictionary = bindings.dictionary(defaultColumnNames: [column]) - case .Single(let column): + case .Column(let column): keyDictionary = bindings.dictionary(defaultColumnNames: [column]) - case .Multiple(let columns): + case .Columns(let columns): keyDictionary = bindings.dictionary(defaultColumnNames: columns) } @@ -351,9 +351,9 @@ extension Database { fatalError("Missing primary key") case .RowID(let column): sql = "SELECT * FROM \(tableName.sqliteQuotedIdentifier) WHERE \(column.sqliteQuotedIdentifier) = ?" - case .Single(let column): + case .Column(let column): sql = "SELECT * FROM \(tableName.sqliteQuotedIdentifier) WHERE \(column.sqliteQuotedIdentifier) = ?" - case .Multiple(let columns): + case .Columns(let columns): if columns.count == 1 { sql = "SELECT * FROM \(tableName.sqliteQuotedIdentifier) WHERE \(columns.first!.sqliteQuotedIdentifier) = ?" } else { diff --git a/Playground.playground/Contents.swift b/Playground.playground/Contents.swift index c59e651e7..b49a8b303 100644 --- a/Playground.playground/Contents.swift +++ b/Playground.playground/Contents.swift @@ -106,7 +106,7 @@ class Pet: RowModel { } override class var databasePrimaryKey: PrimaryKey { - return .Single("UUID") + return .Column("UUID") } override var databaseDictionary: [String: SQLiteValueConvertible?] { diff --git a/README.md b/README.md index 6cf517148..0832b8738 100644 --- a/README.md +++ b/README.md @@ -563,7 +563,8 @@ dbQueue.inDatabase { db in There are four kinds of primary keys: -- **None**: the default +- **None** (the default): no primary key + - **RowID**: use it when you rely on SQLite to automatically generate IDs (see https://www.sqlite.org/autoinc.html). ```swift @@ -575,23 +576,23 @@ There are four kinds of primary keys: db.fetchOne(Person.self, primaryKey: 123) ``` -- **Single**: for single-column primary keys that are not managed by SQLite. +- **Column**: for single-column primary keys that are not managed by SQLite. ```swift class Book : RowModel { override class var databasePrimaryKey: PrimaryKey { - return .Single("uuid") + return .Column("uuid") } } db.fetchOne(Book.self, primaryKey: "b3fc...") ``` -- **Multiple**: for primary keys that span accross several columns. +- **Columns**: for primary keys that span accross several columns. ```swift class Citizenship : RowModel { override class var databasePrimaryKey: PrimaryKey { - return .Multiple("personID", "countryID") + return .Columns("personID", "countryID") } } db.fetchOne(Citizenship.self, primaryKey: [arthur.id, france.id]) @@ -696,7 +697,7 @@ try arthur.insert(db) arthur.id // some value ``` -Other primary keys (None, Single, Multiple) are not managed by GRDB: you have to manage them yourself. +Other primary keys (single or multiple columns) are not managed by GRDB: you have to manage them yourself. You can for example **override primitive methods**: @@ -704,6 +705,10 @@ You can for example **override primitive methods**: class Book : RowModel { ... + override class var databasePrimaryKey: PrimaryKey { + return .Column("uuid") + } + // Before insertion, set uuid if not set yet. override func insert(db: Database) throws { if uuid == nil {