-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathpg_database_dialect.ts
192 lines (158 loc) · 6.8 KB
/
pg_database_dialect.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
/*
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 { DatabaseDialect, DatabaseType } from "../../../common/lib/database_dialect/database_dialect";
import { HostListProviderService } from "../../../common/lib/host_list_provider_service";
import { HostListProvider } from "../../../common/lib/host_list_provider/host_list_provider";
import { ConnectionStringHostListProvider } from "../../../common/lib/host_list_provider/connection_string_host_list_provider";
import { AwsWrapperError, UnsupportedMethodError } from "../../../common/lib/utils/errors";
import { DatabaseDialectCodes } from "../../../common/lib/database_dialect/database_dialect_codes";
import { TransactionIsolationLevel } from "../../../common/lib/utils/transaction_isolation_level";
import { ClientWrapper } from "../../../common/lib/client_wrapper";
import { FailoverRestriction } from "../../../common/lib/plugins/failover/failover_restriction";
import { ErrorHandler } from "../../../common/lib/error_handler";
import { PgErrorHandler } from "../pg_error_handler";
import { SessionState } from "../../../common/lib/session_state";
import { Messages } from "../../../common/lib/utils/messages";
export class PgDatabaseDialect implements DatabaseDialect {
protected dialectName: string = this.constructor.name;
protected defaultPort: number = 5432;
getDefaultPort(): number {
return this.defaultPort;
}
getDialectUpdateCandidates(): string[] {
return [DatabaseDialectCodes.RDS_MULTI_AZ_PG, DatabaseDialectCodes.AURORA_PG, DatabaseDialectCodes.RDS_PG];
}
getHostAliasQuery(): string {
return "SELECT CONCAT(inet_server_addr(), ':', inet_server_port())";
}
async getHostAliasAndParseResults(targetClient: ClientWrapper): Promise<string> {
return targetClient
.query(this.getHostAliasQuery())
.then((rows: any) => {
return rows.rows[0]["concat"];
})
.catch((error: any) => {
throw new AwsWrapperError("Unable to fetch host alias or could not parse results: ", error.message);
});
}
getServerVersionQuery(): string {
return "SELECT 'version', VERSION()";
}
getSetReadOnlyQuery(readOnly: boolean): string {
return `SET SESSION CHARACTERISTICS AS TRANSACTION READ ${readOnly ? "ONLY" : "WRITE"}`;
}
getSetAutoCommitQuery(autoCommit: boolean): string {
throw new UnsupportedMethodError(Messages.get("Client.methodNotSupported", "setAutoCommit"));
}
getSetTransactionIsolationQuery(level: TransactionIsolationLevel): string {
let transactionIsolationLevel: string;
switch (level) {
case TransactionIsolationLevel.TRANSACTION_READ_UNCOMMITTED:
transactionIsolationLevel = "READ UNCOMMITTED";
break;
case TransactionIsolationLevel.TRANSACTION_READ_COMMITTED:
transactionIsolationLevel = "READ COMMITTED";
break;
case TransactionIsolationLevel.TRANSACTION_REPEATABLE_READ:
transactionIsolationLevel = "REPEATABLE READ";
break;
case TransactionIsolationLevel.TRANSACTION_SERIALIZABLE:
transactionIsolationLevel = "SERIALIZABLE";
break;
default:
throw new AwsWrapperError(Messages.get("Client.invalidTransactionIsolationLevel", String(level)));
}
return `SET SESSION CHARACTERISTICS AS TRANSACTION ISOLATION LEVEL ${transactionIsolationLevel}`;
}
getSetCatalogQuery(catalog: string): string {
throw new UnsupportedMethodError(Messages.get("Client.methodNotSupported", "setCatalog"));
}
getSetSchemaQuery(schema: string): string {
return `SET search_path TO ${schema}`;
}
async isDialect(targetClient: ClientWrapper): Promise<boolean> {
return await targetClient
.query("SELECT 1 FROM pg_proc LIMIT 1")
.then((result: { rows: any }) => {
return !!result.rows[0];
})
.catch(() => {
return false;
});
}
getHostListProvider(props: Map<string, any>, originalUrl: string, hostListProviderService: HostListProviderService): HostListProvider {
return new ConnectionStringHostListProvider(props, originalUrl, this.getDefaultPort(), hostListProviderService);
}
getErrorHandler(): ErrorHandler {
return new PgErrorHandler();
}
async isClientValid(targetClient: ClientWrapper): Promise<boolean> {
try {
return await targetClient
.query("SELECT 1")
.then(() => {
return true;
})
.catch((error: any) => {
return false;
});
} catch (error: any) {
return false;
}
}
getDatabaseType(): DatabaseType {
return DatabaseType.POSTGRES;
}
getDialectName(): string {
return this.dialectName;
}
getFailoverRestrictions(): FailoverRestriction[] {
return [];
}
doesStatementSetAutoCommit(statement: string): boolean | undefined {
return undefined;
}
doesStatementSetCatalog(statement: string): string | undefined {
return undefined;
}
doesStatementSetReadOnly(statement: string): boolean | undefined {
if (statement.toLowerCase().includes("set session characteristics as transaction read only")) {
return true;
}
if (statement.toLowerCase().includes("set session characteristics as transaction read write")) {
return false;
}
return undefined;
}
doesStatementSetSchema(statement: string): string | undefined {
if (statement.toLowerCase().includes("set search_path to ")) {
return statement.split(" ")[3];
}
return undefined;
}
doesStatementSetTransactionIsolation(statement: string): TransactionIsolationLevel | undefined {
if (statement.toLowerCase().includes("set session characteristics as transaction isolation level read uncommitted")) {
return TransactionIsolationLevel.TRANSACTION_READ_COMMITTED;
}
if (statement.toLowerCase().includes("set session characteristics as transaction isolation level read committed")) {
return TransactionIsolationLevel.TRANSACTION_READ_COMMITTED;
}
if (statement.toLowerCase().includes("set session characteristics as transaction isolation level repeatable read")) {
return TransactionIsolationLevel.TRANSACTION_REPEATABLE_READ;
}
if (statement.toLowerCase().includes("set session characteristics as transaction isolation level serializable")) {
return TransactionIsolationLevel.TRANSACTION_SERIALIZABLE;
}
return undefined;
}
}