serverless-mysql icon indicating copy to clipboard operation
serverless-mysql copied to clipboard

Log the final query

Open laurentrivard opened this issue 6 years ago • 3 comments

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

laurentrivard avatar Jul 18 '19 20:07 laurentrivard

Did you manage to find a solution for this?

DoctorWhich avatar Oct 14 '19 14:10 DoctorWhich

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.

jeremydaly avatar Oct 14 '19 21:10 jeremydaly

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);
      }

DoctorWhich avatar Oct 15 '19 10:10 DoctorWhich