forked from deephaven/web-client-ui
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDbNameValidator.ts
More file actions
167 lines (145 loc) · 3.66 KB
/
DbNameValidator.ts
File metadata and controls
167 lines (145 loc) · 3.66 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
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
const TABLE_PREFIX = 'table_';
const COLUMN_PREFIX = 'column_';
// From io.deephaven.db.tables.utils.DBNameValidator#DB_RESERVED_VARIABLE_NAMES
const DB_RESERVED_VARIABLE_NAMES = new Set(['in', 'not', 'i', 'ii', 'k']);
// From javax.lang.model.SourceVersion#keywords
const JAVA_KEYWORDS = new Set([
'abstract',
'continue',
'for',
'new',
'switch',
'assert',
'default',
'if',
'package',
'synchronized',
'boolean',
'do',
'goto',
'private',
'this',
'break',
'double',
'implements',
'protected',
'throw',
'byte',
'else',
'import',
'public',
'throws',
'case',
'enum',
'instanceof',
'return',
'transient',
'catch',
'extends',
'int',
'short',
'try',
'char',
'final',
'interface',
'static',
'void',
'class',
'finally',
'long',
'strictfp',
'volatile',
'const',
'float',
'native',
'super',
'while',
'null',
'true',
'false',
]);
// From io.deephaven.db.tables.utils.DBNameValidator#STERILE_TABLE_AND_NAMESPACE_REGEX
const STERILE_TABLE_AND_NAMESPACE_REGEX = /[^a-zA-Z0-9_$\-+@]/g;
// From io.deephaven.db.tables.utils.DBNameValidator#STERILE_COLUMN_AND_QUERY_REGEX
const STERILE_COLUMN_AND_QUERY_REGEX = /[^A-Za-z0-9_$]/g;
// From io.deephaven.db.tables.utils.DBNameValidator#TABLE_NAME_PATTERN
const TABLE_NAME_PATTERN = /^[a-zA-Z_$][a-zA-Z0-9_$\-+@]*$/g;
function columnNameReplacer(input: string): string {
// Replace all dashes and spaces with underscores
return input.replace(/[ -]/g, '_');
}
function tableNameReplacer(input: string): string {
// Replace spaces with underscores
return input.replace(/\s/g, '_');
}
/**
* Similar to DBNameValidator.java, this class has utilities for validating and legalizing
* Table and Column names.
*/
class DbNameValidator {
static legalize = (
name: string,
replace: (input: string) => string,
prefix: string,
regex: RegExp,
checkReserved: boolean,
i: number
): string => {
let legalName = replace(name.trim());
// Add prefix to reserved names
if (
checkReserved &&
(DB_RESERVED_VARIABLE_NAMES.has(legalName) ||
JAVA_KEYWORDS.has(legalName))
) {
legalName = prefix + legalName;
}
// Remove illegal characters
legalName = legalName.replace(regex, '');
// Check if the name ended up blank
if (!legalName) {
legalName = prefix + i;
}
// If name starts with a number, append prefix to the front
if (!Number.isNaN(Number(legalName.charAt(0)))) {
legalName = prefix + legalName;
}
return legalName;
};
static legalizeTableName = (name: string): string =>
DbNameValidator.legalize(
name,
tableNameReplacer,
TABLE_PREFIX,
STERILE_TABLE_AND_NAMESPACE_REGEX,
false,
0
);
static legalizeColumnNames = (headers: string[]): string[] => {
const legalHeaders: string[] = [];
headers.forEach((header, i) => {
let legalHeader = DbNameValidator.legalizeColumnName(header, i);
// Check if the name is already in use
if (legalHeaders.includes(legalHeader)) {
legalHeader = COLUMN_PREFIX + i;
}
legalHeaders.push(legalHeader);
});
return legalHeaders;
};
static legalizeColumnName = (header: string, i = 0): string =>
// Replace all dashes and spaces with underscores
DbNameValidator.legalize(
header,
columnNameReplacer,
COLUMN_PREFIX,
STERILE_COLUMN_AND_QUERY_REGEX,
true,
i
);
static isValidTableName = (name: string): boolean =>
TABLE_NAME_PATTERN.test(name);
static isValidColumnName = (name: string): boolean =>
DbNameValidator.legalizeColumnName(name) === name;
}
export default DbNameValidator;