Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Log the final query #43

Open
laurentrivard opened this issue Jul 18, 2019 · 3 comments
Open

Log the final query #43

laurentrivard opened this issue Jul 18, 2019 · 3 comments
Labels
question Further information is requested

Comments

@laurentrivard
Copy link

Is there a way to console.log the final query?

const query = 'INSERT into table SET ?'
const result = await mysql.query(query, [object])

Is there a way to know what the final sql query looked like ?

Thanks

@jeremydaly jeremydaly added the question Further information is requested label Aug 28, 2019
@DoctorWhich
Copy link

Did you manage to find a solution for this?

@jeremydaly
Copy link
Owner

I don't believe the mysql library returns the final escaped SQL except on an error. If you catch an error, .sql on the error object will contain the SQL statement executed. I'll see if there is a way to bring that data back for a successful query.

@DoctorWhich
Copy link

I don't believe the mysql library returns the final escaped SQL except on an error.

It does, you can use this.sql inside the callback function to get the final SQL query, regardless of whether an error occurs or not, as in this example exerpt:

con.query(sql, sqlData, function(err, result, fields) {
      if (err) {
        console.log(this.sql);
        resolve ({"found":false, "code": -1, "message": "Error retrieving " + selection + " from " + table, "errType": err.name, "errMessage": err.message, "errNo": err.errno});
      } else {
        console.log(this.sql);
        result.found = true;
        resolve(result);
      }

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
question Further information is requested
Projects
None yet
Development

No branches or pull requests

3 participants