Skip to content

Commit

Permalink
Merge pull request #43 from vapor/index
Browse files Browse the repository at this point in the history
add CREATE and DROP index queries
  • Loading branch information
tanner0101 authored Jun 22, 2018
2 parents 3a73152 + 045d010 commit 500748e
Show file tree
Hide file tree
Showing 4 changed files with 71 additions and 17 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
17 changes: 0 additions & 17 deletions circle.yml
Original file line number Diff line number Diff line change
@@ -1,14 +1,6 @@
version: 2

jobs:
macos:
macos:
xcode: "9.2"
steps:
- checkout
- run: swift build
- run: swift test

linux:
docker:
- image: codevapor/swift:4.1
Expand All @@ -20,7 +12,6 @@ jobs:
- run:
name: Run unit tests
command: swift test

linux-release:
docker:
- image: codevapor/swift:4.1
Expand All @@ -29,8 +20,6 @@ jobs:
- run:
name: Compile code with optimizations
command: swift build -c release


linux-fluent:
docker:
- image: codevapor/swift:4.1
Expand All @@ -47,17 +36,13 @@ jobs:
name: Run Fluent SQLite unit tests
command: swift test
working_directory: ~/fluent-sqlite


workflows:
version: 2
tests:
jobs:
- linux
- linux-fluent
- linux-release
# - macos

nightly:
triggers:
- schedule:
Expand All @@ -68,5 +53,3 @@ workflows:
- master
jobs:
- linux
# - macos

0 comments on commit 500748e

Please sign in to comment.