PostgreSQL通过UPDATE / INSERT查询

时间:2015-12-15 00:30:53

标签: sql postgresql return plpgsql

行。所以我有这两个查询。两者都做同样的事情:

1)如果配置文件已存在,则仅使用新信息更新配置文件 2)如果配置文件不存在,则创建新的配置文件

限制: 1)用户名是唯一的(不能重复) 2)user_id是唯一的(不能重复)

我试图从服务器获取布尔响应,以便我的应用程序可以检查查询是否有效执行并且行受到影响。例如,如果用户想要更改其用户名,但他选择的用户名已在使用中,则查询将不会执行,因此不会影响任何行。我需要某种返回(受影响的行的数量?)或真/假告诉我行是否受到影响???

我是postgreSQL的新手,我非常感谢任何形式的帮助。谢谢=) 关心Jose Maria Landa =)

UPDATE people.profile SET profile_picture='test_profile_picture_url', alias='test_username' WHERE 

user_id='test_user_id'
AND NOT EXISTS (SELECT 1 FROM people.profile WHERE alias = 'test_username' AND user_id NOT LIKE 'test_user_id');

INSERT INTO people.profile (user_id, profile_picture, alias)
   SELECT 'test_user_id', 'test_profile_picture_url', 'test_username'
   WHERE NOT EXISTS (SELECT 1 FROM people.profile WHERE user_id = 'test_user_id' OR alias = 'test_username');
DO
$do$
BEGIN

IF EXISTS (SELECT 1 FROM people.profile WHERE user_id='test_user_id'
AND NOT EXISTS (SELECT 1 FROM people.profile WHERE alias = 'test_username' AND user_id NOT LIKE 'test_user_id')) 

THEN

-- UPDATE PROFILE INFO
UPDATE people.profile SET profile_picture='test_profile_picture_url', alias='test_username' WHERE 

user_id='test_user_id'
AND NOT EXISTS (SELECT 1 FROM people.profile WHERE alias = 'test_username' AND user_id NOT LIKE 'test_user_id');

ELSE 

-- CREATE NEW ACCOUNT
INSERT INTO people.profile (user_id, profile_picture, alias)
   SELECT 'test_user_id', 'test_profile_picture_url', 'test_username'
   WHERE NOT EXISTS (SELECT 1 FROM people.profile WHERE user_id = 'test_user_id' OR alias = 'test_username');

END IF;
END
$do$

1 个答案:

答案 0 :(得分:2)

我会创建一个函数,让它返回文本或带有结果的枚举。这是一个例子:

CREATE OR REPLACE FUNCTION people.update_profile(USERID text,
    USER_NAME text, PHOTO_URL text)
  RETURNS text AS
$BODY$
DECLARE
  rec profile;
  matching_uid boolean;
  matching_name boolean;
  matching_both boolean;
  result text;
BEGIN

  for rec in select * from people.profile where user_id = USERID or USER_NAME = alias
  loop
    if rec.user_id = USERID and rec.alias = USER_NAME then matching_both = true;
    elsif rec.user_id = USERID then matching_uid = true;
    elsif rec.alias = USER_NAME then matching_name = true;
    end if;
  end loop;

  if matching_both then
    update people.profile
    set profile_picture = PHOTO_URL
    where user_id = USERID;

    result := 'Photo Updated';    
  elsif matching_name then
    result := 'Username is in use.  Please choose another';
  elsif matching_uid then
    update people.profile
    set profile_picture = PHOTO_URL, alias = USER_NAME
    where user_id = USERID;

    result := 'Photo and User Name Updated';
  else
    insert into people.profile
    (user_id, alias, profile_picture)
    values (USERID, USER_NAME, PHOTO_URL);

    result := 'New User Added';
  end if;

  return result;
END;
$BODY$
  LANGUAGE plpgsql VOLATILE
  COST 100;

如果你这样调用这个函数:

select people.update_profile('hamb', 'Hambone', 'http://purple.com');

它会告诉您是否已成功更新的消息,以及是否采取了哪些措施。

您肯定可以在插入/更新后返回一个受此类影响的行数:

GET DIAGNOSTICS rowcount = ROW_COUNT;

但是假设该函数只返回1或0,我认为这些消息的文本或枚举会更有价值。