vapor / sql-kit

*️⃣ Build SQL queries in Swift. Extensible, protocol-based design that supports DQL, DML, and DDL.
MIT License
248 stars 58 forks source link

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

Closed gwynne closed 3 years ago

gwynne commented 3 years ago

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:

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.

Joannis commented 3 years ago

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

VaporBot commented 3 years ago

These changes are now available in 3.14.0