diff --git a/GRDB.xcworkspace/contents.xcworkspacedata b/GRDB.xcworkspace/contents.xcworkspacedata index 83fcba978..9c44df313 100644 --- a/GRDB.xcworkspace/contents.xcworkspacedata +++ b/GRDB.xcworkspace/contents.xcworkspacedata @@ -13,6 +13,9 @@ + + diff --git a/Playgrounds/DatabaseTimestamp.playground/Contents.swift b/Playgrounds/DatabaseTimestamp.playground/Contents.swift new file mode 100644 index 000000000..139701b03 --- /dev/null +++ b/Playgrounds/DatabaseTimestamp.playground/Contents.swift @@ -0,0 +1,61 @@ +// To run this playground, select and build the GRDBOSX scheme. + +import GRDB + +struct DatabaseTimestamp: DatabaseValueConvertible { + + // NSDate conversion + // + // Value types should consistently use the Swift nil to represent the + // database NULL: the date property is a non-optional NSDate. + let date: NSDate + + // As a convenience, the NSDate initializer accepts an optional NSDate, and + // is failable: the result is nil if and only if *date* is nil. + init?(_ date: NSDate?) { + guard let date = date else { + return nil + } + self.date = date + } + + + // DatabaseValueConvertible adoption + + /// Returns a value that can be stored in the database. + var databaseValue: DatabaseValue { + // Double itself adopts DatabaseValueConvertible: + return date.timeIntervalSince1970.databaseValue + } + + /// Returns a value initialized from *databaseValue*, if possible. + static func fromDatabaseValue(databaseValue: DatabaseValue) -> DatabaseTimestamp? { + // Double itself adopts DatabaseValueConvertible: + guard let timeInterval = Double.fromDatabaseValue(databaseValue) else { + // No Double, no NSDate! + return nil + } + return DatabaseTimestamp(NSDate(timeIntervalSince1970: timeInterval)) + } +} + + +var configuration = Configuration() +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! migrator.migrate(dbQueue) + +try! dbQueue.inDatabase { db in + try db.execute("INSERT INTO events (date) VALUES (?)", arguments: [DatabaseTimestamp(NSDate())]) + let row = Row.fetchOne(db, "SELECT * FROM events")! + let timestamp: Double = row.value(named: "date") + let date = (row.value(named: "date") as DatabaseTimestamp).date + print("timestamp: \(timestamp)") + print("date: \(date)") +} diff --git a/Playgrounds/DatabaseTimestamp.playground/contents.xcplayground b/Playgrounds/DatabaseTimestamp.playground/contents.xcplayground new file mode 100644 index 000000000..06828af92 --- /dev/null +++ b/Playgrounds/DatabaseTimestamp.playground/contents.xcplayground @@ -0,0 +1,4 @@ + + + + \ No newline at end of file diff --git a/Playgrounds/DatabaseTimestamp.playground/timeline.xctimeline b/Playgrounds/DatabaseTimestamp.playground/timeline.xctimeline new file mode 100644 index 000000000..bf468afec --- /dev/null +++ b/Playgrounds/DatabaseTimestamp.playground/timeline.xctimeline @@ -0,0 +1,6 @@ + + + + + diff --git a/Playgrounds/JSONSynchronization.playground/Contents.swift b/Playgrounds/JSONSynchronization.playground/Contents.swift index 9a93213a7..4ffb1c694 100644 --- a/Playgrounds/JSONSynchronization.playground/Contents.swift +++ b/Playgrounds/JSONSynchronization.playground/Contents.swift @@ -148,4 +148,4 @@ do { try synchronizePersonsWithJSON(jsonString, inDatabase: db) return .Commit } -} \ No newline at end of file +} diff --git a/Playgrounds/TransactionObserver.playground/Contents.swift b/Playgrounds/TransactionObserver.playground/Contents.swift index ffa0dd640..30e133d68 100644 --- a/Playgrounds/TransactionObserver.playground/Contents.swift +++ b/Playgrounds/TransactionObserver.playground/Contents.swift @@ -1,4 +1,4 @@ -//: Playground - noun: a place where people can play +// To run this playground, select and build the GRDBOSX scheme. import GRDB @@ -6,6 +6,7 @@ import GRDB // Create the databsae var configuration = Configuration() +configuration.trace = { print($0) } let dbQueue = DatabaseQueue(configuration: configuration) // Memory database var migrator = DatabaseMigrator() migrator.registerMigration("createPersons") { db in