Instance Method
with(recursive:columns:as:)
@discardableResult func with(recursive name: some SQLExpression, columns: [String], as query: some SQLExpression) -> Self
Parameters
nameThe name to assign to the query’s results.
columnsAn optional list of unqualified column names to use for referencing the query’s results. If no column names are provided, the names are inferred from the query. If column names are provided, the number of names provided must match the number of columns returned by the query.
queryAn expression which provides the contents of the CTE. For a recursive CTE, this must be an expression representing at least one
SELECTstatement which does not refer to the CTE and at least oneUNION ALLorUNION DISTINCTclause terminating with aSELECTstatement which explicitly refers to the CTE itself.
Discussion
Example usage:
try await sqlDatabase.update("table1")
.with(recursive: "c", columns: ["n"], as: SQLSubquery
.union { $0.column(SQLBind("1"), as: "n") }
.union(all: { $0
.column(SQLBinaryExpression("n", .add, 1))
.from("c").where("n", .lessThan, 3)
}).finish())
.set("foo", to: "bar")
.where("foo", .equal, SQLColumn("n", table: "c"))
.run()
Warning
As with SQLCommonTableExpression, SQLCommonTableExpressionBuilder does NOT validate that a recursive CTE’s query takes the proper form. It is the responsibility of the user to invoke the appropriate variant of this method. Failure to do so will result in generating invalid SQL.