Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
/**
* @typedef {Record<string, unknown>} ViewData
*/
const { getStorageParameters, getBasicValue } = require('./tableHelper');

/**
* @param {{ viewData: ViewData }}
* @returns {string}
*/
const getOptions = ({ viewData }) => {
const configs = [
{ key: 'usingMethod', getValue: getBasicValue('USING') },
{ key: 'storage_parameter', getValue: getStorageParameters },
{ key: 'view_tablespace_name', getValue: getBasicValue('TABLESPACE') },
];

const statements = configs
.map(config => config.getValue(viewData[config.key], viewData))
.filter(Boolean)
.join('\n')
.trim();

return statements ? ` ${statements}` : '';
};

/**
* @param {{ viewData: ViewData }}
* @returns {string}
*/
const getWithDataClause = ({ viewData }) => {
if (viewData.withDataOption) {
return '\nWITH DATA';
}

return '\nWITH NO DATA';
};

module.exports = {
getOptions,
getWithDataClause,
};
2 changes: 2 additions & 0 deletions forward_engineering/ddlProvider/ddlHelpers/tableHelper.js
Original file line number Diff line number Diff line change
Expand Up @@ -119,6 +119,8 @@ const getStorageParameters = value => {
};

module.exports = {
getBasicValue,
getTableTemporaryValue,
getTableOptions,
getStorageParameters,
};
23 changes: 23 additions & 0 deletions forward_engineering/ddlProvider/ddlProvider.js
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ const {
} = require('./ddlHelpers/columnDefinitionHelper');
const { getTriggersScript, hydrateTriggers } = require('./ddlHelpers/triggerHelper');
const { getLocaleProperties } = require('./ddlHelpers/databaseHelper');
const materializedViewHelper = require('./ddlHelpers/materializedViewHelper');

module.exports = (baseProvider, options, app) => {
return {
Expand Down Expand Up @@ -494,6 +495,22 @@ module.exports = (baseProvider, options, app) => {
}
};

if (viewData.materialized) {
const createViewScript = commentIfDeactivated(
assignTemplates(templates.createMaterializedView, {
name: viewName,
ifNotExist: viewData.ifNotExist ? ' IF NOT EXISTS' : '',
options: materializedViewHelper.getOptions({ viewData }),
comment: viewData.comment ? comment : '',
withDataClause: materializedViewHelper.getWithDataClause({ viewData }),
selectStatement,
}),
{ isActivated: !deactivatedWholeStatement },
);

return createViewScript + '\n';
}

const createViewScript = commentIfDeactivated(
assignTemplates(templates.createView, {
name: viewName,
Expand Down Expand Up @@ -741,6 +758,12 @@ module.exports = (baseProvider, options, app) => {
withCheckOption: detailsTab.withCheckOption,
checkTestingScope: detailsTab.withCheckOption ? detailsTab.checkTestingScope : '',
schemaName: viewData.schemaData.schemaName,
materialized: detailsTab.materialized,
ifNotExist: detailsTab.ifNotExist,
usingMethod: detailsTab.usingMethod,
storage_parameter: detailsTab.storage_parameter,
view_tablespace_name: detailsTab.view_tablespace_name,
withDataOption: detailsTab.withDataOption,
triggers,
};
},
Expand Down
3 changes: 3 additions & 0 deletions forward_engineering/ddlProvider/templates.js
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,9 @@ module.exports = {
createView:
'CREATE${orReplace}${temporary} VIEW ${name}${withOptions}\nAS ${selectStatement}${checkOption};\n\n${comment}\n',

createMaterializedView:
'CREATE MATERIALIZED VIEW${ifNotExist} ${name}${options}\nAS ${selectStatement}${withDataClause};\n\n${comment}\n',

viewSelectStatement: 'SELECT ${keys}\n\tFROM ${tableName}',

dropView: 'DROP VIEW IF EXISTS ${viewName};',
Expand Down
Loading