-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathclient.ts
213 lines (188 loc) · 8.27 KB
/
client.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
/*
Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
Licensed under the Apache License, Version 2.0 (the "License").
You may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
import { QueryResult } from "pg";
import { AwsClient } from "../../common/lib/aws_client";
import { PgConnectionUrlParser } from "./pg_connection_url_parser";
import { DatabaseDialect, DatabaseType } from "../../common/lib/database_dialect/database_dialect";
import { DatabaseDialectCodes } from "../../common/lib/database_dialect/database_dialect_codes";
import { RdsPgDatabaseDialect } from "./dialect/rds_pg_database_dialect";
import { PgDatabaseDialect } from "./dialect/pg_database_dialect";
import { AuroraPgDatabaseDialect } from "./dialect/aurora_pg_database_dialect";
import { AwsWrapperError, UnsupportedMethodError } from "../../common/lib/utils/errors";
import { Messages } from "../../common/lib/utils/messages";
import { ClientWrapper } from "../../common/lib/client_wrapper";
import { RdsMultiAZPgDatabaseDialect } from "./dialect/rds_multi_az_pg_database_dialect";
import { HostInfo } from "../../common/lib/host_info";
import { TelemetryTraceLevel } from "../../common/lib/utils/telemetry/telemetry_trace_level";
import { NodePostgresDriverDialect } from "./dialect/node_postgres_driver_dialect";
import { TransactionIsolationLevel } from "../../common/lib/utils/transaction_isolation_level";
import { isDialectTopologyAware } from "../../common/lib/utils/utils";
export class AwsPGClient extends AwsClient {
private static readonly knownDialectsByCode: Map<string, DatabaseDialect> = new Map([
[DatabaseDialectCodes.PG, new PgDatabaseDialect()],
[DatabaseDialectCodes.RDS_PG, new RdsPgDatabaseDialect()],
[DatabaseDialectCodes.AURORA_PG, new AuroraPgDatabaseDialect()],
[DatabaseDialectCodes.RDS_MULTI_AZ_PG, new RdsMultiAZPgDatabaseDialect()]
]);
constructor(config: any) {
super(config, DatabaseType.POSTGRES, AwsPGClient.knownDialectsByCode, new PgConnectionUrlParser(), new NodePostgresDriverDialect());
}
async connect(): Promise<void> {
await this.internalConnect();
const context = this.telemetryFactory.openTelemetryContext("AwsClient.connect", TelemetryTraceLevel.TOP_LEVEL);
return await context.start(async () => {
const hostInfo = this.pluginService.getCurrentHostInfo();
if (hostInfo == null) {
throw new AwsWrapperError(Messages.get("HostInfo.noHostParameter"));
}
const result: ClientWrapper = await this.pluginManager.connect(hostInfo, this.properties, true);
if (isDialectTopologyAware(this.pluginService.getDialect())) {
try {
const role = await this.pluginService.getHostRole(result);
// The current host role may be incorrect, use the created client to confirm the host role.
if (role !== result.hostInfo.role) {
result.hostInfo.role = role;
this.pluginService.setCurrentHostInfo(result.hostInfo);
this.pluginService.setInitialConnectionHostInfo(result.hostInfo);
}
} catch (error) {
// Ignore
}
}
await this.pluginService.setCurrentClient(result, result.hostInfo);
await this.internalPostConnect();
});
}
async query(text: string): Promise<QueryResult> {
const context = this.telemetryFactory.openTelemetryContext("awsClient.query", TelemetryTraceLevel.TOP_LEVEL);
return await context.start(async () => {
return await this.pluginManager.execute(
this.pluginService.getCurrentHostInfo(),
this.properties,
"query",
async () => {
await this.pluginService.updateState(text);
return this.targetClient?.query(text);
},
text
);
});
}
private async queryWithoutUpdate(text: string): Promise<QueryResult> {
return this.pluginManager.execute(
this.pluginService.getCurrentHostInfo(),
this.properties,
"query",
async () => {
return this.targetClient?.client.query(text);
},
text
);
}
async setReadOnly(readOnly: boolean): Promise<QueryResult | void> {
this.pluginService.getSessionStateService().setupPristineReadOnly();
const result = await this.queryWithoutUpdate(`SET SESSION CHARACTERISTICS AS TRANSACTION READ ${readOnly ? "ONLY" : "WRITE"}`);
this.pluginService.getSessionStateService().updateReadOnly(readOnly);
return result;
}
isReadOnly(): boolean {
return this.pluginService.getSessionStateService().getReadOnly();
}
async setAutoCommit(autoCommit: boolean): Promise<QueryResult | void> {
throw new UnsupportedMethodError(Messages.get("Client.methodNotSupported", "setAutoCommit"));
}
getAutoCommit(): boolean {
throw new UnsupportedMethodError(Messages.get("Client.methodNotSupported", "getAutoCommit"));
}
async setTransactionIsolation(level: TransactionIsolationLevel): Promise<QueryResult | void> {
if (level == this.getTransactionIsolation()) {
return;
}
this.pluginService.getSessionStateService().setupPristineTransactionIsolation();
switch (level) {
case TransactionIsolationLevel.TRANSACTION_READ_UNCOMMITTED:
await this.queryWithoutUpdate("SET SESSION CHARACTERISTICS AS TRANSACTION ISOLATION LEVEL READ UNCOMMITTED");
break;
case TransactionIsolationLevel.TRANSACTION_READ_COMMITTED:
await this.queryWithoutUpdate("SET SESSION CHARACTERISTICS AS TRANSACTION ISOLATION LEVEL READ COMMITTED");
break;
case TransactionIsolationLevel.TRANSACTION_REPEATABLE_READ:
await this.queryWithoutUpdate("SET SESSION CHARACTERISTICS AS TRANSACTION ISOLATION LEVEL REPEATABLE READ");
break;
case TransactionIsolationLevel.TRANSACTION_SERIALIZABLE:
await this.queryWithoutUpdate("SET SESSION CHARACTERISTICS AS TRANSACTION ISOLATION LEVEL SERIALIZABLE");
break;
default:
throw new AwsWrapperError(Messages.get("Client.invalidTransactionIsolationLevel", String(level)));
}
this.pluginService.getSessionStateService().setTransactionIsolation(level);
}
getTransactionIsolation(): TransactionIsolationLevel {
return this.pluginService.getSessionStateService().getTransactionIsolation();
}
async setCatalog(catalog: string): Promise<void> {
throw new UnsupportedMethodError(Messages.get("Client.methodNotSupported", "setCatalog"));
}
getCatalog(): string {
throw new UnsupportedMethodError(Messages.get("Client.methodNotSupported", "getCatalog"));
}
async setSchema(schema: string): Promise<QueryResult | void> {
if (!schema) {
return;
}
if (schema === this.getSchema()) {
return;
}
this.pluginService.getSessionStateService().setupPristineSchema();
const result = await this.queryWithoutUpdate(`SET search_path TO ${schema};`);
this.pluginService.getSessionStateService().setSchema(schema);
return result;
}
getSchema(): string {
return this.pluginService.getSessionStateService().getSchema();
}
async end() {
if (!this.isConnected || !this.targetClient) {
// No connections have been initialized.
// This might happen if end is called in a finally block when an error occurred while initializing the first connection.
return;
}
const hostInfo: HostInfo | null = this.pluginService.getCurrentHostInfo();
return await this.pluginManager.execute(
hostInfo,
this.properties,
"end",
() => {
const res = this.targetClient!.end();
this.targetClient = null;
return res;
},
null
);
}
async rollback(): Promise<any> {
return this.pluginManager.execute(
this.pluginService.getCurrentHostInfo(),
this.properties,
"rollback",
async () => {
if (this.targetClient) {
this.pluginService.updateInTransaction("rollback");
return await this.targetClient.rollback();
}
return null;
},
null
);
}
}