Skip to content

Commit

Permalink
Fix/hck 4491 function based indexes (#77)
Browse files Browse the repository at this point in the history
* - Added new index property - "Conditional function"
- Replaced all <textarea> index inputs with code editor inputs

* - Added support for "Conditional function" in FE
- Fixed issue with inability to create index with try-catch block (trailing ';' character caused an error when applying the script)

* - Renamed conditional function keyword

* - Fixed function based indexes expression handling

* - Fixed CREATE INDEX indentation

* - Fixed try-catch block

---------

Co-authored-by: unknown <unknown>
  • Loading branch information
ArzamastsevVladyslav authored Feb 16, 2024
1 parent a6cc402 commit d85c93e
Show file tree
Hide file tree
Showing 6 changed files with 165 additions and 58 deletions.
59 changes: 39 additions & 20 deletions forward_engineering/ddlProvider/ddlHelpers/indexHelper.js
Original file line number Diff line number Diff line change
@@ -1,37 +1,56 @@
module.exports = ({ _, wrapInQuotes }) => {
module.exports = ({_, wrapInQuotes}) => {
const getIndexType = (indexType) => {
return indexType ? ` ${_.toUpper(indexType)}` : '';
};

/**
* @param indxKey {Array<Object> | undefined}
* @param column_expression {string | undefined}
* @return {string}
* */
const getIndexKeys = ({
indxKey,
column_expression,
}) => {
indxKey,
column_expression,
}) => {
const { escapeSingleQuote } = require('../../utils/general')();

if (column_expression) {
const ddlColumnExpression = escapeSingleQuote(column_expression);
return `\n(\n\t${ddlColumnExpression}\n)\n\t`;
}
if (_.isArray(indxKey) && !_.isEmpty(indxKey)) {
return `\n(\n\t${_.map(indxKey, ({name, type}) => `${wrapInQuotes(name)} ${_.toUpper(type)}`).join(',\n\t')}\n)\n\t`;
const indexedColumnsClause = _.map(
indxKey,
({
name,
type
}) => `${wrapInQuotes(name)} ${_.toUpper(type)}`
)
.join(',\n\t');
return `\n(\n\t${indexedColumnsClause}\n)\n\t`;
}
return _.isEmpty(column_expression) ? '' : `(${_.map(column_expression, expr => expr.value)})`;
return '';
};

const getIndexOptions = ({
indxDescription,
comments,
tablespace,
index_properties,
index_attributes,
index_compression,
logging_clause,
indxKey,
column_expression,
}) => {
indxDescription,
comments,
tablespace,
index_properties,
index_attributes,
index_compression,
logging_clause,
indxKey,
column_expression,
}) => {
let options = `${logging_clause ? ` ${_.toUpper(logging_clause)}` : ''}` +
`${tablespace ? ` TABLESPACE ${tablespace}` : ''}` +
`${index_compression ? ` ${index_compression}` : ''}`
`${tablespace ? ` TABLESPACE ${tablespace}` : ''}` +
`${index_compression ? ` ${index_compression}` : ''}`

if (index_properties) {
options = ` ${index_properties}`;
options = ` ${index_properties}`;
} else if (index_attributes) {
options = ` ${index_attributes}`;
options = ` ${index_attributes}`;
}
const isKeysEmpty = _.isEmpty(indxKey) && _.isEmpty(column_expression);

Expand Down
9 changes: 6 additions & 3 deletions forward_engineering/ddlProvider/ddlProvider.js
Original file line number Diff line number Diff line change
Expand Up @@ -97,8 +97,11 @@ module.exports = (baseProvider, options, app) => {

const wrapIfNotExists = (statement, ifNotExist, errorCode = 955) => {
if (ifNotExist) {
const ddlToWrap = _.trim(tab(tab(statement)));
return assignTemplates(templates.ifNotExists, {statement: ddlToWrap, errorCode})
const tabbedStatement = tab(tab(statement));
const trimmedStatement = _.trim(tabbedStatement);
const statementWithNoTrailingDelimiter = trimmedStatement.replace(/;$/, '');

return assignTemplates(templates.ifNotExists, {statement: statementWithNoTrailingDelimiter, errorCode})
}
return statement + ';';
};
Expand Down Expand Up @@ -446,7 +449,7 @@ module.exports = (baseProvider, options, app) => {
const name = getIndexName(index.indxName);
const indexType = getIndexType(index.indxType);
const keys = getIndexKeys(index);
const indexOptions = getIndexOptions(index, isParentActivated);
const indexOptions = getIndexOptions(index);
const dbVersion = options.dbVersion || '';
const usingTryCatchWrapper = shouldUseTryCatchIfNotExistsWrapper(dbVersion);
const statement = assignTemplates(templates.createIndex, {
Expand Down
2 changes: 1 addition & 1 deletion forward_engineering/ddlProvider/templates.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ module.exports = {
createForeignKey:
'ALTER TABLE ${foreignTable} ADD CONSTRAINT ${name} FOREIGN KEY (${foreignKey}) REFERENCES ${primaryTable} (${primaryKey})${onDelete};',

createIndex: `CREATE$\{indexType} INDEX$\{ifNotExists}$\{name} ON $\{tableName}$\{keys}$\{options};`,
createIndex: `CREATE$\{indexType} INDEX$\{ifNotExists}$\{name} ON $\{tableName}$\{keys}$\{options};\n`,

createView:
'CREATE${orReplace}${force}${viewType}${materialized} VIEW${ifNotExists} ${name} ${sharing}${viewProperties}\n\tAS ${selectStatement}',
Expand Down
9 changes: 9 additions & 0 deletions forward_engineering/utils/general.js
Original file line number Diff line number Diff line change
Expand Up @@ -165,6 +165,14 @@ module.exports = _ => {
: ' (' + columns.map(mapColumn).join(', ') + ')';
};

/**
* @param str {string}
* @return {string}
* */
const escapeSingleQuote = str => {
return str.replaceAll("'", "''");
};

return {
getDbName,
getBucketName,
Expand All @@ -187,5 +195,6 @@ module.exports = _ => {
getNamePrefixedWithSchemaName,
checkFieldPropertiesChanged,
getColumnsList,
escapeSingleQuote,
};
};
94 changes: 69 additions & 25 deletions properties_pane/entity_level/entityLevelConfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,9 @@
* Copyright © 2016-2020 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.
* 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.

In order to define custom properties for any object's properties pane, you may copy/paste from the following,
making sure that you maintain a proper JSON format.
Expand Down Expand Up @@ -70,8 +70,8 @@ making sure that you maintain a proper JSON format.
]
},
// “groupInput” can have the following states - 0 items, 1 item, and many items.
// “blockInput” has only 2 states - 0 items or 1 item.
// This gives us an easy way to represent it as an object and not as an array internally which is beneficial for processing
// “blockInput” has only 2 states - 0 items or 1 item.
// This gives us an easy way to represent it as an object and not as an array internally which is beneficial for processing
// and forward-engineering in particular.
{
"propertyName": "Block",
Expand Down Expand Up @@ -99,7 +99,7 @@ making sure that you maintain a proper JSON format.
"propertyKeyword": "keyList",
"propertyType": "fieldList",
"template": "orderedList"
},
},
{
"propertyName": "List with attribute",
"propertyKeyword": "keyListOrder",
Expand Down Expand Up @@ -910,7 +910,7 @@ making sure that you maintain a proper JSON format.
}
]
}
},
},
{
"tooltip": "Remove or update the existing composite primary key definition prior to unlock the possibility to create a new composite primary key definition for this table",
"dependency": {
Expand Down Expand Up @@ -971,7 +971,7 @@ making sure that you maintain a proper JSON format.
"propertyType": "primaryKeySetter",
"abbr": "pk"
}]
},
},
{
"propertyName": "Unique key",
"propertyType": "group",
Expand Down Expand Up @@ -1034,52 +1034,93 @@ making sure that you maintain a proper JSON format.
"attributeList": [
"asc",
"desc"
]
],
"dependency": {
"type": "or",
"values": [
{
"key": "column_expression",
"exist": false
},
{
"key": "column_expression",
"value": ""
}
]
}
},
{
"propertyName": "Conditional function",
"propertyKeyword": "column_expression",
"propertyTooltip": "Enter conditional function for a function-based index",
"propertyType": "details",
"markdown": false,
"template": "codeEditor",
"templateOptions": {
"editorDialect": "sql"
},
"dependency": {
"type": "or",
"values": [
{
"key": "indxKey",
"exist": false
},
{
"key": "indxKey",
"isEmpty": true
}
]
}
},
{
"propertyName": "Description",
"propertyKeyword": "indxDescription",
"propertyTooltip": "description",
"propertyType": "details",
"template": "textarea"
"template": "codeEditor",
"templateOptions": {
"editorDialect": "markdown"
}
},
{
"propertyName": "Tablespace",
"propertyKeyword": "tablespace",
"propertyTooltip": "Specify the tablespace in which Oracle Database creates the table. If you omit TABLESPACE, then the database creates that item in the default tablespace of the owner of the schema containing the table.",
"propertyType": "text"
},
{
"propertyName": "Expression",
"propertyKeyword": "column_expression",
"propertyTooltip": "For function-based index. Specify an expression built from columns of table, constants, SQL functions, and user-defined functions. ",
"propertyType": "details",
"template": "textarea",
"markdown": false
},
{
"propertyName": "Properties",
"propertyKeyword": "index_properties",
"propertyTooltip": "Specify the optional index attributes.",
"propertyType": "details",
"template": "textarea",
"markdown": false
"markdown": false,
"template": "codeEditor",
"templateOptions": {
"editorDialect": "sql"
}
},
{
"propertyName": "Attributes",
"propertyKeyword": "index_attributes",
"propertyTooltip": "Specify the optional index attributes.",
"propertyType": "details",
"template": "textarea",
"markdown": false
"markdown": false,
"template": "codeEditor",
"templateOptions": {
"editorDialect": "sql"
}
},
{
"propertyName": "Compression",
"propertyKeyword": "index_compression",
"propertyTooltip": "If you want to use compression for a partitioned index, then you must create the index with compression enabled at the index level. You can subsequently enable and disable the compression setting for individual partitions of such a partitioned index. ",
"propertyType": "details",
"template": "textarea",
"markdown": false
"markdown": false,
"template": "codeEditor",
"templateOptions": {
"editorDialect": "sql"
}
},
{
"propertyName": "Logging",
Expand All @@ -1099,7 +1140,10 @@ making sure that you maintain a proper JSON format.
"propertyTooltip": "comments",
"addTimestampButton": false,
"propertyType": "details",
"template": "textarea"
"template": "codeEditor",
"templateOptions": {
"editorDialect": "markdown"
}
}
]
}]
Expand Down
50 changes: 41 additions & 9 deletions properties_pane/view_level/viewLevelConfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -562,7 +562,47 @@
"propertyKeyword": "indxKey",
"propertyType": "fieldList",
"template": "orderedList",
"attributeList": ["asc", "desc"]
"attributeList": [
"asc",
"desc"
],
"dependency": {
"type": "or",
"values": [
{
"key": "column_expression",
"exist": false
},
{
"key": "column_expression",
"value": ""
}
]
}
},
{
"propertyName": "Conditional function",
"propertyKeyword": "column_expression",
"propertyTooltip": "Enter conditional function for a function-based index",
"propertyType": "details",
"markdown": false,
"template": "codeEditor",
"templateOptions": {
"editorDialect": "sql"
},
"dependency": {
"type": "or",
"values": [
{
"key": "indxKey",
"exist": false
},
{
"key": "indxKey",
"isEmpty": true
}
]
}
},
{
"propertyName": "Description",
Expand All @@ -577,14 +617,6 @@
"propertyTooltip": "Specify the tablespace in which Oracle Database creates the table. If you omit TABLESPACE, then the database creates that item in the default tablespace of the owner of the schema containing the table.",
"propertyType": "text"
},
{
"propertyName": "Expression",
"propertyKeyword": "column_expression",
"propertyTooltip": "For function-based index. Specify an expression built from columns of table, constants, SQL functions, and user-defined functions. ",
"propertyType": "details",
"template": "textarea",
"markdown": false
},
{
"propertyName": "Properties",
"propertyKeyword": "index_properties",
Expand Down

0 comments on commit d85c93e

Please sign in to comment.