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 table name is empty from map result #160

Closed paulocagol closed 3 years ago

paulocagol commented 3 years ago

Hello, thank you very much for the package!

I need help in a situation that involves query and function ...

In this case I have a table with the name "todo" and when I use its columns everything works, however when I use a function in select the name of the table is empty ...

Example below.

Funcion:

create or replace function timestamp_iso8601(ts timestamptz, tz text) returns text as $$
declare
  res text;
begin
  set datestyle = 'ISO';
  perform set_config('timezone', tz, true);
  res := ts::timestamptz(3)::text;
  reset datestyle;
  reset timezone;
  return replace(res, ' ', 'T') || ':00';
end;
$$ language plpgsql volatile;

Select:

select timestamp_iso8601(now()::timestamptz, 'America/Sao_Paulo') as createdAt from todo

Dart code:

List<Map<String, Map<String, dynamic>>> results =
      await connection.mappedResultsQuery(
    "select timestamp_iso8601(now()::timestamptz, 'America/Sao_Paulo') as createdAt from todo",
  );

Return in results:

[{: {createdat: 2021-04-18T11:00:22.966-03:00}}]

If I use the column itself Select:

select createdAt from todo

Dart code:

  List<Map<String, Map<String, dynamic>>> results =
      await connection.mappedResultsQuery(
    "select createdAt from todo",
  );

Return in results:

[{todo: {createdat: 2021-04-18 03:50:21.682133Z}}]

It's OK

I am in doubt if there is any correct way to use function in select query ...

I thank you for your attention.

I believe that I can simulate this case with any type of function in the select...

isoos commented 3 years ago

If you use a function, it is no longer a table column, is it? In such cases neither postgresql, nor the client package can now what the origin of the data was (e.g. it could be no table, or multiple tables).

You could also use regular query, and call the row's toColumnMap method: https://pub.dev/documentation/postgres/latest/postgres/PostgreSQLResultRow/toColumnMap.html

It is just a flat map, with the column names as keys.

paulocagol commented 3 years ago

Okay, I understand the case, I will test alternatives ...

Thank you very much for your punctual reply and sorry for my ignorance.