alaisi / postgres-async-driver

Asynchronous PostgreSQL Java driver
Apache License 2.0
287 stars 38 forks source link

PgRow pgColumns index order different from actual indexes (caused by relying on HashMap.values() order) #58

Open ratamaa opened 6 years ago

ratamaa commented 6 years ago

In: https://github.com/alaisi/postgres-async-driver/blob/master/src/main/java/com/github/pgasync/impl/PgRow.java#L47 PgColumn[] pgColumns order might be different from PgColumn.index values which makes get...(int index) methods quite useless at the moment.

See example situation in debugger:

screen shot 2017-12-11 at 22 09 12

Unfortunately this issue can most likely re-produce by random, so I can not provide a particular test case that would always fail.

This is caused by relying on HashMap.values() order although the order is not the same as the order they are put into the Map by its contract. This issue could most likely be resolved by using LinkedHashMap in com.github.pgasync.impl.PgConnection#getColumns: https://github.com/alaisi/postgres-async-driver/blob/master/src/main/java/com/github/pgasync/impl/PgConnection.java#L187

However, the parameter is of of Map interface type which means, one can not assume anything about the implementation. Therefore, more appropriate fix could be e.g. in the constructor of PgRow:

    pgColumns = new PgColumn[row.getColumns().size()];
    for (PgColumn col : row.getColumns().values()) {
        pgColumns[col.index] = col;
    }