@noldova/context.data.sql.postgresql 0.0.0-stage → 0.0.6
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/LICENSE +21 -0
- package/README.md +89 -2
- package/THIRD-PARTY-NOTICES.txt +33 -0
- package/api/index.d.ts +94 -0
- package/api/index.js +12 -0
- package/api/index.js.map +1 -0
- package/enums/backend-message.js +35 -0
- package/enums/backend-message.js.map +1 -0
- package/enums/channel-binding.js +14 -0
- package/enums/channel-binding.js.map +1 -0
- package/enums/cursor-action.js +13 -0
- package/enums/cursor-action.js.map +1 -0
- package/enums/exchange-kind.js +17 -0
- package/enums/exchange-kind.js.map +1 -0
- package/enums/frontend-message.js +24 -0
- package/enums/frontend-message.js.map +1 -0
- package/enums/lease-kind.js +13 -0
- package/enums/lease-kind.js.map +1 -0
- package/enums/message-target.js +13 -0
- package/enums/message-target.js.map +1 -0
- package/enums/prepared-state.js +14 -0
- package/enums/prepared-state.js.map +1 -0
- package/enums/row-mode.js +14 -0
- package/enums/row-mode.js.map +1 -0
- package/enums/ssl-mode.js +16 -0
- package/enums/ssl-mode.js.map +1 -0
- package/enums/token-kind.js +15 -0
- package/enums/token-kind.js.map +1 -0
- package/enums/transaction-status.js +14 -0
- package/enums/transaction-status.js.map +1 -0
- package/enums/write-outcome.js +14 -0
- package/enums/write-outcome.js.map +1 -0
- package/exceptions/batch-skipped.exception.js +19 -0
- package/exceptions/batch-skipped.exception.js.map +1 -0
- package/exceptions/batch.exception.js +19 -0
- package/exceptions/batch.exception.js.map +1 -0
- package/exceptions/cancel-unanswered.exception.js +20 -0
- package/exceptions/cancel-unanswered.exception.js.map +1 -0
- package/exceptions/connection-closed.exception.js +23 -0
- package/exceptions/connection-closed.exception.js.map +1 -0
- package/exceptions/connection-ended.exception.js +20 -0
- package/exceptions/connection-ended.exception.js.map +1 -0
- package/exceptions/postgresql.exception.js +32 -0
- package/exceptions/postgresql.exception.js.map +1 -0
- package/exceptions/query-canceled.exception.js +16 -0
- package/exceptions/query-canceled.exception.js.map +1 -0
- package/exceptions/query-timeout.exception.js +19 -0
- package/exceptions/query-timeout.exception.js.map +1 -0
- package/exceptions/queue-timeout.exception.js +18 -0
- package/exceptions/queue-timeout.exception.js.map +1 -0
- package/exceptions/transaction-rolled-back.exception.js +15 -0
- package/exceptions/transaction-rolled-back.exception.js.map +1 -0
- package/exceptions/type-not-found.exception.js +20 -0
- package/exceptions/type-not-found.exception.js.map +1 -0
- package/exceptions/unsafe-transaction.exception.js +15 -0
- package/exceptions/unsafe-transaction.exception.js.map +1 -0
- package/extensions/db-context-options-builder.extensions.js +17 -0
- package/extensions/db-context-options-builder.extensions.js.map +1 -0
- package/models/backend-key.js +16 -0
- package/models/backend-key.js.map +1 -0
- package/models/binary-columns.js +16 -0
- package/models/binary-columns.js.map +1 -0
- package/models/built-query.js +18 -0
- package/models/built-query.js.map +1 -0
- package/models/channel-listener.js +16 -0
- package/models/channel-listener.js.map +1 -0
- package/models/client/client-listener.js +158 -0
- package/models/client/client-listener.js.map +1 -0
- package/models/client/client-query.js +54 -0
- package/models/client/client-query.js.map +1 -0
- package/models/client/client-timers.js +44 -0
- package/models/client/client-timers.js.map +1 -0
- package/models/client/connection-socket.js +13 -0
- package/models/client/connection-socket.js.map +1 -0
- package/models/client/copy-both-stream.js +33 -0
- package/models/client/copy-both-stream.js.map +1 -0
- package/models/client/copy-in-stream.js +30 -0
- package/models/client/copy-in-stream.js.map +1 -0
- package/models/client/copy-out-stream.js +28 -0
- package/models/client/copy-out-stream.js.map +1 -0
- package/models/client/deferred.js +25 -0
- package/models/client/deferred.js.map +1 -0
- package/models/client/exchange.js +100 -0
- package/models/client/exchange.js.map +1 -0
- package/models/client/listen-subscription.js +26 -0
- package/models/client/listen-subscription.js.map +1 -0
- package/models/client/pool-job.js +24 -0
- package/models/client/pool-job.js.map +1 -0
- package/models/client/pool-lease.js +88 -0
- package/models/client/pool-lease.js.map +1 -0
- package/models/client/pool-queue.js +56 -0
- package/models/client/pool-queue.js.map +1 -0
- package/models/client/postgresql-client.js +83 -0
- package/models/client/postgresql-client.js.map +1 -0
- package/models/client/postgresql-connection.js +1557 -0
- package/models/client/postgresql-connection.js.map +1 -0
- package/models/client/postgresql-pool.js +896 -0
- package/models/client/postgresql-pool.js.map +1 -0
- package/models/client/query.js +216 -0
- package/models/client/query.js.map +1 -0
- package/models/client/reserved-client.js +29 -0
- package/models/client/reserved-client.js.map +1 -0
- package/models/client/statement-cache.js +111 -0
- package/models/client/statement-cache.js.map +1 -0
- package/models/client/transaction-scope.js +136 -0
- package/models/client/transaction-scope.js.map +1 -0
- package/models/client-configuration.js +16 -0
- package/models/client-configuration.js.map +1 -0
- package/models/client-options.js +69 -0
- package/models/client-options.js.map +1 -0
- package/models/column.js +30 -0
- package/models/column.js.map +1 -0
- package/models/command-result.js +16 -0
- package/models/command-result.js.map +1 -0
- package/models/connection-options.js +47 -0
- package/models/connection-options.js.map +1 -0
- package/models/date-parts.js +28 -0
- package/models/date-parts.js.map +1 -0
- package/models/der-element.js +18 -0
- package/models/der-element.js.map +1 -0
- package/models/listen-channel.js +17 -0
- package/models/listen-channel.js.map +1 -0
- package/models/notification.js +18 -0
- package/models/notification.js.map +1 -0
- package/models/parsed-url.js +22 -0
- package/models/parsed-url.js.map +1 -0
- package/models/pool-stats.js +22 -0
- package/models/pool-stats.js.map +1 -0
- package/models/postgresql-connection.js +21 -0
- package/models/postgresql-connection.js.map +1 -0
- package/models/postgresql-session.js +59 -0
- package/models/postgresql-session.js.map +1 -0
- package/models/postgresql-socket.js +129 -0
- package/models/postgresql-socket.js.map +1 -0
- package/models/prepared-statement.js +25 -0
- package/models/prepared-statement.js.map +1 -0
- package/models/query-batch.js +25 -0
- package/models/query-batch.js.map +1 -0
- package/models/query-cost.js +15 -0
- package/models/query-cost.js.map +1 -0
- package/models/query-options.js +16 -0
- package/models/query-options.js.map +1 -0
- package/models/query-statement.js +23 -0
- package/models/query-statement.js.map +1 -0
- package/models/query-wire.js +16 -0
- package/models/query-wire.js.map +1 -0
- package/models/result.js +32 -0
- package/models/result.js.map +1 -0
- package/models/row-column.js +16 -0
- package/models/row-column.js.map +1 -0
- package/models/started-session.js +31 -0
- package/models/started-session.js.map +1 -0
- package/models/statement-description.js +20 -0
- package/models/statement-description.js.map +1 -0
- package/models/statement-shape.js +16 -0
- package/models/statement-shape.js.map +1 -0
- package/models/system-certificates.js +17 -0
- package/models/system-certificates.js.map +1 -0
- package/models/transaction-state.js +18 -0
- package/models/transaction-state.js.map +1 -0
- package/models/transform-options.js +28 -0
- package/models/transform-options.js.map +1 -0
- package/models/type-definition.js +23 -0
- package/models/type-definition.js.map +1 -0
- package/models/type-registry.js +287 -0
- package/models/type-registry.js.map +1 -0
- package/models/typed-parameter.js +16 -0
- package/models/typed-parameter.js.map +1 -0
- package/package.json +26 -4
- package/resources.js +278 -0
- package/resources.js.map +1 -0
- package/services/authenticator.js +145 -0
- package/services/authenticator.js.map +1 -0
- package/services/certificate-hash.js +81 -0
- package/services/certificate-hash.js.map +1 -0
- package/services/client/connection-events.js +10 -0
- package/services/client/connection-events.js.map +1 -0
- package/services/client/connection-lease.js +10 -0
- package/services/client/connection-lease.js.map +1 -0
- package/services/client/copy-host.js +10 -0
- package/services/client/copy-host.js.map +1 -0
- package/services/client/cursor-iterator.js +46 -0
- package/services/client/cursor-iterator.js.map +1 -0
- package/services/client/file-query-handler.js +32 -0
- package/services/client/file-query-handler.js.map +1 -0
- package/services/client/query-handler.js +10 -0
- package/services/client/query-handler.js.map +1 -0
- package/services/client/row.reader.js +87 -0
- package/services/client/row.reader.js.map +1 -0
- package/services/client/sql-runner.js +64 -0
- package/services/client/sql-runner.js.map +1 -0
- package/services/client/type-loader-events.js +61 -0
- package/services/client/type-loader-events.js.map +1 -0
- package/services/client-options.parser.js +187 -0
- package/services/client-options.parser.js.map +1 -0
- package/services/location.parser.js +25 -0
- package/services/location.parser.js.map +1 -0
- package/services/parameter-rewriter.js +158 -0
- package/services/parameter-rewriter.js.map +1 -0
- package/services/postgresql-dialect.js +24 -0
- package/services/postgresql-dialect.js.map +1 -0
- package/services/postgresql-provider.js +8 -0
- package/services/postgresql-provider.js.map +1 -0
- package/services/protocol/backend.js +135 -0
- package/services/protocol/backend.js.map +1 -0
- package/services/protocol/frame.reader.js +167 -0
- package/services/protocol/frame.reader.js.map +1 -0
- package/services/protocol/frontend.js +177 -0
- package/services/protocol/frontend.js.map +1 -0
- package/services/protocol/protocol-writer.js +118 -0
- package/services/protocol/protocol-writer.js.map +1 -0
- package/services/session-starter.js +166 -0
- package/services/session-starter.js.map +1 -0
- package/services/tls-options.reader.js +60 -0
- package/services/tls-options.reader.js.map +1 -0
- package/services/types/array-serializer.js +35 -0
- package/services/types/array-serializer.js.map +1 -0
- package/services/types/array.parser.js +64 -0
- package/services/types/array.parser.js.map +1 -0
- package/services/types/binary-readers.js +135 -0
- package/services/types/binary-readers.js.map +1 -0
- package/services/types/date.parser.js +86 -0
- package/services/types/date.parser.js.map +1 -0
- package/services/value-converter.js +37 -0
- package/services/value-converter.js.map +1 -0
- package/types/binary-decoder.js +9 -0
- package/types/binary-decoder.js.map +1 -0
- package/types/client-settings.js +9 -0
- package/types/client-settings.js.map +1 -0
- package/types/host-target.js +9 -0
- package/types/host-target.js.map +1 -0
- package/types/option-values.js +9 -0
- package/types/option-values.js.map +1 -0
- package/types/query-outcome.js +9 -0
- package/types/query-outcome.js.map +1 -0
- package/types/query-settings.js +9 -0
- package/types/query-settings.js.map +1 -0
- package/types/transaction-result.js +9 -0
- package/types/transaction-result.js.map +1 -0
- package/types/transform-settings.js +9 -0
- package/types/transform-settings.js.map +1 -0
- package/types/value-parser.js +9 -0
- package/types/value-parser.js.map +1 -0
- package/types/value-serializer.js +9 -0
- package/types/value-serializer.js.map +1 -0
|
@@ -0,0 +1,1557 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @license
|
|
3
|
+
* Copyright (c) Noldova.
|
|
4
|
+
*
|
|
5
|
+
* This source code is licensed under the license found in the
|
|
6
|
+
* LICENSE file in the root directory of this source tree.
|
|
7
|
+
*/
|
|
8
|
+
// This file uses Node APIs and will change when we replace Node.
|
|
9
|
+
import { Buffer } from "node:buffer";
|
|
10
|
+
import { Readable } from "node:stream";
|
|
11
|
+
import "@noldova/context.core";
|
|
12
|
+
import { DataException } from "@noldova/context.data";
|
|
13
|
+
import { ExceptionOptions } from "@noldova/context.exceptions";
|
|
14
|
+
import { BackendMessage } from "../../enums/backend-message.js";
|
|
15
|
+
import { CursorAction } from "../../enums/cursor-action.js";
|
|
16
|
+
import { ExchangeKind } from "../../enums/exchange-kind.js";
|
|
17
|
+
import { LeaseKind } from "../../enums/lease-kind.js";
|
|
18
|
+
import { MessageTarget } from "../../enums/message-target.js";
|
|
19
|
+
import { PreparedState } from "../../enums/prepared-state.js";
|
|
20
|
+
import { RowMode } from "../../enums/row-mode.js";
|
|
21
|
+
import { TransactionStatus } from "../../enums/transaction-status.js";
|
|
22
|
+
import { WriteOutcome } from "../../enums/write-outcome.js";
|
|
23
|
+
import { BatchException } from "../../exceptions/batch.exception.js";
|
|
24
|
+
import { BatchSkippedException } from "../../exceptions/batch-skipped.exception.js";
|
|
25
|
+
import { CancelUnansweredException } from "../../exceptions/cancel-unanswered.exception.js";
|
|
26
|
+
import { ConnectionClosedException } from "../../exceptions/connection-closed.exception.js";
|
|
27
|
+
import { PostgreSQLException } from "../../exceptions/postgresql.exception.js";
|
|
28
|
+
import { QueryCanceledException } from "../../exceptions/query-canceled.exception.js";
|
|
29
|
+
import { UnsafeTransactionException } from "../../exceptions/unsafe-transaction.exception.js";
|
|
30
|
+
import { BinaryColumns } from "../binary-columns.js";
|
|
31
|
+
import { Column } from "../column.js";
|
|
32
|
+
import { PreparedStatement } from "../prepared-statement.js";
|
|
33
|
+
import { QueryBatch } from "../query-batch.js";
|
|
34
|
+
import { Result } from "../result.js";
|
|
35
|
+
import { StatementShape } from "../statement-shape.js";
|
|
36
|
+
import { Resources } from "../../resources.js";
|
|
37
|
+
import { Backend } from "../../services/protocol/backend.js";
|
|
38
|
+
import { FrameReader } from "../../services/protocol/frame.reader.js";
|
|
39
|
+
import { Frontend } from "../../services/protocol/frontend.js";
|
|
40
|
+
import { ProtocolWriter } from "../../services/protocol/protocol-writer.js";
|
|
41
|
+
import { CopyBothStream } from "./copy-both-stream.js";
|
|
42
|
+
import { CopyInStream } from "./copy-in-stream.js";
|
|
43
|
+
import { CopyOutStream } from "./copy-out-stream.js";
|
|
44
|
+
import { Deferred } from "./deferred.js";
|
|
45
|
+
import { Exchange } from "./exchange.js";
|
|
46
|
+
import { RowReader } from "../../services/client/row.reader.js";
|
|
47
|
+
export class PostgreSQLConnection {
|
|
48
|
+
static slow = 100;
|
|
49
|
+
static notable = 5;
|
|
50
|
+
static limit = 1000;
|
|
51
|
+
static unknownCost = 100;
|
|
52
|
+
static flushLength = 65536;
|
|
53
|
+
static ids = 1;
|
|
54
|
+
id;
|
|
55
|
+
host;
|
|
56
|
+
port;
|
|
57
|
+
backend;
|
|
58
|
+
parameters;
|
|
59
|
+
prepared = new Map();
|
|
60
|
+
lease = null;
|
|
61
|
+
fatal = null;
|
|
62
|
+
uncertain = false;
|
|
63
|
+
releasing = false;
|
|
64
|
+
retiring = false;
|
|
65
|
+
loading = false;
|
|
66
|
+
typing = null;
|
|
67
|
+
idleTimer = null;
|
|
68
|
+
lifeTimer = null;
|
|
69
|
+
idleSince = 0;
|
|
70
|
+
retired = [];
|
|
71
|
+
session;
|
|
72
|
+
options;
|
|
73
|
+
events;
|
|
74
|
+
socket;
|
|
75
|
+
timers;
|
|
76
|
+
frontend = new Frontend();
|
|
77
|
+
sent = [];
|
|
78
|
+
pending = [];
|
|
79
|
+
running = new Set();
|
|
80
|
+
open = new Set();
|
|
81
|
+
out = new ProtocolWriter();
|
|
82
|
+
frames;
|
|
83
|
+
blocker = null;
|
|
84
|
+
stashType = 0;
|
|
85
|
+
stashData = null;
|
|
86
|
+
heard;
|
|
87
|
+
rolling = false;
|
|
88
|
+
fence = null;
|
|
89
|
+
drained = null;
|
|
90
|
+
currentStatus;
|
|
91
|
+
flushing = false;
|
|
92
|
+
closedError = null;
|
|
93
|
+
closingPromise = null;
|
|
94
|
+
onended = null;
|
|
95
|
+
portals = 0;
|
|
96
|
+
epoch = 0;
|
|
97
|
+
headSince = 0;
|
|
98
|
+
notables = 0;
|
|
99
|
+
constructor(session, options, events) {
|
|
100
|
+
this.session = session;
|
|
101
|
+
this.options = options;
|
|
102
|
+
this.events = events;
|
|
103
|
+
this.socket = session.socket;
|
|
104
|
+
this.timers = options.timers;
|
|
105
|
+
this.id = PostgreSQLConnection.ids;
|
|
106
|
+
PostgreSQLConnection.ids += 1;
|
|
107
|
+
this.host = session.host;
|
|
108
|
+
this.port = session.port;
|
|
109
|
+
this.backend = session.backend;
|
|
110
|
+
this.parameters = new Map(session.parameters);
|
|
111
|
+
this.currentStatus = session.status;
|
|
112
|
+
this.heard = this.timers.readTime();
|
|
113
|
+
this.frames = new FrameReader((type, data, start, end) => this.handleFrame(type, data, start, end), error => {
|
|
114
|
+
this.fatal = error;
|
|
115
|
+
this.socket.destroy();
|
|
116
|
+
}, this.socket.isShared);
|
|
117
|
+
this.socket.onData = data => {
|
|
118
|
+
this.heard = this.timers.readTime();
|
|
119
|
+
this.frames.push(data);
|
|
120
|
+
};
|
|
121
|
+
this.socket.onDrain = () => this.ondrain();
|
|
122
|
+
this.socket.onClose = error => this.onclose(error);
|
|
123
|
+
if (session.rest.length > 0)
|
|
124
|
+
this.frames.push(session.rest);
|
|
125
|
+
this.socket.resume();
|
|
126
|
+
}
|
|
127
|
+
get status() {
|
|
128
|
+
return this.currentStatus;
|
|
129
|
+
}
|
|
130
|
+
get inflight() {
|
|
131
|
+
return this.sent.length + this.pending.length;
|
|
132
|
+
}
|
|
133
|
+
get exclusive() {
|
|
134
|
+
return !Object.isNull(this.blocker) && this.blocker.exclusive;
|
|
135
|
+
}
|
|
136
|
+
get copying() {
|
|
137
|
+
return !Object.isNull(this.blocker) && this.blocker.streaming && this.blocker.open;
|
|
138
|
+
}
|
|
139
|
+
get closed() {
|
|
140
|
+
return this.closedError;
|
|
141
|
+
}
|
|
142
|
+
get closing() {
|
|
143
|
+
return !Object.isNull(this.closingPromise);
|
|
144
|
+
}
|
|
145
|
+
get notable() {
|
|
146
|
+
return this.notables > 0;
|
|
147
|
+
}
|
|
148
|
+
send(query) {
|
|
149
|
+
const statement = PostgreSQLConnection.readWire(query).statement;
|
|
150
|
+
const exchange = new Exchange(ExchangeKind.Query, query, this.lease, item => this.writeExtended(item));
|
|
151
|
+
exchange.statement = statement;
|
|
152
|
+
exchange.ends = PostgreSQLConnection.isEnding(statement);
|
|
153
|
+
if (this.options.scheduler)
|
|
154
|
+
this.estimateCost(exchange);
|
|
155
|
+
this.enqueue(exchange);
|
|
156
|
+
}
|
|
157
|
+
batch(query, parts) {
|
|
158
|
+
const batch = new QueryBatch(query, parts);
|
|
159
|
+
parts.forEach((part, index) => {
|
|
160
|
+
part.redirect(result => this.settleBatchItem(batch, index, null, result), error => this.settleBatchItem(batch, index, error, null));
|
|
161
|
+
if (!Object.isNull(batch.aborted)) {
|
|
162
|
+
this.settleBatchItem(batch, index, new BatchSkippedException(index, batch.aborted), null);
|
|
163
|
+
return;
|
|
164
|
+
}
|
|
165
|
+
const statement = PostgreSQLConnection.readWire(part).statement;
|
|
166
|
+
const exchange = new Exchange(ExchangeKind.Query, part, this.lease, item => this.writeExtended(item));
|
|
167
|
+
exchange.statement = statement;
|
|
168
|
+
exchange.ends = PostgreSQLConnection.isEnding(statement);
|
|
169
|
+
exchange.batch = batch;
|
|
170
|
+
exchange.nosync = index < parts.length - 1;
|
|
171
|
+
if (this.options.scheduler)
|
|
172
|
+
this.estimateCost(exchange);
|
|
173
|
+
this.enqueue(exchange);
|
|
174
|
+
});
|
|
175
|
+
}
|
|
176
|
+
sendSimple(query, text) {
|
|
177
|
+
const exchange = new Exchange(ExchangeKind.Simple, query, this.lease, item => {
|
|
178
|
+
this.logDebug(text, [], []);
|
|
179
|
+
Frontend.writeQuery(this.out, text);
|
|
180
|
+
item.synced = true;
|
|
181
|
+
return WriteOutcome.Written;
|
|
182
|
+
});
|
|
183
|
+
exchange.text = text;
|
|
184
|
+
exchange.results = [];
|
|
185
|
+
exchange.ends = Resources.endingStatement.test(text);
|
|
186
|
+
exchange.streaming = query.streaming;
|
|
187
|
+
exchange.open = query.streaming;
|
|
188
|
+
if (this.options.scheduler)
|
|
189
|
+
this.estimateCost(exchange);
|
|
190
|
+
this.enqueue(exchange);
|
|
191
|
+
}
|
|
192
|
+
describeAsync(text, oids, query) {
|
|
193
|
+
const deferred = new Deferred();
|
|
194
|
+
const exchange = new Exchange(ExchangeKind.Describe, null, this.lease, item => {
|
|
195
|
+
Frontend.writeParse(this.out, "", text, oids);
|
|
196
|
+
Frontend.writeDescribe(this.out, MessageTarget.Statement, "");
|
|
197
|
+
this.sync(item);
|
|
198
|
+
return WriteOutcome.Written;
|
|
199
|
+
});
|
|
200
|
+
exchange.describedFor = query;
|
|
201
|
+
exchange.resolve = shape => deferred.resolve(shape);
|
|
202
|
+
exchange.reject = error => deferred.reject(error);
|
|
203
|
+
this.enqueue(exchange);
|
|
204
|
+
return deferred.promise;
|
|
205
|
+
}
|
|
206
|
+
failLocally(query, error) {
|
|
207
|
+
this.enqueue(new Exchange(ExchangeKind.Query, query, this.lease, () => {
|
|
208
|
+
throw error;
|
|
209
|
+
}));
|
|
210
|
+
}
|
|
211
|
+
cancelAsync(query) {
|
|
212
|
+
if (!Object.isNull(query.parts))
|
|
213
|
+
return this.cancelBatchAsync(query);
|
|
214
|
+
const head = this.sent[0];
|
|
215
|
+
if (!Object.isNull(this.stashData) && !Object.isUndefined(head) && head.query === query) {
|
|
216
|
+
head.own = new QueryCanceledException();
|
|
217
|
+
this.go();
|
|
218
|
+
return Promise.resolve();
|
|
219
|
+
}
|
|
220
|
+
if (this.running.has(query) || (!Object.isUndefined(head) && head.query === query && head.awaiting)) {
|
|
221
|
+
query.aborted = new QueryCanceledException();
|
|
222
|
+
return Promise.resolve();
|
|
223
|
+
}
|
|
224
|
+
const index = this.pending.findIndex(item => item.query === query || item.describedFor === query);
|
|
225
|
+
const waiting = this.pending[index];
|
|
226
|
+
if (!Object.isUndefined(waiting)) {
|
|
227
|
+
this.pending.splice(index, 1);
|
|
228
|
+
if (waiting === this.fence)
|
|
229
|
+
this.fence = null;
|
|
230
|
+
if (waiting.isNotable(PostgreSQLConnection.notable))
|
|
231
|
+
this.notables -= 1;
|
|
232
|
+
this.settleError(waiting, new QueryCanceledException());
|
|
233
|
+
this.release();
|
|
234
|
+
return Promise.resolve();
|
|
235
|
+
}
|
|
236
|
+
const exchange = this.sent.find(item => item.query === query || item.describedFor === query);
|
|
237
|
+
if (Object.isUndefined(exchange))
|
|
238
|
+
return Promise.resolve();
|
|
239
|
+
if (exchange === head && exchange.completed) {
|
|
240
|
+
this.expect(exchange);
|
|
241
|
+
return Promise.resolve();
|
|
242
|
+
}
|
|
243
|
+
if (exchange === head && (query.active || !Object.isNull(exchange.describedFor)))
|
|
244
|
+
return this.cancelExchangeAsync(exchange);
|
|
245
|
+
this.watchSilence(exchange);
|
|
246
|
+
exchange.cancel = new Deferred();
|
|
247
|
+
return exchange.cancel.promise;
|
|
248
|
+
}
|
|
249
|
+
closeAsync() {
|
|
250
|
+
if (!Object.isNull(this.closingPromise))
|
|
251
|
+
return this.closingPromise;
|
|
252
|
+
this.closingPromise = new Promise(resolve => {
|
|
253
|
+
this.onended = () => resolve();
|
|
254
|
+
});
|
|
255
|
+
if (this.isIdle())
|
|
256
|
+
this.terminate();
|
|
257
|
+
return this.closingPromise;
|
|
258
|
+
}
|
|
259
|
+
destroy(error = null) {
|
|
260
|
+
this.closingPromise ??= Promise.resolve();
|
|
261
|
+
this.socket.destroy(error);
|
|
262
|
+
}
|
|
263
|
+
go() {
|
|
264
|
+
const data = this.stashData;
|
|
265
|
+
if (Object.isNull(data))
|
|
266
|
+
return;
|
|
267
|
+
this.dispatch(this.stashType, data, 0, data.length);
|
|
268
|
+
this.stashData = null;
|
|
269
|
+
this.frames.resume();
|
|
270
|
+
}
|
|
271
|
+
ref() {
|
|
272
|
+
this.socket.ref();
|
|
273
|
+
}
|
|
274
|
+
unref() {
|
|
275
|
+
this.socket.unref();
|
|
276
|
+
}
|
|
277
|
+
computeBacklog(time) {
|
|
278
|
+
let wait = 0;
|
|
279
|
+
this.sent.forEach((item, index) => {
|
|
280
|
+
const cost = item.cost ?? 0;
|
|
281
|
+
if (index === 0) {
|
|
282
|
+
const elapsed = time - this.headSince;
|
|
283
|
+
wait += elapsed < cost ? cost - elapsed : elapsed;
|
|
284
|
+
}
|
|
285
|
+
else
|
|
286
|
+
wait += cost;
|
|
287
|
+
});
|
|
288
|
+
for (const item of this.pending)
|
|
289
|
+
wait += item.cost ?? 0;
|
|
290
|
+
return wait;
|
|
291
|
+
}
|
|
292
|
+
isHeavy() {
|
|
293
|
+
return this.sent.some(item => item.isNotable(PostgreSQLConnection.slow)) || this.pending.some(item => item.isNotable(PostgreSQLConnection.slow));
|
|
294
|
+
}
|
|
295
|
+
writeCopyData(chunk, callback) {
|
|
296
|
+
Frontend.writeCopyData(this.out, chunk);
|
|
297
|
+
if (this.flush())
|
|
298
|
+
callback();
|
|
299
|
+
else
|
|
300
|
+
this.drained = callback;
|
|
301
|
+
}
|
|
302
|
+
finishCopy(exchange, callback) {
|
|
303
|
+
Frontend.writeCopyDone(this.out);
|
|
304
|
+
exchange.copied = true;
|
|
305
|
+
exchange.open = false;
|
|
306
|
+
exchange.final = callback;
|
|
307
|
+
this.schedule(true);
|
|
308
|
+
this.refresh();
|
|
309
|
+
}
|
|
310
|
+
failCopy(exchange, error) {
|
|
311
|
+
exchange.stream = null;
|
|
312
|
+
if (exchange.copied || !Object.isNull(this.closedError) || !Object.isNull(exchange.error))
|
|
313
|
+
return;
|
|
314
|
+
Frontend.writeCopyFail(this.out, Object.isNull(error) ? Resources.copyStreamDestroyed : String(error));
|
|
315
|
+
exchange.copied = true;
|
|
316
|
+
exchange.open = false;
|
|
317
|
+
this.schedule(true);
|
|
318
|
+
this.refresh();
|
|
319
|
+
}
|
|
320
|
+
readCopy(exchange) {
|
|
321
|
+
exchange.wanted = true;
|
|
322
|
+
if (!this.frames.isPaused)
|
|
323
|
+
return;
|
|
324
|
+
this.frames.resume();
|
|
325
|
+
if (!this.frames.isPaused)
|
|
326
|
+
this.socket.resume();
|
|
327
|
+
}
|
|
328
|
+
abandonCopy(exchange) {
|
|
329
|
+
if (Object.isNull(exchange.stream))
|
|
330
|
+
return;
|
|
331
|
+
exchange.stream = null;
|
|
332
|
+
if (!this.frames.isPaused)
|
|
333
|
+
return;
|
|
334
|
+
this.frames.resume();
|
|
335
|
+
if (!this.frames.isPaused)
|
|
336
|
+
this.socket.resume();
|
|
337
|
+
}
|
|
338
|
+
ondrain() {
|
|
339
|
+
const drained = this.drained;
|
|
340
|
+
this.drained = null;
|
|
341
|
+
drained?.();
|
|
342
|
+
}
|
|
343
|
+
schedule(now = false) {
|
|
344
|
+
if (now || this.out.length >= PostgreSQLConnection.flushLength)
|
|
345
|
+
return this.flush();
|
|
346
|
+
if (!this.flushing) {
|
|
347
|
+
this.flushing = true;
|
|
348
|
+
this.timers.nextTick(() => this.flush());
|
|
349
|
+
}
|
|
350
|
+
return true;
|
|
351
|
+
}
|
|
352
|
+
flush() {
|
|
353
|
+
this.flushing = false;
|
|
354
|
+
if (!Object.isNull(this.closedError)) {
|
|
355
|
+
this.out.take();
|
|
356
|
+
return true;
|
|
357
|
+
}
|
|
358
|
+
return this.out.length > 0 ? this.socket.write(this.out.take()) : true;
|
|
359
|
+
}
|
|
360
|
+
refresh() {
|
|
361
|
+
if (!Object.isNull(this.blocker) && !this.blocker.isHolding())
|
|
362
|
+
this.blocker = null;
|
|
363
|
+
this.release();
|
|
364
|
+
}
|
|
365
|
+
enqueue(exchange) {
|
|
366
|
+
if (!Object.isNull(this.closedError)) {
|
|
367
|
+
this.settleError(exchange, this.closedError);
|
|
368
|
+
return;
|
|
369
|
+
}
|
|
370
|
+
if (!Object.isNull(this.blocker) && this.blocker.streaming && this.blocker.open) {
|
|
371
|
+
this.settleError(exchange, new DataException(Resources.copyInProgress));
|
|
372
|
+
return;
|
|
373
|
+
}
|
|
374
|
+
if (!Object.isNull(exchange.query))
|
|
375
|
+
exchange.query.state = this.backend;
|
|
376
|
+
if (!Object.isNull(this.fence))
|
|
377
|
+
this.pending.splice(this.pending.indexOf(this.fence), 0, exchange);
|
|
378
|
+
else if (exchange.ends && this.open.size > 0) {
|
|
379
|
+
this.fence = exchange;
|
|
380
|
+
this.pending.push(exchange);
|
|
381
|
+
}
|
|
382
|
+
else if (!Object.isNull(this.blocker) || this.pending.length > 0)
|
|
383
|
+
this.pending.push(exchange);
|
|
384
|
+
else {
|
|
385
|
+
this.write(exchange);
|
|
386
|
+
this.schedule();
|
|
387
|
+
return;
|
|
388
|
+
}
|
|
389
|
+
this.release();
|
|
390
|
+
}
|
|
391
|
+
release() {
|
|
392
|
+
for (let next = this.pending[0]; Object.isNull(this.blocker) && !Object.isUndefined(next) && next !== this.fence; next = this.pending[0]) {
|
|
393
|
+
this.pending.shift();
|
|
394
|
+
this.write(next);
|
|
395
|
+
}
|
|
396
|
+
this.schedule();
|
|
397
|
+
}
|
|
398
|
+
write(exchange) {
|
|
399
|
+
const mark = this.out.length;
|
|
400
|
+
let outcome;
|
|
401
|
+
try {
|
|
402
|
+
outcome = exchange.write(exchange);
|
|
403
|
+
}
|
|
404
|
+
catch (error) {
|
|
405
|
+
exchange.own = PostgreSQLConnection.toError(error);
|
|
406
|
+
outcome = WriteOutcome.Skipped;
|
|
407
|
+
}
|
|
408
|
+
if (outcome === WriteOutcome.Held) {
|
|
409
|
+
this.out.truncate(mark);
|
|
410
|
+
exchange.loose = false;
|
|
411
|
+
const blocker = this.sent.find(item => item.gate && item.isHolding());
|
|
412
|
+
if (!Object.isUndefined(blocker)) {
|
|
413
|
+
this.blocker = blocker;
|
|
414
|
+
if (blocker.nosync && !blocker.flushed) {
|
|
415
|
+
blocker.flushed = true;
|
|
416
|
+
Frontend.writeFlush(this.out);
|
|
417
|
+
this.schedule();
|
|
418
|
+
}
|
|
419
|
+
}
|
|
420
|
+
this.pending.unshift(exchange);
|
|
421
|
+
return;
|
|
422
|
+
}
|
|
423
|
+
if (outcome === WriteOutcome.Skipped) {
|
|
424
|
+
this.out.truncate(mark);
|
|
425
|
+
exchange.fresh = null;
|
|
426
|
+
exchange.parses = [];
|
|
427
|
+
exchange.portal = "";
|
|
428
|
+
exchange.describing = false;
|
|
429
|
+
exchange.synced = false;
|
|
430
|
+
exchange.flushing = false;
|
|
431
|
+
exchange.open = false;
|
|
432
|
+
if (!Object.isNull(exchange.batch)) {
|
|
433
|
+
exchange.entry = null;
|
|
434
|
+
Frontend.writeParse(this.out, "", Resources.refusedQuery);
|
|
435
|
+
exchange.parses = [""];
|
|
436
|
+
this.abort(exchange);
|
|
437
|
+
}
|
|
438
|
+
else if (Object.isNull(exchange.own) || exchange.kind === ExchangeKind.Step || Object.isNull(this.lease)) {
|
|
439
|
+
exchange.kind = ExchangeKind.Local;
|
|
440
|
+
if (exchange.isNotable(PostgreSQLConnection.notable))
|
|
441
|
+
this.notables -= 1;
|
|
442
|
+
exchange.cost = null;
|
|
443
|
+
if (this.sent.length === 0)
|
|
444
|
+
queueMicrotask(() => this.settle(exchange));
|
|
445
|
+
else
|
|
446
|
+
this.sent.push(exchange);
|
|
447
|
+
return;
|
|
448
|
+
}
|
|
449
|
+
else {
|
|
450
|
+
exchange.kind = ExchangeKind.Simple;
|
|
451
|
+
exchange.text = Resources.refusedQuery;
|
|
452
|
+
exchange.results = [];
|
|
453
|
+
exchange.entry = null;
|
|
454
|
+
this.sync(exchange, true);
|
|
455
|
+
}
|
|
456
|
+
}
|
|
457
|
+
if (!Object.isNull(exchange.fresh))
|
|
458
|
+
this.register(exchange.fresh);
|
|
459
|
+
if (exchange.portal.length > 0 && exchange.kind === ExchangeKind.Query)
|
|
460
|
+
this.open.add(exchange);
|
|
461
|
+
if (!Object.isNull(exchange.query))
|
|
462
|
+
exchange.query.active = this.sent.length === 0;
|
|
463
|
+
if (exchange.ends)
|
|
464
|
+
this.epoch += 1;
|
|
465
|
+
exchange.epoch = this.epoch;
|
|
466
|
+
if (this.options.scheduler && this.sent.length === 0)
|
|
467
|
+
this.headSince = this.timers.readTime();
|
|
468
|
+
this.sent.push(exchange);
|
|
469
|
+
if (exchange.isHolding())
|
|
470
|
+
this.blocker = exchange;
|
|
471
|
+
}
|
|
472
|
+
sync(exchange, refuse = false) {
|
|
473
|
+
if (exchange.synced)
|
|
474
|
+
return;
|
|
475
|
+
exchange.synced = true;
|
|
476
|
+
if (refuse)
|
|
477
|
+
Frontend.writeQuery(this.out, Resources.refusedQuery);
|
|
478
|
+
else
|
|
479
|
+
Frontend.writeSync(this.out);
|
|
480
|
+
this.schedule();
|
|
481
|
+
}
|
|
482
|
+
register(entry) {
|
|
483
|
+
this.prepared.delete(entry.name);
|
|
484
|
+
this.prepared.set(entry.name, entry);
|
|
485
|
+
if (!Object.isNull(entry.owner)) {
|
|
486
|
+
entry.owner.entry = entry;
|
|
487
|
+
entry.owner.fresh = null;
|
|
488
|
+
}
|
|
489
|
+
if (this.prepared.size <= PostgreSQLConnection.limit)
|
|
490
|
+
return;
|
|
491
|
+
for (const [name, prepared] of this.prepared) {
|
|
492
|
+
if (prepared.state !== PreparedState.Parsing) {
|
|
493
|
+
this.prepared.delete(name);
|
|
494
|
+
Frontend.writeClose(this.out, MessageTarget.Statement, name);
|
|
495
|
+
return;
|
|
496
|
+
}
|
|
497
|
+
}
|
|
498
|
+
}
|
|
499
|
+
drop(entry) {
|
|
500
|
+
if (this.prepared.get(entry.name) === entry)
|
|
501
|
+
this.prepared.delete(entry.name);
|
|
502
|
+
}
|
|
503
|
+
settleBatchItem(batch, index, error, result) {
|
|
504
|
+
if (batch.settled[index] === true)
|
|
505
|
+
return;
|
|
506
|
+
batch.settled[index] = true;
|
|
507
|
+
batch.left -= 1;
|
|
508
|
+
if (!Object.isNull(error)) {
|
|
509
|
+
if (Object.isNull(batch.error) || (index < batch.index && !(error instanceof BatchSkippedException))) {
|
|
510
|
+
batch.error = error;
|
|
511
|
+
batch.index = index;
|
|
512
|
+
}
|
|
513
|
+
}
|
|
514
|
+
else
|
|
515
|
+
batch.results[index] = result;
|
|
516
|
+
if (batch.left > 0)
|
|
517
|
+
return;
|
|
518
|
+
const failure = batch.error;
|
|
519
|
+
if (!Object.isNull(failure)) {
|
|
520
|
+
batch.parts.forEach((part, partIndex) => {
|
|
521
|
+
part.fail(partIndex === batch.index ? failure : new BatchSkippedException(batch.index, failure));
|
|
522
|
+
part.promise.catch(() => undefined);
|
|
523
|
+
});
|
|
524
|
+
batch.query.reject(new BatchException(batch.index, failure));
|
|
525
|
+
return;
|
|
526
|
+
}
|
|
527
|
+
batch.parts.forEach((part, partIndex) => {
|
|
528
|
+
const result = batch.results[partIndex];
|
|
529
|
+
if (!Object.isUndefined(result))
|
|
530
|
+
part.complete(result);
|
|
531
|
+
});
|
|
532
|
+
batch.query.resolve(batch.results);
|
|
533
|
+
}
|
|
534
|
+
completeExchange(exchange) {
|
|
535
|
+
this.sent.shift();
|
|
536
|
+
if (this.options.scheduler)
|
|
537
|
+
this.recordTiming(exchange);
|
|
538
|
+
exchange.done = true;
|
|
539
|
+
exchange.open = false;
|
|
540
|
+
if (this.blocker === exchange)
|
|
541
|
+
this.blocker = null;
|
|
542
|
+
this.settle(exchange);
|
|
543
|
+
this.activateHead();
|
|
544
|
+
this.release();
|
|
545
|
+
}
|
|
546
|
+
abort(exchange) {
|
|
547
|
+
const batch = exchange.batch;
|
|
548
|
+
if (exchange.aborted || Object.isNull(batch))
|
|
549
|
+
return;
|
|
550
|
+
exchange.aborted = true;
|
|
551
|
+
const error = exchange.own ?? exchange.error;
|
|
552
|
+
batch.aborted = error;
|
|
553
|
+
let synced = !exchange.nosync && exchange.synced;
|
|
554
|
+
const at = this.sent.indexOf(exchange);
|
|
555
|
+
for (let index = at === -1 ? this.sent.length : at + 1; index < this.sent.length;) {
|
|
556
|
+
const item = this.sent[index];
|
|
557
|
+
if (!Object.isUndefined(item) && item.batch === batch) {
|
|
558
|
+
this.sent.splice(index, 1);
|
|
559
|
+
synced ||= !item.nosync && item.synced;
|
|
560
|
+
this.dropFromBatch(item, batch, error);
|
|
561
|
+
if (this.blocker === item)
|
|
562
|
+
this.blocker = null;
|
|
563
|
+
}
|
|
564
|
+
else
|
|
565
|
+
index += 1;
|
|
566
|
+
}
|
|
567
|
+
for (let index = 0; index < this.pending.length;) {
|
|
568
|
+
const item = this.pending[index];
|
|
569
|
+
if (!Object.isUndefined(item) && item.batch === batch) {
|
|
570
|
+
this.pending.splice(index, 1);
|
|
571
|
+
this.dropFromBatch(item, batch, error);
|
|
572
|
+
}
|
|
573
|
+
else
|
|
574
|
+
index += 1;
|
|
575
|
+
}
|
|
576
|
+
if (!synced)
|
|
577
|
+
Frontend.writeSync(this.out);
|
|
578
|
+
exchange.nosync = false;
|
|
579
|
+
exchange.synced = true;
|
|
580
|
+
this.schedule();
|
|
581
|
+
}
|
|
582
|
+
dropFromBatch(item, batch, error) {
|
|
583
|
+
if (!Object.isNull(item.entry) && item.entry.owner === item && item.entry.state === PreparedState.Parsing)
|
|
584
|
+
item.entry.state = PreparedState.Stale;
|
|
585
|
+
item.done = true;
|
|
586
|
+
if (item.isNotable(PostgreSQLConnection.notable))
|
|
587
|
+
this.notables -= 1;
|
|
588
|
+
this.settleError(item, new BatchSkippedException(batch.parts.findIndex(part => part === item.query), error));
|
|
589
|
+
}
|
|
590
|
+
estimateCost(exchange) {
|
|
591
|
+
const query = exchange.query;
|
|
592
|
+
if (!Object.isNull(query) && !Object.isNull(query.cost) && Object.isNull(query.cursorFn) && !query.streaming)
|
|
593
|
+
exchange.cost = query.cost.estimate ?? PostgreSQLConnection.unknownCost;
|
|
594
|
+
if (exchange.isNotable(PostgreSQLConnection.notable))
|
|
595
|
+
this.notables += 1;
|
|
596
|
+
}
|
|
597
|
+
recordTiming(exchange) {
|
|
598
|
+
const time = this.timers.readTime();
|
|
599
|
+
if (!Object.isNull(exchange.cost)) {
|
|
600
|
+
if (exchange.isNotable(PostgreSQLConnection.notable))
|
|
601
|
+
this.notables -= 1;
|
|
602
|
+
const sample = time - this.headSince;
|
|
603
|
+
const cost = exchange.query?.cost;
|
|
604
|
+
if (!Object.isNullOrUndefined(cost))
|
|
605
|
+
cost.estimate = Object.isNull(cost.estimate) || sample > cost.estimate ? sample : cost.estimate + (sample - cost.estimate) * 0.1;
|
|
606
|
+
this.events.onTimed(sample, time);
|
|
607
|
+
}
|
|
608
|
+
this.headSince = time;
|
|
609
|
+
}
|
|
610
|
+
writeExtended(exchange) {
|
|
611
|
+
const query = PostgreSQLConnection.queryOf(exchange);
|
|
612
|
+
const wire = PostgreSQLConnection.readWire(query);
|
|
613
|
+
const built = wire.built;
|
|
614
|
+
const statement = wire.statement;
|
|
615
|
+
const name = statement.name;
|
|
616
|
+
const current = name.length > 0 ? this.prepared.get(name) ?? null : null;
|
|
617
|
+
if (!Object.isNull(query.cursorFn)) {
|
|
618
|
+
exchange.rows = query.cursorRows;
|
|
619
|
+
exchange.portal = this.isInTransaction() ? Resources.portalPrefix + String(this.portals++) : "";
|
|
620
|
+
exchange.flushing = exchange.portal.length === 0;
|
|
621
|
+
}
|
|
622
|
+
exchange.streaming = query.streaming;
|
|
623
|
+
exchange.open = query.streaming;
|
|
624
|
+
if (!Object.isNull(current) && current.state === PreparedState.Parsing && Object.isNull(current.oids) && !Object.isNull(current.owner)
|
|
625
|
+
&& Object.isNull(current.owner.error)) {
|
|
626
|
+
current.owner.gate = true;
|
|
627
|
+
return WriteOutcome.Held;
|
|
628
|
+
}
|
|
629
|
+
if (!Object.isNull(current) && !Object.isNull(current.oids) && current !== exchange.previous && PostgreSQLConnection.isUsable(current, statement)) {
|
|
630
|
+
const binary = query.rowMode === RowMode.Raw || !Object.isNull(query.ownParsers) ? null : this.readBinaryColumns(current);
|
|
631
|
+
exchange.entry = current;
|
|
632
|
+
exchange.columns = Object.isNull(binary) ? current.columns : binary.columns;
|
|
633
|
+
exchange.formats = Object.isNull(binary) ? null : binary.formats;
|
|
634
|
+
exchange.values = this.serialize(current.oids, built, current);
|
|
635
|
+
this.run(exchange, name);
|
|
636
|
+
return WriteOutcome.Written;
|
|
637
|
+
}
|
|
638
|
+
if (exchange.retried) {
|
|
639
|
+
exchange.verify = true;
|
|
640
|
+
Frontend.writeParse(this.out, "", statement.text, built.oids);
|
|
641
|
+
Frontend.writeDescribe(this.out, MessageTarget.Statement, "");
|
|
642
|
+
Frontend.writeFlush(this.out);
|
|
643
|
+
exchange.describing = true;
|
|
644
|
+
return WriteOutcome.Written;
|
|
645
|
+
}
|
|
646
|
+
const typed = !PostgreSQLConnection.hasInferredTypes(built);
|
|
647
|
+
if (name.length > 0)
|
|
648
|
+
exchange.fresh = new PreparedStatement(name, exchange, typed ? built.oids : null);
|
|
649
|
+
if (!Object.isNull(current))
|
|
650
|
+
Frontend.writeClose(this.out, MessageTarget.Statement, name);
|
|
651
|
+
this.parse(exchange, name, statement.text, built.oids);
|
|
652
|
+
Frontend.writeDescribe(this.out, MessageTarget.Statement, name);
|
|
653
|
+
if (!typed && !this.isLoose(exchange, built)) {
|
|
654
|
+
Frontend.writeFlush(this.out);
|
|
655
|
+
exchange.describing = true;
|
|
656
|
+
return WriteOutcome.Written;
|
|
657
|
+
}
|
|
658
|
+
exchange.learn = true;
|
|
659
|
+
exchange.values = typed ? this.serialize(built.oids, built, exchange.fresh) : this.formatUntypedValues(built);
|
|
660
|
+
this.run(exchange, name);
|
|
661
|
+
return WriteOutcome.Written;
|
|
662
|
+
}
|
|
663
|
+
readBinaryColumns(entry) {
|
|
664
|
+
const columns = entry.columns;
|
|
665
|
+
if (Object.isNull(columns) || entry.binaryOf === columns)
|
|
666
|
+
return entry.binary;
|
|
667
|
+
const formats = [];
|
|
668
|
+
let binary = null;
|
|
669
|
+
columns.forEach((column, index) => {
|
|
670
|
+
const decode = this.events.findBinaryDecoder(column.type);
|
|
671
|
+
formats.push(Object.isNull(decode) ? 0 : 1);
|
|
672
|
+
if (!Object.isNull(decode)) {
|
|
673
|
+
binary ??= [...columns];
|
|
674
|
+
binary[index] = column.withDecoder(decode);
|
|
675
|
+
}
|
|
676
|
+
});
|
|
677
|
+
entry.binaryOf = columns;
|
|
678
|
+
entry.binary = Object.isNull(binary) ? null : new BinaryColumns(binary, formats);
|
|
679
|
+
return entry.binary;
|
|
680
|
+
}
|
|
681
|
+
isLoose(exchange, built) {
|
|
682
|
+
if (!this.events.plain || !Object.isNull(PostgreSQLConnection.queryOf(exchange).cursorFn) || exchange.streaming)
|
|
683
|
+
return false;
|
|
684
|
+
const tight = built.oids.some((oid, index) => {
|
|
685
|
+
const value = built.values[index];
|
|
686
|
+
return !oid && !Object.isNull(value) && !(Object.isNumber(value) && Number.isFinite(value));
|
|
687
|
+
});
|
|
688
|
+
if (tight)
|
|
689
|
+
return false;
|
|
690
|
+
exchange.loose = true;
|
|
691
|
+
return true;
|
|
692
|
+
}
|
|
693
|
+
formatUntypedValues(built) {
|
|
694
|
+
return built.values.map((value, index) => {
|
|
695
|
+
if (Object.isNull(value))
|
|
696
|
+
return null;
|
|
697
|
+
const oid = built.oids[index] ?? 0;
|
|
698
|
+
return oid ? this.events.findSerializer(oid)(value) : String(value);
|
|
699
|
+
});
|
|
700
|
+
}
|
|
701
|
+
parse(exchange, name, text, oids) {
|
|
702
|
+
Frontend.writeParse(this.out, name, text, oids);
|
|
703
|
+
exchange.parses.push(name);
|
|
704
|
+
}
|
|
705
|
+
run(exchange, name) {
|
|
706
|
+
const mark = this.out.length;
|
|
707
|
+
try {
|
|
708
|
+
if (exchange.nosync) {
|
|
709
|
+
Frontend.writeBind(this.out, exchange.portal, name, exchange.values, [], exchange.formats ?? []);
|
|
710
|
+
Frontend.writeExecute(this.out, exchange.portal, exchange.rows);
|
|
711
|
+
}
|
|
712
|
+
else
|
|
713
|
+
Frontend.writeRun(this.out, exchange.portal, name, exchange.values, exchange.rows, exchange.flushing, exchange.formats);
|
|
714
|
+
}
|
|
715
|
+
catch (error) {
|
|
716
|
+
this.out.truncate(mark);
|
|
717
|
+
throw error;
|
|
718
|
+
}
|
|
719
|
+
if (!exchange.flushing)
|
|
720
|
+
exchange.synced = true;
|
|
721
|
+
const oids = !Object.isNull(exchange.entry)
|
|
722
|
+
? exchange.entry.oids
|
|
723
|
+
: exchange.params ?? (exchange.loose ? null : PostgreSQLConnection.readWire(PostgreSQLConnection.queryOf(exchange)).built.oids);
|
|
724
|
+
if (Object.isNull(oids))
|
|
725
|
+
exchange.later = true;
|
|
726
|
+
else
|
|
727
|
+
this.logDebug(PostgreSQLConnection.readStatement(exchange).text, exchange.values, oids);
|
|
728
|
+
}
|
|
729
|
+
logLateExchange(exchange, oids) {
|
|
730
|
+
if (!exchange.later)
|
|
731
|
+
return;
|
|
732
|
+
exchange.later = false;
|
|
733
|
+
this.logDebug(PostgreSQLConnection.readStatement(exchange).text, exchange.values, oids);
|
|
734
|
+
}
|
|
735
|
+
serialize(oids, built, cache) {
|
|
736
|
+
const serializers = cache?.serializers ?? oids.map(oid => this.events.findSerializer(oid));
|
|
737
|
+
if (!Object.isNull(cache))
|
|
738
|
+
cache.serializers = serializers;
|
|
739
|
+
return built.values.map((value, index) => {
|
|
740
|
+
if (Object.isNull(value))
|
|
741
|
+
return null;
|
|
742
|
+
const serializer = serializers[index];
|
|
743
|
+
if (Object.isUndefined(serializer))
|
|
744
|
+
throw new DataException(Resources.formatParameterCount(oids.length, built.values.length));
|
|
745
|
+
return serializer(value);
|
|
746
|
+
});
|
|
747
|
+
}
|
|
748
|
+
isInTransaction() {
|
|
749
|
+
return this.lease?.kind === LeaseKind.Transaction;
|
|
750
|
+
}
|
|
751
|
+
handleDescription(exchange, columns) {
|
|
752
|
+
exchange.describing = false;
|
|
753
|
+
const wire = PostgreSQLConnection.readWire(PostgreSQLConnection.queryOf(exchange));
|
|
754
|
+
const built = wire.built;
|
|
755
|
+
const statement = wire.statement;
|
|
756
|
+
const params = exchange.params ?? [];
|
|
757
|
+
try {
|
|
758
|
+
if (exchange.verify) {
|
|
759
|
+
const previous = exchange.previous;
|
|
760
|
+
if (!Object.isNull(previous) && PostgreSQLConnection.isSameShape(previous, params, columns)) {
|
|
761
|
+
if (!Object.isNull(exchange.original)) {
|
|
762
|
+
exchange.own = exchange.original;
|
|
763
|
+
this.sync(exchange);
|
|
764
|
+
return;
|
|
765
|
+
}
|
|
766
|
+
}
|
|
767
|
+
else if (!Object.isNull(previous)) {
|
|
768
|
+
this.markStale(previous);
|
|
769
|
+
statement.version += 1;
|
|
770
|
+
}
|
|
771
|
+
exchange.values = this.serialize(params, built, null);
|
|
772
|
+
this.run(exchange, "");
|
|
773
|
+
return;
|
|
774
|
+
}
|
|
775
|
+
const entry = exchange.entry;
|
|
776
|
+
if (!Object.isNull(entry)) {
|
|
777
|
+
entry.oids = params;
|
|
778
|
+
entry.columns = columns;
|
|
779
|
+
PostgreSQLConnection.recordShape(entry, statement, columns);
|
|
780
|
+
}
|
|
781
|
+
exchange.values = this.serialize(params, built, entry);
|
|
782
|
+
this.run(exchange, statement.name);
|
|
783
|
+
}
|
|
784
|
+
catch (error) {
|
|
785
|
+
exchange.own = PostgreSQLConnection.toError(error);
|
|
786
|
+
if (!Object.isNull(exchange.batch)) {
|
|
787
|
+
Frontend.writeParse(this.out, "", Resources.refusedQuery);
|
|
788
|
+
exchange.parses.push("");
|
|
789
|
+
this.abort(exchange);
|
|
790
|
+
}
|
|
791
|
+
else
|
|
792
|
+
this.sync(exchange, !Object.isNull(this.lease));
|
|
793
|
+
}
|
|
794
|
+
}
|
|
795
|
+
turn(exchange, cancel) {
|
|
796
|
+
if (!Object.isNull(exchange.silence)) {
|
|
797
|
+
this.timers.clearTimeout(exchange.silence);
|
|
798
|
+
exchange.silence = null;
|
|
799
|
+
}
|
|
800
|
+
const go = () => {
|
|
801
|
+
exchange.silence = null;
|
|
802
|
+
if (!Object.isNull(this.closedError) || exchange.done || this.sent[0] !== exchange) {
|
|
803
|
+
cancel.resolve();
|
|
804
|
+
return;
|
|
805
|
+
}
|
|
806
|
+
if (exchange.completed) {
|
|
807
|
+
this.expect(exchange);
|
|
808
|
+
cancel.resolve();
|
|
809
|
+
return;
|
|
810
|
+
}
|
|
811
|
+
this.cancelExchangeAsync(exchange).then(() => cancel.resolve(), (error) => cancel.reject(error));
|
|
812
|
+
};
|
|
813
|
+
if (exchange.query?.timedOut === true && this.options.queryTimeout > 0)
|
|
814
|
+
exchange.silence = this.timers.setTimeout(go, this.options.queryTimeout * 1000);
|
|
815
|
+
else
|
|
816
|
+
this.timers.nextTick(go);
|
|
817
|
+
}
|
|
818
|
+
watchSilence(exchange) {
|
|
819
|
+
if (!this.options.cancelTimeout || !Object.isNull(exchange.silence))
|
|
820
|
+
return;
|
|
821
|
+
const milliseconds = this.options.cancelTimeout * 1000;
|
|
822
|
+
const check = () => {
|
|
823
|
+
exchange.silence = null;
|
|
824
|
+
const query = exchange.query ?? exchange.describedFor;
|
|
825
|
+
if (!Object.isNull(this.closedError) || exchange.done || Object.isNull(query) || query.settled)
|
|
826
|
+
return;
|
|
827
|
+
const idle = this.timers.readTime() - this.heard;
|
|
828
|
+
if (idle >= milliseconds)
|
|
829
|
+
this.destroy(new CancelUnansweredException(this.options.cancelTimeout, true));
|
|
830
|
+
else
|
|
831
|
+
exchange.silence = this.timers.setTimeout(check, milliseconds - idle);
|
|
832
|
+
};
|
|
833
|
+
exchange.silence = this.timers.setTimeout(check, milliseconds);
|
|
834
|
+
}
|
|
835
|
+
cancelExchangeAsync(exchange) {
|
|
836
|
+
this.expect(exchange);
|
|
837
|
+
const cancel = this.session.cancelAsync(this.events.cancels);
|
|
838
|
+
this.events.onCancelling(cancel);
|
|
839
|
+
return cancel;
|
|
840
|
+
}
|
|
841
|
+
expect(exchange) {
|
|
842
|
+
if (!this.options.cancelTimeout || !Object.isNull(exchange.unanswered))
|
|
843
|
+
return;
|
|
844
|
+
exchange.unanswered = this.timers.setTimeout(() => this.handleUnanswered(exchange), this.options.cancelTimeout * 1000);
|
|
845
|
+
exchange.batch?.query.promise.then(() => this.clearUnanswered(exchange), () => this.clearUnanswered(exchange));
|
|
846
|
+
}
|
|
847
|
+
clearUnanswered(exchange) {
|
|
848
|
+
if (Object.isNull(exchange.unanswered))
|
|
849
|
+
return;
|
|
850
|
+
this.timers.clearTimeout(exchange.unanswered);
|
|
851
|
+
exchange.unanswered = null;
|
|
852
|
+
}
|
|
853
|
+
handleUnanswered(exchange) {
|
|
854
|
+
exchange.unanswered = null;
|
|
855
|
+
const query = Object.isNull(exchange.batch) ? exchange.query ?? exchange.describedFor : exchange.batch.query;
|
|
856
|
+
if (!Object.isNull(this.closedError) || (exchange.done && Object.isNull(exchange.batch)) || Object.isNull(query) || query.settled)
|
|
857
|
+
return;
|
|
858
|
+
this.destroy(new CancelUnansweredException(this.options.cancelTimeout, false));
|
|
859
|
+
}
|
|
860
|
+
markAnswered(exchange) {
|
|
861
|
+
if (!Object.isNull(exchange.silence)) {
|
|
862
|
+
this.timers.clearTimeout(exchange.silence);
|
|
863
|
+
exchange.silence = null;
|
|
864
|
+
}
|
|
865
|
+
if (Object.isNull(exchange.batch) || exchange.batch.query.settled)
|
|
866
|
+
this.clearUnanswered(exchange);
|
|
867
|
+
}
|
|
868
|
+
cancelBatchAsync(query) {
|
|
869
|
+
const exchange = this.sent.find(item => item.batch?.query === query);
|
|
870
|
+
if (!Object.isUndefined(exchange)) {
|
|
871
|
+
if (exchange === this.sent[0])
|
|
872
|
+
return this.cancelExchangeAsync(exchange);
|
|
873
|
+
exchange.cancel = new Deferred();
|
|
874
|
+
return exchange.cancel.promise;
|
|
875
|
+
}
|
|
876
|
+
const first = this.pending.find(item => item.batch?.query === query);
|
|
877
|
+
if (!Object.isUndefined(first)) {
|
|
878
|
+
const own = new QueryCanceledException();
|
|
879
|
+
first.own = own;
|
|
880
|
+
first.write = () => {
|
|
881
|
+
throw own;
|
|
882
|
+
};
|
|
883
|
+
}
|
|
884
|
+
return Promise.resolve();
|
|
885
|
+
}
|
|
886
|
+
isIdle() {
|
|
887
|
+
return this.sent.length === 0 && this.pending.length === 0;
|
|
888
|
+
}
|
|
889
|
+
terminate() {
|
|
890
|
+
if (!Object.isNull(this.closedError)) {
|
|
891
|
+
this.onended?.();
|
|
892
|
+
return;
|
|
893
|
+
}
|
|
894
|
+
this.flush();
|
|
895
|
+
this.socket.end(this.frontend.terminate());
|
|
896
|
+
}
|
|
897
|
+
onclose(error) {
|
|
898
|
+
this.out.take();
|
|
899
|
+
this.blocker = null;
|
|
900
|
+
this.fence = null;
|
|
901
|
+
this.notables = 0;
|
|
902
|
+
const head = this.sent[0];
|
|
903
|
+
const failed = head?.error instanceof PostgreSQLException && head.error.severity === Resources.fatalSeverity ? head.error : null;
|
|
904
|
+
const fatal = this.fatal ?? failed;
|
|
905
|
+
this.uncertain = !Object.isNull(fatal) && !Object.isUndefined(head) && head.ends;
|
|
906
|
+
const closed = new ConnectionClosedException(this.closing, this.host, this.port, fatal ?? error);
|
|
907
|
+
this.closedError = closed;
|
|
908
|
+
for (const item of [...this.sent, ...this.pending])
|
|
909
|
+
item.cancel?.resolve();
|
|
910
|
+
for (const item of this.sent.splice(0)) {
|
|
911
|
+
item.stream?.destroy(closed);
|
|
912
|
+
if (!Object.isNull(item.own))
|
|
913
|
+
this.settle(item);
|
|
914
|
+
else
|
|
915
|
+
this.settleError(item, failed ?? closed);
|
|
916
|
+
}
|
|
917
|
+
for (const item of this.pending.splice(0)) {
|
|
918
|
+
if (!Object.isNull(item.own))
|
|
919
|
+
this.settle(item);
|
|
920
|
+
else
|
|
921
|
+
this.settleError(item, closed);
|
|
922
|
+
}
|
|
923
|
+
for (const query of this.running)
|
|
924
|
+
if (!query.settled)
|
|
925
|
+
query.reject(closed);
|
|
926
|
+
this.running.clear();
|
|
927
|
+
this.open.clear();
|
|
928
|
+
this.onended?.();
|
|
929
|
+
this.events.onClose(this, closed);
|
|
930
|
+
}
|
|
931
|
+
settleError(exchange, error) {
|
|
932
|
+
this.markAnswered(exchange);
|
|
933
|
+
if (exchange.kind === ExchangeKind.Describe)
|
|
934
|
+
exchange.reject?.(error);
|
|
935
|
+
else if (!Object.isNull(exchange.query) && !exchange.query.settled)
|
|
936
|
+
exchange.query.reject(error);
|
|
937
|
+
}
|
|
938
|
+
logDebug(text, values, oids) {
|
|
939
|
+
const debug = this.options.debug;
|
|
940
|
+
if (!Object.isBoolean(debug))
|
|
941
|
+
debug(this.id, text, values, oids);
|
|
942
|
+
}
|
|
943
|
+
handleFrame(type, data, start, end) {
|
|
944
|
+
if (!this.events.typed && Object.isNull(this.stashData) && (type === BackendMessage.RowDescription || type === BackendMessage.ParameterDescription)
|
|
945
|
+
&& this.events.hasUnknownTypes(type === BackendMessage.RowDescription ? Backend.readColumnTypes(data, start) : Backend.readParameterDescription(data, start))) {
|
|
946
|
+
this.stashType = type;
|
|
947
|
+
this.stashData = Buffer.from(data.subarray(start, end));
|
|
948
|
+
this.frames.pause();
|
|
949
|
+
this.events.requestTypes(this);
|
|
950
|
+
return;
|
|
951
|
+
}
|
|
952
|
+
this.dispatch(type, data, start, end);
|
|
953
|
+
}
|
|
954
|
+
dispatch(type, data, start, end) {
|
|
955
|
+
try {
|
|
956
|
+
this.handleMessage(type, data, start, end);
|
|
957
|
+
}
|
|
958
|
+
catch (error) {
|
|
959
|
+
const head = this.sent[0];
|
|
960
|
+
if (Object.isUndefined(head))
|
|
961
|
+
this.destroy(PostgreSQLConnection.toError(error));
|
|
962
|
+
else
|
|
963
|
+
this.fail(head, PostgreSQLConnection.toError(error));
|
|
964
|
+
}
|
|
965
|
+
if (!Object.isNull(this.blocker) && !this.blocker.isHolding()) {
|
|
966
|
+
this.blocker = null;
|
|
967
|
+
this.release();
|
|
968
|
+
}
|
|
969
|
+
if (this.out.length > 0)
|
|
970
|
+
this.schedule();
|
|
971
|
+
}
|
|
972
|
+
handleMessage(type, data, start, end) {
|
|
973
|
+
switch (type) {
|
|
974
|
+
case BackendMessage.DataRow:
|
|
975
|
+
this.handleDataRow(data, start);
|
|
976
|
+
break;
|
|
977
|
+
case BackendMessage.CopyData:
|
|
978
|
+
this.copyData(data, start, end);
|
|
979
|
+
break;
|
|
980
|
+
case BackendMessage.ReadyForQuery:
|
|
981
|
+
this.handleReadyForQuery(data, start);
|
|
982
|
+
break;
|
|
983
|
+
case BackendMessage.CommandComplete:
|
|
984
|
+
this.handleCommandComplete(data, start, end);
|
|
985
|
+
break;
|
|
986
|
+
case BackendMessage.BindComplete:
|
|
987
|
+
this.bindComplete();
|
|
988
|
+
break;
|
|
989
|
+
case BackendMessage.ParseComplete:
|
|
990
|
+
this.parseComplete();
|
|
991
|
+
break;
|
|
992
|
+
case BackendMessage.ParameterDescription:
|
|
993
|
+
this.handleParameterDescription(data, start);
|
|
994
|
+
break;
|
|
995
|
+
case BackendMessage.RowDescription:
|
|
996
|
+
this.receiveColumns(this.peekHead(), Backend.readColumns(data, start, (name, table, number, oid) => this.createColumn(name, table, number, oid)));
|
|
997
|
+
break;
|
|
998
|
+
case BackendMessage.NoData:
|
|
999
|
+
this.receiveColumns(this.peekHead(), []);
|
|
1000
|
+
break;
|
|
1001
|
+
case BackendMessage.ErrorResponse:
|
|
1002
|
+
this.handleErrorResponse(data, start, end);
|
|
1003
|
+
break;
|
|
1004
|
+
case BackendMessage.PortalSuspended:
|
|
1005
|
+
this.handlePortalSuspended();
|
|
1006
|
+
break;
|
|
1007
|
+
case BackendMessage.EmptyQueryResponse:
|
|
1008
|
+
this.handleEmptyQueryResponse();
|
|
1009
|
+
break;
|
|
1010
|
+
case BackendMessage.NotificationResponse:
|
|
1011
|
+
this.handleNotificationResponse(data, start);
|
|
1012
|
+
break;
|
|
1013
|
+
case BackendMessage.NoticeResponse:
|
|
1014
|
+
this.handleNoticeResponse(data, start, end);
|
|
1015
|
+
break;
|
|
1016
|
+
case BackendMessage.ParameterStatus:
|
|
1017
|
+
this.handleParameterStatus(data, start);
|
|
1018
|
+
break;
|
|
1019
|
+
case BackendMessage.CopyInResponse:
|
|
1020
|
+
this.copyInResponse();
|
|
1021
|
+
break;
|
|
1022
|
+
case BackendMessage.CopyOutResponse:
|
|
1023
|
+
this.copyOutResponse();
|
|
1024
|
+
break;
|
|
1025
|
+
case BackendMessage.CopyBothResponse:
|
|
1026
|
+
this.copyBothResponse();
|
|
1027
|
+
break;
|
|
1028
|
+
case BackendMessage.CopyDone:
|
|
1029
|
+
this.copyDone();
|
|
1030
|
+
break;
|
|
1031
|
+
case BackendMessage.FunctionCallResponse:
|
|
1032
|
+
case BackendMessage.NegotiateProtocolVersion:
|
|
1033
|
+
this.rejectUnsupported(String(BackendMessage[type]));
|
|
1034
|
+
break;
|
|
1035
|
+
default:
|
|
1036
|
+
break;
|
|
1037
|
+
}
|
|
1038
|
+
}
|
|
1039
|
+
fail(exchange, error) {
|
|
1040
|
+
exchange.error ??= error;
|
|
1041
|
+
exchange.describing = false;
|
|
1042
|
+
exchange.open = false;
|
|
1043
|
+
if (Object.isNull(exchange.batch))
|
|
1044
|
+
this.sync(exchange);
|
|
1045
|
+
else
|
|
1046
|
+
this.abort(exchange);
|
|
1047
|
+
}
|
|
1048
|
+
rejectUnsupported(name) {
|
|
1049
|
+
const head = this.sent[0];
|
|
1050
|
+
if (!Object.isUndefined(head))
|
|
1051
|
+
this.fail(head, new DataException(Resources.formatMessageNotSupported(name)));
|
|
1052
|
+
}
|
|
1053
|
+
parseComplete() {
|
|
1054
|
+
const exchange = this.peekHead();
|
|
1055
|
+
const name = exchange.parses.shift();
|
|
1056
|
+
const entry = exchange.entry;
|
|
1057
|
+
if (!Object.isUndefined(name) && name.length > 0 && !Object.isNull(entry) && entry.name === name && entry.owner === exchange
|
|
1058
|
+
&& entry.state === PreparedState.Parsing)
|
|
1059
|
+
entry.state = PreparedState.Ready;
|
|
1060
|
+
}
|
|
1061
|
+
bindComplete() {
|
|
1062
|
+
const exchange = this.peekHead();
|
|
1063
|
+
exchange.bound = true;
|
|
1064
|
+
if (Object.isNull(exchange.columns) && !Object.isNull(exchange.entry)) {
|
|
1065
|
+
exchange.columns = exchange.entry.columns;
|
|
1066
|
+
exchange.result.columns = exchange.columns;
|
|
1067
|
+
}
|
|
1068
|
+
}
|
|
1069
|
+
handleParameterDescription(data, start) {
|
|
1070
|
+
const exchange = this.peekHead();
|
|
1071
|
+
const params = Backend.readParameterDescription(data, start);
|
|
1072
|
+
exchange.params = params;
|
|
1073
|
+
const entry = exchange.entry;
|
|
1074
|
+
if (exchange.loose && !Object.isNull(entry) && entry.owner === exchange && Object.isNull(entry.oids))
|
|
1075
|
+
entry.oids = params;
|
|
1076
|
+
this.logLateExchange(exchange, params);
|
|
1077
|
+
}
|
|
1078
|
+
receiveColumns(exchange, columns) {
|
|
1079
|
+
exchange.columns = columns;
|
|
1080
|
+
exchange.result.columns = columns;
|
|
1081
|
+
if (exchange.learn) {
|
|
1082
|
+
exchange.learn = false;
|
|
1083
|
+
const entry = exchange.entry;
|
|
1084
|
+
if (!Object.isNull(entry) && entry.owner === exchange) {
|
|
1085
|
+
entry.columns = columns;
|
|
1086
|
+
PostgreSQLConnection.recordShape(entry, PostgreSQLConnection.readStatement(exchange), columns);
|
|
1087
|
+
}
|
|
1088
|
+
}
|
|
1089
|
+
if (exchange.describing)
|
|
1090
|
+
this.handleDescription(exchange, columns);
|
|
1091
|
+
}
|
|
1092
|
+
createColumn(name, table, number, type) {
|
|
1093
|
+
const parser = this.events.findParser(type);
|
|
1094
|
+
const columnFrom = this.options.transform.columnFrom;
|
|
1095
|
+
return new Column(Object.isNull(columnFrom) ? name : columnFrom(name), parser, this.events.findDecoder(type, parser), table, number, type);
|
|
1096
|
+
}
|
|
1097
|
+
handleDataRow(data, start) {
|
|
1098
|
+
const exchange = this.peekHead();
|
|
1099
|
+
const query = PostgreSQLConnection.queryOf(exchange);
|
|
1100
|
+
const mode = query.rowMode;
|
|
1101
|
+
const transform = this.options.transform;
|
|
1102
|
+
const columns = exchange.columns;
|
|
1103
|
+
if (!Object.isNull(columns) && exchange.shaped !== columns) {
|
|
1104
|
+
exchange.shaped = columns;
|
|
1105
|
+
exchange.reader = !Object.isNull(query.ownParsers) && mode !== RowMode.Raw ? RowReader.readColumns(columns, query.ownParsers) : columns;
|
|
1106
|
+
}
|
|
1107
|
+
const row = RowReader.read(data, start, exchange.reader, mode, mode === RowMode.Raw ? null : transform.valueFrom);
|
|
1108
|
+
const output = Object.isNull(transform.rowFrom) ? row : transform.rowFrom(row);
|
|
1109
|
+
if (Object.isNull(query.forEachFn))
|
|
1110
|
+
exchange.result.push(output);
|
|
1111
|
+
else
|
|
1112
|
+
query.forEachFn(output, exchange.result);
|
|
1113
|
+
}
|
|
1114
|
+
handleCommandComplete(data, start, end) {
|
|
1115
|
+
const exchange = this.peekHead();
|
|
1116
|
+
const complete = Backend.readCommandComplete(data, start, end);
|
|
1117
|
+
const result = exchange.result;
|
|
1118
|
+
result.command = complete.command;
|
|
1119
|
+
result.count = complete.count;
|
|
1120
|
+
result.state = this.backend;
|
|
1121
|
+
result.statement = exchange;
|
|
1122
|
+
if (!Object.isNull(exchange.query))
|
|
1123
|
+
exchange.query.source = exchange;
|
|
1124
|
+
result.columns ??= exchange.columns;
|
|
1125
|
+
const final = exchange.final;
|
|
1126
|
+
if (!Object.isNull(final)) {
|
|
1127
|
+
if (!Object.isNull(exchange.stream)) {
|
|
1128
|
+
exchange.stream.count = complete.count;
|
|
1129
|
+
exchange.stream.command = complete.command;
|
|
1130
|
+
}
|
|
1131
|
+
exchange.final = null;
|
|
1132
|
+
final();
|
|
1133
|
+
}
|
|
1134
|
+
if (exchange.kind === ExchangeKind.Simple) {
|
|
1135
|
+
exchange.results.push(exchange.result);
|
|
1136
|
+
exchange.result = new Result();
|
|
1137
|
+
return;
|
|
1138
|
+
}
|
|
1139
|
+
exchange.completed = true;
|
|
1140
|
+
if (exchange.rows > 0) {
|
|
1141
|
+
exchange.finished = true;
|
|
1142
|
+
if (exchange.flushing)
|
|
1143
|
+
this.sync(exchange);
|
|
1144
|
+
const cursorFn = exchange.query?.cursorFn;
|
|
1145
|
+
if (result.count && !Object.isNullOrUndefined(cursorFn)) {
|
|
1146
|
+
const last = new Promise(resolve => resolve(cursorFn(result)));
|
|
1147
|
+
last.catch(() => undefined);
|
|
1148
|
+
exchange.last = last;
|
|
1149
|
+
}
|
|
1150
|
+
}
|
|
1151
|
+
if (exchange.nosync && Object.isNull(exchange.error))
|
|
1152
|
+
this.completeExchange(exchange);
|
|
1153
|
+
}
|
|
1154
|
+
markUnsafe(exchange) {
|
|
1155
|
+
this.rolling = true;
|
|
1156
|
+
exchange.own = new UnsafeTransactionException(Resources.unsafeTransaction);
|
|
1157
|
+
for (const item of this.sent)
|
|
1158
|
+
if (item.kind !== ExchangeKind.Local)
|
|
1159
|
+
item.own = new UnsafeTransactionException(Resources.unsafeTransaction);
|
|
1160
|
+
this.pending.unshift(new Exchange(ExchangeKind.Internal, null, null, item => {
|
|
1161
|
+
Frontend.writeQuery(this.out, Resources.rollback);
|
|
1162
|
+
item.synced = true;
|
|
1163
|
+
return WriteOutcome.Written;
|
|
1164
|
+
}));
|
|
1165
|
+
}
|
|
1166
|
+
handleEmptyQueryResponse() {
|
|
1167
|
+
const exchange = this.peekHead();
|
|
1168
|
+
if (exchange.kind === ExchangeKind.Simple) {
|
|
1169
|
+
exchange.results.push(exchange.result);
|
|
1170
|
+
exchange.result = new Result();
|
|
1171
|
+
}
|
|
1172
|
+
if (exchange.nosync && Object.isNull(exchange.error))
|
|
1173
|
+
this.completeExchange(exchange);
|
|
1174
|
+
}
|
|
1175
|
+
handlePortalSuspended() {
|
|
1176
|
+
const exchange = this.peekHead();
|
|
1177
|
+
if (!exchange.flushing) {
|
|
1178
|
+
exchange.suspended = true;
|
|
1179
|
+
return;
|
|
1180
|
+
}
|
|
1181
|
+
void this.continueCursorAsync(exchange);
|
|
1182
|
+
}
|
|
1183
|
+
async continueCursorAsync(exchange) {
|
|
1184
|
+
const query = PostgreSQLConnection.queryOf(exchange);
|
|
1185
|
+
try {
|
|
1186
|
+
exchange.awaiting = true;
|
|
1187
|
+
const next = await query.callCursor(exchange.result);
|
|
1188
|
+
exchange.awaiting = false;
|
|
1189
|
+
exchange.result = new Result();
|
|
1190
|
+
if (!Object.isNull(this.closedError) || exchange.synced || this.sent[0] !== exchange)
|
|
1191
|
+
return;
|
|
1192
|
+
if (!Object.isNull(query.aborted))
|
|
1193
|
+
throw query.aborted;
|
|
1194
|
+
if (next === CursorAction.Close) {
|
|
1195
|
+
Frontend.writeClose(this.out, MessageTarget.Portal, "");
|
|
1196
|
+
this.sync(exchange);
|
|
1197
|
+
}
|
|
1198
|
+
else {
|
|
1199
|
+
Frontend.writeExecute(this.out, "", exchange.rows);
|
|
1200
|
+
Frontend.writeFlush(this.out);
|
|
1201
|
+
this.schedule();
|
|
1202
|
+
}
|
|
1203
|
+
}
|
|
1204
|
+
catch (error) {
|
|
1205
|
+
exchange.awaiting = false;
|
|
1206
|
+
if (!Object.isNull(this.closedError) || exchange.synced || this.sent[0] !== exchange)
|
|
1207
|
+
return;
|
|
1208
|
+
this.fail(exchange, PostgreSQLConnection.toError(error));
|
|
1209
|
+
}
|
|
1210
|
+
this.refresh();
|
|
1211
|
+
}
|
|
1212
|
+
handleErrorResponse(data, start, end) {
|
|
1213
|
+
const exchange = this.sent[0];
|
|
1214
|
+
const error = new PostgreSQLException(Backend.readError(data, start, end));
|
|
1215
|
+
if (Object.isUndefined(exchange)) {
|
|
1216
|
+
this.fatal = error;
|
|
1217
|
+
return;
|
|
1218
|
+
}
|
|
1219
|
+
exchange.stream?.destroy(error);
|
|
1220
|
+
const final = exchange.final;
|
|
1221
|
+
if (!Object.isNull(final)) {
|
|
1222
|
+
exchange.final = null;
|
|
1223
|
+
final(error);
|
|
1224
|
+
}
|
|
1225
|
+
this.fail(exchange, error);
|
|
1226
|
+
}
|
|
1227
|
+
handleReadyForQuery(data, start) {
|
|
1228
|
+
this.currentStatus = Backend.readReadyForQuery(data, start);
|
|
1229
|
+
this.frames.setOwning(false);
|
|
1230
|
+
const exchange = this.sent.shift();
|
|
1231
|
+
if (Object.isUndefined(exchange))
|
|
1232
|
+
throw new DataException(Resources.unexpectedMessage);
|
|
1233
|
+
if (this.options.scheduler)
|
|
1234
|
+
this.recordTiming(exchange);
|
|
1235
|
+
exchange.done = true;
|
|
1236
|
+
exchange.open = false;
|
|
1237
|
+
if (exchange.kind === ExchangeKind.Internal)
|
|
1238
|
+
this.rolling = false;
|
|
1239
|
+
if (this.currentStatus !== TransactionStatus.Idle && !this.rolling && Object.isNull(exchange.lease) && Object.isNull(this.lease) && this.options.max !== 1)
|
|
1240
|
+
this.markUnsafe(exchange);
|
|
1241
|
+
if (this.blocker === exchange)
|
|
1242
|
+
this.blocker = null;
|
|
1243
|
+
this.settle(exchange);
|
|
1244
|
+
if (exchange.entry?.owner === exchange)
|
|
1245
|
+
exchange.entry.owner = null;
|
|
1246
|
+
for (let next = this.sent[0]; next?.kind === ExchangeKind.Local; next = this.sent[0]) {
|
|
1247
|
+
this.sent.shift();
|
|
1248
|
+
this.settle(next);
|
|
1249
|
+
}
|
|
1250
|
+
this.release();
|
|
1251
|
+
this.activateHead();
|
|
1252
|
+
if (this.closing && this.isIdle())
|
|
1253
|
+
this.terminate();
|
|
1254
|
+
this.events.onReady(this);
|
|
1255
|
+
}
|
|
1256
|
+
activateHead() {
|
|
1257
|
+
const next = this.sent[0];
|
|
1258
|
+
if (Object.isUndefined(next) || Object.isNull(next.query))
|
|
1259
|
+
return;
|
|
1260
|
+
next.query.active = true;
|
|
1261
|
+
if (!Object.isNull(next.cancel))
|
|
1262
|
+
this.turn(next, next.cancel);
|
|
1263
|
+
}
|
|
1264
|
+
settle(exchange) {
|
|
1265
|
+
this.markAnswered(exchange);
|
|
1266
|
+
if (exchange.kind === ExchangeKind.Internal)
|
|
1267
|
+
return;
|
|
1268
|
+
if (exchange.kind === ExchangeKind.Describe) {
|
|
1269
|
+
if (Object.isNull(exchange.error))
|
|
1270
|
+
exchange.resolve?.(new StatementShape(exchange.params ?? [], exchange.columns));
|
|
1271
|
+
else
|
|
1272
|
+
exchange.reject?.(exchange.error);
|
|
1273
|
+
return;
|
|
1274
|
+
}
|
|
1275
|
+
const query = PostgreSQLConnection.queryOf(exchange);
|
|
1276
|
+
const cursor = exchange.origin ?? (exchange.portal.length > 0 ? exchange : null);
|
|
1277
|
+
if (!Object.isNull(cursor) && (!Object.isNull(exchange.own) || !Object.isNull(exchange.error) || !exchange.suspended))
|
|
1278
|
+
this.unfence(cursor);
|
|
1279
|
+
if (!Object.isNull(exchange.own)) {
|
|
1280
|
+
query.reject(exchange.own);
|
|
1281
|
+
return;
|
|
1282
|
+
}
|
|
1283
|
+
if (!Object.isNull(exchange.error)) {
|
|
1284
|
+
this.handleFailure(exchange, exchange.error);
|
|
1285
|
+
return;
|
|
1286
|
+
}
|
|
1287
|
+
if (exchange.suspended) {
|
|
1288
|
+
this.handleSuspension(exchange, query);
|
|
1289
|
+
return;
|
|
1290
|
+
}
|
|
1291
|
+
const last = exchange.last;
|
|
1292
|
+
if (Object.isNull(last)) {
|
|
1293
|
+
if (!query.settled)
|
|
1294
|
+
this.finish(exchange, query);
|
|
1295
|
+
return;
|
|
1296
|
+
}
|
|
1297
|
+
last.then(() => {
|
|
1298
|
+
if (!query.settled)
|
|
1299
|
+
this.finish(exchange, query);
|
|
1300
|
+
}, (error) => {
|
|
1301
|
+
if (!query.settled)
|
|
1302
|
+
query.reject(PostgreSQLConnection.toError(error));
|
|
1303
|
+
});
|
|
1304
|
+
}
|
|
1305
|
+
unfence(cursor) {
|
|
1306
|
+
if (!this.open.delete(cursor) || this.open.size > 0 || Object.isNull(this.fence))
|
|
1307
|
+
return;
|
|
1308
|
+
this.fence = null;
|
|
1309
|
+
this.release();
|
|
1310
|
+
}
|
|
1311
|
+
handleFailure(exchange, error) {
|
|
1312
|
+
const entry = exchange.kind === ExchangeKind.Query ? exchange.entry : null;
|
|
1313
|
+
const parsing = !Object.isNull(exchange.fresh) || (!Object.isNull(entry) && entry.owner === exchange && entry.state === PreparedState.Parsing);
|
|
1314
|
+
const code = error instanceof PostgreSQLException ? error.sqlState : null;
|
|
1315
|
+
const statement = exchange.statement;
|
|
1316
|
+
if (code === Resources.duplicatePreparedStatementState && exchange.kind === ExchangeKind.Query && !Object.isNull(statement) && statement.name.length > 0
|
|
1317
|
+
&& parsing) {
|
|
1318
|
+
if (!Object.isNull(entry))
|
|
1319
|
+
this.drop(entry);
|
|
1320
|
+
statement.name += Resources.statementNameSuffix;
|
|
1321
|
+
if (this.isRetryable(exchange))
|
|
1322
|
+
this.retryExchange(exchange, null);
|
|
1323
|
+
else
|
|
1324
|
+
this.settleError(exchange, error);
|
|
1325
|
+
return;
|
|
1326
|
+
}
|
|
1327
|
+
if (Object.isNull(entry)) {
|
|
1328
|
+
this.settleError(exchange, error);
|
|
1329
|
+
return;
|
|
1330
|
+
}
|
|
1331
|
+
if (entry.owner === exchange && entry.state === PreparedState.Parsing) {
|
|
1332
|
+
this.drop(entry);
|
|
1333
|
+
this.settleError(exchange, error);
|
|
1334
|
+
return;
|
|
1335
|
+
}
|
|
1336
|
+
if (code === Resources.invalidStatementNameState) {
|
|
1337
|
+
this.drop(entry);
|
|
1338
|
+
if (this.isRetryable(exchange))
|
|
1339
|
+
this.retryExchange(exchange, null);
|
|
1340
|
+
else
|
|
1341
|
+
this.settleError(exchange, error);
|
|
1342
|
+
return;
|
|
1343
|
+
}
|
|
1344
|
+
if (exchange.bound) {
|
|
1345
|
+
this.settleError(exchange, error);
|
|
1346
|
+
return;
|
|
1347
|
+
}
|
|
1348
|
+
if (this.isRetryable(exchange)) {
|
|
1349
|
+
this.retryExchange(exchange, error);
|
|
1350
|
+
return;
|
|
1351
|
+
}
|
|
1352
|
+
this.markStale(entry);
|
|
1353
|
+
this.settleError(exchange, error);
|
|
1354
|
+
}
|
|
1355
|
+
markStale(entry) {
|
|
1356
|
+
if (this.prepared.get(entry.name) === entry)
|
|
1357
|
+
entry.state = PreparedState.Stale;
|
|
1358
|
+
}
|
|
1359
|
+
isRetryable(exchange) {
|
|
1360
|
+
const query = exchange.query;
|
|
1361
|
+
return !exchange.retried
|
|
1362
|
+
&& Object.isNull(exchange.batch)
|
|
1363
|
+
&& Object.isNull(exchange.cancel)
|
|
1364
|
+
&& !Object.isNull(query)
|
|
1365
|
+
&& !query.cancelled
|
|
1366
|
+
&& Object.isNull(query.cursorFn)
|
|
1367
|
+
&& !exchange.streaming
|
|
1368
|
+
&& exchange.lease === this.lease
|
|
1369
|
+
&& this.currentStatus === TransactionStatus.Idle
|
|
1370
|
+
&& !this.closing
|
|
1371
|
+
&& (this.sent.length === 0 || (Object.isNull(exchange.lease) && this.options.max !== 1));
|
|
1372
|
+
}
|
|
1373
|
+
retryExchange(exchange, original) {
|
|
1374
|
+
const retry = new Exchange(ExchangeKind.Query, exchange.query, exchange.lease, item => this.writeExtended(item));
|
|
1375
|
+
retry.statement = exchange.statement;
|
|
1376
|
+
retry.ends = exchange.ends;
|
|
1377
|
+
retry.retried = true;
|
|
1378
|
+
retry.original = original;
|
|
1379
|
+
retry.previous = exchange.entry;
|
|
1380
|
+
retry.cost = exchange.cost;
|
|
1381
|
+
if (this.sent.length > 0) {
|
|
1382
|
+
this.enqueue(retry);
|
|
1383
|
+
return;
|
|
1384
|
+
}
|
|
1385
|
+
this.pending.unshift(retry);
|
|
1386
|
+
this.release();
|
|
1387
|
+
}
|
|
1388
|
+
handleSuspension(exchange, query) {
|
|
1389
|
+
const cursor = exchange.origin ?? exchange;
|
|
1390
|
+
this.running.add(query);
|
|
1391
|
+
new Promise(resolve => resolve(query.callCursor(exchange.result))).then(next => {
|
|
1392
|
+
this.running.delete(query);
|
|
1393
|
+
if (query.settled) {
|
|
1394
|
+
this.unfence(cursor);
|
|
1395
|
+
return;
|
|
1396
|
+
}
|
|
1397
|
+
this.enqueue(this.step(cursor, next === CursorAction.Close || !Object.isNull(query.aborted), query.aborted));
|
|
1398
|
+
}, (error) => {
|
|
1399
|
+
this.running.delete(query);
|
|
1400
|
+
if (query.settled) {
|
|
1401
|
+
this.unfence(cursor);
|
|
1402
|
+
return;
|
|
1403
|
+
}
|
|
1404
|
+
this.enqueue(this.step(cursor, true, PostgreSQLConnection.toError(error)));
|
|
1405
|
+
});
|
|
1406
|
+
}
|
|
1407
|
+
step(cursor, end, failure) {
|
|
1408
|
+
const exchange = new Exchange(ExchangeKind.Step, cursor.query, cursor.lease, item => this.writeCursorStep(item));
|
|
1409
|
+
exchange.origin = cursor;
|
|
1410
|
+
exchange.statement = cursor.statement;
|
|
1411
|
+
exchange.entry = cursor.entry;
|
|
1412
|
+
exchange.columns = cursor.columns;
|
|
1413
|
+
exchange.portal = cursor.portal;
|
|
1414
|
+
exchange.rows = cursor.rows;
|
|
1415
|
+
exchange.end = end;
|
|
1416
|
+
exchange.failure = failure;
|
|
1417
|
+
return exchange;
|
|
1418
|
+
}
|
|
1419
|
+
writeCursorStep(exchange) {
|
|
1420
|
+
const cursor = exchange.origin;
|
|
1421
|
+
if (Object.isNull(cursor) || this.lease !== cursor.lease || cursor.epoch !== this.epoch) {
|
|
1422
|
+
if (!Object.isNull(exchange.failure))
|
|
1423
|
+
throw exchange.failure;
|
|
1424
|
+
if (!exchange.end)
|
|
1425
|
+
throw new UnsafeTransactionException(Resources.cursorOutlived);
|
|
1426
|
+
return WriteOutcome.Skipped;
|
|
1427
|
+
}
|
|
1428
|
+
exchange.own = exchange.failure;
|
|
1429
|
+
if (exchange.end)
|
|
1430
|
+
Frontend.writeClose(this.out, MessageTarget.Portal, exchange.portal);
|
|
1431
|
+
else
|
|
1432
|
+
Frontend.writeExecute(this.out, exchange.portal, exchange.rows);
|
|
1433
|
+
this.sync(exchange);
|
|
1434
|
+
return WriteOutcome.Written;
|
|
1435
|
+
}
|
|
1436
|
+
finish(exchange, query) {
|
|
1437
|
+
if (exchange.kind !== ExchangeKind.Simple) {
|
|
1438
|
+
query.resolve(exchange.result);
|
|
1439
|
+
return;
|
|
1440
|
+
}
|
|
1441
|
+
const results = exchange.results;
|
|
1442
|
+
query.resolve(results.length > 1 ? results : results[0] ?? exchange.result);
|
|
1443
|
+
}
|
|
1444
|
+
handleNotificationResponse(data, start) {
|
|
1445
|
+
const notification = Backend.readNotification(data, start);
|
|
1446
|
+
PostgreSQLConnection.guard(() => this.events.onNotify(notification.channel, notification.payload, notification.pid));
|
|
1447
|
+
}
|
|
1448
|
+
handleNoticeResponse(data, start, end) {
|
|
1449
|
+
const notice = Backend.readError(data, start, end);
|
|
1450
|
+
const onnotice = this.options.onnotice;
|
|
1451
|
+
if (Object.isNull(onnotice))
|
|
1452
|
+
console.log(Object.fromEntries(notice));
|
|
1453
|
+
else if (onnotice !== false)
|
|
1454
|
+
PostgreSQLConnection.guard(() => onnotice(notice));
|
|
1455
|
+
}
|
|
1456
|
+
handleParameterStatus(data, start) {
|
|
1457
|
+
const [name, value] = Backend.readParameterStatus(data, start);
|
|
1458
|
+
this.parameters.set(name, value);
|
|
1459
|
+
PostgreSQLConnection.guard(() => this.events.onParameter(name, value));
|
|
1460
|
+
}
|
|
1461
|
+
copyInResponse() {
|
|
1462
|
+
const exchange = this.peekHead();
|
|
1463
|
+
const stream = new CopyInStream(this, exchange);
|
|
1464
|
+
exchange.stream = stream;
|
|
1465
|
+
PostgreSQLConnection.queryOf(exchange).resolve(stream);
|
|
1466
|
+
}
|
|
1467
|
+
copyOutResponse() {
|
|
1468
|
+
const exchange = this.peekHead();
|
|
1469
|
+
const stream = new CopyOutStream(this, exchange);
|
|
1470
|
+
exchange.stream = stream;
|
|
1471
|
+
exchange.open = false;
|
|
1472
|
+
this.frames.setOwning(true);
|
|
1473
|
+
PostgreSQLConnection.queryOf(exchange).resolve(stream);
|
|
1474
|
+
}
|
|
1475
|
+
copyBothResponse() {
|
|
1476
|
+
const exchange = this.peekHead();
|
|
1477
|
+
const stream = new CopyBothStream(this, exchange);
|
|
1478
|
+
exchange.stream = stream;
|
|
1479
|
+
this.frames.setOwning(true);
|
|
1480
|
+
PostgreSQLConnection.queryOf(exchange).resolve(stream);
|
|
1481
|
+
}
|
|
1482
|
+
copyData(data, start, end) {
|
|
1483
|
+
const exchange = this.peekHead();
|
|
1484
|
+
const stream = exchange.stream;
|
|
1485
|
+
if (!(stream instanceof Readable))
|
|
1486
|
+
return;
|
|
1487
|
+
exchange.wanted = false;
|
|
1488
|
+
if (!stream.push(data.subarray(start + 5, end)) && !exchange.wanted) {
|
|
1489
|
+
this.frames.pause();
|
|
1490
|
+
this.socket.pause();
|
|
1491
|
+
}
|
|
1492
|
+
}
|
|
1493
|
+
copyDone() {
|
|
1494
|
+
this.frames.setOwning(false);
|
|
1495
|
+
const exchange = this.peekHead();
|
|
1496
|
+
if (exchange.stream instanceof Readable)
|
|
1497
|
+
exchange.stream.push(null);
|
|
1498
|
+
exchange.stream = null;
|
|
1499
|
+
}
|
|
1500
|
+
peekHead() {
|
|
1501
|
+
const head = this.sent[0];
|
|
1502
|
+
if (Object.isUndefined(head))
|
|
1503
|
+
throw new DataException(Resources.unexpectedMessage);
|
|
1504
|
+
return head;
|
|
1505
|
+
}
|
|
1506
|
+
static recordShape(entry, statement, columns) {
|
|
1507
|
+
const shape = (entry.oids ?? []).join() + Resources.shapeSeparator + columns.map(column => column.type).join();
|
|
1508
|
+
if (statement.shape !== shape) {
|
|
1509
|
+
if (!Object.isNull(statement.shape))
|
|
1510
|
+
statement.version += 1;
|
|
1511
|
+
statement.shape = shape;
|
|
1512
|
+
}
|
|
1513
|
+
entry.version = statement.version;
|
|
1514
|
+
}
|
|
1515
|
+
static isUsable(entry, statement) {
|
|
1516
|
+
return entry.state === PreparedState.Parsing || (entry.state === PreparedState.Ready && entry.version === statement.version);
|
|
1517
|
+
}
|
|
1518
|
+
static isSameShape(entry, params, columns) {
|
|
1519
|
+
const oids = entry.oids ?? [];
|
|
1520
|
+
const known = entry.columns;
|
|
1521
|
+
return !Object.isNull(known)
|
|
1522
|
+
&& oids.length === params.length && params.every((oid, index) => oid === oids[index])
|
|
1523
|
+
&& known.length === columns.length && columns.every((column, index) => column.type === known[index]?.type);
|
|
1524
|
+
}
|
|
1525
|
+
static hasInferredTypes(built) {
|
|
1526
|
+
return built.oids.some(oid => !oid);
|
|
1527
|
+
}
|
|
1528
|
+
static isEnding(statement) {
|
|
1529
|
+
statement.ends ??= Resources.endingStatement.test(statement.text);
|
|
1530
|
+
return statement.ends;
|
|
1531
|
+
}
|
|
1532
|
+
static readWire(query) {
|
|
1533
|
+
if (Object.isNull(query.wire))
|
|
1534
|
+
throw new DataException(Resources.queryWithoutStatement);
|
|
1535
|
+
return query.wire;
|
|
1536
|
+
}
|
|
1537
|
+
static queryOf(exchange) {
|
|
1538
|
+
if (Object.isNull(exchange.query))
|
|
1539
|
+
throw new DataException(Resources.unexpectedMessage);
|
|
1540
|
+
return exchange.query;
|
|
1541
|
+
}
|
|
1542
|
+
static readStatement(exchange) {
|
|
1543
|
+
return PostgreSQLConnection.readWire(PostgreSQLConnection.queryOf(exchange)).statement;
|
|
1544
|
+
}
|
|
1545
|
+
static guard(action) {
|
|
1546
|
+
try {
|
|
1547
|
+
action();
|
|
1548
|
+
}
|
|
1549
|
+
catch {
|
|
1550
|
+
return;
|
|
1551
|
+
}
|
|
1552
|
+
}
|
|
1553
|
+
static toError(error) {
|
|
1554
|
+
return error instanceof Error ? error : new DataException(Resources.nonErrorThrown, new ExceptionOptions(error));
|
|
1555
|
+
}
|
|
1556
|
+
}
|
|
1557
|
+
//# sourceMappingURL=postgresql-connection.js.map
|