diff --git a/GRDB/QueryInterface/TableBuilder.swift b/GRDB/QueryInterface/TableBuilder.swift index b32019936..ea32bea07 100644 --- a/GRDB/QueryInterface/TableBuilder.swift +++ b/GRDB/QueryInterface/TableBuilder.swift @@ -1,9 +1,15 @@ public class SQLTableBuilder { let name: String + let temporary: Bool + let ifNotExists: Bool + let withoutRowID: Bool var columns: [SQLColumnBuilder] = [] - init(name: String) { + init(name: String, temporary: Bool, ifNotExists: Bool, withoutRowID: Bool) { self.name = name + self.temporary = temporary + self.ifNotExists = ifNotExists + self.withoutRowID = withoutRowID } public func column(name: String, _ type: SQLColumnType) -> SQLColumnBuilder { @@ -15,9 +21,18 @@ public class SQLTableBuilder { var sql: String { var chunks: [String] = [] chunks.append("CREATE") + if temporary { + chunks.append("TEMPORARY") + } chunks.append("TABLE") + if ifNotExists { + chunks.append("IF NOT EXISTS") + } chunks.append(name.quotedDatabaseIdentifier) chunks.append("(" + columns.map { $0.sql }.joinWithSeparator(", ") + ")") + if withoutRowID { + chunks.append("WITHOUT ROWID") + } return chunks.joinWithSeparator(" ") } } @@ -25,29 +40,64 @@ public class SQLTableBuilder { public class SQLColumnBuilder { let name: String let type: SQLColumnType - var isPrimaryKey: Bool = false + var primaryKeyBuilder: SQLPrimaryKeyBuilder? init(name: String, type: SQLColumnType) { self.name = name self.type = type } - public func primaryKey() { - isPrimaryKey = true + public func primaryKey(ordering ordering: SQLPrimaryKeyOrdering? = nil, onConflict conflictResolution: SQLConflictResolution? = nil, autoincrement: Bool = false) { + primaryKeyBuilder = SQLPrimaryKeyBuilder(ordering: ordering, conflictResolution: conflictResolution, autoincrement: autoincrement) } var sql: String { var chunks: [String] = [] chunks.append(name.quotedDatabaseIdentifier) chunks.append(type.rawValue) - if isPrimaryKey { - chunks.append("PRIMARY KEY") + if let primaryKeyBuilder = primaryKeyBuilder { + chunks.append(primaryKeyBuilder.sql) } return chunks.joinWithSeparator(" ") } } -public enum SQLColumnType: String { +struct SQLPrimaryKeyBuilder { + let ordering: SQLPrimaryKeyOrdering? + let conflictResolution: SQLConflictResolution? + let autoincrement: Bool + + var sql: String { + var chunks: [String] = [] + chunks.append("PRIMARY KEY") + if let ordering = ordering { + chunks.append(ordering.rawValue) + } + if let conflictResolution = conflictResolution { + chunks.append("ON CONFLICT") + chunks.append(conflictResolution.rawValue) + } + if autoincrement { + chunks.append("AUTOINCREMENT") + } + return chunks.joinWithSeparator(" ") + } +} + +public enum SQLPrimaryKeyOrdering : String { + case Asc = "ASC" + case Desc = "DESC" +} + +public enum SQLConflictResolution : String { + case Rollback = "ROLLBACK" + case Abort = "ABORT" + case Fail = "FAIL" + case Ignore = "IGNORE" + case Replace = "REPLACE" +} + +public enum SQLColumnType : String { case Text = "TEXT" case Integer = "INTEGER" case Double = "DOUBLE" @@ -59,9 +109,16 @@ public enum SQLColumnType: String { } extension Database { - public func create(table name: String, body: (SQLTableBuilder) -> Void) throws { - let builder = SQLTableBuilder(name: name) + // TODO: doc + // TODO: Don't expose withoutRowID if not available + public func create(table name: String, temporary: Bool = false, ifNotExists: Bool = false, withoutRowID: Bool = false, body: (SQLTableBuilder) -> Void) throws { + let builder = SQLTableBuilder(name: name, temporary: temporary, ifNotExists: ifNotExists, withoutRowID: withoutRowID) body(builder) try execute(builder.sql) } + + // TODO: doc + public func drop(table name: String) throws { + try execute("DROP TABLE \(name.quotedDatabaseIdentifier)") + } } diff --git a/Tests/Public/SQLTableBuilder/SQLTableBuilderTests.swift b/Tests/Public/SQLTableBuilder/SQLTableBuilderTests.swift index 5f8dec12e..3f67b5fe3 100644 --- a/Tests/Public/SQLTableBuilder/SQLTableBuilderTests.swift +++ b/Tests/Public/SQLTableBuilder/SQLTableBuilderTests.swift @@ -13,11 +13,22 @@ class SQLTableBuilderTests: GRDBTestCase { assertNoError { let dbQueue = try makeDatabaseQueue() try dbQueue.inDatabase { db in + // Simple table creation try db.create(table: "test") { t in t.column("id", .Integer).primaryKey() t.column("name", .Text) } XCTAssertEqual(self.lastSQLQuery, "CREATE TABLE \"test\" (\"id\" INTEGER PRIMARY KEY, \"name\" TEXT)") + + // Drop table + try db.drop(table: "test") + XCTAssertEqual(self.lastSQLQuery, "DROP TABLE \"test\"") + + // Table options and column primary key options + try db.create(table: "test", temporary: true, ifNotExists: true, withoutRowID: true) { t in + t.column("id", .Integer).primaryKey(ordering: .Desc, onConflict: .Fail) + } + XCTAssertEqual(self.lastSQLQuery, "CREATE TEMPORARY TABLE IF NOT EXISTS \"test\" (\"id\" INTEGER PRIMARY KEY DESC ON CONFLICT FAIL) WITHOUT ROWID") } } }