Skip to content

Commit 94dc7fd

Browse files
committed
treat undefined values as NULL. Include test to verify.
1 parent 558cbbb commit 94dc7fd

File tree

2 files changed

+19
-1
lines changed

2 files changed

+19
-1
lines changed

lib/connection.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -132,7 +132,7 @@ p.bind = function(config, more) {
132132
.addInt16(len); //number of parameters
133133
for(var i = 0; i < len; i++) {
134134
var val = values[i];
135-
if(val === null) {
135+
if(val === null || typeof val === "undefined") {
136136
buffer.addInt32(-1);
137137
} else {
138138
val = val.toString();

test/integration/client/api-tests.js

+18
Original file line numberDiff line numberDiff line change
@@ -140,3 +140,21 @@ test('can provide callback and config and parameters', function() {
140140
}))
141141
}))
142142
})
143+
144+
test('null and undefined are both inserted as NULL', function() {
145+
pg.connect(connectionString, assert.calls(function(err, client) {
146+
assert.isNull(err);
147+
client.query("CREATE TEMP TABLE my_nulls(a varchar(1), b varchar(1), c integer, d integer, e date, f date)");
148+
client.query("INSERT INTO my_nulls(a,b,c,d,e,f) VALUES ($1,$2,$3,$4,$5,$6)", [ null, undefined, null, undefined, null, undefined ]);
149+
client.query("SELECT * FROM my_nulls", assert.calls(function(err, result) {
150+
assert.isNull(err);
151+
assert.equal(result.rows.length, 1);
152+
assert.isNull(result.rows[0].a);
153+
assert.isNull(result.rows[0].b);
154+
assert.isNull(result.rows[0].c);
155+
assert.isNull(result.rows[0].d);
156+
assert.isNull(result.rows[0].e);
157+
assert.isNull(result.rows[0].f);
158+
}))
159+
}))
160+
})

0 commit comments

Comments
 (0)