Skip to content

Commit

Permalink
Properly expand table.* columns
Browse files Browse the repository at this point in the history
Signed-off-by: Stephen Celis <stephen@stephencelis.com>
  • Loading branch information
stephencelis committed Dec 19, 2014
1 parent 01cb756 commit 1f070f7
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 4 deletions.
16 changes: 16 additions & 0 deletions SQLite Common Tests/QueryTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,22 @@ class QueryTests: XCTestCase {
ExpectExecutions(db, [SQL: 1]) { _ in for _ in middleManagers {} }
}

func test_join_withNamespacedStar_expandsColumnNames() {
let managers = db["users"].alias("managers")

let aliceID = users.insert(email <- "alice@example.com")!
users.insert(email <- "betty@example.com", manager_id <- aliceID)!

let query = users
.select(users[*], managers[*])
.join(managers, on: managers[id] == users[manager_id])

let SQL = "SELECT \"users\".*, \"managers\".* FROM \"users\" " +
"INNER JOIN \"users\" AS \"managers\" " +
"ON (\"managers\".\"id\" = \"users\".\"manager_id\")"
ExpectExecutions(db, [SQL: 1]) { _ in for row in query { println(row) } }
}

func test_namespacedColumnRowValueAccess() {
let aliceID = users.insert(email <- "alice@example.com")!
let bettyID = users.insert(email <- "betty@example.com", manager_id <- aliceID)!
Expand Down
13 changes: 9 additions & 4 deletions SQLite Common/Query.swift
Original file line number Diff line number Diff line change
Expand Up @@ -753,7 +753,7 @@ public struct QueryGenerator: GeneratorType {

private lazy var columnNames: [String: Int] = {
var (columnNames, idx) = ([String: Int](), 0)
for each in self.query.columns {
column: for each in self.query.columns {
let pair = split(each.expression.SQL) { $0 == "." }
let (tableName, column) = (pair.count > 1 ? pair.first : nil, pair.last!)

Expand All @@ -766,11 +766,16 @@ public struct QueryGenerator: GeneratorType {
}

if column == "*" {
let tables = [self.query] + self.query.joins.map { $0.table }
if let tableName = tableName {
expandGlob(true)(self.query.database[tableName])
continue
for table in tables {
if quote(identifier: table.alias ?? table.tableName) == tableName {
expandGlob(true)(table)
continue column
}
}
assertionFailure("no such table: \(tableName)")
}
let tables = [self.query] + self.query.joins.map { $0.table }
tables.map(expandGlob(self.query.joins.count > 0))
continue
}
Expand Down

0 comments on commit 1f070f7

Please sign in to comment.