stablekernel / postgresql-dart

Dart PostgreSQL driver: supports extended query format, binary protocol and statement reuse.
https://www.dartdocs.org/documentation/postgres/latest
BSD 3-Clause "New" or "Revised" License
129 stars 32 forks source link

mappedResultsQuery should be mapped to table Alias instead table Name #137

Open supermuka opened 4 years ago

supermuka commented 4 years ago

mappedResultsQuery should be mapped to table Alias instead table Name, when has an alias.

Now, this works. But it ignores the alias on the table:

List<Map<String, Map<String, dynamic>>> results = await connection.mappedResultsQuery(
  "SELECT t_alias.id, t_alias.name, u_alias.name FROM t t_alias
   LEFT OUTER JOIN u u_alias ON t.id=u.t_id");

for (final row in results) {
  var tID = row["t"]["id"];
  var tName = row["t"]["name"];
  var uName = row["u"]["name"];
}

But the correct way should be with alias:

for (final row in results) {
  var tID = row["t_alias"]["id"];
  var tName = row["t_alias"]["name"];
  var uName = row["u_alias"]["name"];
}

Column name x alias is working correctly. When it has an alias in column, the mappedResultsQuery returns the alias instead column name.

isoos commented 4 years ago

Hm, this is especially true if the same table is joined with two alias.

supermuka commented 4 years ago

Yes, exactly!

With this query we have problems. But it should work correctly with results below:

List<Map<String, Map<String, dynamic>>> results = await connection.mappedResultsQuery(
  "SELECT t_alias.id, t_alias.name, u_alias.name, u_two_alias.name FROM t t_alias
   LEFT OUTER JOIN u u_alias ON t_alias.id=u_alias.t_id
   LEFT OUTER JOIN u u_two_alias ON t_alias.id_two=u_tow_alias.t_id");

for (final row in results) {
  var tID = row["t_alias"]["id"];
  var tName = row["t_alias"]["name"];
  var uName = row["u_alias"]["name"];
  var uTwoName = row["u_two_alias"]["name"];
}