OK. so I have these 2 queries. Both do the same thing:
1) If profile already exists then only update profile with new info 2) If profile does not exist then create a new profile
Restrictions: 1) username is unique (cannot be duplicate) 2) user_id is unique (cannot be duplicate)
I´m trying to get a boolean response from the server so my app can check if the query was effectively executed and rows were affected. For example if the user wants to change its username but the username he chose is already in use the query would not perform therefore no rows would be affected. I need some sort of return (count of affected rows?) or true/false telling me if a row was affected or not???
I am new to postgreSQL and I would greatly appreciate any form of help. Thanks =) Regards 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$