postgresql-dart icon indicating copy to clipboard operation
postgresql-dart copied to clipboard

Result return, Instance of UndecodedBytes for ENUM attributes on DB

Open enzo-desimone opened this issue 1 year ago • 8 comments

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

enzo-desimone avatar Jan 10 '24 17:01 enzo-desimone

@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.

isoos avatar Jan 10 '24 17:01 isoos

@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

enzo-desimone avatar Jan 10 '24 17:01 enzo-desimone

@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.

isoos avatar Jan 10 '24 17:01 isoos

@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');

enzo-desimone avatar Jan 10 '24 17:01 enzo-desimone

@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

isoos avatar Jan 11 '24 16:01 isoos

@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

ember11498 avatar Jan 31 '24 23:01 ember11498

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

isoos avatar Feb 01 '24 06:02 isoos

@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();
  }
}

ember11498 avatar Feb 01 '24 18:02 ember11498