diff --git a/Sources/SQLite/SQL/SQLiteDropIndex.swift b/Sources/SQLite/SQL/SQLiteDropIndex.swift new file mode 100644 index 0000000..2542672 --- /dev/null +++ b/Sources/SQLite/SQL/SQLiteDropIndex.swift @@ -0,0 +1,39 @@ +public struct SQLiteDropIndex: SQLDropIndex { + public var identifier: SQLiteIdentifier + + /// See `SQLSerializable`. + public func serialize(_ binds: inout [Encodable]) -> String { + var sql: [String] = [] + sql.append("DROP INDEX") + sql.append(identifier.serialize(&binds)) + return sql.joined(separator: " ") + } +} + +public final class SQLiteDropIndexBuilder: SQLQueryBuilder + where Connection: DatabaseQueryable, Connection.Query == SQLiteQuery +{ + /// `AlterTable` query being built. + public var dropIndex: SQLiteDropIndex + + /// See `SQLQueryBuilder`. + public var connection: Connection + + /// See `SQLQueryBuilder`. + public var query: SQLiteQuery { + return .dropIndex(dropIndex) + } + + /// Creates a new `SQLCreateIndexBuilder`. + public init(_ dropIndex: SQLiteDropIndex, on connection: Connection) { + self.dropIndex = dropIndex + self.connection = connection + } +} + + +extension DatabaseQueryable where Query == SQLiteQuery { + public func drop(index identifier: SQLiteIdentifier) -> SQLiteDropIndexBuilder { + return .init(SQLiteDropIndex(identifier: identifier), on: self) + } +} diff --git a/Sources/SQLite/SQL/SQLiteGeneric.swift b/Sources/SQLite/SQL/SQLiteGeneric.swift index c725bff..3c152c8 100644 --- a/Sources/SQLite/SQL/SQLiteGeneric.swift +++ b/Sources/SQLite/SQL/SQLiteGeneric.swift @@ -21,6 +21,11 @@ public typealias SQLiteColumnIdentifier = GenericSQLColumnIdentifier< SQLiteTableIdentifier, SQLiteIdentifier > +/// See `SQLQuery`. +public typealias SQLiteCreateIndex = GenericSQLCreateIndex< + SQLiteIndexModifier, SQLiteIdentifier, SQLiteTableIdentifier +> + /// See `SQLQuery`. public typealias SQLiteDelete = GenericSQLDelete< SQLiteTableIdentifier, SQLiteExpression @@ -54,6 +59,9 @@ public typealias SQLiteGroupBy = GenericSQLGroupBy /// See `SQLQuery`. public typealias SQLiteIdentifier = GenericSQLIdentifier +/// See `SQLQuery`. +public typealias SQLiteIndexModifier = GenericSQLIndexModifier + /// See `SQLQuery`. public typealias SQLiteInsert = GenericSQLInsert< SQLiteTableIdentifier, SQLiteColumnIdentifier, SQLiteExpression diff --git a/Sources/SQLite/SQL/SQLiteQuery.swift b/Sources/SQLite/SQL/SQLiteQuery.swift index 6e8456b..71d05b7 100644 --- a/Sources/SQLite/SQL/SQLiteQuery.swift +++ b/Sources/SQLite/SQL/SQLiteQuery.swift @@ -1,6 +1,9 @@ public enum SQLiteQuery: SQLQuery { /// See `SQLQuery`. public typealias AlterTable = SQLiteAlterTable + + /// See `SQLQuery`. + public typealias CreateIndex = SQLiteCreateIndex /// See `SQLQuery`. public typealias CreateTable = SQLiteCreateTable @@ -8,6 +11,9 @@ public enum SQLiteQuery: SQLQuery { /// See `SQLQuery`. public typealias Delete = SQLiteDelete + /// See `SQLQuery`. + public typealias DropIndex = SQLiteDropIndex + /// See `SQLQuery`. public typealias DropTable = SQLiteDropTable @@ -27,6 +33,11 @@ public enum SQLiteQuery: SQLQuery { public static func alterTable(_ alterTable: SQLiteAlterTable) -> SQLiteQuery { return ._alterTable(alterTable) } + + /// See `SQLQuery`. + public static func createIndex(_ createIndex: SQLiteCreateIndex) -> SQLiteQuery { + return ._createIndex(createIndex) + } /// See `SQLQuery`. public static func createTable(_ createTable: SQLiteCreateTable) -> SQLiteQuery { @@ -38,6 +49,11 @@ public enum SQLiteQuery: SQLQuery { return ._delete(delete) } + /// See `SQLQuery`. + public static func dropIndex(_ dropIndex: SQLiteDropIndex) -> SQLiteQuery { + return ._dropIndex(dropIndex) + } + /// See `SQLQuery`. public static func dropTable(_ dropTable: SQLiteDropTable) -> SQLiteQuery { return ._dropTable(dropTable) @@ -65,6 +81,9 @@ public enum SQLiteQuery: SQLQuery { /// See `SQLQuery`. case _alterTable(SQLiteAlterTable) + + /// See `SQLQuery`. + case _createIndex(SQLiteCreateIndex) /// See `SQLQuery`. case _createTable(SQLiteCreateTable) @@ -72,6 +91,9 @@ public enum SQLiteQuery: SQLQuery { /// See `SQLQuery`. case _delete(SQLiteDelete) + /// See `SQLQuery`. + case _dropIndex(SQLiteDropIndex) + /// See `SQLQuery`. case _dropTable(SQLiteDropTable) @@ -91,8 +113,10 @@ public enum SQLiteQuery: SQLQuery { public func serialize(_ binds: inout [Encodable]) -> String { switch self { case ._alterTable(let alterTable): return alterTable.serialize(&binds) + case ._createIndex(let createIndex): return createIndex.serialize(&binds) case ._createTable(let createTable): return createTable.serialize(&binds) case ._delete(let delete): return delete.serialize(&binds) + case ._dropIndex(let dropIndex): return dropIndex.serialize(&binds) case ._dropTable(let dropTable): return dropTable.serialize(&binds) case ._insert(let insert): return insert.serialize(&binds) case ._select(let select): return select.serialize(&binds)