Skip to content
This repository has been archived by the owner on Mar 30, 2024. It is now read-only.

Commit

Permalink
Merge pull request #14 from morphine-apps/hotfix/query-optional-bindi…
Browse files Browse the repository at this point in the history
…ngs-fix

Fix NULL conversion for optional fields
  • Loading branch information
natebird authored Oct 10, 2016
2 parents fef6f10 + 5e1079a commit 7b372ed
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 0 deletions.
11 changes: 11 additions & 0 deletions Sources/PostgreSQL/Database.swift
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,10 @@ public class Database {
defer {
for i in 0..<values.count {
let p = paramsValues[i]
if p == nil {
continue
}

let mp = UnsafeMutablePointer(mutating: p)
mp?.deinitialize()
var i = 0
Expand Down Expand Up @@ -85,6 +89,13 @@ public class Database {
.allocate(capacity: values.count)

for i in 0..<values.count {
// PQexecParams converts nil pointer to NULL.
// see: https://www.postgresql.org/docs/9.1/static/libpq-exec.html
if values[i] == .null {
paramsValues[i] = nil
continue
}

var ch = values[i].string?.bytes ?? []
ch.append(0)

Expand Down
2 changes: 2 additions & 0 deletions Tests/PostgreSQLTests/PostgreSQLTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,8 @@ class PostgreSQLTests: XCTestCase {
do {
try postgreSQL.execute("DROP TABLE IF EXISTS parameterization")
try postgreSQL.execute("CREATE TABLE parameterization (d FLOAT4, i INT, s VARCHAR(16), u INT)")

try postgreSQL.execute("INSERT INTO parameterization VALUES ($1, $2, $3, $4)", [.null, .null, "life".makeNode(), .null], on: nil)

try postgreSQL.execute("INSERT INTO parameterization VALUES (3.14, NULL, 'pi', NULL)")
try postgreSQL.execute("INSERT INTO parameterization VALUES (NULL, NULL, 'life', 42)")
Expand Down

0 comments on commit 7b372ed

Please sign in to comment.