Re: select few fields as a single field

From: Roman Gavrilov <romio(at)il(dot)aduva(dot)com>
To: Holger Krug <hkrug(at)rationalizer(dot)com>
Cc: pgsql-general(at)postgresql(dot)org
Subject: Re: select few fields as a single field
Date: 2002-01-10 10:58:18
Message-ID: 3C3D73CA.FAA399FF@il.aduva.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-general

Thank you for your response.
Yes I know that i can make a function.
Is there a way to do this transparently I want to treat this function as a
field in the table.
The reason why i want to do that is because i have a class that has generic
method to retrieve data.
It receives list of field names and returns their values from the table.
If I want to implement the function method i will have to modify the class
method and will have to treat specially the
retrieve fullname thing.

Holger Krug wrote:

> On Thu, Jan 10, 2002 at 11:50:08AM +0200, Roman Gavrilov wrote:
> > Suppose that I have a table with 3 fields name, version, release.
> > name | version | release
> > ------------------
> > test | 1 | 2
> > ema | 1.2 | 2.2
> > ------------------
> >
> > I want to retrieve full name as 'select name || '-' || version || '-'
> > release from table';
> > test-1-2
> > ema-1.2-2.2
> >
> > I can do this as regular sql query;
> > But i would like to do this as 'select full_name from table'
> >
> > One way is to create view which will do the job.
> > Other way to do this is to create additional field name full_name and to
> > store the full name inside the field.
> > Is there any possibility to create function or constraint trigger that
> > will know that when I am doing select full_name it should
> > concat name version release and return it as full_name.(full_name is
> > virtual field)
>
> Not a trigger, triggers work only ON UPDATE and ON INSERT but not ON SELECT.
>
> > I don't want to create it as view;
>
> That's they way how PostgreSQL does this kind of work. Why not ?
>
> Alternatively you can use a function:
>
> SELECT fullname(table) FROM table;
>
> Here's the function definition:
>
> CREATE OR REPLACE FUNCTION fullname(table)
> RETURNS text AS
> 'BEGIN
> RETURN $1.name || ''-'' || $1.version || ''-'' $1.release;
> END'
> LANGUAGE plpgsql;
>
> The syntax of the CREATE FUNCTION statement as given here is valid for
> PostgreSQL 7.2, former versions have a slightly different syntax. See the
> command reference page for CREATE FUNCTION.
>
> Good luck !
>
> --
> Holger Krug
> hkrug(at)rationalizer(dot)com

--
-----------------------------------------------------------------------------
Roman Gavrilov
Aduva Inc., Web Development Services.
work +972-3-7534324 mobile +972-54-834668
romio(at)aduva(dot)com, romio(at)netvision(dot)net(dot)il

In response to

Responses

Browse pgsql-general by date

  From Date Subject
Next Message Holger Krug 2002-01-10 11:09:10 Re: select few fields as a single field
Previous Message Gerben 2002-01-10 10:44:16 Serialize subclasses recursively in PostgreSQL db??