Skip to content

Commit

Permalink
HCK-7856: refactor getViewScript
Browse files Browse the repository at this point in the history
  • Loading branch information
Nightlngale committed Nov 25, 2024
1 parent 3db00ea commit 9618dd1
Show file tree
Hide file tree
Showing 3 changed files with 55 additions and 52 deletions.
2 changes: 2 additions & 0 deletions forward_engineering/ddlProvider/ddlTemplates.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
module.exports = {
createView:
'CREATE${orReplace}${global}${temporary} VIEW${ifNotExists} ${name}${columnList}${schemaBinding}${comment}${tablePropertyStatements}${query};${viewUnityTagsStatements}',
dropView: 'DROP VIEW IF EXISTS ${name};',

alterViewName: 'ALTER VIEW ${oldName} RENAME TO ${newName};',
Expand Down
90 changes: 38 additions & 52 deletions forward_engineering/helpers/viewHelper.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@
const { prepareName, encodeStringLiteral, commentDeactivatedStatement } = require('../utils/general');
const { getTablePropertiesClause } = require('./tableHelper');
const { getViewTagsStatement } = require('./unityTagsHelper');
const ddlTemplates = require('../ddlProvider/ddlTemplates');
const assignTemplates = require('../utils/assignTemplates');

const getColumnNames = _ => (collectionRefsDefinitionsMap, columns) => {
return _.uniq(
Expand Down Expand Up @@ -86,11 +88,11 @@ function joinColumnNames(statements) {
}

function getCommentStatement(comment) {
return comment ? `COMMENT '${encodeStringLiteral(comment)}'` : '';
return comment ? `\nCOMMENT '${encodeStringLiteral(comment)}'` : '';
}

function getDefaultColumnList(properties) {
return Object.entries(properties)
const list = Object.entries(properties)
.reduce((columnList, [name, property]) => {
columnList.push({
name: `${prepareName(name)}`,
Expand All @@ -104,11 +106,23 @@ function getDefaultColumnList(properties) {
return commentDeactivatedStatement(`${name} ${getCommentStatement(comment)}`, isActivated);
})
.join(',\n');

return list ? `\n(${list})` : '';
}

function getTableSelectStatement({ _, collectionRefsDefinitionsMap, columns }) {
const fromStatement = getFromStatement(_)(collectionRefsDefinitionsMap, columns);
const columnsNames = getColumnNames(_)(collectionRefsDefinitionsMap, columns);

if (fromStatement && columnsNames?.length) {
return `\nAS SELECT ${joinColumnNames(columnsNames)}\n${fromStatement}`;
}

return '';
}

module.exports = {
getViewScript({ _, schema, viewData, containerData, collectionRefsDefinitionsMap }) {
let script = [];
const columns = schema.properties || {};
const view = _.first(viewData) || {};

Expand All @@ -123,62 +137,34 @@ module.exports = {
const orReplace = schema.viewOrReplace;
const ifNotExists = view.viewIfNotExist;
const name = bucketName ? `${bucketName}.${viewName}` : `${viewName}`;
const createStatement = `CREATE ${orReplace && !ifNotExists ? 'OR REPLACE ' : ''}${isGlobal ? 'GLOBAL ' : ''}${isTemporary ? 'TEMPORARY ' : ''}VIEW${ifNotExists ? ' IF NOT EXISTS' : ''} ${name}`;
const comment = schema.description;
let tablePropertyStatements = '';
const tableProperties =
schema.tableProperties && Array.isArray(schema.tableProperties)
? filterRedundantProperties(schema.tableProperties, ['transient_lastDdlTime'])
: [];
const viewUnityTagsStatements =
schema.unityViewTags && getViewTagsStatement({ viewSchema: schema, viewName: name });

if (tableProperties.length) {
tablePropertyStatements = ` TBLPROPERTIES (${getTablePropertiesClause(_)(tableProperties)})`;
}
script.push(createStatement);
const columnList = view.columnList ? ` (${view.columnList})` : ` (${getDefaultColumnList(columns)})`;
if (schema.selectStatement) {
return (
createStatement +
`${columnList} ${getCommentStatement(comment)} ${tablePropertyStatements} AS ${schema.selectStatement};\n\n${viewUnityTagsStatements}`
);
} else {
script.push(columnList);
}

if (_.isEmpty(columns)) {
return;
}

if (comment) {
script.push(getCommentStatement(comment));
}

if (tablePropertyStatements) {
script.push(tablePropertyStatements);
}

if (!_.isEmpty(columns)) {
const fromStatement = getFromStatement(_)(collectionRefsDefinitionsMap, columns);
const columnsNames = getColumnNames(_)(collectionRefsDefinitionsMap, columns);

if (fromStatement && columnsNames?.length) {
script.push(`AS SELECT ${joinColumnNames(columnsNames)}`);
script.push(fromStatement);
} else {
return;
}
}

if (viewUnityTagsStatements) {
script.push(';\n');
script.push(viewUnityTagsStatements);

return script.join('\n ');
}

return script.join('\n ') + ';\n\n\n\n\n';
return assignTemplates(ddlTemplates.createView, {
orReplace: orReplace && !ifNotExists ? ' OR REPLACE ' : '',
global: isGlobal ? 'GLOBAL ' : '',
temporary: isTemporary ? 'TEMPORARY ' : '',
ifNotExists: ifNotExists ? ' IF NOT EXISTS' : '',
name,
columnList: view.columnList ? `\n(${view.columnList})` : getDefaultColumnList(columns),
schemaBinding: '',
comment: getCommentStatement(schema.description),
tablePropertyStatements: tableProperties.length
? `\nTBLPROPERTIES (${getTablePropertiesClause(_)(tableProperties)})`
: '',
query: schema.selectStatement
? `\nAS ${schema.selectStatement}`
: getTableSelectStatement({
_,
collectionRefsDefinitionsMap,
columns,
}),
viewUnityTagsStatements: viewUnityTagsStatements ? `\n${viewUnityTagsStatements};` : '',
});
},
};

Expand Down
15 changes: 15 additions & 0 deletions forward_engineering/utils/assignTemplates.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
const template = (modifiers = '') => new RegExp('\\$\\{(.*?)}', modifiers);
const getAllTemplates = str => str.match(template('gi')) || [];
const parseTemplate = str => (str.match(template('i')) || [])[1];

const assignTemplates = (str, templates) => {
return getAllTemplates(str).reduce((result, item) => {
const templateName = parseTemplate(item);

return result.replace(item, () => {
return templates[templateName] || templates[templateName] === 0 ? templates[templateName] : '';
});
}, str);
};

module.exports = assignTemplates;

0 comments on commit 9618dd1

Please sign in to comment.