diff --git a/CHANGELOG.md b/CHANGELOG.md index e0b29f5fa..e54fb8509 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -73,11 +73,10 @@ GRDB adheres to [Semantic Versioning](https://semver.org/), with one exception: ## Development Branch -- **New**: You can now define common table expressions without the `` generic qualifier: +- **New**: You can now define common table expressions without any generic qualifier (which defaults to `Row`): - ```diff - -let cte = CommonTableExpression(...) - +let cte = CommonTableExpression(...) + ```swift + let cte = CommonTableExpression(...) ``` The [Common Table Expressions Guide](Documentation/CommonTableExpressions.md) was updated accordingly. diff --git a/Documentation/CommonTableExpressions.md b/Documentation/CommonTableExpressions.md index 7f9463516..55da52e63 100644 --- a/Documentation/CommonTableExpressions.md +++ b/Documentation/CommonTableExpressions.md @@ -182,16 +182,19 @@ try Player In the previous chapter, a common table expression was embedded as a subquery, with the `CommonTableExpression.all()` method. -`all()` builds a regular [query interface request] that you can filter, sort, etc, like all query interface requests. +`cte.all()` builds a regular [query interface request] that you can filter, sort, etc, like all query interface requests. -You can also fetch from it, but only as long as it is provided with the definition of the CTE. This will generally give requests of the form `cte.all().with(cte)`. In SQL, this would give: `WITH cte AS (...) SELECT * FROM cte`: +You can also fetch from `cte.all()`, as long as the request is given the definition of the CTE: `cte.all().with(cte)`. In SQL, this would give: `WITH cte AS (...) SELECT * FROM cte`: + +This request, of type `QueryInterfaceRequest`, can fetch raw database [rows](../README.md#row-queries): ```swift let cte = CommonTableExpression(...) let request = cte.all().with(cte) +let rows = try request.fetchAll(db) // [Row] ``` -This request, of type `QueryInterfaceRequest`, doesn't quite know what to fetch and how. You must help it so that it can actually fetch database [rows](../README.md#row-queries), simple [values](../README.md#value-queries), or custom [records](../README.md#records). You have two possible options: +In order to fetch something else, such as simple [values](../README.md#value-queries), or custom [records](../README.md#records), you have two possible options: 1. Use the `asRequest(of:)` method: @@ -211,32 +214,6 @@ This request, of type `QueryInterfaceRequest`, doesn't quite know what to let players = try request.fetchAll(db) // [Player] ``` -For example, let's fetch a range of integer: - -```swift -func counterRequest(range: ClosedRange) -> QueryInterfaceRequest { - // WITH RECURSIVE counter(x) AS - // (VALUES(...) UNION ALL SELECT x+1 FROM counter WHERE x<...) - // SELECT * FROM counter - let counter = CommonTableExpression( - recursive: true, - named: "counter", - columns: ["x"], - literal: """ - VALUES(\(range.lowerBound)) \ - UNION ALL \ - SELECT x+1 FROM counter WHERE x < \(range.upperBound) - """) - return counter.all().with(counter) -} - -let values = try dbQueue.read { db in - try counterRequest(range: 3...7).fetchAll(db) -} -print(values) // prints "[3, 4, 5, 6, 7]" -``` - - ## Associations to Common Table Expressions GRDB [associations] define "to-one" and "to-many" relationships between two database tables. Here we will define associations between regular tables and common table expressions. diff --git a/GRDB/QueryInterface/Request/CommonTableExpression.swift b/GRDB/QueryInterface/Request/CommonTableExpression.swift index bb9f3d52b..8ace50580 100644 --- a/GRDB/QueryInterface/Request/CommonTableExpression.swift +++ b/GRDB/QueryInterface/Request/CommonTableExpression.swift @@ -147,7 +147,7 @@ extension CommonTableExpression { } } -extension CommonTableExpression where RowDecoder == Void { +extension CommonTableExpression where RowDecoder == Row { /// Creates a common table expression from a request. /// /// For example: @@ -179,7 +179,7 @@ extension CommonTableExpression where RowDecoder == Void { named: tableName, columns: columns, request: request, - type: Void.self) + type: Row.self) } /// Creates a common table expression from an SQL string and @@ -212,7 +212,7 @@ extension CommonTableExpression where RowDecoder == Void { named: tableName, columns: columns, request: SQLRequest(sql: sql, arguments: arguments), - type: Void.self) + type: Row.self) } /// Creates a common table expression from an `SQLLiteral`. @@ -241,7 +241,7 @@ extension CommonTableExpression where RowDecoder == Void { named: tableName, columns: columns, request: SQLRequest(literal: sqlLiteral), - type: Void.self) + type: Row.self) } } diff --git a/GRDB/QueryInterface/Request/QueryInterfaceRequest.swift b/GRDB/QueryInterface/Request/QueryInterfaceRequest.swift index cc0ea8f6e..631894e2a 100644 --- a/GRDB/QueryInterface/Request/QueryInterfaceRequest.swift +++ b/GRDB/QueryInterface/Request/QueryInterfaceRequest.swift @@ -655,7 +655,7 @@ private func prefetch( .removingChildrenForPrefetchedAssociations() .selectOnly(leftColumns.map { SQLExpression.column($0).sqlSelection }) } - let originCTE = CommonTableExpression( + let originCTE = CommonTableExpression( named: "grdb_base", request: SQLSubquery.query(originQuery)) let pivotRowValue = SQLExpression.rowValue(pivotColumns.map(SQLExpression.column))! diff --git a/GRDB/QueryInterface/SQL/SQLSubquery.swift b/GRDB/QueryInterface/SQL/SQLSubquery.swift index 63f8666a9..0cc66a2ae 100644 --- a/GRDB/QueryInterface/SQL/SQLSubquery.swift +++ b/GRDB/QueryInterface/SQL/SQLSubquery.swift @@ -29,7 +29,7 @@ extension SQLSubquery { /// // SELECT player.*, cte.* /// // FROM player /// // JOIN cte - /// let cte = CommonTableExpression(named: "cte", sql: "SELECT 1 AS a, 2 AS b") + /// let cte = CommonTableExpression(named: "cte", sql: "SELECT 1 AS a, 2 AS b") /// let request = Player /// .with(cte) /// .including(required: Player.association(to: cte)) diff --git a/GRDB/QueryInterface/SQLInterpolation+QueryInterface.swift b/GRDB/QueryInterface/SQLInterpolation+QueryInterface.swift index c4ad4a08d..a52dd9119 100644 --- a/GRDB/QueryInterface/SQLInterpolation+QueryInterface.swift +++ b/GRDB/QueryInterface/SQLInterpolation+QueryInterface.swift @@ -264,7 +264,7 @@ extension SQLInterpolation { /// Appends the table name of the common table expression. /// /// // WITH "cte" AS (...) SELECT * FROM "cte" - /// let cte = CommonTableExpression(named: "cte", ...) + /// let cte = CommonTableExpression(named: "cte", ...) /// let request: SQLRequest = """ /// WITH \(definitionFor: cte) SELECT * FROM \(cte) /// """ @@ -275,7 +275,7 @@ extension SQLInterpolation { /// Appends the definition of the common table expression. /// /// // WITH "cte" AS (...) SELECT * FROM "cte" - /// let cte = CommonTableExpression(named: "cte", ...) + /// let cte = CommonTableExpression(named: "cte", ...) /// let request: SQLRequest = """ /// WITH \(definitionFor: cte) SELECT * FROM \(cte) /// """ diff --git a/GRDB/QueryInterface/TableRecord+Association.swift b/GRDB/QueryInterface/TableRecord+Association.swift index 43aa01e01..def776476 100644 --- a/GRDB/QueryInterface/TableRecord+Association.swift +++ b/GRDB/QueryInterface/TableRecord+Association.swift @@ -225,7 +225,7 @@ extension TableRecord { /// .annotated(with: max(Column("date"))) /// .group(Column("chatID")) /// - /// let latestMessageCTE = CommonTableExpression( + /// let latestMessageCTE = CommonTableExpression( /// named: "latestMessage", /// request: latestMessageRequest) /// diff --git a/Tests/GRDBTests/CommonTableExpressionTests.swift b/Tests/GRDBTests/CommonTableExpressionTests.swift index b024203b5..a204b7e07 100644 --- a/Tests/GRDBTests/CommonTableExpressionTests.swift +++ b/Tests/GRDBTests/CommonTableExpressionTests.swift @@ -3,15 +3,20 @@ import GRDB class CommonTableExpressionTests: GRDBTestCase { func testInitializers() { - // Implicit Void - _ = CommonTableExpression(named: "foo", sql: "") - _ = CommonTableExpression(named: "foo", literal: "") - _ = CommonTableExpression(named: "foo", request: SQLRequest("")) - - // Explicit Void - _ = CommonTableExpression(named: "foo", sql: "") - _ = CommonTableExpression(named: "foo", literal: "") - _ = CommonTableExpression(named: "foo", request: SQLRequest("")) + // Implicit generic RowDecoder type is Row + func acceptRowCTE(_ cte: CommonTableExpression) { } + do { + let cte = CommonTableExpression(named: "foo", sql: "") + acceptRowCTE(cte) + } + do { + let cte = CommonTableExpression(named: "foo", literal: "") + acceptRowCTE(cte) + } + do { + let cte = CommonTableExpression(named: "foo", request: SQLRequest("")) + acceptRowCTE(cte) + } // Explicit type struct S { } @@ -361,7 +366,8 @@ class CommonTableExpressionTests: GRDBTestCase { func testFetchFromCTE() throws { try makeDatabaseQueue().read { db in do { - let answer = CommonTableExpression( + // Default row decoder is Row + let answer = CommonTableExpression( named: "answer", sql: "SELECT 42 AS value") let row = try answer.all().with(answer).fetchOne(db)