From 7a046fb27e5cd4c919dadaa451a60944947d0a80 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Gwendal=20Roue=CC=81?= Date: Sat, 16 Oct 2021 16:46:49 +0200 Subject: [PATCH] exists(_:) quality of life improvements --- .../Request/QueryInterfaceRequest.swift | 15 ++++ GRDB/Record/TableRecord.swift | 89 +++++++++++++++++++ 2 files changed, 104 insertions(+) diff --git a/GRDB/QueryInterface/Request/QueryInterfaceRequest.swift b/GRDB/QueryInterface/Request/QueryInterfaceRequest.swift index 490ff67d9..310bcf76a 100644 --- a/GRDB/QueryInterface/Request/QueryInterfaceRequest.swift +++ b/GRDB/QueryInterface/Request/QueryInterfaceRequest.swift @@ -410,6 +410,21 @@ extension QueryInterfaceRequest { } } +// MARK: - Check Existence + +extension QueryInterfaceRequest { + #warning("TODO: test & document") + /// Returns whether a row exists in this request. + /// + /// try Player.filter(Column("name") == "Arthur").exists(db) + /// + /// - parameter db: A database connection. + /// - returns: Whether a row exists in this request + public func exists(_ db: Database) throws -> Bool { + try SQLRequest("SELECT \(exists())").fetchOne(db)! + } +} + // MARK: - Batch Delete extension QueryInterfaceRequest { diff --git a/GRDB/Record/TableRecord.swift b/GRDB/Record/TableRecord.swift index 9f3f8e623..05728c603 100644 --- a/GRDB/Record/TableRecord.swift +++ b/GRDB/Record/TableRecord.swift @@ -155,6 +155,95 @@ extension TableRecord { } } +// MARK: - Check Existence by Single-Column Primary Key + +extension TableRecord { + /// Returns whether a row exists for this primary key. + /// + /// try Player.exists(db, key: 123) + /// try Country.exists(db, key: "FR") + /// + /// When the table has no explicit primary key, GRDB uses the hidden + /// "rowid" column: + /// + /// try Document.exists(db, key: 1) + /// + /// - parameters: + /// - db: A database connection. + /// - key: A primary key value. + /// - returns: Whether a row exists for this primary key. + public static func exists(_ db: Database, key: PrimaryKeyType) + throws -> Bool + where PrimaryKeyType: DatabaseValueConvertible + { + try filter(key: key).exists(db) + } +} + +@available(OSX 10.15, iOS 13.0, tvOS 13.0, watchOS 6, *) +extension TableRecord where Self: Identifiable, ID: DatabaseValueConvertible { + /// Returns whether a row exists for this primary key. + /// + /// try Player.deleteOne(db, id: 123) + /// try Country.deleteOne(db, id: "FR") + /// + /// When the table has no explicit primary key, GRDB uses the hidden + /// "rowid" column: + /// + /// try Document.deleteOne(db, id: 1) + /// + /// - parameters: + /// - db: A database connection. + /// - id: A primary key value. + /// - returns: Whether a row exists for this primary key. + public static func exists(_ db: Database, id: ID) throws -> Bool { + try filter(id: id).exists(db) + } +} + +@available(OSX 10.15, iOS 13.0, tvOS 13.0, watchOS 6, *) +extension TableRecord +where Self: Identifiable, + ID: _OptionalProtocol, + ID.Wrapped: DatabaseValueConvertible +{ + #warning("TODO: test & document") + /// Returns whether a row exists for this primary key. + /// + /// try Player.deleteOne(db, id: 123) + /// try Country.deleteOne(db, id: "FR") + /// + /// When the table has no explicit primary key, GRDB uses the hidden + /// "rowid" column: + /// + /// try Document.deleteOne(db, id: 1) + /// + /// - parameters: + /// - db: A database connection. + /// - id: A primary key value. + /// - returns: Whether a row exists for this primary key. + public static func exists(_ db: Database, id: ID.Wrapped) throws -> Bool { + try filter(id: id).exists(db) + } +} + +// MARK: - Check Existence by Key + +extension TableRecord { + /// Returns whether a row exists for this unique key (primary key or any key + /// with a unique index on it). + /// + /// try Player.exists(db, key: ["name": Arthur"]) + /// + /// - parameters: + /// - db: A database connection. + /// - key: A dictionary of values. + /// - returns: Whether a row exists for this key. + public static func exists(_ db: Database, key: [String: DatabaseValueConvertible?]) throws -> Bool { + try filter(key: key).exists(db) + } +} + // MARK: - Deleting by Single-Column Primary Key extension TableRecord {