语法错误无法理解是什么错误

时间:2018-10-21 09:53:13

标签: postgresql

需要您的帮助,请告诉我该功能在做什么? 根据文档示例,所有工具都应正常工作.. docs

create or replace function loginValidator(nickname varchar, email varchar, u_password varchar) returns boolean as $$
DECLARE
    checked boolean := false; n_regex varchar; e_regex varchar; p_regex varchar;
BEGIN 

 BEGIN;

  select nickname_r into n_regex from regex;
  select email_r into e_regex from regex;
  select password_r into p_regex from regex;

  IF n_regex ~ nickname AND e_regex ~ email AND p_regex ~ u_password THEN checked := true;

 COMMIT;

 return checked;
END;
$$ language plpgsql;
  

错误:“;”或附近的语法错误   第6行:开始;                  ^   SQL状态:42601   字符:222

1 个答案:

答案 0 :(得分:1)

您缺少END IF。我将其重写为:

create or replace function loginValidator(nickname varchar,
                                          email varchar,
                                          u_password varchar)
returns boolean 
as
$$
DECLARE
    checked boolean := false; n_regex varchar; e_regex varchar; p_regex varchar;
BEGIN 
  -- removed BEGIN
  select nickname_r, email_r, password_r into n_regex, e_regex, p_regex from regex;

  IF n_regex ~ nickname AND e_regex ~ email AND p_regex ~ u_password 
  THEN checked := true;
  END IF;    -- added END IF;
  -- removed COMMIT;

  RETURN checked;
END;
$$ language plpgsql;

或更简单:

create or replace function loginValidator(nickname varchar,
                                          email varchar,
                                          u_password varchar)
returns boolean 
as
$$
BEGIN 

  RETURN  (select COUNT(*) 
          from regex
          WHERE nickname_r ~ nickname
            AND  email_r ~ email
            AND password_r ~ u_password)::boolean;

END;
$$ language plpgsql;

我希望您不要以明文形式存储密码。