Skip to content

Commit

Permalink
add CREATE and DROP index queries
Browse files Browse the repository at this point in the history
  • Loading branch information
tanner0101 committed Jun 22, 2018
1 parent 3a73152 commit ee4b6c1
Show file tree
Hide file tree
Showing 3 changed files with 71 additions and 0 deletions.
39 changes: 39 additions & 0 deletions Sources/SQLite/SQL/SQLiteDropIndex.swift
Original file line number Diff line number Diff line change
@@ -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<Connection>: 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<Self> {
return .init(SQLiteDropIndex(identifier: identifier), on: self)
}
}
8 changes: 8 additions & 0 deletions Sources/SQLite/SQL/SQLiteGeneric.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -54,6 +59,9 @@ public typealias SQLiteGroupBy = GenericSQLGroupBy<SQLiteExpression>
/// See `SQLQuery`.
public typealias SQLiteIdentifier = GenericSQLIdentifier

/// See `SQLQuery`.
public typealias SQLiteIndexModifier = GenericSQLIndexModifier

/// See `SQLQuery`.
public typealias SQLiteInsert = GenericSQLInsert<
SQLiteTableIdentifier, SQLiteColumnIdentifier, SQLiteExpression
Expand Down
24 changes: 24 additions & 0 deletions Sources/SQLite/SQL/SQLiteQuery.swift
Original file line number Diff line number Diff line change
@@ -1,13 +1,19 @@
public enum SQLiteQuery: SQLQuery {
/// See `SQLQuery`.
public typealias AlterTable = SQLiteAlterTable

/// See `SQLQuery`.
public typealias CreateIndex = SQLiteCreateIndex

/// See `SQLQuery`.
public typealias CreateTable = SQLiteCreateTable

/// See `SQLQuery`.
public typealias Delete = SQLiteDelete

/// See `SQLQuery`.
public typealias DropIndex = SQLiteDropIndex

/// See `SQLQuery`.
public typealias DropTable = SQLiteDropTable

Expand All @@ -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 {
Expand All @@ -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)
Expand Down Expand Up @@ -65,13 +81,19 @@ public enum SQLiteQuery: SQLQuery {

/// See `SQLQuery`.
case _alterTable(SQLiteAlterTable)

/// See `SQLQuery`.
case _createIndex(SQLiteCreateIndex)

/// See `SQLQuery`.
case _createTable(SQLiteCreateTable)

/// See `SQLQuery`.
case _delete(SQLiteDelete)

/// See `SQLQuery`.
case _dropIndex(SQLiteDropIndex)

/// See `SQLQuery`.
case _dropTable(SQLiteDropTable)

Expand All @@ -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)
Expand Down

0 comments on commit ee4b6c1

Please sign in to comment.