From 2fd3ded6e282f032707e5d63e78ff299e3b6cf83 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Gwendal=20Roue=CC=81?= Date: Mon, 17 Oct 2016 09:09:17 +0200 Subject: [PATCH] TableMapping.none() --- CHANGELOG.md | 7 ++ .../QueryInterfaceRequest.swift | 86 ++++++++++--------- README.md | 4 +- 3 files changed, 53 insertions(+), 44 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 6974384a2..7e859fbb4 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,6 +1,13 @@ Release Notes ============= +## Next Version + +**New** + +- `TableMapping.none()`: a fetch request which fetches nothing. + + ## 0.88.0 Released October 16, 2016 diff --git a/GRDB/QueryInterface/QueryInterfaceRequest.swift b/GRDB/QueryInterface/QueryInterfaceRequest.swift index afcece91c..d16f224f3 100644 --- a/GRDB/QueryInterface/QueryInterfaceRequest.swift +++ b/GRDB/QueryInterface/QueryInterfaceRequest.swift @@ -30,7 +30,7 @@ extension QueryInterfaceRequest where T: RowConvertible { // MARK: Fetching Record and RowConvertible - /// Returns a sequence of values. + /// A sequence of fetched records. /// /// let nameColumn = Column("name") /// let request = Person.order(nameColumn) @@ -51,7 +51,7 @@ extension QueryInterfaceRequest where T: RowConvertible { return T.fetch(db, self) } - /// Returns an array of values fetched from a fetch request. + /// An array of fetched records. /// /// let nameColumn = Column("name") /// let request = Person.order(nameColumn) @@ -62,7 +62,7 @@ extension QueryInterfaceRequest where T: RowConvertible { return T.fetchAll(db, self) } - /// Returns a single value fetched from a fetch request. + /// The first fetched record. /// /// let nameColumn = Column("name") /// let request = Person.order(nameColumn) @@ -79,7 +79,7 @@ extension QueryInterfaceRequest { // MARK: Request Derivation - /// Returns a new QueryInterfaceRequest with a new net of selected columns. + /// A new QueryInterfaceRequest with a new net of selected columns. /// /// // SELECT id, email FROM persons /// var request = Person.all() @@ -95,7 +95,7 @@ extension QueryInterfaceRequest { return select(selection) } - /// Returns a new QueryInterfaceRequest with a new net of selected columns. + /// A new QueryInterfaceRequest with a new net of selected columns. /// /// // SELECT id, email FROM persons /// var request = Person.all() @@ -113,7 +113,7 @@ extension QueryInterfaceRequest { return QueryInterfaceRequest(query: query) } - /// Returns a new QueryInterfaceRequest with a new net of selected columns. + /// A new QueryInterfaceRequest with a new net of selected columns. /// /// // SELECT id, email FROM persons /// var request = Person.all() @@ -129,7 +129,7 @@ extension QueryInterfaceRequest { return select(SQLExpressionLiteral(sql, arguments: arguments)) } - /// Returns a new QueryInterfaceRequest which returns distinct rows. + /// A new QueryInterfaceRequest which returns distinct rows. /// /// // SELECT DISTINCT * FROM persons /// var request = Person.all() @@ -144,8 +144,8 @@ extension QueryInterfaceRequest { return QueryInterfaceRequest(query: query) } - /// Returns a new QueryInterfaceRequest with the provided *predicate* added - /// to the eventual set of already applied predicates. + /// A new QueryInterfaceRequest with the provided *predicate* added to the + /// eventual set of already applied predicates. /// /// // SELECT * FROM persons WHERE email = 'arthur@example.com' /// var request = Person.all() @@ -160,8 +160,8 @@ extension QueryInterfaceRequest { return QueryInterfaceRequest(query: query) } - /// Returns a new QueryInterfaceRequest with the provided *predicate* added - /// to the eventual set of already applied predicates. + /// A new QueryInterfaceRequest with the provided *predicate* added to the + /// eventual set of already applied predicates. /// /// // SELECT * FROM persons WHERE email = 'arthur@example.com' /// var request = Person.all() @@ -170,25 +170,25 @@ extension QueryInterfaceRequest { return filter(SQLExpressionLiteral(sql, arguments: arguments)) } - /// Returns a new QueryInterfaceRequest grouped according to *expressions*. + /// A new QueryInterfaceRequest grouped according to *expressions*. public func group(_ expressions: SQLExpressible...) -> QueryInterfaceRequest { return group(expressions) } - /// Returns a new QueryInterfaceRequest grouped according to *expressions*. + /// A new QueryInterfaceRequest grouped according to *expressions*. public func group(_ expressions: [SQLExpressible]) -> QueryInterfaceRequest { var query = self.query query.groupByExpressions = expressions.map { $0.sqlExpression } return QueryInterfaceRequest(query: query) } - /// Returns a new QueryInterfaceRequest with a new grouping. + /// A new QueryInterfaceRequest with a new grouping. public func group(sql: String, arguments: StatementArguments? = nil) -> QueryInterfaceRequest { return group(SQLExpressionLiteral(sql, arguments: arguments)) } - /// Returns a new QueryInterfaceRequest with the provided *predicate* added - /// to the eventual set of already applied predicates. + /// A new QueryInterfaceRequest with the provided *predicate* added to the + /// eventual set of already applied predicates. public func having(_ predicate: SQLExpressible) -> QueryInterfaceRequest { var query = self.query if let havingExpression = query.havingExpression { @@ -199,14 +199,13 @@ extension QueryInterfaceRequest { return QueryInterfaceRequest(query: query) } - /// Returns a new QueryInterfaceRequest with the provided *sql* added to - /// the eventual set of already applied predicates. + /// A new QueryInterfaceRequest with the provided *sql* added to the + /// eventual set of already applied predicates. public func having(sql: String, arguments: StatementArguments? = nil) -> QueryInterfaceRequest { return having(SQLExpressionLiteral(sql, arguments: arguments)) } - /// Returns a new QueryInterfaceRequest with the provided *orderings* added - /// to the eventual set of already applied orderings. + /// A new QueryInterfaceRequest with the provided *orderings*. /// /// // SELECT * FROM persons ORDER BY name /// var request = Person.all() @@ -222,8 +221,7 @@ extension QueryInterfaceRequest { return order(orderings) } - /// Returns a new QueryInterfaceRequest with the provided *orderings* added - /// to the eventual set of already applied orderings. + /// A new QueryInterfaceRequest with the provided *orderings*. /// /// // SELECT * FROM persons ORDER BY name /// var request = Person.all() @@ -241,8 +239,7 @@ extension QueryInterfaceRequest { return QueryInterfaceRequest(query: query) } - /// Returns a new QueryInterfaceRequest with the provided *sql* added to the - /// eventual set of already applied orderings. + /// A new QueryInterfaceRequest with the provided *sql* used for sorting. /// /// // SELECT * FROM persons ORDER BY name /// var request = Person.all() @@ -258,7 +255,7 @@ extension QueryInterfaceRequest { return order([SQLExpressionLiteral(sql, arguments: arguments)]) } - /// Returns a new QueryInterfaceRequest sorted in reversed order. + /// A new QueryInterfaceRequest sorted in reversed order. /// /// // SELECT * FROM persons ORDER BY name DESC /// var request = Person.all().order(Column("name")) @@ -269,8 +266,8 @@ extension QueryInterfaceRequest { return QueryInterfaceRequest(query: query) } - /// Returns a QueryInterfaceRequest which fetches *limit* rows, starting at - /// *offset*. + /// A QueryInterfaceRequest which fetches *limit* rows, starting + /// at *offset*. /// /// // SELECT * FROM persons LIMIT 1 /// var request = Person.all() @@ -287,7 +284,7 @@ extension QueryInterfaceRequest { // MARK: Counting - /// Returns the number of rows matched by the request. + /// The number of rows matched by the request. /// /// - parameter db: A database connection. public func fetchCount(_ db: Database) -> Int { @@ -316,7 +313,7 @@ extension TableMapping { // MARK: Request Derivation - /// Returns a QueryInterfaceRequest which fetches all rows in the table. + /// Creates a QueryInterfaceRequest which fetches all records. /// /// // SELECT * FROM persons /// var request = Person.all() @@ -336,7 +333,12 @@ extension TableMapping { return QueryInterfaceRequest(query: QueryInterfaceSelectQueryDefinition(select: selection, from: .table(name: databaseTableName, alias: nil))) } - /// Returns a QueryInterfaceRequest which selects *selection*. + /// Creates a QueryInterfaceRequest which fetches no record. + public static func none() -> QueryInterfaceRequest { + return filter(false) + } + + /// Creates a QueryInterfaceRequest which selects *selection*. /// /// // SELECT id, email FROM persons /// var request = Person.select(Column("id"), Column("email")) @@ -344,7 +346,7 @@ extension TableMapping { return all().select(selection) } - /// Returns a QueryInterfaceRequest which selects *selection*. + /// Creates a QueryInterfaceRequest which selects *selection*. /// /// // SELECT id, email FROM persons /// var request = Person.select([Column("id"), Column("email")]) @@ -352,7 +354,7 @@ extension TableMapping { return all().select(selection) } - /// Returns a QueryInterfaceRequest which selects *sql*. + /// Creates a QueryInterfaceRequest which selects *sql*. /// /// // SELECT id, email FROM persons /// var request = Person.select(sql: "id, email") @@ -360,7 +362,7 @@ extension TableMapping { return all().select(sql: sql, arguments: arguments) } - /// Returns a QueryInterfaceRequest with the provided *predicate*. + /// Creates a QueryInterfaceRequest with the provided *predicate*. /// /// // SELECT * FROM persons WHERE email = 'arthur@example.com' /// var request = Person.filter(Column("email") == "arthur@example.com") @@ -374,7 +376,7 @@ extension TableMapping { return all().filter(predicate) } - /// Returns a QueryInterfaceRequest with the provided *predicate*. + /// Creates a QueryInterfaceRequest with the provided *predicate*. /// /// // SELECT * FROM persons WHERE email = 'arthur@example.com' /// var request = Person.filter(sql: "email = ?", arguments: ["arthur@example.com"]) @@ -388,7 +390,7 @@ extension TableMapping { return all().filter(sql: sql, arguments: arguments) } - /// Returns a QueryInterfaceRequest sorted according to the + /// Creates a QueryInterfaceRequest sorted according to the /// provided *orderings*. /// /// // SELECT * FROM persons ORDER BY name @@ -403,7 +405,7 @@ extension TableMapping { return all().order(orderings) } - /// Returns a QueryInterfaceRequest sorted according to the + /// Creates a QueryInterfaceRequest sorted according to the /// provided *orderings*. /// /// // SELECT * FROM persons ORDER BY name @@ -418,7 +420,7 @@ extension TableMapping { return all().order(orderings) } - /// Returns a QueryInterfaceRequest sorted according to *sql*. + /// Creates a QueryInterfaceRequest sorted according to *sql*. /// /// // SELECT * FROM persons ORDER BY name /// var request = Person.order(sql: "name") @@ -432,7 +434,7 @@ extension TableMapping { return all().order(sql: sql, arguments: arguments) } - /// Returns a QueryInterfaceRequest which fetches *limit* rows, starting at + /// Creates a QueryInterfaceRequest which fetches *limit* rows, starting at /// *offset*. /// /// // SELECT * FROM persons LIMIT 1 @@ -453,7 +455,7 @@ extension TableMapping { // MARK: Counting - /// Returns the number of records. + /// The number of records. /// /// - parameter db: A database connection. public static func fetchCount(_ db: Database) -> Int { @@ -481,7 +483,7 @@ extension RowConvertible where Self: TableMapping { // MARK: Fetching All - /// Returns a sequence of all records fetched from the database. + /// A sequence of all records fetched from the database. /// /// let persons = Person.fetch(db) // DatabaseSequence /// @@ -500,7 +502,7 @@ extension RowConvertible where Self: TableMapping { return all().fetch(db) } - /// Returns an array of all records fetched from the database. + /// An array of all records fetched from the database. /// /// let persons = Person.fetchAll(db) // [Person] /// @@ -509,7 +511,7 @@ extension RowConvertible where Self: TableMapping { return all().fetchAll(db) } - /// Returns the first record fetched from a fetch request. + /// The first found record. /// /// let person = Person.fetchOne(db) // Person? /// diff --git a/README.md b/README.md index fc0065f6e..e251ae004 100644 --- a/README.md +++ b/README.md @@ -2611,9 +2611,9 @@ let idColumn = Column("id") let nameColumn = Column("name") ``` -You can now build requests with the following methods: `all`, `select`, `distinct`, `filter`, `matching`, `group`, `having`, `order`, `reversed`, `limit`. All those methods return another request, which you can further refine by applying another method: `Person.select(...).filter(...).order(...)`. +You can now build requests with the following methods: `all`, `none`, `select`, `distinct`, `filter`, `matching`, `group`, `having`, `order`, `reversed`, `limit`. All those methods return another request, which you can further refine by applying another method: `Person.select(...).filter(...).order(...)`. -- `all()`: the request for all rows. +- `all()`, `none()`: the requests for all rows, or no rows ```swift // SELECT * FROM persons