Skip to content

Commit

Permalink
fix(MySQL Node): Fix "Maximum call stack size exceeded" error when ha…
Browse files Browse the repository at this point in the history
…ndling a large number of rows (#11242)
  • Loading branch information
netroy authored Oct 14, 2024
1 parent 566529c commit b7ee0c4
Showing 1 changed file with 24 additions and 24 deletions.
48 changes: 24 additions & 24 deletions packages/nodes-base/nodes/MySql/v2/helpers/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -209,22 +209,22 @@ export function configureQueryRunner(
return [];
}

const returnData: INodeExecutionData[] = [];
let returnData: INodeExecutionData[] = [];
const mode = (options.queryBatching as QueryMode) || BATCH_MODE.SINGLE;

const connection = await pool.getConnection();

if (mode === BATCH_MODE.SINGLE) {
const formatedQueries = queries.map(({ query, values }) => connection.format(query, values));
const formattedQueries = queries.map(({ query, values }) => connection.format(query, values));
try {
//releasing connection after formating queries, otherwise pool.query() will fail with timeout
//releasing connection after formatting queries, otherwise pool.query() will fail with timeout
connection.release();

let singleQuery = '';
if (formatedQueries.length > 1) {
singleQuery = formatedQueries.map((query) => query.trim().replace(/;$/, '')).join(';');
if (formattedQueries.length > 1) {
singleQuery = formattedQueries.map((query) => query.trim().replace(/;$/, '')).join(';');
} else {
singleQuery = formatedQueries[0];
singleQuery = formattedQueries[0];
}

let response: IDataObject | IDataObject[] = (
Expand All @@ -249,11 +249,11 @@ export function configureQueryRunner(
response = [response];
}

//because single query is used in this mode mapping itemIndex not posible, setting all items as paired
//because single query is used in this mode mapping itemIndex not possible, setting all items as paired
const pairedItem = generatePairedItemData(queries.length);

returnData.push(
...prepareOutput(
returnData = returnData.concat(
prepareOutput(
response,
options,
statements,
Expand All @@ -262,24 +262,24 @@ export function configureQueryRunner(
),
);
} catch (err) {
const error = parseMySqlError.call(this, err, 0, formatedQueries);
const error = parseMySqlError.call(this, err, 0, formattedQueries);

if (!this.continueOnFail()) throw error;
returnData.push({ json: { message: error.message, error: { ...error } } });
}
} else {
if (mode === BATCH_MODE.INDEPENDENTLY) {
let formatedQuery = '';
let formattedQuery = '';
for (const [index, queryWithValues] of queries.entries()) {
try {
const { query, values } = queryWithValues;
formatedQuery = connection.format(query, values);
formattedQuery = connection.format(query, values);

let statements;
if ((options?.nodeVersion as number) <= 2.3) {
statements = formatedQuery.split(';').map((q) => q.trim());
statements = formattedQuery.split(';').map((q) => q.trim());
} else {
statements = splitQueryToStatements(formatedQuery, false);
statements = splitQueryToStatements(formattedQuery, false);
}

const responses: IDataObject[] = [];
Expand All @@ -290,8 +290,8 @@ export function configureQueryRunner(
responses.push(response);
}

returnData.push(
...prepareOutput(
returnData = returnData.concat(
prepareOutput(
responses,
options,
statements,
Expand All @@ -300,7 +300,7 @@ export function configureQueryRunner(
),
);
} catch (err) {
const error = parseMySqlError.call(this, err, index, [formatedQuery]);
const error = parseMySqlError.call(this, err, index, [formattedQuery]);

if (!this.continueOnFail()) {
connection.release();
Expand All @@ -314,17 +314,17 @@ export function configureQueryRunner(
if (mode === BATCH_MODE.TRANSACTION) {
await connection.beginTransaction();

let formatedQuery = '';
let formattedQuery = '';
for (const [index, queryWithValues] of queries.entries()) {
try {
const { query, values } = queryWithValues;
formatedQuery = connection.format(query, values);
formattedQuery = connection.format(query, values);

let statements;
if ((options?.nodeVersion as number) <= 2.3) {
statements = formatedQuery.split(';').map((q) => q.trim());
statements = formattedQuery.split(';').map((q) => q.trim());
} else {
statements = splitQueryToStatements(formatedQuery, false);
statements = splitQueryToStatements(formattedQuery, false);
}

const responses: IDataObject[] = [];
Expand All @@ -335,8 +335,8 @@ export function configureQueryRunner(
responses.push(response);
}

returnData.push(
...prepareOutput(
returnData = returnData.concat(
prepareOutput(
responses,
options,
statements,
Expand All @@ -345,7 +345,7 @@ export function configureQueryRunner(
),
);
} catch (err) {
const error = parseMySqlError.call(this, err, index, [formatedQuery]);
const error = parseMySqlError.call(this, err, index, [formattedQuery]);

if (connection) {
await connection.rollback();
Expand Down

0 comments on commit b7ee0c4

Please sign in to comment.