sql-kit icon indicating copy to clipboard operation
sql-kit copied to clipboard

Expand SQLDataType options

Open kerusan opened this issue 6 years ago • 6 comments

Code snippet

        try self.db.create(table: "galaxies")
            .column("id", type: .int, .primaryKey)
            .column("name", type: .text)
            .run().wait()

Generates SQL as

CREATE TABLE "galaxies"("id" INTEGER PRIMARY KEY DEFAULT UNIQUE, "name" TEXT)

The data type TEXT is not part of SQL 92, and is not recognized by Frontbase. There needs to be some way to declare or alias to a VARCHAR or CHARACTER VARYING type.

Vapor 3 was more flexible.

(Autoincrement is implemented in Frontbase by setting the default to the UNIQUE function).

kerusan avatar Oct 18 '19 09:10 kerusan

You can pass any SQLExpression to the type and constraints fields:

try self.db.create(table: "galaxies")
    .column("id", type: SQLRaw("INTEGER"), SQLRaw("UNIQUE"))
    .column("name", type: SQLRaw("VARCHAR"))
    .run().wait()

That said, maybe we could have SQLDataType integrate with SQLDialect. Perhaps instead of having defaults like .int, .text, etc, we have SQLDialect attempt to return the best possible type for a given Swift type? I'm open to ideas here.

tanner0101 avatar Oct 18 '19 20:10 tanner0101

+1

kerusan avatar Oct 21 '19 07:10 kerusan

The +1 is for solving it with ' SQLDataType integrate with SQLDialect'

kerusan avatar Oct 23 '19 07:10 kerusan

+1

HagvallDataAB avatar Dec 05 '19 08:12 HagvallDataAB

You can pass any SQLExpression to the type and constraints fields:

try self.db.create(table: "galaxies")
    .column("id", type: SQLRaw("INTEGER"), SQLRaw("UNIQUE"))
    .column("name", type: SQLRaw("VARCHAR"))
    .run().wait()

This solution does work, although it needs to be expressed as either

try self.db.create(table: "galaxies")
    .column("id", type: .custom(SQLRaw("INTEGER")), .custom(SQLRaw("UNIQUE")))
    .column("name", type: .custom(SQLRaw("VARCHAR")))
    .run().wait()

or

try self.db.create(table: "galaxies")
    .column(SQLRaw("id"), type: SQLRaw("INTEGER"), SQLRaw("UNIQUE"))
    .column(SQLRaw("name"), type: SQLRaw("VARCHAR"))
    .run().wait()

johan-carlberg avatar Jan 29 '20 14:01 johan-carlberg

SQLDataType goes through SQLDialect customDataType(for dataType: SQLDataType) function so this issue can be closed now I think.

fatto avatar Aug 19 '22 14:08 fatto