mirror of
https://github.com/groue/GRDB.swift
synced 2026-11-24 04:07:47 +00:00
2db6e33f421bc766bc4ac7e30578946cc1b8a0ef
GRDB
A sqlite3 wrapper for Swift 2.
Goals
- RAII
- Leverage the Swift standard library (errors, generators, etc.)
- Foster SQL
- Focus on rows, not on tables.
Inspirations
Usage (work in progress)
// GRDB uses database queues for database accesses serialization,
// just like ccgus/FMDB:
let dbQueue = try DatabaseQueue(path: "/tmp/GRDB.sqlite")
// DatabaseMigrator sets up migrations:
var migrator = DatabaseMigrator()
migrator.registerMigration("createPersons") { db in
try db.execute(
"CREATE TABLE persons (" +
"id INTEGER PRIMARY KEY, " +
"name TEXT, " +
"age INT)")
}
migrator.registerMigration("createPets") { db in
// Support for foreign keys is enabled by default:
try db.execute(
"CREATE TABLE pets (" +
"id INTEGER PRIMARY KEY, " +
"masterID INTEGER NOT NULL " +
" REFERENCES persons(id) " +
" ON DELETE CASCADE ON UPDATE CASCADE, " +
"name TEXT)")
}
try migrator.migrate(dbQueue)
// Transactions:
try dbQueue.inTransaction { db in
try db.execute(
"INSERT INTO persons (name, age) VALUES (?, ?)",
bindings: ["Arthur", 36])
try db.execute(
"INSERT INTO persons (name, age) VALUES (:name, :age)",
bindings: [":name": "Barbara", ":age": 37])
return .Commit
}
// Fetching rows and values:
dbQueue.inDatabase { db in
for row in db.fetchRows("SELECT * FROM persons") {
// Leverage Swift type inference
let name: String? = row.value(atIndex: 1)
// Force unwrap when column is NOT NULL
let id: Int64 = row.value(named: "id")!
// Both Int and Int64 are supported
let age: Int? = row.value(named: "age")
print("id: \(id), name: \(name), age: \(age)")
}
// Value sequences require explicit `type` parameter
for name in db.fetchValues("SELECT name FROM persons", type: String.self) {
// name is `String?` because some rows may have a NULL name.
print(name)
}
}
// Extracting values out of a database block:
let names = dbQueue.inDatabase { db in
db.fetchValues("SELECT name FROM persons ORDER BY name", type: String.self).map { $0! }
}
// names is [String]: ["Arthur", "Barbara"]
Languages
Swift
99.4%
Ruby
0.3%
Makefile
0.2%
C
0.1%