Query results off-by-one in stored procedure callback
To replicate the problem:
Call a stored procedure using the module, then in the callback for the stored procedure perform another query. The results of that inner query seems to be status information related to the outer stored procedure call. If you run yet another query inside that callback, you will get the results of the first inner query.
First run the following code to setup the test database:
-- CREATE TEST DATABASE
DROP DATABASE IF EXISTS testdb123;
CREATE DATABASE testdb123;
USE testdb123;
-- DEFINE A BASIC STORED PROCEDURE
DELIMITER $$
CREATE PROCEDURE testproc() BEGIN
SELECT 'sproc result';
END $$
DELIMITER ;
Then run the following code in Node.js to replicate the problem:
// Connect to MySQL
var MySqlClient = require('mysql').Client;
var client = new MySqlClient(
{
user: 'root',
password: 'letmein',
database: 'testdb123'
});
client.connect();
client.query(
'CALL testproc()',
function(inError, inResult) {
console.log('Stored procedure result:');
console.log(inResult);
client.query(
"SELECT 'one'",
function(inError, inResult) {
console.log('First query result (should be one):')
console.log(inResult);
client.query(
"SELECT 'two'",
function(inError, inResult) {
console.log('Second query result (should be two):')
console.log(inResult);
}
);
}
);
}
);
setTimeout(
function() {
client.query(
"SELECT 'three'",
function(inError, inResult) {
console.log('Third query result (should be three):')
console.log(inResult);
}
);
},
2500
);
The output is:
Stored procedure result:
[ { 'sproc result': 'sproc result' } ]
First query result (should be one):
{ affectedRows: 0
, insertId: 0
, serverStatus: 2
, warningCount: 0
, message: ''
, emit: [Function]
, addListener: [Function]
, on: [Function]
, removeListener: [Function]
, removeAllListeners: [Function]
, listeners: [Function]
}
Second query result (should be two):
[ { one: 'one' } ]
Third query result (should be three):
[ { three: 'three' } ]
Hm, looks like stored procedures use an undocumented packet combination. I'll try to look into this as soon as I can.
Poke
This has not slipped of my radar yet. I've spend a lot of time lately sharpening my tools (unit testing wise). Once that is done I'll cut through my node-mysql backlog quickly : ).
Poking as well; trying to implement stored procedures and getting simliar strange results.
I am using stored procedures too and i can confirm this behaviour.
I've done some tcpdump regarding this issue.
It seems that a stored procedure calls emit multiple responses.
The server emits, for every select statement in the procedure, an extra result response and quits the whole procedure call with a response OK packet.
So far I've tested this with three versions of the testproc procedure:
First:
create procedure testproc() begin end;
emits response ok.
second:
create procedure testproc() begin select 'sproc result'; end;
emits
response [fields and results] response ok
third:
create procedure testproc() begin select 'sproc result1'; select 'sproc result2'; end;
emits
response [fields and results for the first select] response [fields and results for the second select] response ok
I hope this helps :-)
Thanks for the analysis, this will help me tackle this quicker!
Note to myself: Check out pull #105 and apply if it makes sense.
I tried this solution and apparently works. Haven't tested it out thoroughly though.
Subscribing.
I'd like to see support for stored procedures too. This is actually kind of a big deal for my current project, because we're stored procedure-heavy, and this is causing us intermittent issues.
Thanks.
Is there anything I can do to help get stored procedures working? I need stored procedure support for my node app, and none of the existing libraries seem to have it working.
Need stored procedure support + 1 Thanks
This has been fixed in v2.0.0-alpha, see: http://debuggable.com/posts/releasing-node-mysql-2-0-0-alpha:4fb21c18-7f84-4e8c-a1ef-599bcbdd56cb