-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathfetchKeywords.ts
More file actions
106 lines (91 loc) · 2.59 KB
/
fetchKeywords.ts
File metadata and controls
106 lines (91 loc) · 2.59 KB
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
import {
ExecuteStatementCommand,
Field,
RDSDataClient
} from "@aws-sdk/client-rds-data";
import { Keywords } from './types';
import mysqlKeywords from './mysqlKeywords';
const replKeywords = new Set([
'.break',
'.clear',
'.editor',
'.exit',
'.help',
'.save'
]);
export default async function fetchKeywords(rdsDataClient: RDSDataClient, resourceArn: string, secretArn: string, database?: string): Promise<Keywords> {
let records: Field[][] | undefined;
try {
({ records } = await rdsDataClient.send(
new ExecuteStatementCommand({
resourceArn,
secretArn,
sql: 'show schemas'
})
));
} catch (err) {
console.warn(`Warning: Failed to query for schemas, autocomplete will be limited (${err.message})`);
}
let schemaNames: Set<string>;
if (records) {
schemaNames = new Set(records.map(record => record[0].stringValue as string));
} else {
schemaNames = new Set();
}
let objectNames: Set<string>;
const objectDotNames: Set<string> = new Set();
if (database) {
let records: Field[][] | undefined;
try {
({ records } = await rdsDataClient.send(
new ExecuteStatementCommand({
resourceArn,
secretArn,
sql: 'show tables',
database
})
));
} catch (err) {
console.warn(`Warning: Failed to query for tables, autocomplete will be limited (${err.message})`);
}
let tableNames: Set<string>;
if (records) {
tableNames = new Set(records.map(record => record[0].stringValue as string));
objectNames = new Set(tableNames);
} else {
tableNames = new Set();
objectNames = new Set();
}
for (const tableName of tableNames) {
let records: Field[][] | undefined;
try {
({ records } = await rdsDataClient.send(
new ExecuteStatementCommand({
resourceArn,
secretArn,
sql: `show columns from \`${tableName.replace(/`/g, '``')}\``,
database
})
));
} catch (err) {
console.warn(`Warning: Failed to query for columns from table '${tableName}', autocomplete will be limited (${err.message})`);
}
if (records) {
const columnNames = records.map(record => record[0].stringValue as string);
for (const columnName of columnNames) {
objectNames.add(columnName);
objectDotNames.add(`${tableName}.${columnName}`);
}
}
}
} else {
objectNames = new Set();
}
return {
replKeywords,
mysqlKeywords,
schemaNames,
objectNames,
objectDotNames
};
}