Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Returning boolean from SQL function

I'm trying to write a PL/pgSQL function in postgres that returns a boolean expression. Currently the code looks like this:

CREATE OR REPLACE FUNCTION is_visible_to(role integer, from_role integer)
  RETURNS boolean
  LANGUAGE SQL STABLE STRICT AS $$
    RETURN $1 = $2 OR first_predicate($1, $2) OR second_predicate($2, $1);
 $$;

The functions first_predicate and second_predicate exist and return booleans, but I'm not getting much help looking at them because they are defined with something like

SELECT COUNT(*) > 0 FROM ... WHERE ...

I am getting the following syntax error:

ERROR:  syntax error at or near "RETURN"
LINE 4:     RETURN $1 = $2 first_predicate($1, $2) OR second_predicate(...
            ^

So obviously I'm misunderstanding something rather fundamental here. I'm sort of new to the whole PL/pgSQL thing, so links to a good tutorial or two would also be appreciated.

The project I'm working on uses postgres exclusively, and already contains a lot of postgres specific code, so solutions don't have to be portable to other databases.

Edit: Fixed it myself with

CREATE FUNCTION is_visible_to (role integer, from_role integer)
  RETURNS BOOLEAN AS '
    DECLARE
        role ALIAS FOR $1;
        from_role ALIAS FOR $2;
    BEGIN
      RETURN (role = from_role) OR
             first_predicate(from_role, role) OR
             second_predicate(from_role, role)
    END;
' LANGUAGE 'plpgsql';

Would still appreciate an answer that explains why the later works and the former (even with the language changed to plpgsql as suggested by @a_horse_with_no_name) does not, or provides some useful tutorials.

like image 797
jjm Avatar asked Dec 02 '14 21:12

jjm


People also ask

Can a function return a boolean?

A Boolean function is like a built-in function except that it returns a value of true or false instead of number, string, or date. The result of a Boolean function cannot be printed; it can only be used as a condition.

How do I query a boolean in SQL?

You can insert a boolean value using the INSERT statement: INSERT INTO testbool (sometext, is_checked) VALUES ('a', TRUE); INSERT INTO testbool (sometext, is_checked) VALUES ('b', FALSE); When you select a boolean value, it is displayed as either 't' or 'f'.


1 Answers

SQL

CREATE OR REPLACE FUNCTION is_visible_to(role integer, from_role integer)
  RETURNS boolean AS
$func$
   SELECT $1 = $2 OR first_predicate($1, $2) OR second_predicate($2, $1);
$func$ LANGUAGE sql STABLE STRICT;

With SELECT. There is no RETURN in plain SQL. Details in the manual here.

PL/pgSQL

CREATE OR REPLACE FUNCTION is_visible_to(role integer, from_role integer)
  RETURNS boolean AS
$func$
BEGIN
   RETURN $1 = $2 OR first_predicate($1, $2) OR second_predicate($2, $1);
END
$func$ LANGUAGE plpgsql STABLE STRICT;

BEGIN and END are required for PL/pgSQL. Details in the manual here.

Don't use the outdated (and redundant here) ALIAS FOR syntax. It's discouraged in the manual. You can reference parameter names directly (in SQL functions, too, since pg 9.2).

like image 81
Erwin Brandstetter Avatar answered Sep 20 '22 16:09

Erwin Brandstetter