Update sample playgrounds

This commit is contained in:
Gwendal Roué committed 2016-11-28 16:33:14 +01:00
1 parent 5e2dc95331
commit f3cc0a07bc
7 files changed
+17 -80

No files matched your search

-3
View File
@@ -7,9 +7,6 @@
<FileRef
location = "group:Tour.playground">
</FileRef>
<FileRef
location = "group:DatabaseTimestamp.playground">
</FileRef>
<FileRef
location = "group:JSONSynchronization.playground">
</FileRef>
@@ -1,61 +0,0 @@
// 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 = try 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)"
@@ -1,4 +0,0 @@
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<playground version='5.0' target-platform='osx'>
<timeline fileName='timeline.xctimeline'/>
</playground>
@@ -147,4 +147,5 @@ do {
// INSERT INTO "persons" ("id","name") VALUES (4,'Daniel')
try synchronizePersons(with: jsonString, in: db)
return .commit
}
}
@@ -17,4 +17,5 @@ try! dbQueue.inDatabase { db in
try db.execute("INSERT INTO persons (name) VALUES (?)", arguments: ["Barbara"])
let names = try String.fetchAll(db, "SELECT name FROM persons")
print(names
print(names)
}
+4 -3
View File
@@ -99,7 +99,7 @@ try dbQueue.inDatabase { db in
//: ## Fetching Records
dbQueue.inDatabase { db in
try dbQueue.inDatabase { db in
//: Fetch records from the database:
let allPersons = try Person.fetchAll(db)
@@ -121,7 +121,8 @@ dbQueue.inDatabase { db in
}
//: Sort
let personsSortedByName = Person.order(Col.firstName, Col.lastName).fetchAll(db)
let personsSortedByName = try Person.order(Col.firstName, Col.lastName).fetchAll(db)
//: Filter
let streisands = Person.filter(Col.lastName == "Streisand").fet
let streisands = try Person.filter(Col.lastName == "Streisand").fetchAll(db)
}
+9 -7
View File
@@ -39,8 +39,9 @@ try dbQueue.inDatabase { db in
//: Fetch database rows and values
dbQueue.inDatabase { db in
for row in Row.fetchAll(db, "SELECT * FROM pointOfInterests") {
try dbQueue.inDatabase { db in
let rows = try Row.fetchCursor(db, "SELECT * FROM pointOfInterests")
while let row = try rows.next() {
let title: String = row.value(named: "title")
let favorite: Bool = row.value(named: "favorite")
let coordinate = CLLocationCoordinate2DMake(
@@ -111,7 +112,7 @@ try dbQueue.inDatabase { db in
try berlin.update(db)
// Fetch from SQL
let pois = PointOfInterest.fetchAll(db, "SELECT * FROM pointOfInterests") // [PointOfInterest]
let pois = try PointOfInterest.fetchAll(db, "SELECT * FROM pointOfInterests") // [PointOfInterest]
//: Avoid SQL with the query interface:
@@ -119,9 +120,10 @@ try dbQueue.inDatabase { db in
let title = Column("title")
let favorite = Column("favorite")
berlin = PointOfInterest.filter(title == "Berlin").fetchOne(db)! // PointOfInterest
let paris = PointOfInterest.fetchOne(db, key: 1) // PointOfInterest?
let favoritePois = PointOfInterest // [PointOfInterest]
berlin = try PointOfInterest.filter(title == "Berlin").fetchOne(db)! // PointOfInterest
let paris = try PointOfInterest.fetchOne(db, key: 1) // PointOfInterest?
let favoritePois = try PointOfInterest // [PointOfInterest]
.filter(favorite)
.order(title)
.fetchAl
.fetchAll(db)
}