Re: Postgres / plpgsql equivalent to python's getattr() ?

Поиск
Список
Период
Сортировка
От Florian Pflug
Тема Re: Postgres / plpgsql equivalent to python's getattr() ?
Дата
Msg-id ECFB9FCF-FCF1-4B56-A0E8-5B516A97127E@phlo.org
обсуждение исходный текст
Ответ на Re: Postgres / plpgsql equivalent to python's getattr() ?  (Tom Lane <tgl@sss.pgh.pa.us>)
Ответы Re: Postgres / plpgsql equivalent to python's getattr() ?  (Pavel Stehule <pavel.stehule@gmail.com>)
Re: Postgres / plpgsql equivalent to python's getattr() ?  (James Robinson <jlrobins@socialserve.com>)
Список pgsql-hackers
On Aug4, 2011, at 00:48 , Tom Lane wrote:
> James Robinson <jlrobins@socialserve.com> writes:
>> Python's getattr() allows for dynamic lookup of attributes on an
>> object, as in:
>
>>     inst = MyClass(x=12, y=24)
>>     v = getattr(inst, 'x')
>>     assert v == 12
>
>> Oftentimes in writing data validating trigger functions, it'd be real
>> handy to be able to do a similar thing in plpgsql
>
>> Is there something in the internals which inherently prevent this?
>
> plpgsql is strongly typed (much more so than python, anyway).

You've brought forth that argument against dynamic attribute lookup
in the past, but I remain unconvinced. The fact that plpgsql is strongly
(or, more precisely, statically) types doesn't really get in the way as
long as you require the dynamic attribute accessor's return type to be
determined at compile time (make that "prepare time" in the case of
plpgsql).

The fact that I was able to implement dynamic accessor functions without
any change to postgres or plpgsql proves that IMHO. The only bit that slightly
kludgy about this in my opinion is the return-type determining "defvalue"
argument of fieldvalue(). But again, this has little to do with static vs.
dynamic typing but rather with the limitations of our support for polymorphic
functions.

@OP: Here's my implementation of the feature you desire as a set of C-language
functions: https://github.com/fgp/pg_record_inspect. Other people did code up
similar things in the past, but I currently cannot find any links to their work.
But it little bit digging in the mailing list archives should turn them up.

best regards,
Florian Pflug



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

Предыдущее
От: Alexander Korotkov
Дата:
Сообщение: Re: WIP: Fast GiST index build
Следующее
От: Dean Rasheed
Дата:
Сообщение: Re: cataloguing NOT NULL constraints