Thread: get column name passed to a function

get column name passed to a function

From
"Rhys A.D. Stewart"
Date:
Greetings, 

I'm looking to obtain the name of a column that is passed to a function, similar to xmlforest. Suggestions?

Regards,


Rhys

Re: get column name passed to a function

From
Adrian Klaver
Date:
On 11/20/2012 11:36 AM, Rhys A.D. Stewart wrote:
> Greetings,
>
> I'm looking to obtain the name of a column that is passed to a function,
> similar to xmlforest. Suggestions?

This is going to require some more information.

1) What language is the function written in?

2) Trigger function or not?

3) A sample of the code?

>
> Regards,
>
>
> Rhys


--
Adrian Klaver
adrian.klaver@gmail.com


Re: get column name passed to a function

From
Rob Sargent
Date:
On 11/20/2012 01:35 PM, Adrian Klaver wrote:
> On 11/20/2012 11:36 AM, Rhys A.D. Stewart wrote:
>> Greetings,
>>
>> I'm looking to obtain the name of a column that is passed to a function,
>> similar to xmlforest. Suggestions?
>
> This is going to require some more information.
>
> 1) What language is the function written in?
>
> 2) Trigger function or not?
>
> 3) A sample of the code?
>
>>
>> Regards,
>>
>>
>> Rhys
>
>

Is this along the right line(s)?


create or replace function f(cname text) returns table(c text)
as
$$
declare
   v text;
begin
   select 'hello' into v;
   raise notice 'Column  name is %', v;
   execute 'select ' || v || ' as ' || cname;
end;
$$ language plpgsql;



Re: get column name passed to a function

From
Pavel Stehule
Date:
Hello

2012/11/20 Rhys A.D. Stewart <rhys.stewart@gmail.com>:
> Greetings,
>
> I'm looking to obtain the name of a column that is passed to a function,
> similar to xmlforest. Suggestions?
>

It is not possible :(

you cannot to do it without postgres's parser hacking

Regards

Pavel Stehule

> Regards,
>
>
> Rhys


Re: get column name passed to a function

From
Adrian Klaver
Date:
On 11/20/2012 12:51 PM, Rob Sargent wrote:
> On 11/20/2012 01:35 PM, Adrian Klaver wrote:
>> On 11/20/2012 11:36 AM, Rhys A.D. Stewart wrote:
>>> Greetings,
>>>
>>> I'm looking to obtain the name of a column that is passed to a function,
>>> similar to xmlforest. Suggestions?
>>
>> This is going to require some more information.
>>
>> 1) What language is the function written in?
>>
>> 2) Trigger function or not?
>>
>> 3) A sample of the code?
>>
>>>
>>> Regards,
>>>
>>>
>>> Rhys
>>
>>
>
> Is this along the right line(s)?
>
>
> create or replace function f(cname text) returns table(c text)
> as
> $$
> declare
>    v text;
> begin
>    select 'hello' into v;
>    raise notice 'Column  name is %', v;
>    execute 'select ' || v || ' as ' || cname;
> end;
> $$ language plpgsql;

So what you are looking to do is build dynamic queries?

If so take a look at:

http://www.postgresql.org/docs/9.2/interactive/plpgsql-statements.html#PLPGSQL-STATEMENTS-EXECUTING-DYN

In particular:

Example 39-1. Quoting Values In Dynamic Queries

>
>
>


--
Adrian Klaver
adrian.klaver@gmail.com


Re: get column name passed to a function

From
Rob Sargent
Date:
On 11/20/2012 02:03 PM, Adrian Klaver wrote:
> On 11/20/2012 12:51 PM, Rob Sargent wrote:
>> On 11/20/2012 01:35 PM, Adrian Klaver wrote:
>>> On 11/20/2012 11:36 AM, Rhys A.D. Stewart wrote:
>>>> Greetings,
>>>>
>>>> I'm looking to obtain the name of a column that is passed to a function,
>>>> similar to xmlforest. Suggestions?
>>>
>>> This is going to require some more information.
>>>
>>> 1) What language is the function written in?
>>>
>>> 2) Trigger function or not?
>>>
>>> 3) A sample of the code?
>>>
>>>>
>>>> Regards,
>>>>
>>>>
>>>> Rhys
>>>
>>>
>>
>> Is this along the right line(s)?
>>
>>
>> create or replace function f(cname text) returns table(c text)
>> as
>> $$
>> declare
>>     v text;
>> begin
>>     select 'hello' into v;
>>     raise notice 'Column  name is %', v;
>>     execute 'select ' || v || ' as ' || cname;
>> end;
>> $$ language plpgsql;
>
> So what you are looking to do is build dynamic queries?
>
> If so take a look at:
>
> http://www.postgresql.org/docs/9.2/interactive/plpgsql-statements.html#PLPGSQL-STATEMENTS-EXECUTING-DYN
>
> In particular:
>
> Example 39-1. Quoting Values In Dynamic Queries
>
>>
>>
>>
>
>

A correction to earlier

create or replace function f(cname text) returns table(c text)
as
$$
declare
   v text;
begin
   raise notice 'Column  name is %', cname;
   execute 'select quote_literal(hello) ' || '  as ' || cname;
end;
$$ language plpgsql;



Re: get column name passed to a function

From
Adrian Klaver
Date:
On 11/20/2012 01:34 PM, Rhys A.D. Stewart wrote:
> bummer.
>
> No, not dynamic queries just wanted to have the name of the columns
> along with the column data. Literally just like xmlforest, but without
> the xml.

Well in pl/pythonu you can, if you use the plpy module:

http://www.postgresql.org/docs/9.2/interactive/plpython-database.html

>
> Thanks,
>
> Rhys



--
Adrian Klaver
adrian.klaver@gmail.com


Re: get column name passed to a function

From
"Rhys A.D. Stewart"
Date:
Feature request?

Could it be shoved in before 9.2.2 or 9.2.3?


On Tue, Nov 20, 2012 at 5:23 PM, Adrian Klaver <adrian.klaver@gmail.com> wrote:
On 11/20/2012 01:34 PM, Rhys A.D. Stewart wrote:
bummer.

No, not dynamic queries just wanted to have the name of the columns
along with the column data. Literally just like xmlforest, but without
the xml.

Well in pl/pythonu you can, if you use the plpy module:

http://www.postgresql.org/docs/9.2/interactive/plpython-database.html


Thanks,

Rhys



--
Adrian Klaver
adrian.klaver@gmail.com

Re: get column name passed to a function

From
Adrian Klaver
Date:
On 11/22/2012 09:41 AM, Rhys A.D. Stewart wrote:
> Feature request?
>
> Could it be shoved in before 9.2.2 or 9.2.3?

Problems I see.

1) New features are not added to minor releases.

2) Still unclear what the problem is.
An example showing what you want to happen would be nice.

3) Even less clear what the feature would be.
As pointed out previously it would seem what you want is available in
plpythonu. So where do you propose the feature go?

>
>


--
Adrian Klaver
adrian.klaver@gmail.com