From f3cc0a07bc9ce353128e0fa461dbb2dbc8469a98 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Gwendal=20Roue=CC=81?= Date: Mon, 28 Nov 2016 16:33:14 +0100 Subject: [PATCH] Update sample playgrounds --- GRDB.xcworkspace/contents.xcworkspacedata | 3 - .../Contents.swift | 61 ------------------- .../contents.xcplayground | 4 -- .../Contents.swift | 3 +- .../MyPlayground.playground/Contents.swift | 3 +- Playgrounds/Record.playground/Contents.swift | 7 ++- Playgrounds/Tour.playground/Contents.swift | 16 ++--- 7 files changed, 17 insertions(+), 80 deletions(-) delete mode 100644 Playgrounds/DatabaseTimestamp.playground/Contents.swift delete mode 100644 Playgrounds/DatabaseTimestamp.playground/contents.xcplayground diff --git a/GRDB.xcworkspace/contents.xcworkspacedata b/GRDB.xcworkspace/contents.xcworkspacedata index 9879bfae9..993c59dd8 100644 --- a/GRDB.xcworkspace/contents.xcworkspacedata +++ b/GRDB.xcworkspace/contents.xcworkspacedata @@ -7,9 +7,6 @@ - - diff --git a/Playgrounds/DatabaseTimestamp.playground/Contents.swift b/Playgrounds/DatabaseTimestamp.playground/Contents.swift deleted file mode 100644 index e959548b9..000000000 --- a/Playgrounds/DatabaseTimestamp.playground/Contents.swift +++ /dev/null @@ -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)" \ No newline at end of file diff --git a/Playgrounds/DatabaseTimestamp.playground/contents.xcplayground b/Playgrounds/DatabaseTimestamp.playground/contents.xcplayground deleted file mode 100644 index 06828af92..000000000 --- a/Playgrounds/DatabaseTimestamp.playground/contents.xcplayground +++ /dev/null @@ -1,4 +0,0 @@ - - - - \ No newline at end of file diff --git a/Playgrounds/JSONSynchronization.playground/Contents.swift b/Playgrounds/JSONSynchronization.playground/Contents.swift index 480b2956c..ec0eb2f1b 100644 --- a/Playgrounds/JSONSynchronization.playground/Contents.swift +++ b/Playgrounds/JSONSynchronization.playground/Contents.swift @@ -147,4 +147,5 @@ do { // INSERT INTO "persons" ("id","name") VALUES (4,'Daniel') try synchronizePersons(with: jsonString, in: db) return .commit - \ No newline at end of file + } +} diff --git a/Playgrounds/MyPlayground.playground/Contents.swift b/Playgrounds/MyPlayground.playground/Contents.swift index 3ed097e74..31c58b913 100644 --- a/Playgrounds/MyPlayground.playground/Contents.swift +++ b/Playgrounds/MyPlayground.playground/Contents.swift @@ -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 \ No newline at end of file + print(names) +} diff --git a/Playgrounds/Record.playground/Contents.swift b/Playgrounds/Record.playground/Contents.swift index ea74dc017..58fa8e996 100644 --- a/Playgrounds/Record.playground/Contents.swift +++ b/Playgrounds/Record.playground/Contents.swift @@ -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 \ No newline at end of file + let streisands = try Person.filter(Col.lastName == "Streisand").fetchAll(db) +} diff --git a/Playgrounds/Tour.playground/Contents.swift b/Playgrounds/Tour.playground/Contents.swift index 1489cbd58..f0fdeb3b6 100644 --- a/Playgrounds/Tour.playground/Contents.swift +++ b/Playgrounds/Tour.playground/Contents.swift @@ -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 \ No newline at end of file + .fetchAll(db) +}