Skip to content

Restrict use of qualified table names #396

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

Merged
merged 2 commits into from
Apr 4, 2016
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
7 changes: 7 additions & 0 deletions SQLite/Typed/Query.swift
Original file line number Diff line number Diff line change
Expand Up @@ -744,6 +744,13 @@ extension QueryType {
])
}

func tableName(qualified qualified: Bool) -> Expressible {
if qualified {
return tableName()
}
return Expression<Void>(clauses.from.alias ?? clauses.from.name)
}

func database(namespace name: String) -> Expressible {
let name = Expression<Void>(name)

Expand Down
4 changes: 2 additions & 2 deletions SQLite/Typed/Schema.swift
Original file line number Diff line number Diff line change
Expand Up @@ -134,7 +134,7 @@ extension Table {
let clauses: [Expressible?] = [
create("INDEX", indexName(columns), unique ? .Unique : nil, ifNotExists),
Expression<Void>(literal: "ON"),
tableName(),
tableName(qualified: false),
"".wrap(columns) as Expression<Void>
]

Expand Down Expand Up @@ -505,7 +505,7 @@ private func definition(column: Expressible, _ datatype: String, _ primaryKey: P
private func reference(primary: (QueryType, Expressible)) -> Expressible {
return " ".join([
Expression<Void>(literal: "REFERENCES"),
primary.0.tableName(),
primary.0.tableName(qualified: false),
"".wrap(primary.1) as Expression<Void>
])
}
Expand Down
8 changes: 8 additions & 0 deletions SQLiteTests/SchemaTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -300,6 +300,10 @@ class SchemaTests : XCTestCase {
"CREATE TABLE \"table\" (\"int64\" INTEGER NOT NULL REFERENCES \"table\" (\"int64\"))",
table.create { t in t.column(int64, references: table, int64) }
)
XCTAssertEqual(
"CREATE TABLE \"table\" (\"int64\" INTEGER NOT NULL REFERENCES \"table\" (\"int64\"))",
table.create { t in t.column(int64, references: qualifiedTable, int64) }
)
XCTAssertEqual(
"CREATE TABLE \"table\" (\"int64\" INTEGER NOT NULL UNIQUE REFERENCES \"table\" (\"int64\"))",
table.create { t in t.column(int64, unique: true, references: table, int64) }
Expand Down Expand Up @@ -724,6 +728,10 @@ class SchemaTests : XCTestCase {
"CREATE UNIQUE INDEX IF NOT EXISTS \"index_table_on_int64\" ON \"table\" (\"int64\")",
table.createIndex([int64], unique: true, ifNotExists: true)
)
XCTAssertEqual(
"CREATE UNIQUE INDEX IF NOT EXISTS \"main\".\"index_table_on_int64\" ON \"table\" (\"int64\")",
qualifiedTable.createIndex([int64], unique: true, ifNotExists: true)
)
}

func test_dropIndex_compilesCreateIndexExpression() {
Expand Down
1 change: 1 addition & 0 deletions SQLiteTests/TestHelpers.swift
Original file line number Diff line number Diff line change
Expand Up @@ -110,5 +110,6 @@ func AssertThrows<T>(@autoclosure expression: () throws -> T, file: StaticString
}

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