Access bool integer solution

From: Sim Zacks <sim(at)compulab(dot)co(dot)il>
To: pgsql-general(at)postgresql(dot)org
Subject: Access bool integer solution
Date: 2005-12-18 19:36:09
Message-ID: do4dpl$2f6t$1@news.hub.org
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-general

I've been having problems with bools in my Access frontend and
PostGreSQL backend. The problem is that Access uses -1 for true and 0
for false and when it does a select it uses those numbers instead of the
true or false values. PostGreSQL does not have an implicit conversion
from int to bool, so it was failing was with an error "Operator does not
exist: boolean=integer"
I got around this in my queries by using the cbool function, which
passed in the actual true or false values. This solution did not work
for internal filters, because it internally calls the select statement
and does not give an interface to the code.

Using PostGreSQL flexibility and extensibility I have solved this
problem by defining an = and <> operator between int and boolean.

I hope this code can help someone else, as I couldn't find any real
solution in the archives, but I saw the same question a number of times.

create or replace function inttobool(num int,val bool) returns bool as
$$
begin
if num=0 and not val then
return true;
elsif num<>0 and val then
return true;
else return false;
end if;
end;
$$ language 'plpgsql';
create or replace function inttobool(val bool, num int) returns bool as
$$
begin
return inttobool(num,val);
end;
$$ language 'plpgsql';
create or replace function notinttobool(val bool, num int) returns bool as
$$
begin
return not inttobool(num,val);
end;
$$ language 'plpgsql';
create or replace function notinttobool(num int, val bool) returns bool as
$$
begin
return not inttobool(num,val);
end;
$$ language 'plpgsql';

CREATE OPERATOR = (
leftarg = integer,
rightarg = boolean,
procedure = inttobool,
commutator = =,
negator = !=
);
CREATE OPERATOR = (
leftarg = boolean,
rightarg = integer,
procedure = inttobool,
commutator = =,
negator = !=
);
CREATE OPERATOR <> (
leftarg = integer,
rightarg = boolean,
procedure = notinttobool,
commutator = <>,
negator = =
);
CREATE OPERATOR <> (
leftarg = boolean,
rightarg = integer,
procedure = notinttobool,
commutator = <>,
negator = =
);

Sim Zacks

Responses

Browse pgsql-general by date

  From Date Subject
Next Message stig erikson 2005-12-18 20:03:39 synchronization of news.postgresql.org and news.fr.postgresql.org
Previous Message Sim Zacks 2005-12-18 19:29:42 Re: convert integer to bool implicitly