Thread: inserting boolean values in dynamic queries

inserting boolean values in dynamic queries

From
"Raphael Bauduin"
Date:
Hi,

I'm building a dynamic query as described at
http://www.postgresql.org/docs/8.3/interactive/plpgsql-statements.html#PLPGSQL-STATEMENTS-EXECUTING-DYN
:

EXECUTE 'UPDATE tbl SET '       || quote_ident(colname)       || ' = '       || quote_literal(newvalue)       || '
WHEREkey = '       || quote_literal(keyvalue); 



It works fine, except when I want to include a boolean value: the cast
of newvalue from boolean to text causes problem.

I can change my code and work with 't' and 'f' values as text, but
wondered if there wasa way to use boolean values in a dynamically
generated query.

Thanks

Raphaël

--
Web database: http://www.myowndb.com
Free Software Developers Meeting: http://www.fosdem.org


Re: inserting boolean values in dynamic queries

From
Tom Lane
Date:
"Raphael Bauduin" <rblists@gmail.com> writes:
> EXECUTE 'UPDATE tbl SET '
>         || quote_ident(colname)
>         || ' = '
>         || quote_literal(newvalue)
>         || ' WHERE key = '
>         || quote_literal(keyvalue);

> It works fine, except when I want to include a boolean value: the cast
> of newvalue from boolean to text causes problem.

What problem?  'true' and 'false' are accepted as input for boolean
AFAICS.
        regards, tom lane


Re: inserting boolean values in dynamic queries

From
"Raphael Bauduin"
Date:
On Thu, Sep 18, 2008 at 2:35 PM, Tom Lane <tgl@sss.pgh.pa.us> wrote:
> "Raphael Bauduin" <rblists@gmail.com> writes:
>> EXECUTE 'UPDATE tbl SET '
>>         || quote_ident(colname)
>>         || ' = '
>>         || quote_literal(newvalue)
>>         || ' WHERE key = '
>>         || quote_literal(keyvalue);
>
>> It works fine, except when I want to include a boolean value: the cast
>> of newvalue from boolean to text causes problem.
>
> What problem?  'true' and 'false' are accepted as input for boolean
> AFAICS.
>


yes, but I have problems to use them to build the query passed to execute.
For example, working on this table
create table test(b_val bool);

I want to create a function that I cal call as   select test_bool(true)
and that will insert an entry in this test table.

Below are several attemps, all unsuccessful. The way I've made it work
it by accepting a char as input, t or f:   create or replace function test_bool(val char(1)) returns void as $$


create or replace function test_bool(val bool) returns void as $$   BEGIN       RAISE INFO 'insert into test(b_val)
values(''%'')', val;       execute 'insert into test (b_val) values ('|| val || ')';   END 
$$
language plpgsql;
--> ERROR:  array value must start with "{" or dimension information


create or replace function test_bool(val bool) returns void as $$   BEGIN       RAISE INFO 'insert into test(b_val)
values(''%'')', val;       execute 'insert into test (b_val) values ('|| val::text || ')';   END 
$$
language plpgsql;
--> ERROR:  cannot cast type boolean to text


create or replace function test_bool(val bool) returns void as $$   BEGIN       RAISE INFO 'insert into test(b_val)
values(''%'')', val;       execute 'insert into test (b_val) values ('|| quote_literal(val) || ')';   END 
$$
language plpgsql;
--> ERROR:  function quote_literal(boolean) does not exist


I guess I'm missing something....

Thanks.

Raphaël



>                        regards, tom lane
>






--
Web database: http://www.myowndb.com
Free Software Developers Meeting: http://www.fosdem.org


Re: inserting boolean values in dynamic queries

From
Tom Lane
Date:
"Raphael Bauduin" <rblists@gmail.com> writes:
> --> ERROR:  function quote_literal(boolean) does not exist

Oh, you must be using some version that didn't have a bool->text cast.
I'd suggest making one.  Or just make a quote_literal(boolean)
function.
        regards, tom lane