mirror of
https://github.com/groue/GRDB.swift
synced 2026-11-24 04:07:47 +00:00
Cleanup
This commit is contained in:
1 parent
2028c064f1
commit
c07f7475ae
6 files changed
+45
-38
No files matched your search
@@ -735,6 +735,18 @@ enum PrimaryKey {
|
||||
return columns
|
||||
}
|
||||
}
|
||||
|
||||
/// The name of the INTEGER PRIMARY KEY
|
||||
var rowIDColumn: String? {
|
||||
switch self {
|
||||
case .None:
|
||||
return nil
|
||||
case .RowID(let column):
|
||||
return column
|
||||
case .Regular:
|
||||
return nil
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
@@ -210,11 +210,7 @@ public extension MutableDatabasePersistable {
|
||||
let dataMapper = DataMapper(db, self)
|
||||
let changes = try dataMapper.insertStatement().execute()
|
||||
if let rowID = changes.insertedRowID {
|
||||
if case .RowID(let column) = dataMapper.primaryKey {
|
||||
didInsertWithRowID(rowID, forColumn: column)
|
||||
} else {
|
||||
didInsertWithRowID(rowID, forColumn: nil)
|
||||
}
|
||||
didInsertWithRowID(rowID, forColumn: dataMapper.primaryKey.rowIDColumn)
|
||||
}
|
||||
}
|
||||
|
||||
@@ -386,11 +382,7 @@ public extension DatabasePersistable {
|
||||
let dataMapper = DataMapper(db, self)
|
||||
let changes = try dataMapper.insertStatement().execute()
|
||||
if let rowID = changes.insertedRowID {
|
||||
if case .RowID(let column) = dataMapper.primaryKey {
|
||||
didInsertWithRowID(rowID, forColumn: column)
|
||||
} else {
|
||||
didInsertWithRowID(rowID, forColumn: nil)
|
||||
}
|
||||
didInsertWithRowID(rowID, forColumn: dataMapper.primaryKey.rowIDColumn)
|
||||
}
|
||||
}
|
||||
|
||||
@@ -462,7 +454,7 @@ final class DataMapper {
|
||||
return value
|
||||
}
|
||||
let pkColumn = pkColumn.lowercaseString
|
||||
for (persistentColumn, value) in persistentDictionary where persistentColumn.lowercaseString == pkColumn {
|
||||
for (column, value) in persistentDictionary where column.lowercaseString == pkColumn {
|
||||
return value
|
||||
}
|
||||
return nil
|
||||
|
||||
+12
-13
@@ -209,22 +209,21 @@ public class Record : RowConvertible, DatabaseTableMapping, DatabasePersistable
|
||||
var persistentDictionary = dataMapper.persistentDictionary
|
||||
let changes = try dataMapper.insertStatement().execute()
|
||||
if let rowID = changes.insertedRowID {
|
||||
if case .RowID(let column) = dataMapper.primaryKey {
|
||||
// Update persistentDictionary with inserted id
|
||||
if persistentDictionary[column] != nil {
|
||||
persistentDictionary[column] = rowID
|
||||
let rowIDColumn = dataMapper.primaryKey.rowIDColumn
|
||||
didInsertWithRowID(rowID, forColumn: rowIDColumn)
|
||||
|
||||
// Update persistentDictionary with inserted id, so that we can
|
||||
// set hasPersistentChangedValues to false:
|
||||
if let rowIDColumn = rowIDColumn {
|
||||
if persistentDictionary[rowIDColumn] != nil {
|
||||
persistentDictionary[rowIDColumn] = rowID
|
||||
} else {
|
||||
let lowercaseColumn = column.lowercaseString
|
||||
for persistentColumn in persistentDictionary.keys {
|
||||
if lowercaseColumn == persistentColumn.lowercaseString {
|
||||
persistentDictionary[persistentColumn] = rowID
|
||||
break
|
||||
}
|
||||
let rowIDColumn = rowIDColumn.lowercaseString
|
||||
for column in persistentDictionary.keys where column.lowercaseString == rowIDColumn {
|
||||
persistentDictionary[column] = rowID
|
||||
break
|
||||
}
|
||||
}
|
||||
didInsertWithRowID(rowID, forColumn: column)
|
||||
} else {
|
||||
didInsertWithRowID(rowID, forColumn: nil)
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -45,8 +45,9 @@ configuration.trace = { print($0) }
|
||||
let dbQueue = DatabaseQueue(configuration: configuration) // Memory database
|
||||
var migrator = DatabaseMigrator()
|
||||
migrator.registerMigration("createEvents") { db in
|
||||
try db.execute("CREATE TABLE events (" +
|
||||
"date DATETIME " +
|
||||
try db.execute(
|
||||
"CREATE TABLE events (" +
|
||||
"date DATETIME " +
|
||||
")")
|
||||
}
|
||||
try! migrator.migrate(dbQueue)
|
||||
|
||||
@@ -6,14 +6,15 @@ import GRDB
|
||||
// Create the databsae
|
||||
|
||||
var configuration = Configuration()
|
||||
configuration.trace = LogSQL
|
||||
configuration.trace = { print($0) }
|
||||
let dbQueue = DatabaseQueue(configuration: configuration) // Memory database
|
||||
var migrator = DatabaseMigrator()
|
||||
migrator.registerMigration("createPersons") { db in
|
||||
try db.execute("CREATE TABLE persons (" +
|
||||
"id INTEGER PRIMARY KEY, " +
|
||||
"firstName TEXT, " +
|
||||
"lastName TEXT" +
|
||||
try db.execute(
|
||||
"CREATE TABLE persons (" +
|
||||
"id INTEGER PRIMARY KEY, " +
|
||||
"firstName TEXT, " +
|
||||
"lastName TEXT" +
|
||||
")")
|
||||
}
|
||||
try! migrator.migrate(dbQueue)
|
||||
|
||||
@@ -10,16 +10,18 @@ configuration.trace = { print($0) }
|
||||
let dbQueue = DatabaseQueue(configuration: configuration) // Memory database
|
||||
var migrator = DatabaseMigrator()
|
||||
migrator.registerMigration("createPersons") { db in
|
||||
try db.execute("CREATE TABLE persons (" +
|
||||
"id INTEGER PRIMARY KEY, " +
|
||||
"name TEXT NOT NULL " +
|
||||
try db.execute(
|
||||
"CREATE TABLE persons (" +
|
||||
"id INTEGER PRIMARY KEY, " +
|
||||
"name TEXT NOT NULL " +
|
||||
")")
|
||||
}
|
||||
migrator.registerMigration("createPets") { db in
|
||||
try db.execute("CREATE TABLE pets (" +
|
||||
"id INTEGER PRIMARY KEY, " +
|
||||
"name TEXT, " +
|
||||
"ownerId INTEGER NOT NULL REFERENCES persons(id) ON DELETE CASCADE" +
|
||||
try db.execute(
|
||||
"CREATE TABLE pets (" +
|
||||
"id INTEGER PRIMARY KEY, " +
|
||||
"name TEXT, " +
|
||||
"ownerId INTEGER NOT NULL REFERENCES persons(id) ON DELETE CASCADE" +
|
||||
")")
|
||||
}
|
||||
try! migrator.migrate(dbQueue)
|
||||
|
||||
Reference in new issue
Block a user