diff --git a/Playgrounds/Record.playground/Contents.swift b/Playgrounds/Record.playground/Contents.swift index 27e4dd506..c46c9609e 100644 --- a/Playgrounds/Record.playground/Contents.swift +++ b/Playgrounds/Record.playground/Contents.swift @@ -99,27 +99,30 @@ try dbQueue.inDatabase { db in //: ## Fetching Records -//: Fetch records from the database: -let allPersons = Person.fetchAll(dbQueue) +dbQueue.inDatabase { db in + + //: Fetch records from the database: + let allPersons = Person.fetchAll(db) -//: Fetch record by primary key: -let person = Person.fetchOne(dbQueue, key: arthur.id)! -person.fullName + //: Fetch record by primary key: + let person = Person.fetchOne(db, key: arthur.id)! + person.fullName -//: Fetch persons with an SQL query: -let millers = Person.fetchAll(dbQueue, "SELECT * FROM persons WHERE lastName = ?", arguments: ["Miller"]) -millers.first!.fullName + //: Fetch persons with an SQL query: + let millers = Person.fetchAll(db, "SELECT * FROM persons WHERE lastName = ?", arguments: ["Miller"]) + millers.first!.fullName -//: To fetch persons using the query interface, you need some colums that can filter or sort: + //: To fetch persons using the query interface, you need some colums that can filter or sort: -struct Col { - static let firstName = SQLColumn("firstName") - static let lastName = SQLColumn("lastName") + struct Col { + static let firstName = SQLColumn("firstName") + static let lastName = SQLColumn("lastName") + } + + //: Sort + let personsSortedByName = Person.order(Col.firstName, Col.lastName).fetchAll(db) + + //: Filter + let streisands = Person.filter(Col.lastName == "Streisand").fetchAll(db) } - -//: Sort -let personsSortedByName = Person.order(Col.firstName, Col.lastName).fetchAll(dbQueue) - -//: Filter -let streisands = Person.filter(Col.lastName == "Streisand").fetchAll(dbQueue) diff --git a/Playgrounds/Record.playground/timeline.xctimeline b/Playgrounds/Record.playground/timeline.xctimeline deleted file mode 100644 index bf468afec..000000000 --- a/Playgrounds/Record.playground/timeline.xctimeline +++ /dev/null @@ -1,6 +0,0 @@ - - - - - diff --git a/Playgrounds/Tour.playground/Contents.swift b/Playgrounds/Tour.playground/Contents.swift index 597a755db..29522bce4 100644 --- a/Playgrounds/Tour.playground/Contents.swift +++ b/Playgrounds/Tour.playground/Contents.swift @@ -19,35 +19,39 @@ let dbQueue = DatabaseQueue(configuration: configuration) //: Execute SQL queries -try dbQueue.execute( - "CREATE TABLE pointOfInterests (" + - "id INTEGER PRIMARY KEY, " + - "title TEXT, " + - "favorite BOOLEAN NOT NULL, " + - "latitude DOUBLE NOT NULL, " + - "longitude DOUBLE NOT NULL" + - ")") - -let parisId = try dbQueue.execute( - "INSERT INTO pointOfInterests (title, favorite, latitude, longitude) " + - "VALUES (?, ?, ?, ?)", - arguments: ["Paris", true, 48.85341, 2.3488]).insertedRowID! +try dbQueue.inDatabase { db in + try db.execute( + "CREATE TABLE pointOfInterests (" + + "id INTEGER PRIMARY KEY, " + + "title TEXT, " + + "favorite BOOLEAN NOT NULL, " + + "latitude DOUBLE NOT NULL, " + + "longitude DOUBLE NOT NULL" + + ")") + + let parisId = try db.execute( + "INSERT INTO pointOfInterests (title, favorite, latitude, longitude) " + + "VALUES (?, ?, ?, ?)", + arguments: ["Paris", true, 48.85341, 2.3488]).insertedRowID! +} //: Fetch database rows and values -for row in Row.fetchAll(dbQueue, "SELECT * FROM pointOfInterests") { - let title: String = row.value(named: "title") - let favorite: Bool = row.value(named: "favorite") - let coordinate = CLLocationCoordinate2DMake( - row.value(named: "latitude"), - row.value(named: "longitude")) - print("Fetched", title, favorite, coordinate) +try dbQueue.inDatabase { db in + for row in Row.fetchAll(db, "SELECT * FROM pointOfInterests") { + let title: String = row.value(named: "title") + let favorite: Bool = row.value(named: "favorite") + let coordinate = CLLocationCoordinate2DMake( + row.value(named: "latitude"), + row.value(named: "longitude")) + print("Fetched", title, favorite, coordinate) + } + + let poiCount = Int.fetchOne(db, "SELECT COUNT(*) FROM pointOfInterests")! // Int + let poiTitles = String.fetchAll(db, "SELECT title FROM pointOfInterests") // [String] } -let poiCount = Int.fetchOne(dbQueue, "SELECT COUNT(*) FROM pointOfInterests")! // Int -let poiTitles = String.fetchAll(dbQueue, "SELECT title FROM pointOfInterests") // [String] - //: Insert and fetch records @@ -94,30 +98,32 @@ extension PointOfInterest : MutablePersistable { } } -var berlin = PointOfInterest( - id: nil, - title: "Berlin", - favorite: false, - coordinate: CLLocationCoordinate2DMake(52.52437, 13.41053)) - -try berlin.insert(dbQueue) -berlin.id // some value - -berlin.favorite = true -try berlin.update(dbQueue) - -// Fetch from SQL -let pois = PointOfInterest.fetchAll(dbQueue, "SELECT * FROM pointOfInterests") // [PointOfInterest] - - -//: Avoid SQL with the query interface: - -let title = SQLColumn("title") -let favorite = SQLColumn("favorite") - -berlin = PointOfInterest.filter(title == "Berlin").fetchOne(dbQueue)! // PointOfInterest -let paris = PointOfInterest.fetchOne(dbQueue, key: 1) // PointOfInterest? -let favoritePois = PointOfInterest // [PointOfInterest] - .filter(favorite) - .order(title) - .fetchAll(dbQueue) +try dbQueue.inDatabase { db in + var berlin = PointOfInterest( + id: nil, + title: "Berlin", + favorite: false, + coordinate: CLLocationCoordinate2DMake(52.52437, 13.41053)) + + try berlin.insert(db) + berlin.id // some value + + berlin.favorite = true + try berlin.update(db) + + // Fetch from SQL + let pois = PointOfInterest.fetchAll(db, "SELECT * FROM pointOfInterests") // [PointOfInterest] + + + //: Avoid SQL with the query interface: + + let title = SQLColumn("title") + let favorite = SQLColumn("favorite") + + berlin = PointOfInterest.filter(title == "Berlin").fetchOne(db)! // PointOfInterest + let paris = PointOfInterest.fetchOne(db, key: 1) // PointOfInterest? + let favoritePois = PointOfInterest // [PointOfInterest] + .filter(favorite) + .order(title) + .fetchAll(db) +} \ No newline at end of file diff --git a/Playgrounds/Tour.playground/timeline.xctimeline b/Playgrounds/Tour.playground/timeline.xctimeline deleted file mode 100644 index bf468afec..000000000 --- a/Playgrounds/Tour.playground/timeline.xctimeline +++ /dev/null @@ -1,6 +0,0 @@ - - - - - diff --git a/Playgrounds/UserDefaults.playground/timeline.xctimeline b/Playgrounds/UserDefaults.playground/timeline.xctimeline deleted file mode 100644 index bf468afec..000000000 --- a/Playgrounds/UserDefaults.playground/timeline.xctimeline +++ /dev/null @@ -1,6 +0,0 @@ - - - - -