DatabaseTimestamp playground

This commit is contained in:
Gwendal Roué committed 2016-01-16 17:22:30 +01:00
1 parent a8f98b7880
commit 831dbfbe4e
6 files changed
+77 -2

No files matched your search

+3
View File
@@ -13,6 +13,9 @@
<FileRef
location = "group:TransactionObserver.playground">
</FileRef>
<FileRef
location = "group:DatabaseTimestamp.playground">
</FileRef>
</Group>
<FileRef
location = "group:GRDB.xcodeproj">
@@ -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)")
}
@@ -0,0 +1,4 @@
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<playground version='5.0' target-platform='osx'>
<timeline fileName='timeline.xctimeline'/>
</playground>
@@ -0,0 +1,6 @@
<?xml version="1.0" encoding="UTF-8"?>
<Timeline
version = "3.0">
<TimelineItems>
</TimelineItems>
</Timeline>
@@ -148,4 +148,4 @@ do {
try synchronizePersonsWithJSON(jsonString, inDatabase: db)
return .Commit
}
}
}
@@ -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