mirror of
https://github.com/groue/GRDB.swift
synced 2026-11-24 04:07:47 +00:00
41 lines
1.1 KiB
Swift
41 lines
1.1 KiB
Swift
struct SQLIndexGenerator {
|
|
let name: String
|
|
let table: String
|
|
let columns: [String]
|
|
let options: IndexOptions
|
|
let condition: SQLExpression?
|
|
|
|
func sql(_ db: Database) throws -> String {
|
|
var chunks: [String] = []
|
|
chunks.append("CREATE")
|
|
if options.contains(.unique) {
|
|
chunks.append("UNIQUE")
|
|
}
|
|
chunks.append("INDEX")
|
|
if options.contains(.ifNotExists) {
|
|
chunks.append("IF NOT EXISTS")
|
|
}
|
|
chunks.append(name.quotedDatabaseIdentifier)
|
|
chunks.append("ON")
|
|
chunks.append("""
|
|
\(table.quotedDatabaseIdentifier)(\
|
|
\(columns.map(\.quotedDatabaseIdentifier).joined(separator: ", "))\
|
|
)
|
|
""")
|
|
if let condition {
|
|
try chunks.append("WHERE \(condition.quotedSQL(db))")
|
|
}
|
|
return chunks.joined(separator: " ")
|
|
}
|
|
}
|
|
|
|
extension SQLIndexGenerator {
|
|
init(index: IndexDefinition) {
|
|
name = index.name
|
|
table = index.table
|
|
columns = index.columns
|
|
options = index.options
|
|
condition = index.condition
|
|
}
|
|
}
|