diff --git a/CHANGELOG.md b/CHANGELOG.md index 9e4eaec89..1f3886da3 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -100,6 +100,7 @@ GRDB adheres to [Semantic Versioning](https://semver.org/), with one exception: ## Next Release - **Fixed**: Fixed a bug with HasManyThrough associations when the "through" association has the same association key as the association itself ([#1274](https://github.com/groue/GRDB.swift/discussions/1274)). +- **Fixed**: [#1275](https://github.com/groue/GRDB.swift/issues/1275) Enhance error message for some requests involving associations on common table expressions. - **New**: Bump custom SQLite builds v3.39.3 ## 6.0.0-beta.4 diff --git a/GRDB/QueryInterface/Request/CommonTableExpression.swift b/GRDB/QueryInterface/Request/CommonTableExpression.swift index 26392b01e..4853b4240 100644 --- a/GRDB/QueryInterface/Request/CommonTableExpression.swift +++ b/GRDB/QueryInterface/Request/CommonTableExpression.swift @@ -10,7 +10,9 @@ public struct CommonTableExpression { /// named: "answer", /// sql: "SELECT 42") /// answer.tableName // "answer" - public var tableName: String + public var tableName: String { + cte.tableName + } var cte: SQLCTE @@ -43,8 +45,8 @@ public struct CommonTableExpression { request: some SQLSubqueryable, type: RowDecoder.Type) { - self.tableName = tableName self.cte = SQLCTE( + tableName: tableName, columns: columns, sqlSubquery: request.sqlSubquery, isRecursive: recursive) @@ -303,6 +305,9 @@ extension CommonTableExpression { /// A low-level common table expression struct SQLCTE { + /// The table name of the common table expression. + var tableName: String + /// The columns of the common table expression. /// /// When nil, the CTE selects the columns of the request: @@ -334,7 +339,35 @@ struct SQLCTE { return columns.count } - return try sqlSubquery.columnCount(db) + do { + return try sqlSubquery.columnCount(db) + } catch let error as DatabaseError where error.resultCode == .SQLITE_ERROR { + // Maybe the CTE refers to other CTEs: https://github.com/groue/GRDB.swift/issues/1275 + // We can't modify the CTE request by creating or extending the + // WITH clause with other CTEs, because we'd need to parse SQL. + // So let's rewrite the error message, and guide the user towards + // a more precise CTE definition: + let message = [ + [ + """ + Can't compute the number of columns in the \ + \(String(reflecting: tableName)) common table expression + """, + error.message, + ].compactMap { $0 }.joined(separator: ": "), + """ + Check the syntax of the SQL definition, or provide the \ + explicit list of selected columns with the `columns` parameter \ + in the CommonTableExpression initializer. + """, + ].joined(separator: ". ") + throw DatabaseError( + resultCode: error.extendedResultCode, + message: message, + sql: error.sql, + arguments: error.arguments, + publicStatementArguments: error.publicStatementArguments) + } } } diff --git a/Tests/GRDBTests/CommonTableExpressionTests.swift b/Tests/GRDBTests/CommonTableExpressionTests.swift index 1352da2de..c12a52ebe 100644 --- a/Tests/GRDBTests/CommonTableExpressionTests.swift +++ b/Tests/GRDBTests/CommonTableExpressionTests.swift @@ -783,4 +783,45 @@ class CommonTableExpressionTests: GRDBTestCase { } } } + + // https://github.com/groue/GRDB.swift/issues/1275 + func testIssue1275() throws { + try makeDatabaseQueue().read { db in + do { + // Failing case: test that error message suggests to fix the cte + // definition by declaring columns. + let cte1 = CommonTableExpression(named: "cte1", sql: "SELECT * FROM cte2") + let cte2 = CommonTableExpression(named: "cte2", sql: "SELECT 1 AS a") + let association = cte1.association(to: cte2) + let request = cte1.all().with(cte1).with(cte2).including(required: association) + _ = try request.asRequest(of: Row.self).fetchOne(db) + } catch let error as DatabaseError { + XCTAssertEqual(error.resultCode, .SQLITE_ERROR) + XCTAssertEqual(error.message, """ + Can't compute the number of columns in the "cte1" common table expression: \ + no such table: cte2. Check the syntax of the SQL definition, \ + or provide the explicit list of selected columns with the \ + `columns` parameter in the CommonTableExpression initializer. + """) + } + + do { + // Fixed case: specify columns + let cte1 = CommonTableExpression(named: "cte1", columns: ["a"], sql: "SELECT * FROM cte2") + let cte2 = CommonTableExpression(named: "cte2", sql: "SELECT 1 AS a") + let association = cte1.association(to: cte2) + let request = cte1.all().with(cte1).with(cte2).including(required: association) + _ = try request.asRequest(of: Row.self).fetchOne(db) + } + + do { + // Handled case: no need to specify columns + let cte1 = CommonTableExpression(named: "cte1", request: Table("cte2").select(Column("a"))) + let cte2 = CommonTableExpression(named: "cte2", sql: "SELECT 1 AS a") + let association = cte1.association(to: cte2) + let request = cte1.all().with(cte1).with(cte2).including(required: association) + _ = try request.asRequest(of: Row.self).fetchOne(db) + } + } + } }