Skip to content

Commit

Permalink
fix(Microsoft SQL Node): Prevent double escaping table name (#7801)
Browse files Browse the repository at this point in the history
Github issue / Community forum post (link here to close automatically):
https://community.n8n.io/t/issue-my-mssql-update-in-latest-version/32966
  • Loading branch information
michael-radency authored and netroy committed Nov 29, 2023
1 parent e932bef commit 6a8e7b1
Showing 1 changed file with 16 additions and 5 deletions.
21 changes: 16 additions & 5 deletions packages/nodes-base/nodes/Microsoft/Sql/GenericFunctions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -111,6 +111,15 @@ export function configurePool(credentials: IDataObject) {
return new mssql.ConnectionPool(config);
}

const escapeTableName = (table: string) => {
table = table.trim();
if (table.startsWith('[') && table.endsWith(']')) {
return table;
} else {
return `[${table}]`;
}
};

export async function insertOperation(tables: ITables, pool: mssql.ConnectionPool) {
return executeQueryQueue(
tables,
Expand All @@ -128,7 +137,7 @@ export async function insertOperation(tables: ITables, pool: mssql.ConnectionPoo
}
}

const query = `INSERT INTO [${table}] (${formatColumns(
const query = `INSERT INTO ${escapeTableName(table)} (${formatColumns(
columnString,
)}) VALUES ${valuesPlaceholder.join(', ')};`;

Expand All @@ -155,7 +164,9 @@ export async function updateOperation(tables: ITables, pool: mssql.ConnectionPoo
request.input(`v${index}`, item[col]);
}

const query = `UPDATE [${table}] SET ${setValues.join(', ')} WHERE ${condition};`;
const query = `UPDATE ${escapeTableName(table)} SET ${setValues.join(
', ',
)} WHERE ${condition};`;

return request.query(query);
});
Expand All @@ -182,9 +193,9 @@ export async function deleteOperation(tables: ITables, pool: mssql.ConnectionPoo
request.input(`v${index}`, entry[deleteKey]);
}

const query = `DELETE FROM [${table}] WHERE [${deleteKey}] IN (${valuesPlaceholder.join(
', ',
)});`;
const query = `DELETE FROM ${escapeTableName(
table,
)} WHERE [${deleteKey}] IN (${valuesPlaceholder.join(', ')});`;

return request.query(query);
});
Expand Down

0 comments on commit 6a8e7b1

Please sign in to comment.