mirror of
https://github.com/groue/GRDB.swift
synced 2026-11-25 04:07:47 +00:00
Rename request.exists(_:) to isEmpty(_:)
This commit is contained in:
1 parent
55b938a1f5
commit
5f44162abe
4 files changed
+17
-17
No files matched your search
@@ -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)!
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -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)
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -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)
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -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')")
|
||||
}
|
||||
}
|
||||
|
||||
Reference in new issue
Block a user