Postgres函数没有按预期返回out参数

时间:2015-01-15 07:27:25

标签: django postgresql stored-procedures

我正在postgres sql中编写一个函数来检查eamil是否在表中。如果存在则检查状态。

CREATE OR REPLACE FUNCTION check_email_status_from_master_list(IN email_to_check character varying, OUT already_exists boolean, OUT dosend boolean)
AS
$BODY$ 
DECLARE 
already_exists boolean := False;
doSend boolean := False;
status character varying(255);
BEGIN
IF EXISTS(SELECT true FROM sendmessage_master_list_emails WHERE email=email_to_check) THEN
  -- do something
  already_exists := True;
  SELECT email_status INTO status FROM sendmessage_master_list_emails WHERE email=email_to_check;
  --SELECT sendmessage_master_list_emails into status FROM sendmessage_master_list_emails WHERE email=email_to_check;
  if(status='Bounce') then
  doSend := False;
  elsif(status='Invalid Email') then
  doSend := False;
  elsif(status='Delivered') then
  doSend :=True;
  else
  doSend := False;
  end if;
ELSE
already_exists := False;
doSend := False;
END IF;
END;
$BODY$
  LANGUAGE plpgsql

我通过传递三封电子邮件从django调用此函数。前两个电子邮件不在数据库中,最后一个在数据库中,状态为Bounce。

users = {1 : { "name" : "M user", "email" : "abc.official@gmail.com" , "phone" : "123456456"},
     2 : { "name" : "M user", "email" : "abc.official@gmail.com" , "phone" : "1234564569" },
     3 : { "name" : "M user", "email" : "abc.xyz@gmail.com" , "phone" : "1234564565" }}

     # Check First if the email is invalid & spam from the master list
    for keys in users:
        c = connection.cursor()
        try:
            c.execute("BEGIN")
            c.callproc("check_email_status_from_master_list", [users[keys]["email"]])
            resultss = c.fetchall()
            c.execute("COMMIT")
        except Exception as e:
            print e
        finally:
            for item in resultss:
                print item
            c.close()

该函数返回类似于此的输出而不是false或true。

(None, None)
(None, None)
(None, None)

1 个答案:

答案 0 :(得分:0)

OUT未按预期工作的原因是因为您重新声明 DECLARE部分中的两个变量。

下面突出显示的两行应位于BEGIN / END块中,而DECLARE块中的。在DECLARE块中,解析器将其视为两个局部变量(与OUT变量同名)。这就是为什么,当返回OUT变量时,它们是NULL,因此你得到一个空输出。

以下是具有上述更改的示例函数:

CREATE OR REPLACE FUNCTION check_email_status_from_master_list(IN email_to_check character varying, OUT already_exists boolean, OUT dosend boolean)
AS
$BODY$ 
DECLARE 
status character varying(255);
BEGIN
already_exists := False; -- MOVE THESE LINES FROM DECLARE ... to BEGIN/END block
doSend := False;         -- MOVE THESE LINES FROM DECLARE ... to BEGIN/END block
IF EXISTS(SELECT true FROM sendmessage_master_list_emails WHERE email=email_to_check) THEN
  -- do something
  already_exists := True;
  SELECT email_status INTO status FROM sendmessage_master_list_emails WHERE email=email_to_check;
  --SELECT sendmessage_master_list_emails into status FROM sendmessage_master_list_emails WHERE email=email_to_check;
  if(status='Bounce') then
  doSend := False;
  elsif(status='Invalid Email') then
  doSend := False;
  elsif(status='Delivered') then
  doSend :=True;
  else
  doSend := False;
  end if;
ELSE
already_exists := False;
doSend := False;
END IF;
END;
$BODY$
  LANGUAGE plpgsql;