Skip to content

Instantly share code, notes, and snippets.

View groue's full-sized avatar

Gwendal Roué groue

View GitHub Profile
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
import GRDB
class Person: Record {
var id: Int64?
var name: String
var score: Int
}
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 " +
")")
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)
}
import UIKit
@UIApplicationMain
class AppDelegate: UIResponder, UIApplicationDelegate {
var window: UIWindow?
func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool {
setupDatabase()
return true
}
@groue
groue / TableChangeObservers.swift
Last active May 12, 2018 07:43
Notification of table changes in an SQLite database http://github.com/groue/GRDB.swift
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 {
@groue
groue / DatabaseTimestamp.swift
Last active January 16, 2016 16:21
A type that reads and stores NSDate as timestamp in an SQLite database http://github.com/groue/GRDB.swift
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
@groue
groue / synchronize.swift
Last active March 9, 2021 16:02
How to synchronize an SQLite table with a JSON payload using https://github.com/groue/GRDB.swift
// 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
/// 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.
// 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