Skip to content

Commit 2c12f0a

Browse files
authored
Merge pull request #1652 from groue/dev/schema-doc
Enhance the database schema documentation
2 parents 8aaeb25 + c97f633 commit 2c12f0a

9 files changed

+822
-429
lines changed

GRDB/Core/Database+Schema.swift

+108-21
Original file line numberDiff line numberDiff line change
@@ -96,7 +96,15 @@ extension Database {
9696

9797
// MARK: - Database Schema
9898

99-
/// Returns the current schema version.
99+
/// Returns the current schema version (`PRAGMA schema_version`).
100+
///
101+
/// For example:
102+
///
103+
/// ```swift
104+
/// let version = try dbQueue.read { db in
105+
/// try db.schemaVersion()
106+
/// }
107+
/// ```
100108
///
101109
/// Related SQLite documentation: <https://www.sqlite.org/pragma.html#pragma_schema_version>
102110
public func schemaVersion() throws -> Int32 {
@@ -234,8 +242,19 @@ extension Database {
234242

235243
/// Returns whether a table exists
236244
///
237-
/// When `schemaName` is not specified, known schemas are iterated in
238-
/// SQLite resolution order and the first matching result is returned.
245+
/// When `schemaName` is not specified, the result is true if any known
246+
/// schema contains the table.
247+
///
248+
/// For example:
249+
///
250+
/// ```swift
251+
/// try dbQueue.read { db in
252+
/// if try db.tableExists("player") { ... }
253+
/// if try db.tableExists("player", in: "main") { ... }
254+
/// if try db.tableExists("player", in: "temp") { ... }
255+
/// if try db.tableExists("player", in: "attached") { ... }
256+
/// }
257+
/// ```
239258
///
240259
/// - throws: A ``DatabaseError`` whenever an SQLite error occurs, or
241260
/// if the specified schema does not exist
@@ -278,8 +297,19 @@ extension Database {
278297
/// Returns whether a view exists, in the main or temp schema, or in an
279298
/// attached database.
280299
///
281-
/// When `schemaName` is not specified, known schemas are iterated in
282-
/// SQLite resolution order and the first matching result is returned.
300+
/// When `schemaName` is not specified, the result is true if any known
301+
/// schema contains the table.
302+
///
303+
/// For example:
304+
///
305+
/// ```swift
306+
/// try dbQueue.read { db in
307+
/// if try db.viewExists("player") { ... }
308+
/// if try db.viewExists("player", in: "main") { ... }
309+
/// if try db.viewExists("player", in: "temp") { ... }
310+
/// if try db.viewExists("player", in: "attached") { ... }
311+
/// }
312+
/// ```
283313
///
284314
/// - throws: A ``DatabaseError`` whenever an SQLite error occurs, or
285315
/// if the specified schema does not exist
@@ -296,8 +326,19 @@ extension Database {
296326
/// Returns whether a trigger exists, in the main or temp schema, or in an
297327
/// attached database.
298328
///
299-
/// When `schemaName` is not specified, known schemas are iterated in
300-
/// SQLite resolution order and the first matching result is returned.
329+
/// When `schemaName` is not specified, the result is true if any known
330+
/// schema contains the table.
331+
///
332+
/// For example:
333+
///
334+
/// ```swift
335+
/// try dbQueue.read { db in
336+
/// if try db.triggerExists("on_player_update") { ... }
337+
/// if try db.triggerExists("on_player_update", in: "main") { ... }
338+
/// if try db.triggerExists("on_player_update", in: "temp") { ... }
339+
/// if try db.triggerExists("on_player_update", in: "attached") { ... }
340+
/// }
341+
/// ```
301342
///
302343
/// - throws: A ``DatabaseError`` whenever an SQLite error occurs, or
303344
/// if the specified schema does not exist
@@ -340,6 +381,15 @@ extension Database {
340381
/// table has no explicit primary key, the result is the hidden
341382
/// "rowid" column.
342383
///
384+
/// For example:
385+
///
386+
/// ```swift
387+
/// try dbQueue.read { db in
388+
/// let primaryKey = try db.primaryKey("player")
389+
/// print(primaryKey.columns)
390+
/// }
391+
/// ```
392+
///
343393
/// When `schemaName` is not specified, known schemas are iterated in
344394
/// SQLite resolution order and the first matching result is returned.
345395
///
@@ -529,14 +579,26 @@ extension Database {
529579

530580
/// The indexes on table named `tableName`.
531581
///
582+
/// For example:
583+
///
584+
/// ```swift
585+
/// try dbQueue.read { db in
586+
/// let indexes = db.indexes(in: "player")
587+
/// for index in indexes {
588+
/// print(index.columns)
589+
/// }
590+
/// }
591+
/// ```
592+
///
532593
/// Only indexes on columns are returned. Indexes on expressions are
533594
/// not returned.
534595
///
535-
/// SQLite does not define any index for INTEGER PRIMARY KEY columns: this
536-
/// method does not return any index that represents the primary key.
596+
/// SQLite does not define any index for INTEGER PRIMARY KEY columns:
597+
/// this method does not return any index that represents the
598+
/// primary key.
537599
///
538-
/// If you want to know if a set of columns uniquely identifies a row, because
539-
/// the columns contain the primary key or a unique index, use
600+
/// If you want to know if a set of columns uniquely identifies a row,
601+
/// because the columns contain the primary key or a unique index, use
540602
/// ``table(_:hasUniqueKey:)``.
541603
///
542604
/// When `schemaName` is not specified, known schemas are iterated in
@@ -617,16 +679,19 @@ extension Database {
617679
/// For example:
618680
///
619681
/// ```swift
620-
/// // One table with one primary key (id), and a unique index (a, b):
621-
/// //
622-
/// // > CREATE TABLE t(id INTEGER PRIMARY KEY, a, b, c);
623-
/// // > CREATE UNIQUE INDEX i ON t(a, b);
624-
/// try db.table("t", hasUniqueKey: ["id"]) // true
625-
/// try db.table("t", hasUniqueKey: ["a", "b"]) // true
626-
/// try db.table("t", hasUniqueKey: ["b", "a"]) // true
627-
/// try db.table("t", hasUniqueKey: ["c"]) // false
628-
/// try db.table("t", hasUniqueKey: ["id", "a"]) // true
629-
/// try db.table("t", hasUniqueKey: ["id", "a", "b", "c"]) // true
682+
/// try dbQueue.read { db in
683+
/// // One table with one primary key (id)
684+
/// // and a unique index (a, b):
685+
/// //
686+
/// // > CREATE TABLE t(id INTEGER PRIMARY KEY, a, b, c);
687+
/// // > CREATE UNIQUE INDEX i ON t(a, b);
688+
/// try db.table("t", hasUniqueKey: ["id"]) // true
689+
/// try db.table("t", hasUniqueKey: ["a", "b"]) // true
690+
/// try db.table("t", hasUniqueKey: ["b", "a"]) // true
691+
/// try db.table("t", hasUniqueKey: ["c"]) // false
692+
/// try db.table("t", hasUniqueKey: ["id", "a"]) // true
693+
/// try db.table("t", hasUniqueKey: ["id", "a", "b", "c"]) // true
694+
/// }
630695
/// ```
631696
public func table(
632697
_ tableName: String,
@@ -640,6 +705,17 @@ extension Database {
640705
/// When `schemaName` is not specified, known schemas are iterated in
641706
/// SQLite resolution order and the first matching result is returned.
642707
///
708+
/// For example:
709+
///
710+
/// ```swift
711+
/// try dbQueue.read { db in
712+
/// let foreignKeys = try db.foreignKeys(in: "player")
713+
/// for foreignKey in foreignKeys {
714+
/// print(foreignKey.destinationTable)
715+
/// }
716+
/// }
717+
/// ```
718+
///
643719
/// - throws: A ``DatabaseError`` whenever an SQLite error occurs, if
644720
/// the specified schema does not exist, or if no such table or view
645721
/// with this name exists in the main or temp schema, or in an attached
@@ -861,6 +937,17 @@ extension Database {
861937
/// When `schemaName` is not specified, known schemas are iterated in
862938
/// SQLite resolution order and the first matching result is returned.
863939
///
940+
/// For example:
941+
///
942+
/// ```swift
943+
/// try dbQueue.read { db in
944+
/// let columns = try db.columns(in: "player")
945+
/// for column in columns {
946+
/// print(column.name)
947+
/// }
948+
/// }
949+
/// ```
950+
///
864951
/// - throws: A ``DatabaseError`` whenever an SQLite error occurs, if
865952
/// the specified schema does not exist,or if no such table or view
866953
/// with this name exists in the main or temp schema, or in an attached

0 commit comments

Comments
 (0)