CodeFoodPixels / node-promise-mysql

A wrapper for mysqljs/mysql that wraps function calls with Bluebird promises.
MIT License
338 stars 64 forks source link

Is it possible to get the SQL query? #23

Closed Scimonster closed 7 years ago

Scimonster commented 7 years ago

I'm getting some SQL syntax errors right now using the prepared statements, and it would be helpful for debugging to be able to see the SQL query that gets executed. In the original mysql package i could do that as q=connection.query();console.log(q.sql) but here q is a Promise. Perhaps even just if it throws an error, include that as a property on the error object.

CodeFoodPixels commented 7 years ago

I can't test it at the moment, but I'd think it would be something like connection.query().then(function(query) { console.log(query.sql); });

Scimonster commented 7 years ago

No, it just calls it with an array of RowDataPackets. And that wouldn't help anyways when there's an error.

CodeFoodPixels commented 7 years ago

If there's an error then that'll be caught in a catch statement as shown in the readme.

I'll have a try later tonight if I get time

On Wed, 7 Sep 2016, 15:55 Scimonster, notifications@github.com wrote:

No, it just calls it with an array of RowDataPackets. And that wouldn't help anyways when there's an error.

— You are receiving this because you commented.

Reply to this email directly, view it on GitHub https://github.com/lukeb-uk/node-promise-mysql/issues/23#issuecomment-245307127, or mute the thread https://github.com/notifications/unsubscribe-auth/AEjy1I8-xQenXRRql4eV8mBsR34z4rH8ks5qntBmgaJpZM4J2SUM .

Scimonster commented 7 years ago

Yes, but the error does not give the bad SQL query, just the error code and description. An error about an unexpected '' doesn't help much if i don't know where that '' got put.

CodeFoodPixels commented 7 years ago

Sorry, just had a chance to have a look and I understand the issue now. I'll see if there's a way I can solve this.

linusnorton commented 7 years ago

I'm not sure if this is helpful but you can set debug: ['ComQueryPacket', 'RowDataPacket'] when you create the connection to debug all queries.

Arnoldnuo commented 7 years ago

Does it have any progress?

CodeFoodPixels commented 7 years ago

Having just had a look into it, @linusnorton's solution should suffice for this. This lib takes the config object you provide and passes it through node-mysql. Thanks Linus!

vanhocpham commented 4 years ago

This is how I am doing it now :)

 self.pool.on('connection', function(connection) {
        connection.on('enqueue', function(sequence) {
            if ('Query' === sequence.constructor.name) {
                console.log('\x1b[36m%s\x1b[0m', sequence.sql );
            }
        });
    });