get column name passed to a function

Started by Rhys A.D. Stewartover 13 years ago9 messagesgeneral
Jump to latest
#1Rhys 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?

Regards,

Rhys

#2Adrian Klaver
adrian.klaver@aklaver.com
In reply to: Rhys A.D. Stewart (#1)
Re: get column name passed to a function

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

#3Rob Sargent
robjsargent@gmail.com
In reply to: Adrian Klaver (#2)
Re: get column name passed to a function

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;

#4Pavel Stehule
pavel.stehule@gmail.com
In reply to: Rhys A.D. Stewart (#1)
Re: get column name passed to a function

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

Show quoted text

Regards,

Rhys

#5Adrian Klaver
adrian.klaver@aklaver.com
In reply to: Rob Sargent (#3)
Re: get column name passed to a function

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

#6Rob Sargent
robjsargent@gmail.com
In reply to: Adrian Klaver (#5)
Re: get column name passed to a function

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;

#7Adrian Klaver
adrian.klaver@aklaver.com
In reply to: Rhys A.D. Stewart (#1)
Re: get column name passed to a function

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

#8Rhys A.D. Stewart
rhys.stewart@gmail.com
In reply to: Adrian Klaver (#7)
Re: get column name passed to a function

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:

Show quoted text

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&lt;http://www.postgresql.org/docs/9.2/interactive/plpython-database.html&gt;

Thanks,

Rhys

--
Adrian Klaver
adrian.klaver@gmail.com

#9Adrian Klaver
adrian.klaver@aklaver.com
In reply to: Rhys A.D. Stewart (#8)
Re: get column name passed to a function

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