vapor-community / postgresql

Robust PostgreSQL interface for Swift
MIT License
131 stars 33 forks source link

Optional Double field #13

Closed pronebird closed 8 years ago

pronebird commented 8 years ago

I have optional double field in my vapor model and if I omit that field when saving model, then database driver spits out:

Uncaught Error: DatabaseError.invalidSQL("ERROR:  invalid input syntax for type double precision: \"\"\n"). Use middleware to catch this error and provide a better response. Otherwise, a 500 error page will be returned in the production environment.% 

I think the problem is in Database.swift which applies bindings:

for i in 0..<values.count {
  var ch = values[i].string?.bytes ?? []
  ch.append(0)

The value that is translated from model is Node.Node.null but then the loop itself simply pushes null terminated string back to database, but instead probably should produce NULL to indicate the intention.

The query itself is:

INSERT INTO users (phone, latitude, longitude, username) VALUES ($1, $2, $3, $4)

Running the following SQL manually works fine:

INSERT INTO users (phone, latitude, longitude, username) VALUES ('+340000000000', NULL, NULL, 'user');

Spending some time reading through docs revealed that PQexecParams converts nil pointer to NULL.

Reference: https://www.postgresql.org/docs/9.1/static/libpq-exec.html