PostgreSQL中的类型比较,如何比较bigint等。

时间:2018-10-31 12:08:58

标签: sql postgresql

我需要函数在比较类型之后会做出一些逻辑,但是我遇到了一个错误:

  

错误:oid类型“ bigint”的输入语法无效

CREATE OR REPLACE FUNCTION loginValidator(luser LoginUserType) RETURNS text [] AS $$

DECLARE
    errors text []; counter SMALLINT = 0;
n_regex varchar; e_regex varchar; p_regex varchar;

BEGIN

  SELECT nickname_r , email_r, password_r INTO n_regex, e_regex, p_regex FROM regex;

    RAISE NOTICE 'Type %',pg_typeof(luser.user_id); // bigint result


    IF luser.nick_name !~ n_regex THEN counter := counter + 1; errors [counter] := luser.nick_name;
    ELSEIF luser.email !~ e_regex THEN counter := counter + 1; errors [counter] := luser.email;
    ELSEIF luser.u_password !~ p_regex THEN counter := counter + 1; errors [counter] := luser.u_password;
    ELSEIF pg_typeof(luser.user_id) != 'bigint' THEN counter := counter + 1; errors [counter] := luser.user_id;
        -- How to compare here the types ?
    END IF;

    return errors;

 END;
$$ language plpgsql;**strong text**


SELECT loginValidator(row(8765768576,'Maks1988','Maks.Burkov88@gmail.com','@PlemiaMaks89987'));

1 个答案:

答案 0 :(得分:1)

pg_typeof给出类型regtype的结果,该结果不能从类型text隐式转换。因此,您必须进行显式转换:

SELECT pg_typeof(1::bigint) = 'bigint' -- ERROR: invalid input syntax for type oid: "bigint"

SELECT pg_typeof(1::bigint) = 'bigint'::regtype  -- ok; TRUE