diff --git a/GRDB/QueryInterface/Request/RequestProtocols.swift b/GRDB/QueryInterface/Request/RequestProtocols.swift index 2ee094189..553511e19 100644 --- a/GRDB/QueryInterface/Request/RequestProtocols.swift +++ b/GRDB/QueryInterface/Request/RequestProtocols.swift @@ -498,7 +498,7 @@ extension TableRequest where Self: FilteredRequest, Self: TypedRequest { /// When executed, this request raises a fatal error if no unique index /// exists on a subset of the key columns. /// - /// - parameter key: A unique key. + /// - parameter key: A key dictionary. public func filter(key: [String: (any DatabaseValueConvertible)?]?) -> Self { guard let key else { return none() @@ -526,7 +526,7 @@ extension TableRequest where Self: FilteredRequest, Self: TypedRequest { /// When executed, this request raises a fatal error if no unique index /// exists on a subset of the key columns. /// - /// - parameter keys: A collection of unique keys + /// - parameter keys: An array of key dictionaries. public func filter(keys: [[String: (any DatabaseValueConvertible)?]]) -> Self { if keys.isEmpty { return none() diff --git a/GRDB/QueryInterface/SQL/Table.swift b/GRDB/QueryInterface/SQL/Table.swift index 6b5b578a6..d6ec258be 100644 --- a/GRDB/QueryInterface/SQL/Table.swift +++ b/GRDB/QueryInterface/SQL/Table.swift @@ -538,7 +538,7 @@ extension Table { /// When executed, this request raises a fatal error if no unique index /// exists on a subset of the key columns. /// - /// - parameter key: A unique key. + /// - parameter key: A key dictionary. public func filter(key: [String: (any DatabaseValueConvertible)?]?) -> QueryInterfaceRequest { all().filter(key: key) } @@ -566,7 +566,7 @@ extension Table { /// When executed, this request raises a fatal error if no unique index /// exists on a subset of the key columns. /// - /// - parameter keys: A collection of unique keys + /// - parameter keys: An array of key dictionaries. public func filter(keys: [[String: (any DatabaseValueConvertible)?]]) -> QueryInterfaceRequest { all().filter(keys: keys) } @@ -1611,7 +1611,7 @@ extension Table { /// /// - parameters: /// - db: A database connection. - /// - key: A unique key. + /// - key: A key dictionary. /// - returns: Whether a row exists for this key. public func exists(_ db: Database, key: [String: (any DatabaseValueConvertible)?]) throws -> Bool { try !filter(key: key).isEmpty(db) @@ -1842,7 +1842,7 @@ extension Table { /// key columns. /// - parameters: /// - db: A database connection. - /// - key: A dictionary of values. + /// - key: A key dictionary. /// - returns: Whether a row was deleted. @discardableResult public func deleteOne(_ db: Database, key: [String: (any DatabaseValueConvertible)?]) throws -> Bool { diff --git a/GRDB/QueryInterface/Schema/TableDefinition.swift b/GRDB/QueryInterface/Schema/TableDefinition.swift index 842d0edc3..b1e538026 100644 --- a/GRDB/QueryInterface/Schema/TableDefinition.swift +++ b/GRDB/QueryInterface/Schema/TableDefinition.swift @@ -467,14 +467,32 @@ public final class TableDefinition { /// /// ```swift /// // CREATE TABLE citizenship ( - /// // citizenID INTEGER NOT NULL, + /// // citizenId INTEGER NOT NULL, /// // countryCode TEXT NOT NULL, - /// // PRIMARY KEY (citizenID, countryCode) + /// // PRIMARY KEY (citizenId, countryCode) /// // ) /// try db.create(table: "citizenship") { t in - /// t.column("citizenID", .integer).notNull() + /// t.column("citizenId", .integer).notNull() /// t.column("countryCode", .text).notNull() - /// t.primaryKey(["citizenID", "countryCode"]) + /// t.primaryKey(["citizenId", "countryCode"]) + /// } + /// ``` + /// + /// - important: Make sure you add not null constraints on your primary key + /// columns, as in the above example, or SQLite will allow null values. + /// See + /// for more information. + /// + /// When defining a primary key on a single column, you can use the + /// ``ColumnDefinition/primaryKey(onConflict:autoincrement:)`` + /// shortcut: + /// + /// ```swift + /// // CREATE TABLE player( + /// // id TEXT NOT NULL PRIMARY KEY + /// // ) + /// try db.create(table: "player") { t in + /// t.column("id", .text).notNull().primaryKey() /// } /// ``` /// @@ -510,6 +528,18 @@ public final class TableDefinition { /// } /// ``` /// + /// When defining a unique constraint on a single column, you can use the + /// ``ColumnDefinition/unique(onConflict:)`` shortcut: + /// + /// ```swift + /// // CREATE TABLE player( + /// // email TEXT UNIQUE + /// // ) + /// try db.create(table: "player") { t in + /// t.column("email", .text).unique() + /// } + /// ``` + /// /// Related SQLite documentation: /// /// - parameter columns: The unique key columns. @@ -526,17 +556,27 @@ public final class TableDefinition { /// ```swift /// // CREATE TABLE passport ( /// // issueDate DATE NOT NULL, - /// // citizenID INTEGER NOT NULL, + /// // citizenId INTEGER NOT NULL, /// // countryCode INTEGER NOT NULL, - /// // FOREIGN KEY (citizenID, countryCode) - /// // REFERENCES citizenship(citizenID, countryCode) + /// // FOREIGN KEY (citizenId, countryCode) + /// // REFERENCES citizenship(citizenId, countryCode) /// // ON DELETE CASCADE /// // ) /// try db.create(table: "passport") { t in /// t.column("issueDate", .date).notNull() - /// t.column("citizenID", .integer).notNull() + /// t.column("citizenId", .integer).notNull() /// t.column("countryCode", .text).notNull() - /// t.foreignKey(["citizenID", "countryCode"], references: "citizenship", onDelete: .cascade) + /// t.foreignKey(["citizenId", "countryCode"], references: "citizenship", onDelete: .cascade) + /// } + /// ``` + /// + /// When defining a foreign key on a single column, you can use the + /// ``ColumnDefinition/references(_:column:onDelete:onUpdate:deferred:)`` + /// shortcut: + /// + /// ```swift + /// try db.create(table: "player") { t in + /// t.column("teamId", .integer).references("team", onDelete: .cascade) /// } /// ``` /// @@ -588,6 +628,18 @@ public final class TableDefinition { /// } /// ``` /// + /// When defining a check constraint on a single column, you can use the + /// ``ColumnDefinition/check(_:)`` shortcut: + /// + /// ```swift + /// // CREATE TABLE player( + /// // name TEXT CHECK (LENGTH(name) > 0) + /// // ) + /// try db.create(table: "player") { t in + /// t.column("name", .text).check { length($0) > 0 } + /// } + /// ``` + /// /// Related SQLite documentation: /// /// - parameter condition: The checked condition. @@ -612,6 +664,18 @@ public final class TableDefinition { /// } /// ``` /// + /// When defining a check constraint on a single column, you can use the + /// ``ColumnDefinition/check(sql:)`` shortcut: + /// + /// ```swift + /// // CREATE TABLE player( + /// // name TEXT CHECK (LENGTH(name) > 0) + /// // ) + /// try db.create(table: "player") { t in + /// t.column("name", .text).check(sql: "LENGTH(name) > 0") + /// } + /// ``` + /// /// Related SQLite documentation: /// /// - parameter sql: An SQL snippet @@ -642,8 +706,8 @@ public final class TableDefinition { /// ``SQL`` literals allow you to safely embed raw values in your SQL, /// without any risk of syntax errors or SQL injection: /// - /// // CREATE TABLE player ( /// ```swift + /// // CREATE TABLE player ( /// // score INTEGER, /// // CHECK (score >= 0) /// // ) @@ -1126,7 +1190,7 @@ public final class ColumnDefinition { /// } /// ``` /// - /// - warning: Make sure you add a not null constraint on your primary key + /// - important: Make sure you add a not null constraint on your primary key /// column, as in the above example, or SQLite will allow null values. /// See /// for more information. diff --git a/GRDB/QueryInterface/TableRecord+QueryInterfaceRequest.swift b/GRDB/QueryInterface/TableRecord+QueryInterfaceRequest.swift index 21af9630b..00ec06396 100644 --- a/GRDB/QueryInterface/TableRecord+QueryInterfaceRequest.swift +++ b/GRDB/QueryInterface/TableRecord+QueryInterfaceRequest.swift @@ -374,7 +374,7 @@ extension TableRecord { /// When executed, this request raises a fatal error if no unique index /// exists on a subset of the key columns. /// - /// - parameter key: A unique key. + /// - parameter key: A key dictionary. public static func filter(key: [String: (any DatabaseValueConvertible)?]?) -> QueryInterfaceRequest { all().filter(key: key) } @@ -402,7 +402,7 @@ extension TableRecord { /// When executed, this request raises a fatal error if no unique index /// exists on a subset of the key columns. /// - /// - parameter keys: A collection of unique keys + /// - parameter keys: An array of key dictionaries. public static func filter(keys: [[String: (any DatabaseValueConvertible)?]]) -> QueryInterfaceRequest { all().filter(keys: keys) } diff --git a/GRDB/Record/FetchableRecord+TableRecord.swift b/GRDB/Record/FetchableRecord+TableRecord.swift index ceb76c048..7dcc3daa7 100644 --- a/GRDB/Record/FetchableRecord+TableRecord.swift +++ b/GRDB/Record/FetchableRecord+TableRecord.swift @@ -266,7 +266,7 @@ extension FetchableRecord where Self: TableRecord & Identifiable, ID: DatabaseVa /// ```swift /// try dbQueue.read { db in /// let player = try Player.fetchOne(db, id: 123) - /// let country = try Country.fetchOne(db, key: "FR") + /// let country = try Country.fetchOne(db, id: "FR") /// } /// ``` /// @@ -421,7 +421,7 @@ extension FetchableRecord where Self: TableRecord { /// /// - parameters: /// - db: A database connection. - /// - key: A dictionary of values. + /// - key: A key dictionary. /// - returns: An optional record. /// - throws: A ``DatabaseError`` whenever an SQLite error occurs. public static func fetchOne(_ db: Database, key: [String: (any DatabaseValueConvertible)?]?) throws -> Self? { diff --git a/GRDB/Record/TableRecord.swift b/GRDB/Record/TableRecord.swift index cb3f26c74..2c91dd50a 100644 --- a/GRDB/Record/TableRecord.swift +++ b/GRDB/Record/TableRecord.swift @@ -362,7 +362,7 @@ extension TableRecord { /// /// - parameters: /// - db: A database connection. - /// - key: A unique key. + /// - key: A key dictionary. /// - returns: Whether a record exists for this key. public static func exists(_ db: Database, key: [String: (any DatabaseValueConvertible)?]) throws -> Bool { try !filter(key: key).isEmpty(db) @@ -552,7 +552,7 @@ extension TableRecord { return try filter(keys: keys).deleteAll(db) } - /// Deletes the record identified by its primary or unique keys, and returns + /// Deletes the record identified by its primary or unique key, and returns /// whether a record was deleted. /// /// For example: @@ -580,7 +580,7 @@ extension TableRecord { /// key columns. /// - parameters: /// - db: A database connection. - /// - key: A dictionary of values. + /// - key: A key dictionary. /// - returns: Whether a record was deleted. @discardableResult public static func deleteOne(_ db: Database, key: [String: (any DatabaseValueConvertible)?]) throws -> Bool {