mirror of
https://github.com/groue/GRDB.swift
synced 2026-11-26 04:07:50 +00:00
PrimaryKey cases renaming
This commit is contained in:
1 parent
7bf8b05563
commit
ffde18dbfc
5 files changed
+24
-19
No files matched your search
@@ -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?] {
|
||||
|
||||
@@ -35,7 +35,7 @@ class Pet: RowModel {
|
||||
}
|
||||
|
||||
override class var databasePrimaryKey: PrimaryKey {
|
||||
return .Single("UUID")
|
||||
return .Column("UUID")
|
||||
}
|
||||
|
||||
override var databaseDictionary: [String: SQLiteValueConvertible?] {
|
||||
|
||||
+10
-10
@@ -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 {
|
||||
|
||||
@@ -106,7 +106,7 @@ class Pet: RowModel {
|
||||
}
|
||||
|
||||
override class var databasePrimaryKey: PrimaryKey {
|
||||
return .Single("UUID")
|
||||
return .Column("UUID")
|
||||
}
|
||||
|
||||
override var databaseDictionary: [String: SQLiteValueConvertible?] {
|
||||
|
||||
@@ -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 {
|
||||
|
||||
Reference in new issue
Block a user