Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -193,6 +193,10 @@ Retrieve the text value at a given tuple (row) and field (column) offset. Both o

Returns `true` if the value at the given offsets is actually `null`. Otherwise returns `false`. This is because `pq.getvalue()` returns an empty string for both an actual empty string and for a `null` value. Weird, huh?

##### `pq.getvalues():Array<string|null>`

Retrieve all the text values of the result in one flat array, row by row: the value at tuple `t` and field `f` is at index `t * pq.nfields() + f`. A null value is returned as `null`. Faster than calling `pq.getvalue()` for each value of a large result.

##### `pq.nparams():int`

Returns the number of parameters a prepared statement expects.
Expand Down
3 changes: 3 additions & 0 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -238,6 +238,9 @@ PQ.prototype.getvalue = PQ.prototype.$getvalue;
//returns true/false if the value is null
PQ.prototype.getisnull = PQ.prototype.$getisnull;

//returns all the values of the result in one flat array, row by row, null for a null value
PQ.prototype.getvalues = PQ.prototype.$getvalues;

//returns the number of parameters of a prepared statement
PQ.prototype.nparams = PQ.prototype.$nparams;

Expand Down
1 change: 1 addition & 0 deletions src/addon.cc
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@ NAN_MODULE_INIT(InitAddon) {
Nan::SetPrototypeMethod(tpl, "$ftype", Connection::Ftype);
Nan::SetPrototypeMethod(tpl, "$getvalue", Connection::Getvalue);
Nan::SetPrototypeMethod(tpl, "$getisnull", Connection::Getisnull);
Nan::SetPrototypeMethod(tpl, "$getvalues", Connection::Getvalues);
Nan::SetPrototypeMethod(tpl, "$nparams", Connection::Nparams);
Nan::SetPrototypeMethod(tpl, "$paramtype", Connection::Paramtype);
Nan::SetPrototypeMethod(tpl, "$cmdStatus", Connection::CmdStatus);
Expand Down
29 changes: 29 additions & 0 deletions src/connection.cc
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
#include "addon.h"
#include <vector>

Connection::Connection() : Nan::ObjectWrap() {
TRACE("Connection::Constructor");
Expand Down Expand Up @@ -309,6 +310,34 @@ NAN_METHOD(Connection::Getisnull) {
info.GetReturnValue().Set(rowValue == 1);
}

// every value of the result in one call: one native call per cell is the main cost of a large result
NAN_METHOD(Connection::Getvalues) {
TRACE("Connection::Getvalues");
Connection *self = NODE_THIS();

PGresult* res = self->lastResult;
int rows = PQntuples(res);
int cols = PQnfields(res);
v8::Isolate* isolate = info.GetIsolate();
std::vector<v8::Local<v8::Value>> values((size_t)rows * cols);
v8::Local<v8::Value> null = Nan::Null();

size_t i = 0;
for (int r = 0; r < rows; r++) {
for (int c = 0; c < cols; c++, i++) {
if (PQgetisnull(res, r, c)) {
values[i] = null;
} else {
values[i] = v8::String::NewFromUtf8(
isolate, PQgetvalue(res, r, c), v8::NewStringType::kNormal, PQgetlength(res, r, c)
).ToLocalChecked();
}
}
}

info.GetReturnValue().Set(v8::Array::New(isolate, values.data(), values.size()));
}

NAN_METHOD(Connection::Nparams) {
TRACE("Connection::Nparams");
Connection *self = NODE_THIS();
Expand Down
1 change: 1 addition & 0 deletions src/connection.h
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ class Connection : public Nan::ObjectWrap {
static NAN_METHOD(Ftype);
static NAN_METHOD(Getvalue);
static NAN_METHOD(Getisnull);
static NAN_METHOD(Getvalues);
static NAN_METHOD(Nparams);
static NAN_METHOD(Paramtype);
static NAN_METHOD(CmdStatus);
Expand Down
4 changes: 4 additions & 0 deletions test/sync-integration.js
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,10 @@ describe('low-level query integration tests', function () {
assert.strictEqual(this.pq.getisnull(2, 1), true);
});

it('has correct values in one call', function () {
assert.deepStrictEqual(this.pq.getvalues(), ['brian', '32', 'aaron', '30', '', null]);
});

it('has correct identifiers', function() {
assert.notEqual(this.pq.ftable(0), 0);
assert.notEqual(this.pq.ftable(1), 0);
Expand Down