mirror of
https://github.com/groue/GRDB.swift
synced 2026-11-30 04:28:44 +00:00
CommonTableExpression has an implicit Row RowDecoder
This commit is contained in:
1 parent
a681ccf2b9
commit
64bb63e304
8 files changed
+34
-52
No files matched your search
+3
-4
@@ -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 `<Void>` generic qualifier:
|
||||
- **New**: You can now define common table expressions without any generic qualifier (which defaults to `Row`):
|
||||
|
||||
```diff
|
||||
-let cte = CommonTableExpression<Void>(...)
|
||||
+let cte = CommonTableExpression(...)
|
||||
```swift
|
||||
let cte = CommonTableExpression(...)
|
||||
```
|
||||
|
||||
The [Common Table Expressions Guide](Documentation/CommonTableExpressions.md) was updated accordingly.
|
||||
|
||||
@@ -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<Row>`, 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<Void>`, 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<Void>`, 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<Int>) -> QueryInterfaceRequest<Int> {
|
||||
// WITH RECURSIVE counter(x) AS
|
||||
// (VALUES(...) UNION ALL SELECT x+1 FROM counter WHERE x<...)
|
||||
// SELECT * FROM counter
|
||||
let counter = CommonTableExpression<Int>(
|
||||
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.
|
||||
|
||||
@@ -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<Void>(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<Void>(literal: sqlLiteral),
|
||||
type: Void.self)
|
||||
type: Row.self)
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -655,7 +655,7 @@ private func prefetch(
|
||||
.removingChildrenForPrefetchedAssociations()
|
||||
.selectOnly(leftColumns.map { SQLExpression.column($0).sqlSelection })
|
||||
}
|
||||
let originCTE = CommonTableExpression<Void>(
|
||||
let originCTE = CommonTableExpression(
|
||||
named: "grdb_base",
|
||||
request: SQLSubquery.query(originQuery))
|
||||
let pivotRowValue = SQLExpression.rowValue(pivotColumns.map(SQLExpression.column))!
|
||||
|
||||
@@ -29,7 +29,7 @@ extension SQLSubquery {
|
||||
/// // SELECT player.*, cte.*
|
||||
/// // FROM player
|
||||
/// // JOIN cte
|
||||
/// let cte = CommonTableExpression<Void>(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))
|
||||
|
||||
@@ -264,7 +264,7 @@ extension SQLInterpolation {
|
||||
/// Appends the table name of the common table expression.
|
||||
///
|
||||
/// // WITH "cte" AS (...) SELECT * FROM "cte"
|
||||
/// let cte = CommonTableExpression<Void>(named: "cte", ...)
|
||||
/// let cte = CommonTableExpression(named: "cte", ...)
|
||||
/// let request: SQLRequest<Row> = """
|
||||
/// 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<Void>(named: "cte", ...)
|
||||
/// let cte = CommonTableExpression(named: "cte", ...)
|
||||
/// let request: SQLRequest<Row> = """
|
||||
/// WITH \(definitionFor: cte) SELECT * FROM \(cte)
|
||||
/// """
|
||||
|
||||
@@ -225,7 +225,7 @@ extension TableRecord {
|
||||
/// .annotated(with: max(Column("date")))
|
||||
/// .group(Column("chatID"))
|
||||
///
|
||||
/// let latestMessageCTE = CommonTableExpression<Void>(
|
||||
/// let latestMessageCTE = CommonTableExpression(
|
||||
/// named: "latestMessage",
|
||||
/// request: latestMessageRequest)
|
||||
///
|
||||
|
||||
@@ -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<Void>(""))
|
||||
|
||||
// Explicit Void
|
||||
_ = CommonTableExpression<Void>(named: "foo", sql: "")
|
||||
_ = CommonTableExpression<Void>(named: "foo", literal: "")
|
||||
_ = CommonTableExpression<Void>(named: "foo", request: SQLRequest<Void>(""))
|
||||
// Implicit generic RowDecoder type is Row
|
||||
func acceptRowCTE(_ cte: CommonTableExpression<Row>) { }
|
||||
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<Void>(""))
|
||||
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<Row>(
|
||||
// Default row decoder is Row
|
||||
let answer = CommonTableExpression(
|
||||
named: "answer",
|
||||
sql: "SELECT 42 AS value")
|
||||
let row = try answer.all().with(answer).fetchOne(db)
|
||||
|
||||
Reference in new issue
Block a user