-
Notifications
You must be signed in to change notification settings - Fork 10
Expand file tree
/
Copy pathgeneral.js
More file actions
316 lines (261 loc) · 8.56 KB
/
Copy pathgeneral.js
File metadata and controls
316 lines (261 loc) · 8.56 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
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
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
/*
* Copyright © 2016-2021 by IntegrIT S.A. dba Hackolade. All rights reserved.
*
* The copyright to the computer software herein is the property of IntegrIT S.A.
* The software may be used and/or copied only with the written permission of
* IntegrIT S.A. or in accordance with the terms and conditions stipulated in
* the agreement/contract under which the software has been supplied.
*/
const _ = require('lodash');
const { AlterCollectionDto, AlterCollectionRoleDto } = require('../alterScript/types/AlterCollectionDto');
const { ReservedWordsAsArray } = require('../enums/reservedWords');
const MUST_BE_ESCAPED = /[\t\n'\f\r]/gm;
const getDbName = containerData => {
return _.get(containerData, '[0].code') || _.get(containerData, '[0].name', '');
};
const getEntityName = entityData => {
return (entityData && (entityData.code || entityData.collectionName || entityData.name)) || '';
};
const getViewName = view => {
return (view && (view.code || view.name)) || '';
};
const getDbData = containerData => {
return { ..._.get(containerData, '[0]', {}), name: getDbName(containerData) };
};
const getViewOn = viewData => _.get(viewData, '[0].viewOn');
const tab = (text, tab = '\t') => {
return text
.split('\n')
.map(line => tab + line)
.join('\n');
};
const hasType = (types, type) => {
return Object.keys(types).map(_.toLower).includes(_.toLower(type));
};
/**
* @param collection {AlterCollectionDto}
* @return {AlterCollectionDto & AlterCollectionRoleDto}
* */
const getSchemaOfAlterCollection = collection => {
return { ...collection, ...(_.omit(collection?.role, 'properties') || {}) };
};
/**
* @param collectionSchema {AlterCollectionDto & AlterCollectionRoleDto}
* @return {string}
* */
const getFullCollectionName = collectionSchema => {
const collectionName = getEntityName(collectionSchema);
const bucketName = collectionSchema.compMod?.keyspaceName;
return getNamePrefixedWithSchemaName(collectionName, bucketName);
};
const clean = obj =>
Object.entries(obj)
.filter(([name, value]) => !_.isNil(value))
.reduce(
(result, [name, value]) => ({
...result,
[name]: value,
}),
{},
);
const checkAllKeysActivated = keys => {
return keys.every(key => _.get(key, 'isActivated', true));
};
/**
* @param keys {Array<{
* isActivated: boolean,
* }>}
* */
const checkAllKeysDeactivated = keys => {
return keys.length ? keys.every(key => !_.get(key, 'isActivated', true)) : false;
};
const divideIntoActivatedAndDeactivated = (items, mapFunction) => {
const activatedItems = items.filter(item => _.get(item, 'isActivated', true)).map(mapFunction);
const deactivatedItems = items.filter(item => !_.get(item, 'isActivated', true)).map(mapFunction);
return { activatedItems, deactivatedItems };
};
const commentIfDeactivated = (statement, { isActivated, isPartOfLine, inlineComment = '--' }) => {
if (!statement) {
return '';
}
if (isActivated !== false) {
return statement;
}
if (isPartOfLine) {
return '/* ' + statement + ' */';
} else if (statement.includes('\n')) {
return '/*\n' + statement + ' */\n';
} else {
return inlineComment + ' ' + statement;
}
};
const wrap = (str, start = "'", end = "'") => {
const firstChar = str[0];
const lastChar = str[str.length - 1];
if (lastChar === start && firstChar === end) {
return str;
} else {
return `${start}${str}${end}`;
}
};
const addCommaPrefix = (string, shouldAddComma) => (shouldAddComma ? `,${string}` : string);
const checkFieldPropertiesChanged = (compMod, propertiesToCheck) => {
return propertiesToCheck.some(prop => compMod?.oldField[prop] !== compMod?.newField[prop]);
};
const getFullTableName = collection => {
const collectionSchema = { ...collection, ...(_.omit(collection?.role, 'properties') || {}) };
const tableName = getEntityName(collectionSchema);
const schemaName = getSchemaNameFromCollection({ collection: collectionSchema });
return getNamePrefixedWithSchemaName(tableName, schemaName);
};
const getSchemaNameFromCollection = ({ collection }) => {
return collection.compMod?.keyspaceName;
};
const getFullColumnName = (collection, columnName) => {
const fullTableName = getFullTableName(collection);
return `${fullTableName}.${wrapInQuotes(columnName)}`;
};
const getFullViewName = view => {
const viewSchema = { ...view, ...(_.omit(view?.role, 'properties') || {}) };
const viewName = getViewName(viewSchema);
const schemaName = viewSchema.compMod?.keyspaceName;
return getNamePrefixedWithSchemaName(viewName, schemaName);
};
/**
* @param udt {Object}
* @return {string}
* */
const getUdtName = udt => {
return udt.code || udt.name;
};
const getDbVersion = (dbVersion = '') => {
const version = dbVersion.match(/\d+/);
return Number(_.get(version, [0], 0));
};
const prepareComment = (comment = '') => comment.replace(MUST_BE_ESCAPED, character => `\\${character}`);
const wrapComment = comment => `E'${prepareComment(JSON.stringify(comment)).slice(1, -1)}'`;
const getFunctionArguments = functionArguments => {
return _.map(functionArguments, arg => {
const defaultExpression = arg.defaultExpression ? `DEFAULT ${arg.defaultExpression}` : '';
return _.trim(`${arg.argumentMode} ${arg.argumentName || ''} ${arg.argumentType} ${defaultExpression}`);
}).join(', ');
};
const getNamePrefixedWithSchemaName = (name, schemaName) => {
if (schemaName) {
return `${wrapInQuotes(schemaName)}.${wrapInQuotes(name)}`;
}
return wrapInQuotes(name);
};
const shouldNameBeEscaped = ({ name }) => /\s|\W/.test(name) || _.includes(ReservedWordsAsArray, _.toUpper(name));
const wrapInQuotes = name => (shouldNameBeEscaped({ name }) ? `"${name}"` : name);
const wrapInSingleQuotes = ({ name }) => (shouldNameBeEscaped({ name }) ? `'${name}'` : name);
const columnMapToString = ({ name }) => wrapInQuotes(name);
const getColumnsList = (columns, isAllColumnsDeactivated, isParentActivated, mapColumn = columnMapToString) => {
const dividedColumns = divideIntoActivatedAndDeactivated(columns, mapColumn);
const deactivatedColumnsAsString = dividedColumns?.deactivatedItems?.length
? commentIfDeactivated(dividedColumns.deactivatedItems.join(', '), {
isActivated: false,
isPartOfLine: true,
})
: '';
return !isAllColumnsDeactivated && isParentActivated
? ' (' + dividedColumns.activatedItems.join(', ') + deactivatedColumnsAsString + ')'
: ' (' + columns.map(mapColumn).join(', ') + ')';
};
const getKeyWithAlias = key => {
if (!key) {
return '';
}
if (key.alias) {
return `${wrapInQuotes(key.name)} as ${wrapInQuotes(key.alias)}`;
} else {
return wrapInQuotes(key.name);
}
};
const getViewData = keys => {
if (!Array.isArray(keys)) {
return { tables: [], columns: [] };
}
return keys.reduce(
(result, key) => {
if (!key.tableName) {
result.columns.push(getKeyWithAlias(key));
return result;
}
const tableName = `${wrapInQuotes(key.dbName)}.${wrapInQuotes(key.tableName)}`;
if (!result.tables.includes(tableName)) {
result.tables.push(tableName);
}
result.columns.push({
statement: `${tableName}.${getKeyWithAlias(key)}`,
isActivated: key.isActivated,
});
return result;
},
{
tables: [],
columns: [],
},
);
};
const getGroupItemsByCompMode = ({ newItems = [], oldItems = [] }) => {
const addedItems = newItems.filter(newItem => !oldItems.some(item => item.id === newItem.id));
const removedItems = [];
const modifiedItems = [];
oldItems.forEach(oldItem => {
const newItem = newItems.find(item => item.id === oldItem.id);
if (!newItem) {
removedItems.push(oldItem);
} else if (!_.isEqual(newItem, oldItem)) {
modifiedItems.push(newItem);
}
});
return {
added: addedItems,
removed: removedItems,
modified: modifiedItems,
};
};
const isObjectInDeltaModelActivated = modelObject => {
return modelObject.compMod?.isActivated?.new ?? modelObject.role?.isActivated;
};
const isParentContainerActivated = collection => {
return (
collection?.compMod?.bucketProperties?.isActivated ?? collection?.role?.compMod?.bucketProperties?.isActivated
);
};
module.exports = {
getDbName,
getDbData,
getEntityName,
getViewName,
getViewOn,
tab,
hasType,
clean,
checkAllKeysActivated,
checkAllKeysDeactivated,
divideIntoActivatedAndDeactivated,
commentIfDeactivated,
wrap,
checkFieldPropertiesChanged,
getFullTableName,
getFullColumnName,
getFullViewName,
getUdtName,
getDbVersion,
wrapComment,
getFunctionArguments,
getNamePrefixedWithSchemaName,
wrapInQuotes,
getColumnsList,
getViewData,
getSchemaOfAlterCollection,
getFullCollectionName,
getSchemaNameFromCollection,
getGroupItemsByCompMode,
wrapInSingleQuotes,
addCommaPrefix,
isObjectInDeltaModelActivated,
isParentContainerActivated,
};