mirror of
https://github.com/groue/GRDB.swift
synced 2026-11-25 04:07:47 +00:00
Replace RowModel.PrimaryKey.None with nil
This commit is contained in:
1 parent
c52c2f6b11
commit
f4ad61528f
3 files changed
+22
-24
No files matched your search
@@ -29,7 +29,7 @@ class Item: RowModel {
|
||||
var name: String?
|
||||
|
||||
override class var databaseTable: Table? {
|
||||
return Table(named: "items", primaryKey: .None)
|
||||
return Table(named: "items")
|
||||
}
|
||||
|
||||
override var storedDatabaseDictionary: [String: SQLiteValueConvertible?] {
|
||||
|
||||
+12
-13
@@ -28,9 +28,6 @@ public class RowModel {
|
||||
|
||||
/// A primary key
|
||||
public enum PrimaryKey {
|
||||
/// No primary key.
|
||||
case None
|
||||
|
||||
/// A primary key managed by SQLite.
|
||||
case RowID(String)
|
||||
|
||||
@@ -47,10 +44,10 @@ public class RowModel {
|
||||
public let name: String
|
||||
|
||||
/// The primary key
|
||||
public let primaryKey: PrimaryKey
|
||||
public let primaryKey: PrimaryKey?
|
||||
|
||||
/// Creates a Table given its name and primary key (default .None)
|
||||
public init(named name: String, primaryKey: PrimaryKey = .None) {
|
||||
/// Creates a Table given its name and primary key (default nil)
|
||||
public init(named name: String, primaryKey: PrimaryKey? = nil) {
|
||||
self.name = name
|
||||
self.primaryKey = primaryKey
|
||||
}
|
||||
@@ -177,9 +174,6 @@ public class RowModel {
|
||||
return nil
|
||||
}
|
||||
switch primaryKey {
|
||||
case .None:
|
||||
return nil
|
||||
|
||||
case .RowID(let column):
|
||||
if let value = self.storedDatabaseDictionary[column] {
|
||||
return [column: value]
|
||||
@@ -257,7 +251,10 @@ public class RowModel {
|
||||
let changes = try db.execute(sql, bindings: Bindings(insertedDic.values))
|
||||
|
||||
// Update RowID column if needed
|
||||
switch table.primaryKey {
|
||||
guard let primaryKey = table.primaryKey else {
|
||||
return nil
|
||||
}
|
||||
switch primaryKey {
|
||||
case .RowID(let column):
|
||||
if let optionalValue = storedDatabaseDictionary[column] {
|
||||
if optionalValue == nil { // ID was not set
|
||||
@@ -441,10 +438,12 @@ extension Database {
|
||||
fatalError("Missing databaseTable.")
|
||||
}
|
||||
|
||||
let sql: String
|
||||
switch table.primaryKey {
|
||||
case .None:
|
||||
guard let tablePrimaryKey = table.primaryKey else {
|
||||
fatalError("Missing primary key")
|
||||
}
|
||||
|
||||
let sql: String
|
||||
switch tablePrimaryKey {
|
||||
case .RowID(let column):
|
||||
sql = "SELECT * FROM \(table.name.sqliteQuotedIdentifier) WHERE \(column.sqliteQuotedIdentifier) = ?"
|
||||
case .Column(let column):
|
||||
|
||||
@@ -514,15 +514,16 @@ Subclasses opt in RowModel features by overriding all or part of the core method
|
||||
|
||||
For example, we see below that **fetching** does not require any coupling to a specific database table:
|
||||
|
||||
| Core Methods | fetch | insert | update | delete |
|
||||
|:------------------------------ |:-----:|:------:|:------:|:------:|
|
||||
| `setSQLiteValue(_:forColumn:)` | X | | | |
|
||||
| `databaseTable` | | X | X | X |
|
||||
| `storedDatabaseDictionary` | | X | X | X |
|
||||
| Core Methods | fetch | insert | update | delete |
|
||||
|:----------------------------------- |:-----:|:------:|:------:|:------:|
|
||||
| `setSQLiteValue(_:forColumn:) ` | X | | | |
|
||||
| `databaseTable` | | X | | |
|
||||
| `databaseTable` with primary key | | | X | X |
|
||||
| `storedDatabaseDictionary` | | X | X | X |
|
||||
|
||||
- [Fetching Row Models](#fetching-row-models)
|
||||
- [Ad Hoc Subclasses](#ad-hoc-subclasses)
|
||||
- [Primary Keys](#primary-keys)
|
||||
- [Tables and Primary Keys](#tables-and-primary-keys)
|
||||
- [Insert, Update and Delete](#insert-update-and-delete)
|
||||
- [RowModel Errors](#rowmodels-errors)
|
||||
- [Full List of RowModel methods](full-list-of-rowmodel-methods)
|
||||
@@ -627,7 +628,7 @@ class PersonsViewController: UITableViewController {
|
||||
```
|
||||
|
||||
|
||||
### Primary Keys
|
||||
### Tables and Primary Keys
|
||||
|
||||
Declare a **Table** given its **name** and **primary key** in order to fetch row models by ID:
|
||||
|
||||
@@ -644,9 +645,7 @@ try dbQueue.inDatabase { db in
|
||||
}
|
||||
```
|
||||
|
||||
There are four kinds of primary keys:
|
||||
|
||||
- **None** (the default): no primary key
|
||||
There are three kinds of primary keys:
|
||||
|
||||
- **RowID**: use it when you rely on SQLite to automatically generate IDs (see https://www.sqlite.org/autoinc.html).
|
||||
|
||||
|
||||
Reference in new issue
Block a user