diff --git a/Documentation/Playgrounds/MyPlayground.playground/Contents.swift b/Documentation/Playgrounds/MyPlayground.playground/Contents.swift index 5e8e36d87..0819f2496 100644 --- a/Documentation/Playgrounds/MyPlayground.playground/Contents.swift +++ b/Documentation/Playgrounds/MyPlayground.playground/Contents.swift @@ -8,27 +8,39 @@ configuration.prepareDatabase { db in } let dbQueue = DatabaseQueue(configuration: configuration) -extension SQLSpecificExpressible { - func like(_ pattern: SQLExpressible, escape: SQLExpressible) -> SQLExpression { - SQL("\(self) LIKE \(pattern) ESCAPE \(escape)").sqlExpression +struct Player: Codable, FetchableRecord, MutablePersistableRecord { + var id: Int64? + var name: String + var score: Int + + mutating func didInsert(with rowID: Int64, for column: String?) { + id = rowID } } -struct Player: Codable, FetchableRecord, PersistableRecord { - var id: Int64 - var name: String - var score: Int -} - -try! dbQueue.inDatabase { db in +try dbQueue.write { db in try db.create(table: "player") { t in t.autoIncrementedPrimaryKey("id") t.column("name", .text).notNull() t.column("score", .integer).notNull() } - try Player(id: 1, name: "toto 10% titi", score: 100).insert(db) - try Player(id: 2, name: "toto", score: 100).insert(db) + do { + var player = Player(id: nil, name: "Arthur", score: 100) + try player.insert(db) + player = Player(id: nil, name: "Barbara", score: 100) + try player.insert(db) + } - try Player.filter(Column("name").like("%10\\%%", escape: "\\")).fetchAll(db) + do { + let players = try Player.fetchAll(db) + for player in players { + print(player) + } + } + + do { + let count = try Player.fetchCount(db) + print(count) + } }