mysql icon indicating copy to clipboard operation
mysql copied to clipboard

MySQL query altering orignal query column order

Open victorigualada opened this issue 4 years ago • 0 comments

I'm submitting a

[ ] bug report [x] feature/update request [ ] support request

Description

There is a problem on the column order when querying the database. It's not a problem with the driver itself but with it's usage of Javascript. Since it's using an Obect to populate the results, the column order when the column name is a numeric value, JS places it at the top of the properties. There is already an open issue with the same problem.

It states:

Hi I've written stored procedure which is returning result as below image but when i'm calling the same query in the application, i'm getting below result: RowDataPacket { '2013': 2, '2014': 3, '2015': 4, 'A':'Test, 'B':'Test, 'C':'Test' }

Steps to reproduce

Making any query with numeric values as column aliases will produce this behaviour. For example:

   SELECT columnA as 'stringColumnName', columnB as '0'
   FROM myTable;

Expected behaviour

It's expected that the order of the columns specified in the SQL query is preserved.

Actual behaviour

The columns are arranged into the object due to the traversal order of object properties in ES6.

Proposed solution

A proper solution to this problem would be using a Map that will preserve the order of the properties. As stated in the MDN Javascript Map:

The keys in Map are ordered. Thus, when iterating over it, a Map object returns keys in order of insertion. This line is the responsible of the lost order, adding the field header as an object and causing the above problem.

Thank you for your time! Happy coding!

victorigualada avatar Jun 18 '20 16:06 victorigualada