node-mysql-libmysqlclient icon indicating copy to clipboard operation
node-mysql-libmysqlclient copied to clipboard

Async Prepared Statement Execution

Open MelvinSE opened this issue 11 years ago • 0 comments

Really great work on this library. Thanks.

I'm working on executing prepared statements asynchronously. By the looks of the code, this is still in development, but I wanted to make sure I wasn't making a mistake in my implementation. When initializing the parent class, I do the following:

  • Create a new statement with initStatementSync();.
  • Prepare the statement SQL with prepareSync();.
  • Store that MysqlStatment object in my parent class.

The parent class has methods to query the DB. When on of those methods is invoked, the code then does the following:

  • Get the saved MysqlStatment that was previously prepared
  • Set the params with bindParamsSync();.
  • Call execute() on the statement.
  • In the callback of the execute method, call bindResultSync();
  • After binding the results, call fetchAll() to get the dataset.

I had previously written this code to use the synchronous methods for testing my SQL statements and it worked great. However, I must use the async calls in the final application. The code above doesn't fail, but returns no data. I have a feeling I'm not binding the results correctly before calling fetchAll().

The source code for the execute() block is here, where the statement object has already been initialized and prepared:

statement.execute(function(err, result) {
  if (err) { return fn(err); }

  statement.bindResultSync();
  statement.fetchAll(function(err, rows) {
    if (err) { return fn(err); }

    return fn(null, rows);
  });
});

As I said, it seems the support for async prepared statement execution might not be completed. That's OK, but I wanted to make sure my code wasn't wrong and that's why I'm getting an empty array back.

MelvinSE avatar Feb 13 '14 14:02 MelvinSE