Re: IMMUTABLE function to cast enum to/from text?

From: Tom Lane <tgl(at)sss(dot)pgh(dot)pa(dot)us>
To: Joe Conway <mail(at)joeconway(dot)com>
Cc: Philip Semanchuk <philip(at)americanefficient(dot)com>, PostgreSQL General <pgsql-general(at)lists(dot)postgresql(dot)org>
Subject: Re: IMMUTABLE function to cast enum to/from text?
Date: 2022-11-10 20:39:08
Message-ID: 229957.1668112748@sss.pgh.pa.us
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-general

Joe Conway <mail(at)joeconway(dot)com> writes:
> On 11/10/22 14:52, Philip Semanchuk wrote:
>> I have seen conversations that suggest creating a custom IMMUTABLE
>> function to perform the cast, but I can't figure out how to do that
>> except with a CASE statement that enumerates every possible value. Is
>> there a more elegant approach?

> CREATE OR REPLACE FUNCTION mood2text(mood)
> RETURNS text AS
> $$
> select $1
> $$ STRICT IMMUTABLE LANGUAGE sql;

Of course, what this is doing is using a SQL-function wrapper to
lie about the mutability of the expression. Whether you consider
that elegant is up to you ;-) ... but it should work, as long as
you don't break things by renaming the enum's values.

regards, tom lane

In response to

Responses

Browse pgsql-general by date

  From Date Subject
Next Message Philip Semanchuk 2022-11-10 23:33:36 Re: IMMUTABLE function to cast enum to/from text?
Previous Message Joe Conway 2022-11-10 20:18:19 Re: IMMUTABLE function to cast enum to/from text?