mirror of
https://github.com/groue/GRDB.swift
synced 2026-11-27 04:14:21 +00:00
Update playgrounds to block-based database accesses.
This commit is contained in:
1 parent
4bcff181c7
commit
af53fe8e79
5 files changed
+77
-86
No files matched your search
@@ -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)
|
||||
@@ -1,6 +0,0 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<Timeline
|
||||
version = "3.0">
|
||||
<TimelineItems>
|
||||
</TimelineItems>
|
||||
</Timeline>
|
||||
@@ -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)
|
||||
}
|
||||
@@ -1,6 +0,0 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<Timeline
|
||||
version = "3.0">
|
||||
<TimelineItems>
|
||||
</TimelineItems>
|
||||
</Timeline>
|
||||
@@ -1,6 +0,0 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<Timeline
|
||||
version = "3.0">
|
||||
<TimelineItems>
|
||||
</TimelineItems>
|
||||
</Timeline>
|
||||
Reference in new issue
Block a user