Re: Additional accessors via the Extension API ?

Поиск
Список
Период
Сортировка
От Julien Rouhaud
Тема Re: Additional accessors via the Extension API ?
Дата
Msg-id 20220220101257.dchizxpkang7vvq4@jrouhaud
обсуждение исходный текст
Ответ на Additional accessors via the Extension API ?  (Markur Sens <markursens@gmail.com>)
Ответы Re: Additional accessors via the Extension API ?  (Markur Sens <markursens@gmail.com>)
Список pgsql-general
Hi,

On Sun, Feb 20, 2022 at 08:07:20AM +0200, Markur Sens wrote:
> Suppose  I have defined an additional type in a PG extension.
>
> Is it possible to add custom accessors to that type -much like jsonb does-
> but use an API/hook without touching the core PG grammar & parser?

Unfortunately no.

> Hypothetical Examples:
>
> Assuming I have a TextFile type I’d like to implement syntax like:
>
> (‘/home/me/a.txt’::TextFile).firstline
> (‘/home/me/a.txt’::TextFile).lastline
> (‘/home/me/a.txt’::TextFile).countlines()
> (‘/home/me/a.txt’::TextFile).size()
> (‘/home/me/a.txt’::TextFile).datemodified()

Maybe you could rely on some old grammar hack to have something a bit similar,
as (expr).funcname is an alias for funcname(expr).  For instance:

# create function f1(int) returns text as $$
begin
return 'val: ' || $1::text;
end;
$$ language plpgsql;

# create table t as select 1 as id;

# select (5).f1, (id).f1 from t;
   f1   |   f1
--------+--------
 val: 5 | val: 1
(1 row)

I don't know if that would be enough for you needs.  Otherwise, the only option
would be tocreate an operator instead, like mytype -> 'myaccessor' or something
like that.



В списке pgsql-general по дате отправления:

Предыдущее
От: Karsten Hilbert
Дата:
Сообщение: Aw: Additional accessors via the Extension API ?
Следующее
От: Markur Sens
Дата:
Сообщение: Re: Additional accessors via the Extension API ?