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 support for CREATE TABLE ... SELECT ... #141

Merged
merged 9 commits into from
Dec 1, 2021
Merged

Add support for CREATE TABLE ... SELECT ... #141

merged 9 commits into from
Dec 1, 2021

Conversation

gwynne
Copy link
Member

@gwynne gwynne commented Nov 21, 2021

Adds SQLKit support for creating tables populated by SELECT queries:

try await sqlDatabase.create(table: "populated")
    .column("id", type: .bigint, .primaryKey, .notNull)
    .column("data", type: .text)
    .select { $0
        .column(SQLLiteral.default, as: "id")
        .column(SQLFunction("UPPER", args:
            SQLFunction("LTRIM", args: SQLColumn("data"))
        ), as: "data")
        .from("original_table")
        .where("id", .in, Array(1 ... 10))
    }
    .run()
CREATE TABLE "populated" (
    "id" BIGINT PRIMARY KEY GENERATED BY DEFAULT AS IDENTITY NOT NULL,
    "data" TEXT
) AS SELECT
    DEFAULT AS "id",
    UPPER(LTRIM("data")) AS "data"
FROM
    "original_table"
WHERE
    "id" IN ($1, $2, $3, $4, $5, $6, $7, $8, $9, $10)

Additional changes:

  • Significant internal reorganization of SQLKit, specifically in SQLSelectBuilder.
  • The .where(), .orWhere(), .having(), and .orHaving() families of methods on SQLSelectBuilder (and other SQLPredicateBuilders) have been normalized so that all four sets of methods offer the same series of overloads as all the others.
  • More async tests.
  • Support for MySQL's DROP TEMPORARY TABLE syntax, as sqlDatabase.drop(table: "table").temporary().

Note: Despite what may seem appearances to the contrary, the public API of SQLSelectBuilder has not lost any methods, though it has gained a small number of overloads. A concerted effort was made to avoid changing any existing API in any source-incompatible way.

…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.
…tocol. 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).
@gwynne gwynne added enhancement New feature or request semver-minor Contains new APIs labels Nov 21, 2021
@gwynne gwynne self-assigned this Nov 21, 2021
@gwynne gwynne added this to Awaiting Review in Vapor 4 via automation Nov 21, 2021
@Joannis
Copy link
Member

Joannis commented Nov 21, 2021

I don't think I can give a good review on this repo, I'll pass.

@gwynne gwynne merged commit bfcaa63 into main Dec 1, 2021
@gwynne gwynne deleted the create-table-as branch December 1, 2021 19:02
Vapor 4 automation moved this from Awaiting Review to Done Dec 1, 2021
@VaporBot
Copy link

VaporBot commented Dec 1, 2021

These changes are now available in 3.14.0

@gwynne gwynne mentioned this pull request Dec 6, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request semver-minor Contains new APIs
Projects
Vapor 4
  
Done
Development

Successfully merging this pull request may close these issues.

None yet

4 participants