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
4 changes: 3 additions & 1 deletion forward_engineering/helpers/tableHelper.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ const {
getDBVersionNumber,
generateFullEntityName,
executeUnlessStreaming,
cleanQuery,
} = require('../utils/general');
const { getColumnsStatement, getColumns } = require('./columnHelper');
const keyHelper = require('./keyHelper');
Expand Down Expand Up @@ -800,7 +801,8 @@ const getCreateStreamingStatement = ({

const rowFilterClause = getRowFilterClause(rowFilterGroup, entityJsonProperties);

const queryClause = selectStreamingStatement ? `AS ${selectStreamingStatement}` : '';
const cleanedStatement = cleanQuery(selectStreamingStatement);
const queryClause = cleanedStatement ? `AS ${cleanedStatement}` : '';

return buildStatement(`${createPrefix}${isNotExistsStatement} ${fullTableName} (`, isActivated)(
tableStructure,
Expand Down
21 changes: 21 additions & 0 deletions forward_engineering/utils/general.js
Original file line number Diff line number Diff line change
Expand Up @@ -328,6 +328,26 @@ const executeUnlessStreaming = (isStreaming, task, fallback = '') => {
return task();
};

/**
* Prepares the raw SQL query for embedding in the final DDL statement.
* It removes leading/trailing whitespace and strips any trailing semicolons
* to prevent syntax errors.
*
* @param {string} query - The raw SQL query string from the user input.
* @returns {string} The cleaned query string without trailing semicolons.
*/
const cleanQuery = query => {
const cleaned = query?.trim();
if (!cleaned) return '';

let finalQuery = cleaned;
while (finalQuery.endsWith(';')) {
finalQuery = finalQuery.slice(0, -1);
}

return finalQuery;
};

module.exports = {
buildStatement,
getName,
Expand Down Expand Up @@ -364,4 +384,5 @@ module.exports = {
checkLiquidClusteringPropertyChanged,
generateFullEntityNameFromBucketAndTableNames,
executeUnlessStreaming,
cleanQuery,
};