From: Markur Sens <markursens@gmail.com>
To: Julien Rouhaud <rjuju123@gmail.com>
Cc: pgsql-general <pgsql-general@lists.postgresql.org>
Subject: Re: Additional accessors via the Extension API ?
Date: Sun, 20 Feb 2022 12:31:22 +0200
Message-ID: <2C9AEE7B-89A0-49F6-AB28-F81AEC9B6078@gmail.com> (raw)
In-Reply-To: <20220220101257.dchizxpkang7vvq4@jrouhaud>
References: <6D4DE1F2-70C9-4BC4-92FD-0699BE0F8952@gmail.com>
<20220220101257.dchizxpkang7vvq4@jrouhaud>
> On 20 Feb 2022, at 12:12 PM, Julien Rouhaud <rjuju123@gmail.com> wrote:
>
> 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:
Is this documented & expected behavior or it’s just happens to work?
>
> # 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.
Yes, that’s what I’m doing at the moment:
Syntax like type -> ‘accessor’ is pretty straight forward to implement as an operator as the rightarg is text.
Things get more complicating as I’m adding support for
mytype -> function(arg=1)
for that case I have to create an intermediate type of function(arg) so that I can then define the left and right args for the -> operator.
But it’s a lot of boilerplate code.
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Reply to all the recipients from your mail client; the link fills in
the headers below: reply via email
Some clients, webmail especially, drop In-Reply-To and References from
a link, and the reply then starts a new thread: use the mbox below.
To: pgsql-general@postgresql.org
Cc: markursens@gmail.com, rjuju123@gmail.com
Subject: Re: Additional accessors via the Extension API ?
In-Reply-To: <2C9AEE7B-89A0-49F6-AB28-F81AEC9B6078@gmail.com>
References: <6D4DE1F2-70C9-4BC4-92FD-0699BE0F8952@gmail.com> <20220220101257.dchizxpkang7vvq4@jrouhaud> <2C9AEE7B-89A0-49F6-AB28-F81AEC9B6078@gmail.com>
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
This inbox is served by DDX for PostgreSQL; see mirroring instructions
for how to clone and mirror all data and code used for this inbox