Slyke
Slyke

Reputation: 134

How to execute a list of SQL commands sequentially?

I'm using MySQL for NodeJS and trying to do something like this:

dbQueries = ['multiple', 'sql', 'statements', 'that', 'need', 'to', 'be', 'executed', 'in', 'order'];
executedQueries = 0;
dbConn = mysql.connect();
maxQueries = dbQueries.length;
dbQueries.forEach(async (dbQuery)  => {
  console.log("Start");
  return dbConn.query(dbQuery, function(err, results, fields) {
    console.log("Finish");
    if (err) {
      errorList.push({ err, results, fields, dbQuery });
    }
    executedQueries++;

    if (executedQueries >= maxQueries) {
      if (errorList.length > 0) {
        console.log("Error: ", databaseTable.name, "  Errors reported: ", errorList);
      }
      if (typeof(callbackFunc) === 'function') { callbackFunc(errorList, executedQueries); }
    }
  });
});

But what ends up happening is that sometimes queries finish out of order. I know this because I setup the columns, then modify the table by adding in primary keys etc, and it sometimes errors with Table 'blah.blah' doesn't exist. The errors change each time. I could see this due to there being multiple Starts before seeing multiple Finishes. Not sure if I am using async correctly or not.

I do not want to turn on multipleStatements if possible.

Upvotes: 0

Views: 731

Answers (1)

Mothas
Mothas

Reputation: 59

The main reason you are seeing unexpected result is because nodejs executes these queries asynchronously. Correct way to run multiple queries:

dbQueries = ['multiple', 'sql', 'statements', 'that', 'need', 'to', 'be', 'executed', 'in', 'order'];
dbConn = mysql.connect();
function runQueries(dbQueriesArr) {
    if(dbQueriesArr.length === 0)
        return;
    var dbQuery = dbQueriesArr[0];
    dbConn.query(dbQuery, function(err, results, fields) {
        if (err) {
            errorList.push({ err, results, fields, dbQuery });
        }
        var dbQueriesArr_rest = dbQueriesArr.splice(1);
        runQueries(dbQueriesArr_rest);
    });
}
runQueries(dbQueries);

Upvotes: 1

Related Questions