@m2k-5f/pgtx 2.7.3 → 2.8.1

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.
@@ -1,11 +1,17 @@
1
- import { ParameterDescription } from "../types";
2
- import { DescribeType, RequestType } from "./constants";
1
+ import { PostgresError } from "../error";
2
+ import { StatementMeta } from "../types";
3
+ import { DataTypeOid, DescribeType, RequestType } from "./constants";
3
4
  export declare class ConnectionRequestBuffer {
4
5
  private buffer;
5
6
  private offset;
6
7
  private lastRequestLenByteOffset;
8
+ private _markedCaret;
7
9
  private constructor();
8
10
  static new(capacity: number): ConnectionRequestBuffer;
11
+ get isEmpty(): boolean;
12
+ get hasMore(): boolean;
13
+ mark(): this;
14
+ rollback(): this;
9
15
  private ensureCapacity;
10
16
  writeCString(string: string): this;
11
17
  writeString(string: string): this;
@@ -37,6 +43,7 @@ export declare class ConnectionRequestBuffer {
37
43
  writeSaslInitial(mechanism: string, clientFirstMessage: string): this;
38
44
  writeSaslResponse(clientFinalMessage: string): this;
39
45
  writeBinaryBool(value: boolean): this;
46
+ writeBinaryString(value: string): this;
40
47
  writeBinaryInt2(value: number): this;
41
48
  writeBinaryInt4(value: number): this;
42
49
  writeBinaryInt8(value: number): this;
@@ -45,8 +52,84 @@ export declare class ConnectionRequestBuffer {
45
52
  writeBinaryFloat8(value: number): this;
46
53
  writeBinaryBytea(value: Uint8Array): this;
47
54
  writeBinaryTimestamp(value: Date): this;
55
+ writeBinaryPoint(value: {
56
+ x: number;
57
+ y: number;
58
+ }): this;
59
+ writeBinaryNumeric(value: string): this;
60
+ writeBinaryDate(value: Date): this;
61
+ writeBinaryTime(value: string): this;
62
+ writeBinaryTimetz(value: string): this;
63
+ writeBinaryInterval(value: {
64
+ months: number;
65
+ days: number;
66
+ microseconds: number | bigint;
67
+ }): this;
68
+ writeBinaryJson(value: unknown): this;
69
+ writeBinaryJsonb(value: unknown): this;
70
+ writeBinaryUuid(value: string): this;
71
+ private static readonly PGSQL_AF_INET;
72
+ private static readonly PGSQL_AF_INET6;
73
+ private ipv4ToBytes;
74
+ private ipv6ToBytes;
75
+ writeBinaryInet(value: string, isCidr?: boolean): this;
76
+ writeBinaryCidr(value: string): this;
77
+ writeBinaryMacaddr(value: string): this;
78
+ writeBinaryOid(value: number): this;
79
+ writeBinaryXid(value: number): this;
80
+ writeBinaryCid(value: number): this;
81
+ writeBinaryRegproc(value: number): this;
82
+ writeBinaryLseg(value: {
83
+ a: {
84
+ x: number;
85
+ y: number;
86
+ };
87
+ b: {
88
+ x: number;
89
+ y: number;
90
+ };
91
+ }): this;
92
+ writeBinaryPath(value: {
93
+ closed: boolean;
94
+ points: {
95
+ x: number;
96
+ y: number;
97
+ }[];
98
+ }): this;
99
+ writeBinaryBox(value: {
100
+ high: {
101
+ x: number;
102
+ y: number;
103
+ };
104
+ low: {
105
+ x: number;
106
+ y: number;
107
+ };
108
+ }): this;
109
+ writeBinaryPolygon(value: {
110
+ points: {
111
+ x: number;
112
+ y: number;
113
+ }[];
114
+ }): this;
115
+ writeBinaryLine(value: {
116
+ a: number;
117
+ b: number;
118
+ c: number;
119
+ }): this;
120
+ private writeLengthPrefixed;
121
+ writeBinaryArray(arr: unknown[], elementOid: DataTypeOid, elementWriter: (val: any) => void): this;
122
+ writeBinaryBoolArray(value: (boolean | null | undefined)[]): this;
123
+ writeBinaryInt2Array(value: (number | null | undefined)[]): this;
124
+ writeBinaryInt4Array(value: (number | null | undefined)[]): this;
125
+ writeBinaryInt8Array(value: (number | bigint | null | undefined)[]): this;
126
+ writeBinaryTextArray(value: (string | null | undefined)[]): this;
127
+ writeBinaryVarcharArray(value: (string | null | undefined)[]): this;
128
+ writeBinaryJsonArray(value: (unknown | null | undefined)[]): this;
129
+ writeBinaryJsonbArray(value: (unknown | null | undefined)[]): this;
130
+ writeBinaryUuidArray(value: (string | null | undefined)[]): this;
131
+ writeBinaryNumericArray(value: (number | string | null | undefined)[]): this;
48
132
  writeNull(): this;
49
- private canWriteBinary;
50
- writeBind(portName: string | "", statementName: string | "", params: unknown[], parameterTypes?: ParameterDescription): this;
133
+ writeBind(portName: string | "", meta: StatementMeta, params: unknown[]): PostgresError | null;
51
134
  }
52
135
  //# sourceMappingURL=connection-request-writer.d.ts.map
@@ -1 +1 @@
1
- {"version":3,"file":"connection-request-writer.d.ts","sourceRoot":"","sources":["../../src/protocol/connection-request-writer.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,oBAAoB,EAAE,MAAM,UAAU,CAAA;AAE/C,OAAO,EAA6B,YAAY,EAAE,WAAW,EAAgB,MAAM,aAAa,CAAA;AAMhG,qBAAa,uBAAuB;IAE5B,OAAO,CAAC,MAAM;IACd,OAAO,CAAC,MAAM;IACd,OAAO,CAAC,wBAAwB;IAHpC,OAAO;IAOP,MAAM,CAAC,GAAG,CAAC,QAAQ,EAAE,MAAM;IAK3B,OAAO,CAAC,cAAc;IAoBtB,YAAY,CAAC,MAAM,EAAE,MAAM;IAY3B,WAAW,CAAC,MAAM,EAAE,MAAM;IAS1B,UAAU,CAAC,MAAM,EAAE,MAAM;IAUzB,UAAU,CAAC,MAAM,EAAE,MAAM;IAUzB,SAAS,CAAC,IAAI,EAAE,MAAM;IAStB,SAAS,CAAC,IAAI,EAAE,MAAM;IAQtB,aAAa,CAAC,KAAK,EAAE,MAAM;IAS3B,WAAW,CAAC,KAAK,EAAE,MAAM;IASzB,WAAW,CAAC,KAAK,EAAE,MAAM;IASzB,UAAU,CAAC,KAAK,EAAE,UAAU;IAa5B,UAAU,CAAC,MAAM,EAAE,MAAM;IAQzB,YAAY,CAAC,MAAM,EAAE,MAAM;IAQ3B,YAAY,CAAC,MAAM,EAAE,MAAM;IAQ3B,YAAY,CAAC,WAAW,EAAE,WAAW;IAQrC,YAAY;IAMZ,UAAU;IAYV,QAAQ;IAKR,KAAK;IAKL,UAAU,CAAC,IAAI,EAAE,MAAM;IASvB,UAAU,CAAC,IAAI,EAAE,MAAM,GAAG,EAAE,EAAE,IAAI,E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AK,IAAI;IAwBrC,oBAAoB,CAAC,KAAK,EAAE,CAAC,OAAO,GAAG,IAAI,GAAG,SAAS,CAAC,EAAE;IAK1D,oBAAoB,CAAC,KAAK,EAAE,CAAC,MAAM,GAAG,IAAI,GAAG,SAAS,CAAC,EAAE;IAKzD,oBAAoB,CAAC,KAAK,EAAE,CAAC,MAAM,GAAG,IAAI,GAAG,SAAS,CAAC,EAAE;IAKzD,oBAAoB,CAAC,KAAK,EAAE,CAAC,MAAM,GAAG,MAAM,GAAG,IAAI,GAAG,SAAS,CAAC,EAAE;IAOlE,oBAAoB,CAAC,KAAK,EAAE,CAAC,MAAM,GAAG,IAAI,GAAG,SAAS,CAAC,EAAE;IAKzD,uBAAuB,CAAC,KAAK,EAAE,CAAC,MAAM,GAAG,IAAI,GAAG,SAAS,CAAC,EAAE;IAK5D,oBAAoB,CAAC,KAAK,EAAE,CAAC,OAAO,GAAG,IAAI,GAAG,SAAS,CAAC,EAAE;IAK1D,qBAAqB,CAAC,KAAK,EAAE,CAAC,OAAO,GAAG,IAAI,GAAG,SAAS,CAAC,EAAE;IAK3D,oBAAoB,CAAC,KAAK,EAAE,CAAC,MAAM,GAAG,IAAI,GAAG,SAAS,CAAC,EAAE;IAKzD,uBAAuB,CAAC,KAAK,EAAE,CAAC,MAAM,GAAG,MAAM,GAAG,IAAI,GAAG,SAAS,CAAC,EAAE;IAKrE,SAAS;IAMT,SAAS,CACL,QAAQ,EAAE,MAAM,GAAG,EAAE,EACrB,IAAI,EAAE,aAAa,EACnB,MAAM,EAAE,OAAO,EAAE,GAClB,aAAa,GAAG,IAAI;CAwC1B"}
@@ -1,15 +1,31 @@
1
- import { prepareValue } from "../utils";
1
+ import { createBindTypeError } from "../error";
2
2
  import { DataTypeOids, RequestTypes } from "./constants";
3
+ import { handlers } from "./types";
3
4
  const POSTGRES_EPOCH = Date.UTC(2000, 0, 1);
4
5
  export class ConnectionRequestBuffer {
5
- constructor(buffer, offset = 0, lastRequestLenByteOffset = 0) {
6
+ constructor(buffer, offset = 0, lastRequestLenByteOffset = 0, _markedCaret = 0) {
6
7
  this.buffer = buffer;
7
8
  this.offset = offset;
8
9
  this.lastRequestLenByteOffset = lastRequestLenByteOffset;
10
+ this._markedCaret = _markedCaret;
9
11
  }
10
12
  static new(capacity) {
11
13
  return new ConnectionRequestBuffer(Buffer.allocUnsafe(capacity));
12
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
+ }
13
29
  ensureCapacity(needed) {
14
30
  const required = needed + this.offset;
15
31
  if (required <= this.buffer.length)
@@ -195,11 +211,17 @@ export class ConnectionRequestBuffer {
195
211
  .endRequest();
196
212
  return this;
197
213
  }
214
+ // ------ Binary writers of js values into Bind parameters ------
198
215
  writeBinaryBool(value) {
199
216
  this.writeInt32(1);
200
217
  this.writeByte(value ? 1 : 0);
201
218
  return this;
202
219
  }
220
+ writeBinaryString(value) {
221
+ this.writeInt32(Buffer.byteLength(value));
222
+ this.writeString(value);
223
+ return this;
224
+ }
203
225
  writeBinaryInt2(value) {
204
226
  this.writeInt32(2);
205
227
  this.writeInt16(value);
@@ -241,107 +263,309 @@ export class ConnectionRequestBuffer {
241
263
  this.writeBigInt64(micros);
242
264
  return this;
243
265
  }
244
- writeNull() {
245
- this.writeInt32(-1);
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);
246
423
  return this;
247
424
  }
248
- canWriteBinary(value, oid) {
249
- if (value == null || !oid) {
250
- return false;
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);
251
449
  }
252
- switch (oid) {
253
- case DataTypeOids.Bool:
254
- return typeof value === "boolean";
255
- case DataTypeOids.Int2:
256
- return (typeof value === "number" &&
257
- Number.isInteger(value) &&
258
- value >= -32768 &&
259
- value <= 32767);
260
- case DataTypeOids.Int4:
261
- return (typeof value === "number" &&
262
- Number.isInteger(value) &&
263
- value >= -2147483648 &&
264
- value <= 2147483647);
265
- case DataTypeOids.Int8:
266
- return (typeof value === "bigint" ||
267
- (typeof value === "number" &&
268
- Number.isSafeInteger(value)));
269
- case DataTypeOids.Float4:
270
- case DataTypeOids.Float8:
271
- return (typeof value === "number" &&
272
- Number.isFinite(value));
273
- case DataTypeOids.Bytea:
274
- return (Buffer.isBuffer(value) ||
275
- value instanceof Uint8Array);
276
- case DataTypeOids.Timestamp:
277
- case DataTypeOids.Timestamptz:
278
- return value instanceof Date;
279
- default:
280
- return false;
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);
281
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));
282
522
  }
283
- writeBind(portName, statementName, params, parameterTypes) {
284
- const binary = params.map((value, i) => this.canWriteBinary(value, parameterTypes?.[i]));
285
- const request = this.startRequest(RequestTypes.Bind)
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)
286
542
  .writeCString(portName)
287
- .writeCString(statementName)
543
+ .writeCString(meta.statement)
288
544
  .writeInt16(params.length);
289
- for (const isBinary of binary) {
290
- request.writeInt16(isBinary ? 1 : 0);
545
+ for (let i = 0; i < params.length; i++) {
546
+ this.writeInt16(1);
291
547
  }
292
- request.writeInt16(params.length);
548
+ this.writeInt16(params.length);
293
549
  for (let i = 0; i < params.length; i++) {
294
550
  const value = params[i];
551
+ const oid = meta.parameters[i];
295
552
  if (value == null) {
296
- request.writeNull();
553
+ this.writeNull();
297
554
  continue;
298
555
  }
299
- if (binary[i]) {
300
- switch (parameterTypes[i]) {
301
- case DataTypeOids.Bool:
302
- request.writeBinaryBool(value);
303
- break;
304
- case DataTypeOids.Int2:
305
- request.writeBinaryInt2(value);
306
- break;
307
- case DataTypeOids.Int4:
308
- request.writeBinaryInt4(value);
309
- break;
310
- case DataTypeOids.Int8:
311
- typeof value === "bigint"
312
- ? request.writeBinaryBigInt8(value)
313
- : request.writeBinaryInt8(value);
314
- break;
315
- case DataTypeOids.Float4:
316
- request.writeBinaryFloat4(value);
317
- break;
318
- case DataTypeOids.Float8:
319
- request.writeBinaryFloat8(value);
320
- break;
321
- case DataTypeOids.Bytea:
322
- request.writeBinaryBytea(value);
323
- break;
324
- case DataTypeOids.Timestamp:
325
- case DataTypeOids.Timestamptz:
326
- request.writeBinaryTimestamp(value);
327
- break;
328
- }
329
- continue;
330
- }
331
- const prepared = prepareValue(value);
332
- if (prepared === null) {
333
- request.writeNull();
334
- }
335
- else {
336
- request
337
- .writeInt32(Buffer.byteLength(prepared))
338
- .writeString(prepared);
556
+ const handler = handlers[oid];
557
+ if (!handler || !handler.validate(value)) {
558
+ this.rollback();
559
+ return createBindTypeError(i, oid, value);
339
560
  }
561
+ handler.write(this, value);
340
562
  }
341
- request
563
+ this
342
564
  .writeInt16(1)
343
565
  .writeInt16(1)
344
566
  .endRequest();
345
- return this;
567
+ return null;
346
568
  }
347
569
  }
570
+ ConnectionRequestBuffer.PGSQL_AF_INET = 2;
571
+ ConnectionRequestBuffer.PGSQL_AF_INET6 = 3;
@@ -6,29 +6,24 @@ export declare class ConnectionResponseBuffer {
6
6
  private caret;
7
7
  private constructor();
8
8
  static from(buffer: Buffer): ConnectionResponseBuffer;
9
+ hasMore(): boolean;
10
+ skipBytes(byteCount: number): void;
11
+ hasFullPacket(): boolean;
12
+ getResidualBuffer(): Buffer<ArrayBufferLike>;
9
13
  readChar(): string;
10
14
  readInt32(): number;
11
15
  readInt16(): number;
12
16
  readCString(): string;
13
17
  readRawString(length: number): string;
14
- hasMore(): boolean;
15
- readBinaryDate(): Date;
18
+ readUInt16(): number;
19
+ readUInt32(): number;
20
+ readFloat32(): number;
21
+ readFloat64(): number;
16
22
  readByte(): number;
17
- readBinaryTimestamp(): Date;
18
- readBinaryTime(): string;
19
- skipBytes(byteCount: number): void;
20
- hasFullPacket(): boolean;
21
- getResidualBuffer(): Buffer<ArrayBufferLike>;
23
+ readBytes(length: number): Buffer<ArrayBuffer>;
22
24
  readBool(): boolean;
23
25
  readBigInt64(): bigint;
24
26
  readInt64(): number;
25
- readNumeric(): string;
26
- readBinaryTimetz(): string;
27
- readFloat32(): number;
28
- readFloat64(): number;
29
- readUuid(): string;
30
- readBytes(length: number): Buffer<ArrayBuffer>;
31
- getBufferHash(length: number): number;
32
27
  readType(): {
33
28
  type: ResponseType;
34
29
  length: number;
@@ -48,12 +43,74 @@ export declare class ConnectionResponseBuffer {
48
43
  readSaslMechanisms(): string[];
49
44
  readSaslMessage(length: number): string;
50
45
  readRowDescription(): ColumnDescription[];
51
- readDataRow(descriptions: ColumnDescription[], int8toBigint: boolean): Record<string, any>;
52
46
  readNotificationResponse(): {
53
47
  name: ChannelName;
54
48
  payload: string;
55
49
  };
56
50
  readCommandComplete(): string;
57
51
  readParameterDescription(): ParameterDescription;
52
+ readBinaryDate(): Date;
53
+ readBinaryTimestamp(): Date;
54
+ readBinaryTime(): string;
55
+ readBinaryTimetz(): string;
56
+ readBinaryNumeric(): string;
57
+ readBinaryInterval(): {
58
+ months: number;
59
+ days: number;
60
+ microseconds: bigint;
61
+ };
62
+ readBinaryJson(fieldLength: number): unknown;
63
+ readBinaryJsonb(fieldLength: number): unknown;
64
+ readBinaryMacaddr(): string;
65
+ readBinaryOid(): number;
66
+ readBinaryXid(): number;
67
+ readBinaryCid(): number;
68
+ readBinaryRegproc(): number;
69
+ readBinaryLseg(): {
70
+ a: {
71
+ x: number;
72
+ y: number;
73
+ };
74
+ b: {
75
+ x: number;
76
+ y: number;
77
+ };
78
+ };
79
+ readBinaryPath(): {
80
+ closed: boolean;
81
+ points: {
82
+ x: number;
83
+ y: number;
84
+ }[];
85
+ };
86
+ readBinaryBox(): {
87
+ high: {
88
+ x: number;
89
+ y: number;
90
+ };
91
+ low: {
92
+ x: number;
93
+ y: number;
94
+ };
95
+ };
96
+ readBinaryPolygon(): {
97
+ points: {
98
+ x: number;
99
+ y: number;
100
+ }[];
101
+ };
102
+ readBinaryLine(): {
103
+ a: number;
104
+ b: number;
105
+ c: number;
106
+ };
107
+ readBinaryUuid(): string;
108
+ readBinaryArray(fieldLength: number, elementParser: (length: number) => any): any[];
109
+ readBinaryInet(fieldLength: number): string;
110
+ readBinaryPoint(): {
111
+ x: number;
112
+ y: number;
113
+ };
114
+ readDataRow(descriptions: ColumnDescription[], int8toBigint: boolean): Record<string, any>;
58
115
  }
59
116
  //# sourceMappingURL=connection-response-reader.d.ts.map