Skip to content

Commit b9fd342

Browse files
HCK-10265: add support for materialized views (#138)
* HCK-10265: add materialized view property config * HCK-10265: add create statement for materialized view * HCK-10265: rename tablespace property keyword * HCK: 10265: add RE from instance * HCK: 10265: fix default values * HCK-10265: add missed table type * HCK-10265: fix formatting * HCK-10265: remove double trimming
1 parent 181e8f3 commit b9fd342

File tree

9 files changed

+613
-28
lines changed

9 files changed

+613
-28
lines changed
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
/**
2+
* @typedef {Record<string, unknown>} ViewData
3+
*/
4+
const { getStorageParameters, getBasicValue } = require('./tableHelper');
5+
6+
/**
7+
* @param {{ viewData: ViewData }}
8+
* @returns {string}
9+
*/
10+
const getOptions = ({ viewData }) => {
11+
const configs = [
12+
{ key: 'usingMethod', getValue: getBasicValue('USING') },
13+
{ key: 'storage_parameter', getValue: getStorageParameters },
14+
{ key: 'view_tablespace_name', getValue: getBasicValue('TABLESPACE') },
15+
];
16+
17+
const statements = configs
18+
.map(config => config.getValue(viewData[config.key], viewData))
19+
.filter(Boolean)
20+
.join('\n')
21+
.trim();
22+
23+
return statements ? ` ${statements}` : '';
24+
};
25+
26+
/**
27+
* @param {{ viewData: ViewData }}
28+
* @returns {string}
29+
*/
30+
const getWithDataClause = ({ viewData }) => {
31+
if (viewData.withDataOption) {
32+
return '\nWITH DATA';
33+
}
34+
35+
return '\nWITH NO DATA';
36+
};
37+
38+
module.exports = {
39+
getOptions,
40+
getWithDataClause,
41+
};

forward_engineering/ddlProvider/ddlHelpers/tableHelper.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -119,6 +119,8 @@ const getStorageParameters = value => {
119119
};
120120

121121
module.exports = {
122+
getBasicValue,
122123
getTableTemporaryValue,
123124
getTableOptions,
125+
getStorageParameters,
124126
};

forward_engineering/ddlProvider/ddlProvider.js

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,7 @@ const {
4646
} = require('./ddlHelpers/columnDefinitionHelper');
4747
const { getTriggersScript, hydrateTriggers } = require('./ddlHelpers/triggerHelper');
4848
const { getLocaleProperties } = require('./ddlHelpers/databaseHelper');
49+
const materializedViewHelper = require('./ddlHelpers/materializedViewHelper');
4950

5051
module.exports = (baseProvider, options, app) => {
5152
return {
@@ -494,6 +495,22 @@ module.exports = (baseProvider, options, app) => {
494495
}
495496
};
496497

498+
if (viewData.materialized) {
499+
const createViewScript = commentIfDeactivated(
500+
assignTemplates(templates.createMaterializedView, {
501+
name: viewName,
502+
ifNotExist: viewData.ifNotExist ? ' IF NOT EXISTS' : '',
503+
options: materializedViewHelper.getOptions({ viewData }),
504+
comment: viewData.comment ? comment : '',
505+
withDataClause: materializedViewHelper.getWithDataClause({ viewData }),
506+
selectStatement,
507+
}),
508+
{ isActivated: !deactivatedWholeStatement },
509+
);
510+
511+
return createViewScript + '\n';
512+
}
513+
497514
const createViewScript = commentIfDeactivated(
498515
assignTemplates(templates.createView, {
499516
name: viewName,
@@ -741,6 +758,12 @@ module.exports = (baseProvider, options, app) => {
741758
withCheckOption: detailsTab.withCheckOption,
742759
checkTestingScope: detailsTab.withCheckOption ? detailsTab.checkTestingScope : '',
743760
schemaName: viewData.schemaData.schemaName,
761+
materialized: detailsTab.materialized,
762+
ifNotExist: detailsTab.ifNotExist,
763+
usingMethod: detailsTab.usingMethod,
764+
storage_parameter: detailsTab.storage_parameter,
765+
view_tablespace_name: detailsTab.view_tablespace_name,
766+
withDataOption: detailsTab.withDataOption,
744767
triggers,
745768
};
746769
},

forward_engineering/ddlProvider/templates.js

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -96,6 +96,9 @@ module.exports = {
9696
createView:
9797
'CREATE${orReplace}${temporary} VIEW ${name}${withOptions}\nAS ${selectStatement}${checkOption};\n\n${comment}\n',
9898

99+
createMaterializedView:
100+
'CREATE MATERIALIZED VIEW${ifNotExist} ${name}${options}\nAS ${selectStatement}${withDataClause};\n\n${comment}\n',
101+
99102
viewSelectStatement: 'SELECT ${keys}\n\tFROM ${tableName}',
100103

101104
dropView: 'DROP VIEW IF EXISTS ${viewName};',

0 commit comments

Comments
 (0)