Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add CREATE and DROP index queries #84

Merged
merged 4 commits into from Jun 22, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
2 changes: 1 addition & 1 deletion Package.swift
Expand Up @@ -23,7 +23,7 @@ let package = Package(
.package(url: "https://github.com/apple/swift-nio.git", from: "1.0.0"),

// *️⃣ Build SQL queries in Swift. Extensible, protocol-based design that supports DQL, DML, and DDL.
.package(url: "https://github.com/vapor/sql.git", from: "2.0.0-beta"),
.package(url: "https://github.com/vapor/sql.git", from: "2.0.0-beta.3"),
],
targets: [
.target(name: "PostgreSQL", dependencies: ["Async", "Bits", "Core", "Crypto", "DatabaseKit", "NIO", "Service", "SQL"]),
Expand Down
39 changes: 39 additions & 0 deletions Sources/PostgreSQL/SQL/PostgreSQLDropIndex.swift
@@ -0,0 +1,39 @@
public struct PostgreSQLDropIndex: SQLDropIndex {
public var identifier: PostgreSQLIdentifier

/// 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 PostgreSQLDropIndexBuilder<Connection>: SQLQueryBuilder
where Connection: DatabaseQueryable, Connection.Query == PostgreSQLQuery
{
/// `AlterTable` query being built.
public var dropIndex: PostgreSQLDropIndex

/// See `SQLQueryBuilder`.
public var connection: Connection

/// See `SQLQueryBuilder`.
public var query: PostgreSQLQuery {
return .dropIndex(dropIndex)
}

/// Creates a new `SQLCreateIndexBuilder`.
public init(_ dropIndex: PostgreSQLDropIndex, on connection: Connection) {
self.dropIndex = dropIndex
self.connection = connection
}
}


extension DatabaseQueryable where Query == PostgreSQLQuery {
public func drop(index identifier: PostgreSQLIdentifier) -> PostgreSQLDropIndexBuilder<Self> {
return .init(PostgreSQLDropIndex(identifier: identifier), on: self)
}
}
10 changes: 9 additions & 1 deletion Sources/PostgreSQL/SQL/PostgreSQLGeneric.swift
Expand Up @@ -18,7 +18,12 @@ public typealias PostgreSQLColumnIdentifier = GenericSQLColumnIdentifier<
PostgreSQLTableIdentifier, PostgreSQLIdentifier
>

/// See `SQLQuery`
/// See `SQLQuery`.
public typealias PostgreSQLCreateIndex = GenericSQLCreateIndex<
PostgreSQLIndexModifier, PostgreSQLIdentifier, PostgreSQLTableIdentifier
>

/// See `SQLQuery`.
public typealias PostgreSQLCreateTable = GenericSQLCreateTable<
PostgreSQLTableIdentifier, PostgreSQLColumnDefinition, PostgreSQLTableConstraint
>
Expand Down Expand Up @@ -53,6 +58,9 @@ public typealias PostgreSQLForeignKeyAction = GenericSQLForeignKeyAction
/// See `SQLQuery`.
public typealias PostgreSQLGroupBy = GenericSQLGroupBy<PostgreSQLExpression>

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

/// See `SQLQuery`.
public typealias PostgreSQLIdentifier = GenericSQLIdentifier

Expand Down
24 changes: 24 additions & 0 deletions Sources/PostgreSQL/SQL/PostgreSQLQuery.swift
Expand Up @@ -2,12 +2,18 @@ public enum PostgreSQLQuery: SQLQuery {
/// See `SQLQuery`.
public typealias AlterTable = PostgreSQLAlterTable

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

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

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

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

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

Expand All @@ -28,6 +34,11 @@ public enum PostgreSQLQuery: SQLQuery {
return ._alterTable(alterTable)
}

/// See `SQLQuery`.
public static func createIndex(_ createIndex: PostgreSQLCreateIndex) -> PostgreSQLQuery {
return ._createIndex(createIndex)
}

/// See `SQLQuery`.
public static func createTable(_ createTable: CreateTable) -> PostgreSQLQuery {
return ._createTable(createTable)
Expand All @@ -38,6 +49,11 @@ public enum PostgreSQLQuery: SQLQuery {
return ._delete(delete)
}

/// See `SQLQuery`.
public static func dropIndex(_ dropIndex: PostgreSQLDropIndex) -> PostgreSQLQuery {
return ._dropIndex(dropIndex)
}

/// See `SQLQuery`.
public static func dropTable(_ dropTable: DropTable) -> PostgreSQLQuery {
return ._dropTable(dropTable)
Expand Down Expand Up @@ -66,12 +82,18 @@ public enum PostgreSQLQuery: SQLQuery {
/// See `SQLQuery`.
case _alterTable(PostgreSQLAlterTable)

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

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

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

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

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

Expand All @@ -91,8 +113,10 @@ public enum PostgreSQLQuery: 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