Skip to content

Commit

Permalink
Added ftable and ftablecol methods (#58)
Browse files Browse the repository at this point in the history
  • Loading branch information
TVrijdag authored Jun 19, 2024
1 parent de20241 commit 14d7c03
Show file tree
Hide file tree
Showing 7 changed files with 60 additions and 0 deletions.
9 changes: 9 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -169,6 +169,15 @@ Retrieve the number of fields (columns) from the result.

Retrieve the name of the field (column) at the given offset. Offset starts at 0.

##### `pq.ftable(fieldNumber:int):int`

Retrieve the `Oid` of the table at the given offset. Offset starts at 0.

##### `pq.ftablenum(fieldNumber:int):int`

Retrieve the column number (within its table) of the field at the given offset. Offset starts at 0.
Query-result column numbers start at 0, but table columns have nonzero numbers.

##### `pq.ftype(fieldNumber:int):int`

Retrieve the `Oid` of the field (column) at the given offset. Offset starts at 0.
Expand Down
10 changes: 10 additions & 0 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -230,6 +230,16 @@ PQ.prototype.fname = function (offset) {
return this.$fname(offset);
};

//returns the Oid of the table of the field at the given offset
PQ.prototype.ftable = function(offset) {
return this.$ftable(offset);
};

//returns the column number (within its table) of the field at the given offset
PQ.prototype.ftablecol = function(offset) {
return this.$ftablecol(offset);
};

//returns the Oid of the type for the given field
PQ.prototype.ftype = function (offset) {
return this.$ftype(offset);
Expand Down
2 changes: 2 additions & 0 deletions src/addon.cc
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,8 @@ NAN_MODULE_INIT(InitAddon) {
Nan::SetPrototypeMethod(tpl, "$ntuples", Connection::Ntuples);
Nan::SetPrototypeMethod(tpl, "$nfields", Connection::Nfields);
Nan::SetPrototypeMethod(tpl, "$fname", Connection::Fname);
Nan::SetPrototypeMethod(tpl, "$ftable", Connection::Ftable);
Nan::SetPrototypeMethod(tpl, "$ftablecol", Connection::Ftablecol);
Nan::SetPrototypeMethod(tpl, "$ftype", Connection::Ftype);
Nan::SetPrototypeMethod(tpl, "$getvalue", Connection::Getvalue);
Nan::SetPrototypeMethod(tpl, "$getisnull", Connection::Getisnull);
Expand Down
22 changes: 22 additions & 0 deletions src/connection.cc
Original file line number Diff line number Diff line change
Expand Up @@ -217,6 +217,28 @@ NAN_METHOD(Connection::Fname) {
info.GetReturnValue().Set(Nan::New<v8::String>(colName).ToLocalChecked());
}

NAN_METHOD(Connection::Ftable) {
TRACE("Connection::Ftable");
Connection *self = NODE_THIS();

PGresult* res = self->lastResult;

int table = PQftable(res, info[0]->Int32Value());

info.GetReturnValue().Set(table);
}

NAN_METHOD(Connection::Ftablecol) {
TRACE("Connection::Ftablecol");
Connection *self = NODE_THIS();

PGresult* res = self->lastResult;

int tablecol = PQftablecol(res, info[0]->Int32Value());

info.GetReturnValue().Set(tablecol);
}

NAN_METHOD(Connection::Ftype) {
TRACE("Connection::Ftype");
Connection *self = NODE_THIS();
Expand Down
2 changes: 2 additions & 0 deletions src/connection.h
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@ class Connection : public Nan::ObjectWrap {
static NAN_METHOD(Ntuples);
static NAN_METHOD(Nfields);
static NAN_METHOD(Fname);
static NAN_METHOD(Ftable);
static NAN_METHOD(Ftablecol);
static NAN_METHOD(Ftype);
static NAN_METHOD(Getvalue);
static NAN_METHOD(Getisnull);
Expand Down
7 changes: 7 additions & 0 deletions test/sync-integration.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,5 +26,12 @@ describe('low-level query integration tests', function () {
assert.strictEqual(this.pq.getvalue(2, 1), '');
assert.strictEqual(this.pq.getisnull(2, 1), true);
});

it('has correct identifiers', function() {
assert.notEqual(this.pq.ftable(0), 0);
assert.notEqual(this.pq.ftable(1), 0);
assert.strictEqual(this.pq.ftablecol(0), 1);
assert.strictEqual(this.pq.ftablecol(1), 2);
});
});
});
8 changes: 8 additions & 0 deletions test/sync.js
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,14 @@ describe('result checking', function() {
assert.equal(this.pq.fname(0), 'my_col');
});

it('has table oid zero', function() {
assert.strictEqual(this.pq.ftable(0), 0);
});

it('has column number of the query-result', function() {
assert.strictEqual(this.pq.ftablecol(0), 0);
});

it('has oid type of timestamptz', function() {
assert.strictEqual(this.pq.ftype(0), 1184);
});
Expand Down

0 comments on commit 14d7c03

Please sign in to comment.