Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix incorrect behavior when preparing SELECT * preceded by a WITH #1179

Merged
merged 2 commits into from
Dec 28, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 19 additions & 0 deletions Sources/SQLite/Typed/Query.swift
Original file line number Diff line number Diff line change
Expand Up @@ -1036,10 +1036,29 @@ extension Connection {
let column = names.removeLast()
let namespace = names.joined(separator: ".")

// Return a copy of the input "with" clause stripping all subclauses besides "select", "join", and "with".
func strip(_ with: WithClauses) -> WithClauses {
var stripped = WithClauses()
stripped.recursive = with.recursive
for subclause in with.clauses {
let query = subclause.query
var strippedQuery = type(of: query).init(query.clauses.from.name, database: query.clauses.from.database)
strippedQuery.clauses.select = query.clauses.select
strippedQuery.clauses.join = query.clauses.join
strippedQuery.clauses.with = strip(query.clauses.with)

var strippedSubclause = WithClauses.Clause(alias: subclause.alias, query: strippedQuery)
strippedSubclause.columns = subclause.columns
stripped.clauses.append(strippedSubclause)
}
return stripped
}

func expandGlob(_ namespace: Bool) -> (QueryType) throws -> Void {
{ (queryType: QueryType) throws -> Void in
var query = type(of: queryType).init(queryType.clauses.from.name, database: queryType.clauses.from.database)
query.clauses.select = queryType.clauses.select
query.clauses.with = strip(queryType.clauses.with)
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The strip function feels a little excessive. Doing this instead also works:

query.clauses.with = queryType.clauses.with

but the query will include things that aren't necessary for determining the result column names, like filters, sorting, and grouping.

let expression = query.expression
var names = try self.prepare(expression.template, expression.bindings).columnNames.map { $0.quote() }
if namespace { names = names.map { "\(queryType.tableName().expression.template).\($0)" } }
Expand Down
33 changes: 33 additions & 0 deletions Tests/SQLiteTests/Typed/QueryIntegrationTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -276,6 +276,39 @@ class QueryIntegrationTests: SQLiteTestCase {

XCTAssertEqual(21, sum)
}

/// Verify that `*` is properly expanded in a SELECT statement following a WITH clause.
func test_with_glob_expansion() throws {
let names = Table("names")
let name = Expression<String>("name")
try db.run(names.create { builder in
builder.column(email)
builder.column(name)
})

try db.run(users.insert(email <- "alice@example.com"))
try db.run(names.insert(email <- "alice@example.com", name <- "Alice"))

// WITH intermediate AS ( SELECT ... ) SELECT * FROM intermediate
let intermediate = Table("intermediate")
let rows = try db.prepare(
intermediate
.with(intermediate,
as: users
.select([id, users[email], name])
.join(names, on: names[email] == users[email])
.where(users[email] == "alice@example.com")
))

// There should be at least one row in the result.
let row = try XCTUnwrap(rows.makeIterator().next())

// Verify the column names
XCTAssertEqual(row.columnNames.count, 3)
XCTAssertNotNil(row[id])
XCTAssertNotNil(row[name])
XCTAssertNotNil(row[email])
}
}

extension Connection {
Expand Down