From 5f44162abe12a978474bb29bc6a9d77bf5ea744e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Gwendal=20Roue=CC=81?= Date: Sun, 17 Oct 2021 09:52:01 +0200 Subject: [PATCH] Rename request.exists(_:) to isEmpty(_:) --- .../QueryInterface/Request/QueryInterfaceRequest.swift | 10 +++++----- GRDB/QueryInterface/SQL/Table.swift | 8 ++++---- GRDB/Record/TableRecord.swift | 8 ++++---- Tests/GRDBTests/QueryInterfaceRequestTests.swift | 8 ++++---- 4 files changed, 17 insertions(+), 17 deletions(-) diff --git a/GRDB/QueryInterface/Request/QueryInterfaceRequest.swift b/GRDB/QueryInterface/Request/QueryInterfaceRequest.swift index d046556bd..1203c1727 100644 --- a/GRDB/QueryInterface/Request/QueryInterfaceRequest.swift +++ b/GRDB/QueryInterface/Request/QueryInterfaceRequest.swift @@ -414,14 +414,14 @@ extension QueryInterfaceRequest { extension QueryInterfaceRequest { #warning("TODO: document") - /// Returns whether a row exists in this request. + /// Returns true if the request matches no row in the database. /// - /// try Player.filter(Column("name") == "Arthur").exists(db) + /// try Player.filter(Column("name") == "Arthur").isEmpty(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)! + /// - returns: Whether the request matches no row in the database. + public func isEmpty(_ db: Database) throws -> Bool { + try !SQLRequest("SELECT \(exists())").fetchOne(db)! } } diff --git a/GRDB/QueryInterface/SQL/Table.swift b/GRDB/QueryInterface/SQL/Table.swift index 924b9a8e1..a2c6ac9de 100644 --- a/GRDB/QueryInterface/SQL/Table.swift +++ b/GRDB/QueryInterface/SQL/Table.swift @@ -1257,7 +1257,7 @@ extension Table { throws -> Bool where PrimaryKeyType: DatabaseValueConvertible { - try filter(key: key).exists(db) + try !filter(key: key).isEmpty(db) } } @@ -1281,7 +1281,7 @@ where RowDecoder: Identifiable, /// - id: A primary key value. /// - returns: Whether a row exists for this primary key. public func exists(_ db: Database, id: RowDecoder.ID) throws -> Bool { - try filter(id: id).exists(db) + try !filter(id: id).isEmpty(db) } } @@ -1306,7 +1306,7 @@ where RowDecoder: Identifiable, /// - id: A primary key value. /// - returns: Whether a row exists for this primary key. public func exists(_ db: Database, id: RowDecoder.ID.Wrapped) throws -> Bool { - try filter(id: id).exists(db) + try !filter(id: id).isEmpty(db) } } @@ -1323,7 +1323,7 @@ extension Table { /// - key: A dictionary of values. /// - returns: Whether a row exists for this key. public func exists(_ db: Database, key: [String: DatabaseValueConvertible?]) throws -> Bool { - try filter(key: key).exists(db) + try !filter(key: key).isEmpty(db) } } diff --git a/GRDB/Record/TableRecord.swift b/GRDB/Record/TableRecord.swift index fbb4d7187..2a1eb8100 100644 --- a/GRDB/Record/TableRecord.swift +++ b/GRDB/Record/TableRecord.swift @@ -176,7 +176,7 @@ extension TableRecord { throws -> Bool where PrimaryKeyType: DatabaseValueConvertible { - try filter(key: key).exists(db) + try !filter(key: key).isEmpty(db) } } @@ -197,7 +197,7 @@ extension TableRecord where Self: Identifiable, ID: DatabaseValueConvertible { /// - 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) + try !filter(id: id).isEmpty(db) } } @@ -223,7 +223,7 @@ where Self: Identifiable, /// - 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) + try !filter(id: id).isEmpty(db) } } @@ -240,7 +240,7 @@ extension TableRecord { /// - 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) + try !filter(key: key).isEmpty(db) } } diff --git a/Tests/GRDBTests/QueryInterfaceRequestTests.swift b/Tests/GRDBTests/QueryInterfaceRequestTests.swift index 0e57b0077..1be985ede 100644 --- a/Tests/GRDBTests/QueryInterfaceRequestTests.swift +++ b/Tests/GRDBTests/QueryInterfaceRequestTests.swift @@ -981,19 +981,19 @@ class QueryInterfaceRequestTests: GRDBTestCase { // MARK: - Exists - func testExists() throws { + func testIsEmpty() throws { let dbQueue = try makeDatabaseQueue() try dbQueue.write { db in - try XCTAssertFalse(tableRequest.exists(db)) + try XCTAssertTrue(tableRequest.isEmpty(db)) XCTAssertEqual(lastSQLQuery, "SELECT EXISTS (SELECT * FROM \"readers\")") try db.execute(sql: "INSERT INTO readers (name, age) VALUES (?, ?)", arguments: ["Arthur", 42]) try db.execute(sql: "INSERT INTO readers (name, age) VALUES (?, ?)", arguments: ["Barbara", 36]) - try XCTAssertTrue(tableRequest.exists(db)) + try XCTAssertFalse(tableRequest.isEmpty(db)) XCTAssertEqual(lastSQLQuery, "SELECT EXISTS (SELECT * FROM \"readers\")") - try XCTAssertTrue(tableRequest.filter(Column("name") == "Arthur").exists(db)) + try XCTAssertFalse(tableRequest.filter(Column("name") == "Arthur").isEmpty(db)) XCTAssertEqual(lastSQLQuery, "SELECT EXISTS (SELECT * FROM \"readers\" WHERE \"name\" = 'Arthur')") } }