@m2k-5f/pgtx 2.7.2 → 2.8.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +65 -37
- package/dist/batch.d.ts +4 -1
- package/dist/batch.d.ts.map +1 -1
- package/dist/batch.js +14 -7
- package/dist/clauses/fragment.clause.js +1 -1
- package/dist/connection.d.ts +17 -16
- package/dist/connection.d.ts.map +1 -1
- package/dist/connection.js +231 -175
- package/dist/error.d.ts +6 -1
- package/dist/error.d.ts.map +1 -1
- package/dist/error.js +26 -7
- package/dist/pool.d.ts +1 -1
- package/dist/pool.d.ts.map +1 -1
- package/dist/pool.js +5 -3
- package/dist/protocol/connection-request-writer.d.ts +107 -11
- package/dist/protocol/connection-request-writer.d.ts.map +1 -1
- package/dist/protocol/connection-request-writer.js +442 -48
- package/dist/protocol/connection-response-reader.d.ts +75 -30
- package/dist/protocol/connection-response-reader.d.ts.map +1 -1
- package/dist/protocol/connection-response-reader.js +285 -289
- package/dist/protocol/constants.d.ts +37 -30
- package/dist/protocol/constants.d.ts.map +1 -1
- package/dist/protocol/constants.js +43 -37
- package/dist/protocol/socket-authorization.d.ts.map +1 -1
- package/dist/protocol/socket-authorization.js +3 -3
- package/dist/protocol/socket-connector.d.ts +4 -4
- package/dist/protocol/socket-connector.d.ts.map +1 -1
- package/dist/protocol/socket-connector.js +2 -2
- package/dist/protocol/types.d.ts +14 -0
- package/dist/protocol/types.d.ts.map +1 -0
- package/dist/protocol/types.js +264 -0
- package/dist/query.d.ts +34 -24
- package/dist/query.d.ts.map +1 -1
- package/dist/query.js +45 -30
- package/dist/queue.d.ts +1 -0
- package/dist/queue.d.ts.map +1 -1
- package/dist/queue.js +1 -0
- package/dist/types.d.ts +3 -1
- package/dist/types.d.ts.map +1 -1
- package/dist/utils/template-compiler.d.ts +5 -2
- package/dist/utils/template-compiler.d.ts.map +1 -1
- package/dist/utils/template-compiler.js +4 -1
- package/dist/utils.d.ts +8 -0
- package/dist/utils.d.ts.map +1 -0
- package/dist/utils.js +73 -0
- package/package.json +2 -2
|
@@ -1,13 +1,31 @@
|
|
|
1
|
-
import {
|
|
1
|
+
import { createBindTypeError } from "../error";
|
|
2
|
+
import { DataTypeOids, RequestTypes } from "./constants";
|
|
3
|
+
import { handlers } from "./types";
|
|
4
|
+
const POSTGRES_EPOCH = Date.UTC(2000, 0, 1);
|
|
2
5
|
export class ConnectionRequestBuffer {
|
|
3
|
-
constructor(buffer, offset = 0, lastRequestLenByteOffset = 0) {
|
|
6
|
+
constructor(buffer, offset = 0, lastRequestLenByteOffset = 0, _markedCaret = 0) {
|
|
4
7
|
this.buffer = buffer;
|
|
5
8
|
this.offset = offset;
|
|
6
9
|
this.lastRequestLenByteOffset = lastRequestLenByteOffset;
|
|
10
|
+
this._markedCaret = _markedCaret;
|
|
7
11
|
}
|
|
8
12
|
static new(capacity) {
|
|
9
13
|
return new ConnectionRequestBuffer(Buffer.allocUnsafe(capacity));
|
|
10
14
|
}
|
|
15
|
+
get isEmpty() {
|
|
16
|
+
return !this.buffer.length;
|
|
17
|
+
}
|
|
18
|
+
get hasMore() {
|
|
19
|
+
return !!this.buffer.length;
|
|
20
|
+
}
|
|
21
|
+
mark() {
|
|
22
|
+
this._markedCaret = this.offset;
|
|
23
|
+
return this;
|
|
24
|
+
}
|
|
25
|
+
rollback() {
|
|
26
|
+
this.offset = this._markedCaret;
|
|
27
|
+
return this;
|
|
28
|
+
}
|
|
11
29
|
ensureCapacity(needed) {
|
|
12
30
|
const required = needed + this.offset;
|
|
13
31
|
if (required <= this.buffer.length)
|
|
@@ -33,26 +51,73 @@ export class ConnectionRequestBuffer {
|
|
|
33
51
|
this.offset += this.buffer.write(string, this.offset, 'utf-8');
|
|
34
52
|
return this;
|
|
35
53
|
}
|
|
36
|
-
writeInt32(number) {
|
|
37
|
-
this.ensureCapacity(4);
|
|
38
|
-
this.buffer.writeInt32BE(number, this.offset);
|
|
39
|
-
this.offset += 4;
|
|
40
|
-
return this;
|
|
41
|
-
}
|
|
42
54
|
writeInt16(number) {
|
|
43
55
|
this.ensureCapacity(2);
|
|
44
56
|
this.buffer.writeInt16BE(number, this.offset);
|
|
45
57
|
this.offset += 2;
|
|
46
58
|
return this;
|
|
47
59
|
}
|
|
60
|
+
writeInt32(number) {
|
|
61
|
+
this.ensureCapacity(4);
|
|
62
|
+
this.buffer.writeInt32BE(number, this.offset);
|
|
63
|
+
this.offset += 4;
|
|
64
|
+
return this;
|
|
65
|
+
}
|
|
48
66
|
writeChar(char) {
|
|
49
67
|
this.ensureCapacity(1);
|
|
50
68
|
this.buffer[this.offset] = char.charCodeAt(0);
|
|
51
69
|
this.offset++;
|
|
52
70
|
return this;
|
|
53
71
|
}
|
|
72
|
+
writeByte(byte) {
|
|
73
|
+
this.ensureCapacity(1);
|
|
74
|
+
this.buffer[this.offset++] = byte;
|
|
75
|
+
return this;
|
|
76
|
+
}
|
|
77
|
+
writeBigInt64(value) {
|
|
78
|
+
this.ensureCapacity(8);
|
|
79
|
+
this.buffer.writeBigInt64BE(value, this.offset);
|
|
80
|
+
this.offset += 8;
|
|
81
|
+
return this;
|
|
82
|
+
}
|
|
83
|
+
writeUInt16(value) {
|
|
84
|
+
this.ensureCapacity(2);
|
|
85
|
+
this.buffer.writeUInt16BE(value, this.offset);
|
|
86
|
+
this.offset += 2;
|
|
87
|
+
return this;
|
|
88
|
+
}
|
|
89
|
+
writeUInt32(value) {
|
|
90
|
+
this.ensureCapacity(4);
|
|
91
|
+
this.buffer.writeUInt32BE(value, this.offset);
|
|
92
|
+
this.offset += 4;
|
|
93
|
+
return this;
|
|
94
|
+
}
|
|
95
|
+
writeBytes(value) {
|
|
96
|
+
this.ensureCapacity(value.byteLength);
|
|
97
|
+
this.buffer.set(value, this.offset);
|
|
98
|
+
this.offset += value.byteLength;
|
|
99
|
+
return this;
|
|
100
|
+
}
|
|
101
|
+
writeInt64(number) {
|
|
102
|
+
this.ensureCapacity(8);
|
|
103
|
+
this.buffer.writeBigInt64BE(BigInt(number), this.offset);
|
|
104
|
+
this.offset += 8;
|
|
105
|
+
return this;
|
|
106
|
+
}
|
|
107
|
+
writeFloat32(number) {
|
|
108
|
+
this.ensureCapacity(4);
|
|
109
|
+
this.buffer.writeFloatBE(number, this.offset);
|
|
110
|
+
this.offset += 4;
|
|
111
|
+
return this;
|
|
112
|
+
}
|
|
113
|
+
writeFloat64(number) {
|
|
114
|
+
this.ensureCapacity(8);
|
|
115
|
+
this.buffer.writeDoubleBE(number, this.offset);
|
|
116
|
+
this.offset += 8;
|
|
117
|
+
return this;
|
|
118
|
+
}
|
|
54
119
|
startRequest(requestType) {
|
|
55
|
-
this.
|
|
120
|
+
this.writeByte(requestType);
|
|
56
121
|
this.lastRequestLenByteOffset = this.offset;
|
|
57
122
|
return this.writeInt32(0);
|
|
58
123
|
}
|
|
@@ -72,57 +137,36 @@ export class ConnectionRequestBuffer {
|
|
|
72
137
|
this.offset = 0;
|
|
73
138
|
this.lastRequestLenByteOffset = 0;
|
|
74
139
|
}
|
|
75
|
-
}
|
|
76
|
-
export class ConnectionRequestWriter {
|
|
77
|
-
constructor(buffer) {
|
|
78
|
-
this.buffer = buffer;
|
|
79
|
-
}
|
|
80
|
-
static new() {
|
|
81
|
-
return new ConnectionRequestWriter(ConnectionRequestBuffer.new(65536));
|
|
82
|
-
}
|
|
83
140
|
writeQuery(text) {
|
|
84
|
-
this.
|
|
141
|
+
this.startRequest(RequestTypes.SimpleQuery)
|
|
85
142
|
.writeCString(text)
|
|
86
143
|
.endRequest();
|
|
87
144
|
return this;
|
|
88
145
|
}
|
|
89
146
|
writeParse(name, text) {
|
|
90
|
-
this.
|
|
147
|
+
this.startRequest(RequestTypes.Parse)
|
|
91
148
|
.writeCString(name)
|
|
92
149
|
.writeCString(text)
|
|
93
150
|
.writeInt16(0)
|
|
94
151
|
.endRequest();
|
|
95
152
|
return this;
|
|
96
153
|
}
|
|
97
|
-
writeDescribe(
|
|
98
|
-
this.
|
|
99
|
-
.writeChar(
|
|
100
|
-
.writeCString(
|
|
154
|
+
writeDescribe(type, name) {
|
|
155
|
+
this.startRequest(RequestTypes.Describe)
|
|
156
|
+
.writeChar(type)
|
|
157
|
+
.writeCString(name)
|
|
101
158
|
.endRequest();
|
|
102
159
|
return this;
|
|
103
160
|
}
|
|
104
|
-
writeBind(portName, statementName, params) {
|
|
105
|
-
const request = this.buffer.startRequest(RequestTypes.Bind)
|
|
106
|
-
.writeCString(portName)
|
|
107
|
-
.writeCString(statementName)
|
|
108
|
-
.writeInt16(0)
|
|
109
|
-
.writeInt16(params.length);
|
|
110
|
-
params.forEach(param => {
|
|
111
|
-
request.writeInt32(param ? Buffer.byteLength(param) : -1);
|
|
112
|
-
param && request.writeString(param);
|
|
113
|
-
});
|
|
114
|
-
request.writeInt16(1).writeInt16(1).endRequest();
|
|
115
|
-
return this;
|
|
116
|
-
}
|
|
117
161
|
writeClose(name) {
|
|
118
|
-
this.
|
|
162
|
+
this.startRequest(RequestTypes.Close)
|
|
119
163
|
.writeChar("P")
|
|
120
164
|
.writeCString(name)
|
|
121
165
|
.endRequest();
|
|
122
166
|
return this;
|
|
123
167
|
}
|
|
124
168
|
writeStartup(user, database) {
|
|
125
|
-
this.
|
|
169
|
+
this.startMessage()
|
|
126
170
|
.writeInt32(196608)
|
|
127
171
|
.writeCString('user').writeCString(user)
|
|
128
172
|
.writeCString('database').writeCString(database)
|
|
@@ -131,30 +175,30 @@ export class ConnectionRequestWriter {
|
|
|
131
175
|
return this;
|
|
132
176
|
}
|
|
133
177
|
writeExecute(portName) {
|
|
134
|
-
this.
|
|
178
|
+
this.startRequest(RequestTypes.Execute)
|
|
135
179
|
.writeCString(portName)
|
|
136
180
|
.writeInt32(0)
|
|
137
181
|
.endRequest();
|
|
138
182
|
return this;
|
|
139
183
|
}
|
|
140
184
|
writeSync() {
|
|
141
|
-
this.
|
|
185
|
+
this.startRequest(RequestTypes.Sync).endRequest();
|
|
142
186
|
return this;
|
|
143
187
|
}
|
|
144
188
|
writeSSLRequest() {
|
|
145
|
-
this.
|
|
189
|
+
this.startMessage()
|
|
146
190
|
.writeInt32(80877103)
|
|
147
191
|
.endRequest();
|
|
148
192
|
return this;
|
|
149
193
|
}
|
|
150
194
|
writePassword(password) {
|
|
151
|
-
this.
|
|
195
|
+
this.startRequest(RequestTypes.Password)
|
|
152
196
|
.writeCString(password)
|
|
153
197
|
.endRequest();
|
|
154
198
|
return this;
|
|
155
199
|
}
|
|
156
200
|
writeSaslInitial(mechanism, clientFirstMessage) {
|
|
157
|
-
this.
|
|
201
|
+
this.startRequest(RequestTypes.Password)
|
|
158
202
|
.writeCString(mechanism)
|
|
159
203
|
.writeInt32(Buffer.byteLength(clientFirstMessage, 'utf-8'))
|
|
160
204
|
.writeString(clientFirstMessage)
|
|
@@ -162,16 +206,366 @@ export class ConnectionRequestWriter {
|
|
|
162
206
|
return this;
|
|
163
207
|
}
|
|
164
208
|
writeSaslResponse(clientFinalMessage) {
|
|
165
|
-
this.
|
|
209
|
+
this.startRequest(RequestTypes.Password)
|
|
166
210
|
.writeString(clientFinalMessage)
|
|
167
211
|
.endRequest();
|
|
168
212
|
return this;
|
|
169
213
|
}
|
|
170
|
-
|
|
171
|
-
|
|
214
|
+
// ------ Binary writers of js values into Bind parameters ------
|
|
215
|
+
writeBinaryBool(value) {
|
|
216
|
+
this.writeInt32(1);
|
|
217
|
+
this.writeByte(value ? 1 : 0);
|
|
218
|
+
return this;
|
|
172
219
|
}
|
|
173
|
-
|
|
174
|
-
this.
|
|
220
|
+
writeBinaryString(value) {
|
|
221
|
+
this.writeInt32(Buffer.byteLength(value));
|
|
222
|
+
this.writeString(value);
|
|
223
|
+
return this;
|
|
224
|
+
}
|
|
225
|
+
writeBinaryInt2(value) {
|
|
226
|
+
this.writeInt32(2);
|
|
227
|
+
this.writeInt16(value);
|
|
228
|
+
return this;
|
|
229
|
+
}
|
|
230
|
+
writeBinaryInt4(value) {
|
|
231
|
+
this.writeInt32(4);
|
|
232
|
+
this.writeInt32(value);
|
|
233
|
+
return this;
|
|
234
|
+
}
|
|
235
|
+
writeBinaryInt8(value) {
|
|
236
|
+
this.writeInt32(8);
|
|
237
|
+
this.writeInt64(value);
|
|
238
|
+
return this;
|
|
239
|
+
}
|
|
240
|
+
writeBinaryBigInt8(value) {
|
|
241
|
+
this.writeInt32(8);
|
|
242
|
+
this.writeBigInt64(value);
|
|
243
|
+
return this;
|
|
244
|
+
}
|
|
245
|
+
writeBinaryFloat4(value) {
|
|
246
|
+
this.writeInt32(4);
|
|
247
|
+
this.writeFloat32(value);
|
|
248
|
+
return this;
|
|
249
|
+
}
|
|
250
|
+
writeBinaryFloat8(value) {
|
|
251
|
+
this.writeInt32(8);
|
|
252
|
+
this.writeFloat64(value);
|
|
253
|
+
return this;
|
|
254
|
+
}
|
|
255
|
+
writeBinaryBytea(value) {
|
|
256
|
+
this.writeInt32(value.byteLength);
|
|
257
|
+
this.writeBytes(value);
|
|
258
|
+
return this;
|
|
259
|
+
}
|
|
260
|
+
writeBinaryTimestamp(value) {
|
|
261
|
+
const micros = BigInt(value.getTime() - POSTGRES_EPOCH) * 1000n;
|
|
262
|
+
this.writeInt32(8);
|
|
263
|
+
this.writeBigInt64(micros);
|
|
175
264
|
return this;
|
|
176
265
|
}
|
|
266
|
+
writeBinaryPoint(value) {
|
|
267
|
+
this.writeInt32(16);
|
|
268
|
+
this.writeFloat64(value.x);
|
|
269
|
+
this.writeFloat64(value.y);
|
|
270
|
+
return this;
|
|
271
|
+
}
|
|
272
|
+
writeBinaryNumeric(value) {
|
|
273
|
+
const negative = value.startsWith('-');
|
|
274
|
+
const unsigned = negative ? value.slice(1) : value;
|
|
275
|
+
const [intPartRaw, fracPartRaw = ''] = unsigned.split('.');
|
|
276
|
+
const intPart = intPartRaw === '' ? '0' : intPartRaw;
|
|
277
|
+
const fracPart = fracPartRaw;
|
|
278
|
+
const dscale = fracPart.length;
|
|
279
|
+
const intPad = (4 - (intPart.length % 4)) % 4;
|
|
280
|
+
const paddedInt = '0'.repeat(intPad) + intPart;
|
|
281
|
+
const fracPad = (4 - (fracPart.length % 4)) % 4;
|
|
282
|
+
const paddedFrac = fracPart + '0'.repeat(fracPad);
|
|
283
|
+
const digitsStr = paddedInt + paddedFrac;
|
|
284
|
+
const digits = [];
|
|
285
|
+
for (let i = 0; i < digitsStr.length; i += 4) {
|
|
286
|
+
digits.push(parseInt(digitsStr.slice(i, i + 4), 10));
|
|
287
|
+
}
|
|
288
|
+
let weight = paddedInt.length / 4 - 1;
|
|
289
|
+
let start = 0;
|
|
290
|
+
while (start < digits.length && digits[start] === 0) {
|
|
291
|
+
start++;
|
|
292
|
+
weight--;
|
|
293
|
+
}
|
|
294
|
+
let end = digits.length;
|
|
295
|
+
while (end > start && digits[end - 1] === 0) {
|
|
296
|
+
end--;
|
|
297
|
+
}
|
|
298
|
+
const trimmed = digits.slice(start, end);
|
|
299
|
+
const isZero = trimmed.length === 0;
|
|
300
|
+
this.writeInt32(8 + trimmed.length * 2);
|
|
301
|
+
this.writeInt16(trimmed.length);
|
|
302
|
+
this.writeInt16(isZero ? 0 : weight);
|
|
303
|
+
this.writeUInt16(negative && !isZero ? 0x4000 : 0x0000);
|
|
304
|
+
this.writeUInt16(dscale);
|
|
305
|
+
for (const digit of trimmed) {
|
|
306
|
+
this.writeInt16(digit);
|
|
307
|
+
}
|
|
308
|
+
return this;
|
|
309
|
+
}
|
|
310
|
+
writeBinaryDate(value) {
|
|
311
|
+
const days = Math.round((Date.UTC(value.getUTCFullYear(), value.getUTCMonth(), value.getUTCDate()) - POSTGRES_EPOCH)
|
|
312
|
+
/ 86400000);
|
|
313
|
+
this.writeInt32(4);
|
|
314
|
+
this.writeInt32(days);
|
|
315
|
+
return this;
|
|
316
|
+
}
|
|
317
|
+
writeBinaryTime(value) {
|
|
318
|
+
const [time, ms] = value.split('.');
|
|
319
|
+
const [hours, minutes, seconds] = time.split(':').map(Number);
|
|
320
|
+
const micros = (hours * 3600000 +
|
|
321
|
+
minutes * 60000 +
|
|
322
|
+
seconds * 1000 +
|
|
323
|
+
Number(ms)) *
|
|
324
|
+
1000;
|
|
325
|
+
this.writeInt32(8);
|
|
326
|
+
this.writeBigInt64(BigInt(micros));
|
|
327
|
+
return this;
|
|
328
|
+
}
|
|
329
|
+
writeBinaryTimetz(value) {
|
|
330
|
+
const time = value.slice(0, 12);
|
|
331
|
+
const offset = value.slice(12);
|
|
332
|
+
const [hours, minutes, seconds] = time
|
|
333
|
+
.slice(0, 8)
|
|
334
|
+
.split(':')
|
|
335
|
+
.map(Number);
|
|
336
|
+
const ms = Number(time.slice(9, 12));
|
|
337
|
+
const sign = offset[0];
|
|
338
|
+
const offHours = Number(offset.slice(1, 3));
|
|
339
|
+
const offMinutes = Number(offset.slice(4, 6));
|
|
340
|
+
const micros = (hours * 3600000 +
|
|
341
|
+
minutes * 60000 +
|
|
342
|
+
seconds * 1000 +
|
|
343
|
+
ms) *
|
|
344
|
+
1000;
|
|
345
|
+
const offsetSeconds = (offHours * 60 + offMinutes) * 60 * (sign === '+' ? -1 : 1);
|
|
346
|
+
this.writeInt32(12);
|
|
347
|
+
this.writeBigInt64(BigInt(micros));
|
|
348
|
+
this.writeInt32(offsetSeconds);
|
|
349
|
+
return this;
|
|
350
|
+
}
|
|
351
|
+
writeBinaryInterval(value) {
|
|
352
|
+
this.writeInt32(16);
|
|
353
|
+
this.writeBigInt64(typeof value.microseconds === 'bigint'
|
|
354
|
+
? value.microseconds
|
|
355
|
+
: BigInt(value.microseconds));
|
|
356
|
+
this.writeInt32(value.days);
|
|
357
|
+
this.writeInt32(value.months);
|
|
358
|
+
return this;
|
|
359
|
+
}
|
|
360
|
+
writeBinaryJson(value) {
|
|
361
|
+
const json = typeof value === 'string' ? value : JSON.stringify(value);
|
|
362
|
+
this.writeInt32(Buffer.byteLength(json));
|
|
363
|
+
this.writeString(json);
|
|
364
|
+
return this;
|
|
365
|
+
}
|
|
366
|
+
writeBinaryJsonb(value) {
|
|
367
|
+
const json = typeof value === 'string' ? value : JSON.stringify(value);
|
|
368
|
+
const byteLen = Buffer.byteLength(json);
|
|
369
|
+
this.writeInt32(byteLen + 1);
|
|
370
|
+
this.writeByte(1);
|
|
371
|
+
this.writeString(json);
|
|
372
|
+
return this;
|
|
373
|
+
}
|
|
374
|
+
writeBinaryUuid(value) {
|
|
375
|
+
const hex = value.replace(/-/g, '');
|
|
376
|
+
this.writeInt32(16);
|
|
377
|
+
this.writeBytes(Buffer.from(hex, 'hex'));
|
|
378
|
+
return this;
|
|
379
|
+
}
|
|
380
|
+
ipv4ToBytes(addr) {
|
|
381
|
+
return Uint8Array.from(addr.split('.').map(Number));
|
|
382
|
+
}
|
|
383
|
+
ipv6ToBytes(addr) {
|
|
384
|
+
const [head, tail] = addr.split('::');
|
|
385
|
+
const headParts = head ? head.split(':') : [];
|
|
386
|
+
const tailParts = tail !== undefined ? (tail ? tail.split(':') : []) : [];
|
|
387
|
+
const missing = 8 - headParts.length - tailParts.length;
|
|
388
|
+
const groups = [...headParts, ...Array(Math.max(missing, 0)).fill('0'), ...tailParts];
|
|
389
|
+
const bytes = new Uint8Array(16);
|
|
390
|
+
groups.forEach((g, i) => {
|
|
391
|
+
const val = parseInt(g || '0', 16);
|
|
392
|
+
bytes[i * 2] = (val >> 8) & 0xff;
|
|
393
|
+
bytes[i * 2 + 1] = val & 0xff;
|
|
394
|
+
});
|
|
395
|
+
return bytes;
|
|
396
|
+
}
|
|
397
|
+
writeBinaryInet(value, isCidr = false) {
|
|
398
|
+
const [addr, prefixStr] = value.split('/');
|
|
399
|
+
const isIPv6 = addr.includes(':');
|
|
400
|
+
const family = isIPv6 ? ConnectionRequestBuffer.PGSQL_AF_INET6 : ConnectionRequestBuffer.PGSQL_AF_INET;
|
|
401
|
+
const addrBytes = isIPv6 ? this.ipv6ToBytes(addr) : this.ipv4ToBytes(addr);
|
|
402
|
+
const bits = prefixStr !== undefined ? parseInt(prefixStr, 10) : (isIPv6 ? 128 : 32);
|
|
403
|
+
this.writeInt32(4 + addrBytes.length);
|
|
404
|
+
this.writeByte(family);
|
|
405
|
+
this.writeByte(bits);
|
|
406
|
+
this.writeByte(isCidr ? 1 : 0);
|
|
407
|
+
this.writeByte(addrBytes.length);
|
|
408
|
+
this.writeBytes(addrBytes);
|
|
409
|
+
return this;
|
|
410
|
+
}
|
|
411
|
+
writeBinaryCidr(value) {
|
|
412
|
+
return this.writeBinaryInet(value, true);
|
|
413
|
+
}
|
|
414
|
+
writeBinaryMacaddr(value) {
|
|
415
|
+
const bytes = Uint8Array.from(value.split(':').map(h => parseInt(h, 16)));
|
|
416
|
+
this.writeInt32(6);
|
|
417
|
+
this.writeBytes(bytes);
|
|
418
|
+
return this;
|
|
419
|
+
}
|
|
420
|
+
writeBinaryOid(value) {
|
|
421
|
+
this.writeInt32(4);
|
|
422
|
+
this.writeUInt32(value);
|
|
423
|
+
return this;
|
|
424
|
+
}
|
|
425
|
+
writeBinaryXid(value) {
|
|
426
|
+
return this.writeBinaryOid(value);
|
|
427
|
+
}
|
|
428
|
+
writeBinaryCid(value) {
|
|
429
|
+
return this.writeBinaryOid(value);
|
|
430
|
+
}
|
|
431
|
+
writeBinaryRegproc(value) {
|
|
432
|
+
return this.writeBinaryOid(value);
|
|
433
|
+
}
|
|
434
|
+
writeBinaryLseg(value) {
|
|
435
|
+
this.writeInt32(32);
|
|
436
|
+
this.writeFloat64(value.a.x);
|
|
437
|
+
this.writeFloat64(value.a.y);
|
|
438
|
+
this.writeFloat64(value.b.x);
|
|
439
|
+
this.writeFloat64(value.b.y);
|
|
440
|
+
return this;
|
|
441
|
+
}
|
|
442
|
+
writeBinaryPath(value) {
|
|
443
|
+
this.writeInt32(1 + 4 + value.points.length * 16);
|
|
444
|
+
this.writeByte(value.closed ? 1 : 0);
|
|
445
|
+
this.writeInt32(value.points.length);
|
|
446
|
+
for (const p of value.points) {
|
|
447
|
+
this.writeFloat64(p.x);
|
|
448
|
+
this.writeFloat64(p.y);
|
|
449
|
+
}
|
|
450
|
+
return this;
|
|
451
|
+
}
|
|
452
|
+
writeBinaryBox(value) {
|
|
453
|
+
this.writeInt32(32);
|
|
454
|
+
this.writeFloat64(value.high.x);
|
|
455
|
+
this.writeFloat64(value.high.y);
|
|
456
|
+
this.writeFloat64(value.low.x);
|
|
457
|
+
this.writeFloat64(value.low.y);
|
|
458
|
+
return this;
|
|
459
|
+
}
|
|
460
|
+
writeBinaryPolygon(value) {
|
|
461
|
+
this.writeInt32(4 + value.points.length * 16);
|
|
462
|
+
this.writeInt32(value.points.length);
|
|
463
|
+
for (const p of value.points) {
|
|
464
|
+
this.writeFloat64(p.x);
|
|
465
|
+
this.writeFloat64(p.y);
|
|
466
|
+
}
|
|
467
|
+
return this;
|
|
468
|
+
}
|
|
469
|
+
writeBinaryLine(value) {
|
|
470
|
+
this.writeInt32(24);
|
|
471
|
+
this.writeFloat64(value.a);
|
|
472
|
+
this.writeFloat64(value.b);
|
|
473
|
+
this.writeFloat64(value.c);
|
|
474
|
+
return this;
|
|
475
|
+
}
|
|
476
|
+
writeLengthPrefixed(writeContent) {
|
|
477
|
+
this.ensureCapacity(4);
|
|
478
|
+
const lengthOffset = this.offset;
|
|
479
|
+
this.writeInt32(0);
|
|
480
|
+
const contentStart = this.offset;
|
|
481
|
+
writeContent();
|
|
482
|
+
const length = this.offset - contentStart;
|
|
483
|
+
this.buffer.writeInt32BE(length, lengthOffset);
|
|
484
|
+
return this;
|
|
485
|
+
}
|
|
486
|
+
writeBinaryArray(arr, elementOid, elementWriter) {
|
|
487
|
+
const hasNull = arr.some(v => v == null);
|
|
488
|
+
this.writeLengthPrefixed(() => {
|
|
489
|
+
this.writeInt32(1);
|
|
490
|
+
this.writeInt32(hasNull ? 1 : 0);
|
|
491
|
+
this.writeInt32(elementOid);
|
|
492
|
+
this.writeInt32(arr.length);
|
|
493
|
+
this.writeInt32(1);
|
|
494
|
+
for (const val of arr) {
|
|
495
|
+
if (val == null) {
|
|
496
|
+
this.writeNull();
|
|
497
|
+
}
|
|
498
|
+
else {
|
|
499
|
+
elementWriter(val);
|
|
500
|
+
}
|
|
501
|
+
}
|
|
502
|
+
});
|
|
503
|
+
return this;
|
|
504
|
+
}
|
|
505
|
+
writeBinaryBoolArray(value) {
|
|
506
|
+
return this.writeBinaryArray(value, DataTypeOids.Bool, v => this.writeBinaryBool(v));
|
|
507
|
+
}
|
|
508
|
+
writeBinaryInt2Array(value) {
|
|
509
|
+
return this.writeBinaryArray(value, DataTypeOids.Int2, v => this.writeBinaryInt2(v));
|
|
510
|
+
}
|
|
511
|
+
writeBinaryInt4Array(value) {
|
|
512
|
+
return this.writeBinaryArray(value, DataTypeOids.Int4, v => this.writeBinaryInt4(v));
|
|
513
|
+
}
|
|
514
|
+
writeBinaryInt8Array(value) {
|
|
515
|
+
return this.writeBinaryArray(value, DataTypeOids.Int8, v => typeof v === 'bigint' ? this.writeBinaryBigInt8(v) : this.writeBinaryInt8(v));
|
|
516
|
+
}
|
|
517
|
+
writeBinaryTextArray(value) {
|
|
518
|
+
return this.writeBinaryArray(value, DataTypeOids.Text, v => this.writeBinaryString(v));
|
|
519
|
+
}
|
|
520
|
+
writeBinaryVarcharArray(value) {
|
|
521
|
+
return this.writeBinaryArray(value, DataTypeOids.Varchar, v => this.writeBinaryString(v));
|
|
522
|
+
}
|
|
523
|
+
writeBinaryJsonArray(value) {
|
|
524
|
+
return this.writeBinaryArray(value, DataTypeOids.Json, v => this.writeBinaryJson(v));
|
|
525
|
+
}
|
|
526
|
+
writeBinaryJsonbArray(value) {
|
|
527
|
+
return this.writeBinaryArray(value, DataTypeOids.Jsonb, v => this.writeBinaryJsonb(v));
|
|
528
|
+
}
|
|
529
|
+
writeBinaryUuidArray(value) {
|
|
530
|
+
return this.writeBinaryArray(value, DataTypeOids.Uuid, v => this.writeBinaryUuid(v));
|
|
531
|
+
}
|
|
532
|
+
writeBinaryNumericArray(value) {
|
|
533
|
+
return this.writeBinaryArray(value, DataTypeOids.Numeric, v => this.writeBinaryNumeric(v));
|
|
534
|
+
}
|
|
535
|
+
writeNull() {
|
|
536
|
+
this.writeInt32(-1);
|
|
537
|
+
return this;
|
|
538
|
+
}
|
|
539
|
+
writeBind(portName, meta, params) {
|
|
540
|
+
this.mark();
|
|
541
|
+
this.startRequest(RequestTypes.Bind)
|
|
542
|
+
.writeCString(portName)
|
|
543
|
+
.writeCString(meta.statement)
|
|
544
|
+
.writeInt16(params.length);
|
|
545
|
+
for (let i = 0; i < params.length; i++) {
|
|
546
|
+
this.writeInt16(1);
|
|
547
|
+
}
|
|
548
|
+
this.writeInt16(params.length);
|
|
549
|
+
for (let i = 0; i < params.length; i++) {
|
|
550
|
+
const value = params[i];
|
|
551
|
+
const oid = meta.parameters[i];
|
|
552
|
+
if (value == null) {
|
|
553
|
+
this.writeNull();
|
|
554
|
+
continue;
|
|
555
|
+
}
|
|
556
|
+
const handler = handlers[oid];
|
|
557
|
+
if (!handler || !handler.validate(value)) {
|
|
558
|
+
this.rollback();
|
|
559
|
+
return createBindTypeError(i, oid, value);
|
|
560
|
+
}
|
|
561
|
+
handler.write(this, value);
|
|
562
|
+
}
|
|
563
|
+
this
|
|
564
|
+
.writeInt16(1)
|
|
565
|
+
.writeInt16(1)
|
|
566
|
+
.endRequest();
|
|
567
|
+
return null;
|
|
568
|
+
}
|
|
177
569
|
}
|
|
570
|
+
ConnectionRequestBuffer.PGSQL_AF_INET = 2;
|
|
571
|
+
ConnectionRequestBuffer.PGSQL_AF_INET6 = 3;
|