This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import GRDB | |
class Person: Record { | |
var id: Int64? | |
var name: String | |
var score: Int | |
init(name: String, score: Int) { | |
self.name = name | |
self.score = score |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import GRDB | |
class Person: Record { | |
var id: Int64? | |
var name: String | |
var score: Int | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
func setupDatabase() { | |
// That "collation" helps us compare person names in a localized case insensitive fashion | |
let collation = DatabaseCollation.localizedCaseInsensitiveCompare | |
try! dbQueue.execute( | |
"CREATE TABLE persons (" + | |
"id INTEGER PRIMARY KEY, " + | |
"name TEXT NOT NULL COLLATE \(collation.name), " + | |
"score INTEGER NOT NULL " + | |
")") |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import GRDB | |
/// The shared database queue. | |
var dbQueue: DatabaseQueue! | |
func setupDatabase() { | |
let documentsPath = NSSearchPathForDirectoriesInDomains(.DocumentDirectory, .UserDomainMask, true).first! as NSString | |
let databasePath = documentsPath.stringByAppendingPathComponent("db.sqlite") | |
dbQueue = try! DatabaseQueue(path: databasePath) | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import UIKit | |
@UIApplicationMain | |
class AppDelegate: UIResponder, UIApplicationDelegate { | |
var window: UIWindow? | |
func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool { | |
setupDatabase() | |
return true | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import Foundation | |
import GRDB | |
/// The notification posted when database tables have changed: | |
let DatabaseTablesDidChangeNotification = "DatabaseTablesDidChangeNotification" | |
let ChangedTableNamesKey = "ChangedTableNames" | |
/// TableChangeObserver posts a DatabaseTablesDidChangeNotification on the main | |
/// thread after database tables have changed. | |
class TableChangeObserver : NSObject, TransactionObserverType { |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// This sample code shows how to use GRDB to synchronize a database table | |
// with a JSON payload. We use as few SQL queries as possible: | |
// | |
// - Only one SELECT query. | |
// - One query per insert, delete, and update. | |
// - Useless UPDATE statements are avoided. | |
import Foundation | |
import GRDB |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/// Given two sorted sequences (left and right), this function emits "merge steps" | |
/// which tell whether elements are only found on the left, on the right, or on | |
/// both sides. | |
/// | |
/// Both sequences do not have to share the same element type. Yet elements must | |
/// share a common comparable *key*. | |
/// | |
/// Both sequences must be sorted by this key. | |
/// | |
/// Keys must be unique in both sequences. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Library code | |
class Model { } | |
protocol Fetchable { | |
typealias FetchableType = Self | |
static func fetchAll(sql: String) -> [FetchableType] | |
} | |
extension Fetchable where Self : Model, FetchableType == Self { | |
static func fetchAll(sql: String) -> [FetchableType] { | |
return [] // bare minimal, for demonstration purpose |