Skip to content

Commit

Permalink
Add support for CREATE TABLE ... SELECT ... (#141)
Browse files Browse the repository at this point in the history
* Heavily renormalizes available overloads of `where()`, `orWhere()`, `having()`, and `orHaving()` for `SQLSelectBuilder`, making them fully consistent with themselves and each other (with the exception of not providing the missing untyped `Encodable` overload for `where()` since it wasn't desirable to have it at all to begin with). Separates the `HAVING` predicate into its own secondary predicate builder.
* Switch to using SQLStatement in SQLCreateTable serialization.
* Add missing SQLSecondaryPredicateGroupBuilder counterpart to SQLPredicateGroupBuilder
* Refactor most of SQLSelectBuilder into a SQLSubqueryClauseBuilder protocol. Despite all appearances to the contrary, the public API does not change (aside from gaining a couple of extra methods for consistency in the `where` and `having` method suites).
* Add support for specifying `CREATE TABLE ... SELECT` queries.
* Organize SQLKitTests.swift (and its async counterpart) a little.
* Add async version of SQLKitTriggerTests for good measure
* Also add support for MySQL's DROP TEMPORARY TABLE syntax, because it's there.
  • Loading branch information
gwynne authored Dec 1, 2021
1 parent 1a788f4 commit bfcaa63
Show file tree
Hide file tree
Showing 13 changed files with 1,173 additions and 629 deletions.
14 changes: 14 additions & 0 deletions Sources/SQLKit/Builders/SQLCreateTableAsSubqueryBuilder.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
/// A builder used to construct a `SELECT` query for use as part of a `CREATE TABLE` query.
///
/// - Note: There's really nothing for this builder to do besides provide a concrete storage
/// for the `select` property of `SQLSubqueryClauseBuilder`. All of the interesting methods
/// are on the protocol.
public final class SQLCreateTableAsSubqueryBuilder: SQLSubqueryClauseBuilder {
// See `SQLSubqueryClauseBuilder.select`.
public var select: SQLSelect

/// Create a new `SQLCreateTableAsSubqueryBuilder`.
internal init() {
self.select = .init()
}
}
11 changes: 11 additions & 0 deletions Sources/SQLKit/Builders/SQLCreateTableBuilder.swift
Original file line number Diff line number Diff line change
Expand Up @@ -109,6 +109,17 @@ public final class SQLCreateTableBuilder: SQLQueryBuilder {
createTable.ifNotExists = true
return self
}

/// Specify a `SELECT` query to be used to populate the new table.
///
/// If called more than once, each subsequent invocation overwrites the query from the one before.
@discardableResult
public func select(_ closure: (SQLCreateTableAsSubqueryBuilder) -> SQLCreateTableAsSubqueryBuilder) -> Self {
let builder = SQLCreateTableAsSubqueryBuilder()
_ = closure(builder)
createTable.asQuery = builder.select
return self
}
}

// MARK: Constraints
Expand Down
12 changes: 10 additions & 2 deletions Sources/SQLKit/Builders/SQLDropTableBuilder.swift
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,10 @@ public final class SQLDropTableBuilder: SQLQueryBuilder {
/// `DropTable` query being built.
public var dropTable: SQLDropTable

/// See `SQLQueryBuilder`.
// See `SQLQueryBuilder.database`.
public var database: SQLDatabase

/// See `SQLQueryBuilder`.
// See `SQLQueryBuilder.query`.
public var query: SQLExpression {
return self.dropTable
}
Expand Down Expand Up @@ -53,6 +53,14 @@ public final class SQLDropTableBuilder: SQLQueryBuilder {
dropTable.behavior = SQLDropBehavior.restrict
return self
}

/// If the "TEMPORARY" keyword occurs between "DROP" and "TABLE" then only temporary tables are dropped,
/// and the drop does not cause an implicit transaction commit.
@discardableResult
public func temporary() -> Self {
dropTable.temporary = true
return self
}
}

// MARK: Connection
Expand Down
Loading

0 comments on commit bfcaa63

Please sign in to comment.