mysql icon indicating copy to clipboard operation
mysql copied to clipboard

Query results off-by-one in stored procedure callback

Open aarong opened this issue 15 years ago • 13 comments

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' } ]

aarong avatar Jan 05 '11 16:01 aarong

Hm, looks like stored procedures use an undocumented packet combination. I'll try to look into this as soon as I can.

felixge avatar Jan 05 '11 20:01 felixge

Poke

TooTallNate avatar Feb 03 '11 02:02 TooTallNate

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 : ).

felixge avatar Feb 04 '11 12:02 felixge

Poking as well; trying to implement stored procedures and getting simliar strange results.

masonhugus avatar Apr 06 '11 21:04 masonhugus

I am using stored procedures too and i can confirm this behaviour.

stefanpoeter avatar Aug 19 '11 09:08 stefanpoeter

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 :-)

stefanpoeter avatar Aug 23 '11 12:08 stefanpoeter

Thanks for the analysis, this will help me tackle this quicker!

felixge avatar Aug 23 '11 15:08 felixge

Note to myself: Check out pull #105 and apply if it makes sense.

felixge avatar Aug 31 '11 14:08 felixge

I tried this solution and apparently works. Haven't tested it out thoroughly though.

rix501 avatar Sep 17 '11 05:09 rix501

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.

dmuth avatar Mar 06 '12 23:03 dmuth

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.

BewhiskeredBard avatar Mar 07 '12 16:03 BewhiskeredBard

Need stored procedure support + 1 Thanks

shuracwf avatar May 03 '12 15:05 shuracwf

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

felixge avatar May 15 '12 09:05 felixge