Skip to content

Commit 14d7c03

Browse files
authored
Added ftable and ftablecol methods (#58)
1 parent de20241 commit 14d7c03

File tree

7 files changed

+60
-0
lines changed

7 files changed

+60
-0
lines changed

README.md

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -169,6 +169,15 @@ Retrieve the number of fields (columns) from the result.
169169

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

172+
##### `pq.ftable(fieldNumber:int):int`
173+
174+
Retrieve the `Oid` of the table at the given offset. Offset starts at 0.
175+
176+
##### `pq.ftablenum(fieldNumber:int):int`
177+
178+
Retrieve the column number (within its table) of the field at the given offset. Offset starts at 0.
179+
Query-result column numbers start at 0, but table columns have nonzero numbers.
180+
172181
##### `pq.ftype(fieldNumber:int):int`
173182

174183
Retrieve the `Oid` of the field (column) at the given offset. Offset starts at 0.

index.js

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -230,6 +230,16 @@ PQ.prototype.fname = function (offset) {
230230
return this.$fname(offset);
231231
};
232232

233+
//returns the Oid of the table of the field at the given offset
234+
PQ.prototype.ftable = function(offset) {
235+
return this.$ftable(offset);
236+
};
237+
238+
//returns the column number (within its table) of the field at the given offset
239+
PQ.prototype.ftablecol = function(offset) {
240+
return this.$ftablecol(offset);
241+
};
242+
233243
//returns the Oid of the type for the given field
234244
PQ.prototype.ftype = function (offset) {
235245
return this.$ftype(offset);

src/addon.cc

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,8 @@ NAN_MODULE_INIT(InitAddon) {
4444
Nan::SetPrototypeMethod(tpl, "$ntuples", Connection::Ntuples);
4545
Nan::SetPrototypeMethod(tpl, "$nfields", Connection::Nfields);
4646
Nan::SetPrototypeMethod(tpl, "$fname", Connection::Fname);
47+
Nan::SetPrototypeMethod(tpl, "$ftable", Connection::Ftable);
48+
Nan::SetPrototypeMethod(tpl, "$ftablecol", Connection::Ftablecol);
4749
Nan::SetPrototypeMethod(tpl, "$ftype", Connection::Ftype);
4850
Nan::SetPrototypeMethod(tpl, "$getvalue", Connection::Getvalue);
4951
Nan::SetPrototypeMethod(tpl, "$getisnull", Connection::Getisnull);

src/connection.cc

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -217,6 +217,28 @@ NAN_METHOD(Connection::Fname) {
217217
info.GetReturnValue().Set(Nan::New<v8::String>(colName).ToLocalChecked());
218218
}
219219

220+
NAN_METHOD(Connection::Ftable) {
221+
TRACE("Connection::Ftable");
222+
Connection *self = NODE_THIS();
223+
224+
PGresult* res = self->lastResult;
225+
226+
int table = PQftable(res, info[0]->Int32Value());
227+
228+
info.GetReturnValue().Set(table);
229+
}
230+
231+
NAN_METHOD(Connection::Ftablecol) {
232+
TRACE("Connection::Ftablecol");
233+
Connection *self = NODE_THIS();
234+
235+
PGresult* res = self->lastResult;
236+
237+
int tablecol = PQftablecol(res, info[0]->Int32Value());
238+
239+
info.GetReturnValue().Set(tablecol);
240+
}
241+
220242
NAN_METHOD(Connection::Ftype) {
221243
TRACE("Connection::Ftype");
222244
Connection *self = NODE_THIS();

src/connection.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,8 @@ class Connection : public Nan::ObjectWrap {
2121
static NAN_METHOD(Ntuples);
2222
static NAN_METHOD(Nfields);
2323
static NAN_METHOD(Fname);
24+
static NAN_METHOD(Ftable);
25+
static NAN_METHOD(Ftablecol);
2426
static NAN_METHOD(Ftype);
2527
static NAN_METHOD(Getvalue);
2628
static NAN_METHOD(Getisnull);

test/sync-integration.js

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,5 +26,12 @@ describe('low-level query integration tests', function () {
2626
assert.strictEqual(this.pq.getvalue(2, 1), '');
2727
assert.strictEqual(this.pq.getisnull(2, 1), true);
2828
});
29+
30+
it('has correct identifiers', function() {
31+
assert.notEqual(this.pq.ftable(0), 0);
32+
assert.notEqual(this.pq.ftable(1), 0);
33+
assert.strictEqual(this.pq.ftablecol(0), 1);
34+
assert.strictEqual(this.pq.ftablecol(1), 2);
35+
});
2936
});
3037
});

test/sync.js

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,14 @@ describe('result checking', function() {
5656
assert.equal(this.pq.fname(0), 'my_col');
5757
});
5858

59+
it('has table oid zero', function() {
60+
assert.strictEqual(this.pq.ftable(0), 0);
61+
});
62+
63+
it('has column number of the query-result', function() {
64+
assert.strictEqual(this.pq.ftablecol(0), 0);
65+
});
66+
5967
it('has oid type of timestamptz', function() {
6068
assert.strictEqual(this.pq.ftype(0), 1184);
6169
});

0 commit comments

Comments
 (0)