Skip to content

Commit ccf4392

Browse files
committed
Merge pull request #396 from jlawton/create-qualified-index
Restrict use of qualified table names
2 parents b43a31f + 3825894 commit ccf4392

File tree

4 files changed

+18
-2
lines changed

4 files changed

+18
-2
lines changed

SQLite/Typed/Query.swift

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -744,6 +744,13 @@ extension QueryType {
744744
])
745745
}
746746

747+
func tableName(qualified qualified: Bool) -> Expressible {
748+
if qualified {
749+
return tableName()
750+
}
751+
return Expression<Void>(clauses.from.alias ?? clauses.from.name)
752+
}
753+
747754
func database(namespace name: String) -> Expressible {
748755
let name = Expression<Void>(name)
749756

SQLite/Typed/Schema.swift

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -134,7 +134,7 @@ extension Table {
134134
let clauses: [Expressible?] = [
135135
create("INDEX", indexName(columns), unique ? .Unique : nil, ifNotExists),
136136
Expression<Void>(literal: "ON"),
137-
tableName(),
137+
tableName(qualified: false),
138138
"".wrap(columns) as Expression<Void>
139139
]
140140

@@ -505,7 +505,7 @@ private func definition(column: Expressible, _ datatype: String, _ primaryKey: P
505505
private func reference(primary: (QueryType, Expressible)) -> Expressible {
506506
return " ".join([
507507
Expression<Void>(literal: "REFERENCES"),
508-
primary.0.tableName(),
508+
primary.0.tableName(qualified: false),
509509
"".wrap(primary.1) as Expression<Void>
510510
])
511511
}

SQLiteTests/SchemaTests.swift

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -300,6 +300,10 @@ class SchemaTests : XCTestCase {
300300
"CREATE TABLE \"table\" (\"int64\" INTEGER NOT NULL REFERENCES \"table\" (\"int64\"))",
301301
table.create { t in t.column(int64, references: table, int64) }
302302
)
303+
XCTAssertEqual(
304+
"CREATE TABLE \"table\" (\"int64\" INTEGER NOT NULL REFERENCES \"table\" (\"int64\"))",
305+
table.create { t in t.column(int64, references: qualifiedTable, int64) }
306+
)
303307
XCTAssertEqual(
304308
"CREATE TABLE \"table\" (\"int64\" INTEGER NOT NULL UNIQUE REFERENCES \"table\" (\"int64\"))",
305309
table.create { t in t.column(int64, unique: true, references: table, int64) }
@@ -724,6 +728,10 @@ class SchemaTests : XCTestCase {
724728
"CREATE UNIQUE INDEX IF NOT EXISTS \"index_table_on_int64\" ON \"table\" (\"int64\")",
725729
table.createIndex([int64], unique: true, ifNotExists: true)
726730
)
731+
XCTAssertEqual(
732+
"CREATE UNIQUE INDEX IF NOT EXISTS \"main\".\"index_table_on_int64\" ON \"table\" (\"int64\")",
733+
qualifiedTable.createIndex([int64], unique: true, ifNotExists: true)
734+
)
727735
}
728736

729737
func test_dropIndex_compilesCreateIndexExpression() {

SQLiteTests/TestHelpers.swift

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -110,5 +110,6 @@ func AssertThrows<T>(@autoclosure expression: () throws -> T, file: StaticString
110110
}
111111

112112
let table = Table("table")
113+
let qualifiedTable = Table("table", database: "main")
113114
let virtualTable = VirtualTable("virtual_table")
114115
let _view = View("view") // avoid Mac XCTestCase collision

0 commit comments

Comments
 (0)