stephencelis / SQLite.swift

A type-safe, Swift-language layer over SQLite3.
MIT License
9.57k stars 1.54k forks source link

[Bug] Broken column resolution for WITH clauses #1191

Closed venkr closed 1 year ago

venkr commented 1 year ago

When using a WITH query, the column resolution function columnNamesForQuery says no such table, despite the fact that the exact same expression works if converted to raw SQL first.

Repro example:

let tempTable = Table("recent_matches")
let q = tempTable.with(tempTable, as: searchRanking.table.match("cat"))

print(q.expression.description)

do {
  // Works
  let array = try Array(try db.prepareRowIterator(q.expression.description))
  // Does not work
  //let array = try Array(try db.prepareRowIterator(q))
} catch {
  print("VONK", error)
}

Logs: WITH "recent_matches" AS (SELECT * FROM "searchRanking" WHERE ("searchRanking" MATCH 'cat')) SELECT * FROM "recent_matches" no such table: recent_matches (code: 1)

It looks like in cases with WITH clauses, columnNamesForQuery behaves incorrectly, while columnNameMap powered by sqlite3_column_name works fine.

venkr commented 1 year ago

This is fixed by 4b9ea97872a241fad38c221afad7a701fc987868