vapor / mysql-nio

🐬 Non-blocking, event-driven Swift client for MySQL.
MIT License
87 stars 28 forks source link

Decode MySQLRow with specified table name #33

Closed t-ae closed 3 years ago

t-ae commented 4 years ago

There seems to be no way to decode models from MySQLRow with specified table name in Vapor 4. (In Vapor 3 I often used this method. https://github.com/vapor/mysql-kit/blob/3.3.0/Sources/MySQL/Connection/MySQLConnection.swift#L66-L69)

I have raw query like below:

SELECT * FROM `table1`, `table2`;

It returns columns like table1.id, table2.id... There are columns with same name but they can be distinguished with table name. MySQLRow conveys this information, but all its decode methods come from SQLRow protocol and they don't consider table names. Thus they can't distinguish table1.id and table2.id, and we get wrong result.

MySQLRow should have its own decoding method decode(model: Decodable.Type, table: String). I think it can be done by creating MySQLRowDecoder that imitates SQLRowDecoder, but first I want your opinion if this design is good or not. I use only MySQL so I'm not sure if this can be generalized to other RDBMSs.

tanner0101 commented 3 years ago

MySQLRow does have this method: https://github.com/vapor/mysql-nio/blob/master/Sources/MySQLNIO/MySQLRow.swift#L29

You will need to cast the SQLRow to MySQLRow to access it.

PostgreSQL does not support this type of column name disambiguation so it's not something we can generalize to SQLKit.