Skip to content

Commit b75530c

Browse files
committed
add int8 datatype support
1 parent 87b4a57 commit b75530c

File tree

8 files changed

+83
-35
lines changed

8 files changed

+83
-35
lines changed

lib/pg_types.js

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,16 @@ const int4recv = function (buf) {
3333
return buf.readInt32BE(0)
3434
}
3535

36+
// int8
37+
const int8send = function (buf, value) {
38+
const tbuf = Buffer.allocUnsafe(8)
39+
tbuf.writeBigInt64BE(value)
40+
buf.put(tbuf)
41+
}
42+
const int8recv = function (buf) {
43+
return buf.readBigInt64BE(0)
44+
}
45+
3646
// text
3747
const textsend = function (buf, value) {
3848
const tbuf = Buffer.from(value, 'utf-8')
@@ -197,6 +207,7 @@ const types = {
197207
bytea: { oid: 17, send: byteasend, recv: bytearecv },
198208
int2: { oid: 21, send: int2send, recv: int2recv },
199209
int4: { oid: 23, send: int4send, recv: int4recv },
210+
int8: { oid: 20, send: int8send, recv: int8recv },
200211
text: { oid: 25, send: textsend, recv: textrecv },
201212
varchar: { oid: 1043, send: varcharsend, recv: varcharrecv },
202213
json: { oid: 114, send: json_send, recv: json_recv },
@@ -208,6 +219,7 @@ const types = {
208219
_bytea: { oid: 1001, send: array_send.bind(null, 'bytea'), recv: array_recv },
209220
_int2: { oid: 1005, send: array_send.bind(null, 'int2'), recv: array_recv },
210221
_int4: { oid: 1007, send: array_send.bind(null, 'int4'), recv: array_recv },
222+
_int8: { oid: 1016, send: array_send.bind(null, 'int8'), recv: array_recv },
211223
_text: { oid: 1009, send: array_send.bind(null, 'text'), recv: array_recv },
212224
_varchar: { oid: 1015, send: array_send.bind(null, 'varchar'), recv: array_recv },
213225
_json: { oid: 199, send: array_send.bind(null, 'json'), recv: array_recv },

test/fieldReader.js

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,15 @@
11
const assert = require('assert')
2-
const pg = require('pg')
32
const { fieldReader } = require('../')
43
const { to: copyTo } = require('pg-copy-streams')
54
const through2 = require('through2')
65
const concat = require('concat-stream')
7-
8-
const getClient = function () {
9-
const client = new pg.Client()
10-
client.connect()
11-
return client
12-
}
6+
const { getClient } = require('./utils')
137

148
const samples = {
159
bool: [null, true, false],
1610
int2: [23, -59, null],
1711
int4: [2938, null, -99283],
12+
int8: [BigInt(2938), null, BigInt(-99283)],
1813
text: ['aaa', 'ééé', null],
1914
json: [JSON.stringify({}), JSON.stringify([1, 2]), null],
2015
jsonb: [JSON.stringify({}), JSON.stringify([1, 2]), null],

test/rawReader.js

Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,8 @@
11
const assert = require('assert')
2-
const pg = require('pg')
32
const { rawReader } = require('../')
43
const { to: copyTo } = require('pg-copy-streams')
54
const concat = require('concat-stream')
6-
7-
const getClient = function () {
8-
const client = new pg.Client()
9-
client.connect()
10-
return client
11-
}
5+
const { getClient } = require('./utils')
126

137
describe('integration test - rawReader', () => {
148
it('stream all raw bytes / small fields including empty and null', (done) => {

test/rowReader.js

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,16 @@
11
const assert = require('assert')
2-
const pg = require('pg')
32
const { rowReader } = require('../')
43
const { to: copyTo } = require('pg-copy-streams')
54
const through2 = require('through2')
65
const concat = require('concat-stream')
7-
8-
const getClient = function () {
9-
const client = new pg.Client()
10-
client.connect()
11-
return client
12-
}
6+
const { getClient } = require('./utils')
137

148
const samples = {
159
bool: [null, true, false],
1610
bytea: [Buffer.from([0x61]), null, Buffer.from([0x62])],
1711
int2: [23, -59, null],
1812
int4: [2938, null, -99283],
13+
int8: [BigInt(2938), null, BigInt(-99283)],
1914
text: ['aaa', 'ééé', null],
2015
json: [JSON.stringify({}), JSON.stringify([1, 2]), null],
2116
jsonb: [JSON.stringify({}), JSON.stringify({ a: true, b: [4, 2] }), null],

test/rowWriter.js

Lines changed: 18 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,8 @@
11
const assert = require('assert')
22
const util = require('util')
3-
const pg = require('pg')
43
const { rowWriter } = require('../')
54
const { from: copyFrom } = require('pg-copy-streams')
6-
7-
const getClient = function () {
8-
const client = new pg.Client()
9-
client.connect()
10-
return client
11-
}
5+
const { getClient } = require('./utils')
126

137
describe('integration test - copyIn', () => {
148
it('ingesting an empty flow should not trigger an error', (done) => {
@@ -57,6 +51,23 @@ describe('integration test - copyIn', () => {
5751
],
5852
'{{1,2},{3,4}}',
5953
],
54+
['int8', 0, null, null],
55+
['int8', 0, BigInt('501007199254740991'), '501007199254740991'],
56+
[
57+
'int8',
58+
1,
59+
[BigInt('501007199254740991'), BigInt('501007199254740999')],
60+
'{501007199254740991,501007199254740999}',
61+
],
62+
[
63+
'int8',
64+
2,
65+
[
66+
[BigInt('501007199254740991'), BigInt('501007199254740999')],
67+
[BigInt('501007199254740993'), BigInt('501007199254740994')],
68+
],
69+
'{{501007199254740991,501007199254740999},{501007199254740993,501007199254740994}}',
70+
],
6071
['float4', 0, 0.2736, '0.2736'],
6172
['float4', 0, 2.928e27, '2.928e+27'],
6273
['float8', 0, 7.23e50, '7.23e+50'],

test/samples.js

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,12 @@ BP.prototype.string = function (s, enc) {
77
return this.put(buf)
88
}
99

10+
function makeBigIntBuffer(value) {
11+
const buf = Buffer.alloc(8)
12+
buf.writeBigInt64BE(BigInt(value))
13+
return buf
14+
}
15+
1016
module.exports = [
1117
// simple types
1218
{ t: 'bool', v: null, r: new BP().word32be(-1).buffer() },
@@ -25,6 +31,12 @@ module.exports = [
2531
{ t: 'int2', v: 128, r: new BP().word32be(2).word16be(128).buffer() },
2632
{ t: 'int4', v: null, r: new BP().word32be(-1).buffer() },
2733
{ t: 'int4', v: 128, r: new BP().word32be(4).word32be(128).buffer() },
34+
{ t: 'int8', v: null, r: new BP().word32be(-1).buffer() },
35+
{
36+
t: 'int8',
37+
v: BigInt('128'),
38+
r: new BP().word32be(8).put(makeBigIntBuffer('128')).buffer(),
39+
},
2840
{ t: 'text', v: null, r: new BP().word32be(-1).buffer() },
2941
{ t: 'text', v: 'hello', r: new BP().word32be(5).put(Buffer.from('hello')).buffer() },
3042
{ t: 'text', v: 'utf8 éà', r: new BP().word32be(9).put(Buffer.from('utf8 éà', 'utf-8')).buffer() },
@@ -149,6 +161,32 @@ module.exports = [
149161
.word32be(6)
150162
.buffer(),
151163
},
164+
{ t: '_int8', v: null, r: new BP().word32be(-1).buffer() },
165+
{
166+
t: '_int8',
167+
v: [
168+
[BigInt('1'), BigInt('2')],
169+
[BigInt('3'), BigInt('4')],
170+
],
171+
r: new BP()
172+
.word32be(76)
173+
.word32be(2)
174+
.word32be(0)
175+
.word32be(types['int8'].oid)
176+
.word32be(2)
177+
.word32be(1)
178+
.word32be(2)
179+
.word32be(1)
180+
.word32be(8)
181+
.put(makeBigIntBuffer('1'))
182+
.word32be(8)
183+
.put(makeBigIntBuffer('2'))
184+
.word32be(8)
185+
.put(makeBigIntBuffer('3'))
186+
.word32be(8)
187+
.put(makeBigIntBuffer('4'))
188+
.buffer(),
189+
},
152190
{ t: '_bytea', v: null, r: new BP().word32be(-1).buffer() },
153191
{
154192
t: '_bytea',

test/transform.js

Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,12 @@
11
const assert = require('assert')
22
const async = require('async')
33

4-
const pg = require('pg')
54
const { to: pgCopyTo, from: pgCopyFrom } = require('pg-copy-streams')
65
const through2 = require('through2')
76

7+
const { getClient } = require('./utils')
88
const { transform } = require('../')
99

10-
const getClient = function (dsn) {
11-
const client = new pg.Client(dsn)
12-
client.connect()
13-
return client
14-
}
15-
1610
describe('integration test - transform', () => {
1711
it('should correclty extract, transform and load data', (done) => {
1812
const clientA = getClient()

test/utils.js

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
const pg = require('pg')
2+
3+
module.exports = {
4+
getClient: () => {
5+
const client = new pg.Client(process.env.PG_URL)
6+
client.connect()
7+
return client
8+
},
9+
}

0 commit comments

Comments
 (0)