From 1cdc111e31110e00f80dc2a48b0dae9f1ce251be Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Gwendal=20Roue=CC=81?= Date: Fri, 5 Feb 2016 21:22:49 +0100 Subject: [PATCH] Record.playground formatting --- Playgrounds/Record.playground/Contents.swift | 103 +++++++++++++----- .../Record.playground/contents.xcplayground | 2 +- 2 files changed, 75 insertions(+), 30 deletions(-) diff --git a/Playgrounds/Record.playground/Contents.swift b/Playgrounds/Record.playground/Contents.swift index be725c403..138914c9c 100644 --- a/Playgrounds/Record.playground/Contents.swift +++ b/Playgrounds/Record.playground/Contents.swift @@ -1,19 +1,24 @@ -// To run this playground, select and build the GRDBOSX scheme. +//: To run this playground, select and build the GRDBOSX scheme. +//: +//: Record +//: ====== +//: +//: This playground is a demo of the Record class, the type that provides fetching methods, persistence methods, and change tracking. import GRDB -// ============================================== -// SETUP - -// Create the databsae +//: ## Setup +//: +//: First open an in-memory database, with a trace function that prints all SQL statements. var configuration = Configuration() -configuration.trace = { print($0) } // Log all SQL statements +configuration.trace = { print($0) } +let dbQueue = DatabaseQueue(configuration: configuration) -let dbQueue = DatabaseQueue(configuration: configuration) // Memory database -var migrator = DatabaseMigrator() -migrator.registerMigration("createPersons") { db in +//: Create a database table which stores persons. + +try dbQueue.inDatabase { db in try db.execute( "CREATE TABLE persons (" + "id INTEGER PRIMARY KEY, " + @@ -21,33 +26,39 @@ migrator.registerMigration("createPersons") { db in "lastName TEXT" + ")") } -try! migrator.migrate(dbQueue) -// Define a Record +//: ## Subclassing Record +//: +//: The Person class is a subclass of Record, with regular properties, and a regular initializer: class Person : Record { var id: Int64? var firstName: String? var lastName: String? + var fullName: String { return [firstName, lastName].flatMap { $0 }.joinWithSeparator(" ") } - init(id: Int64? = nil, firstName: String?, lastName: String?) { - self.id = id + init(firstName: String?, lastName: String?) { + self.id = nil self.firstName = firstName self.lastName = lastName super.init() } - // Record overrides +//: Subclasses of Record have to override the methods that define how they interact with the database. +//: +//: 1. The table name: override class func databaseTableName() -> String { return "persons" } +//: 2. How to build a Person from a database row: + required init(_ row: Row) { id = row.value(named: "id") firstName = row.value(named: "firstName") @@ -55,36 +66,70 @@ class Person : Record { super.init(row) } +//: 3. The dictionary of values that are stored in the database: + override var persistentDictionary: [String: DatabaseValueConvertible?] { return ["id": id, "firstName": firstName, "lastName": lastName] } +//: 4. When relevant, update the person's id after a database row has been inserted: + override func didInsertWithRowID(rowID: Int64, forColumn column: String?) { id = rowID } } -// Define colums +//: ## Insert Records +//: +//: Persons are regular objects, that you can freely create: + +let arthur = Person(firstName: "Arthur", lastName: "Miller") +let barbra = Person(firstName: "Barbra", lastName: "Streisand") +let cinderella = Person(firstName: "Cinderella", lastName: nil) + +//: They are not stored in the database yet. Insert them: + +try dbQueue.inDatabase { db in + try arthur.insert(db) + try barbra.insert(db) + try cinderella.insert(db) +} + + +//: ## Fetching Records + +dbQueue.inDatabase { db in +//: Fetch records from the database: + let allPersons = Person.fetchAll(db) + +//: 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(db, "SELECT * FROM persons WHERE lastName = ?", arguments: ["Miller"]) + millers.first!.fullName +} + + +//: ## The Query Interface +//: +//: The query interface lets you write Swift instead of SQL. +//: +//: First define the colums you want to use in the query interface struct Col { static let firstName = SQLColumn("firstName") static let lastName = SQLColumn("lastName") } +//: Use columns to filter or order fetched records: -// END OF SETUP -// ============================================== - -try! dbQueue.inTransaction { db in - try Person(firstName: "Arthur", lastName: "Miller").insert(db) - try Person(firstName: "Barbra", lastName: "Streisand").insert(db) - try Person(firstName: "Cinderella", lastName: nil).insert(db) - return .Commit +dbQueue.inDatabase { db in +//: Use columns for sorting: + let personsSortedByName = Person.order(Col.firstName, Col.lastName).fetchAll(db) + +//: Use columns for filtering: + let millers = Person.filter(Col.lastName == "Miller").fetchAll(db) } - -let persons = dbQueue.inDatabase { db in - Person.order(Col.firstName, Col.lastName).fetchAll(db) -} - -print(persons.map { $0.fullName }) diff --git a/Playgrounds/Record.playground/contents.xcplayground b/Playgrounds/Record.playground/contents.xcplayground index 06828af92..3de2b51ba 100644 --- a/Playgrounds/Record.playground/contents.xcplayground +++ b/Playgrounds/Record.playground/contents.xcplayground @@ -1,4 +1,4 @@ - + \ No newline at end of file