Kitura / Swift-Kuery-ORM

An ORM for Swift, built on Codable
Apache License 2.0
212 stars 30 forks source link

QueryBuilder wrong type returned for Swift.Double #66

Closed shial4 closed 6 years ago

shial4 commented 6 years ago

Context and Description

if let table = try? Model.getTable(), let column = table.columns.first(where: { $0.name == "aspectratio" }),
    let type = column.type?.create(queryBuilder: QueryBuilder()) {
    let addColumnQuery = "ALTER TABLE " + table.nameInQuery + " ADD COLUMN  " + "\(column.name) \(type)"
    Log.v("WILL ALTER TABLE: \(table.nameInQuery) with column:\(column.name) type:\(type)")
    stash?.getConnection()?.execute(addColumnQuery) { result in
        Log.v("ALTER TABLE: \(table.nameInQuery) result: \(result)")
    }
}

ERROR: type "double" does not exist

QueryBuilder() for swift type Double retourns "double" instead of "double precision"

(lldb) po column.type
▿ Optional<SQLDataType.Type>
  - some : Swift.Double

(lldb) po column.type?.create(queryBuilder: QueryBuilder())
▿ Optional<String>
  - some : "double"

Environment Details

.package(url: "https://github.com/IBM-Swift/Swift-Kuery-ORM.git", from: "0.2.0"),
.package(url: "https://github.com/IBM-Swift/Swift-Kuery-PostgreSQL.git", from: "1.0.0"),

SwiftKueryORM.git -> "0.3.1"
SwiftKueryPostgreSQL -> "1.2.0"

Steps to Reproduce

1) Create Model class with Swift.Double properties 2) Run program to setup table and all 3) Modify Model class adding new properties type of Swift.Double 4) Try executing custom query to alter table. (We want to insert new column) 5) Query example above. Type returned is wrong

ERROR: type "double" does not exist

Expected vs. Actual Behaviour

column.type?.create(queryBuilder: QueryBuilder()) For swift type Double should return "double precision". However, at current version 0.3.1 it does return "double".

kilnerm commented 6 years ago

The issue is here:

let type = column.type?.create(queryBuilder: QueryBuilder()) {

The QueryBuilder being passed to create is the default QueryBuilder. You would need to pass the database specific QueryBuilder to access the correct type substitution. Each database connection has a queryBuilder property which should allow you to do this.

Let me know if you need more help.

kilnerm commented 6 years ago

Marking this as resolved.