isoos / postgresql-dart

Dart PostgreSQL driver: supports extended query format, binary protocol and statement reuse.
https://pub.dev/packages/postgres
BSD 3-Clause "New" or "Revised" License
131 stars 36 forks source link

Result return, Instance of UndecodedBytes for ENUM attributes on DB #276

Open enzo-desimone opened 10 months ago

enzo-desimone commented 10 months ago

When a query is launched, what is returned for the enumeration attributes in the database is Instance of 'UndecodedBytes'.

isoos commented 10 months ago

@enzo-desimone: if you have a reproduction case (either a single SELECT or a CREATE/INSERT/SELECT), I can look into how hard is to add better support.

enzo-desimone commented 10 months ago

@enzo-desimone: if you have a reproduction case (either a single SELECT or a CREATE/INSERT/SELECT), I can look into how hard is to add better support.

Thanks for response @isoos

Query execute

     final res = await PostgresDB.connection.execute(
        Sql.named('SELECT * from portfolio WHERE pathname=@pathname ORDER BY t1.id ASC'),
        parameters: conditions,
      );

      print(res);

Print result [[11, Instance of 'UndecodedBytes', /wizzy]]

Database enum

Screenshot 2024-01-10 183852
isoos commented 10 months ago

@enzo-desimone: I don't have information about your portfolio table. Please provide a SELECT that is self-contained, or a CREATE/INSERT/SELECT pair.

enzo-desimone commented 10 months ago

@isoos, here you are:

PORTFOLIO TABLE

CREATE TABLE IF NOT EXISTS public.portfolio_item
(
    id integer NOT NULL DEFAULT nextval('portfolio_item_portfolio_item_id_seq'::regclass),
    category item_category NOT NULL,
    pathname character varying COLLATE pg_catalog."default" NOT NULL,
    CONSTRAINT portfolio_item_pkey PRIMARY KEY (id),
    CONSTRAINT unique_link_path UNIQUE (pathname)
)

ITEM_CATEGORY ENUM

CREATE TYPE public.item_category AS ENUM
    ('android', 'blackberry', 'flutter');
isoos commented 10 months ago

@enzo-desimone: this is a custom type, and we don't have typed support for it yet. However, I've created a test to provide an example on how you can access the string content of the unknown bytes: https://github.com/isoos/postgresql-dart/pull/278/files

ember11498 commented 9 months ago

@isoos I have read your workaround. I am just wondering when result.first.toColumnMap() will support enum, meaning the custom enum varables being represented as the strings instead of Instance of 'UndecodedBytes'. Wondering if I should bother doing the work around or if i should just wait, Thanks in advance

isoos commented 9 months ago

@ember11498 for the time being I'd suggest to use the workaround

ember11498 commented 9 months ago

@isoos I just wrote an extension on Result that works prety well.

import 'package:postgres/postgres.dart';

extension ResultExtensions on Result {
  List<Map<String, dynamic>> get tableToMap {
    return map((row) {
      return row.toColumnMap().map((key, value) {
        if (value is UndecodedBytes) {
          return MapEntry(key, (value).asString);
        } else {
          return MapEntry(key, value);
        }
      });
    }).toList();
  }
}